Oracle FAQ Your Portal to the Oracle Knowledge Grid
HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US
 

Home -> Community -> Usenet -> c.d.o.misc -> Re: How to select the last item's ID

Re: How to select the last item's ID

From: Michel Cadot <micadot_at_netcourrier.com>
Date: Tue, 4 Sep 2001 16:57:58 +0200
Message-ID: <9n2q5m$g3p$1@s1.read.news.oleane.net>

"David Lee" <pritino2020_at_yahoo.com.tw> a écrit dans le message news: 9n2i6h$2n5t1$1_at_news.shem.etwebs.com...
> After I inserted a row of data, how do I retrieve the auto-increment ID of
> this row ? Because there might be another SQL script also inserting into
> this table at the same time, it is not guaranteed that I will get the
> correct ID by selecting right after inserting. Is there a function for
> this? Thousands of appreciation!!!
>
> Please tell me where I should post this question if I post the wrong
> newsgroup, thanks!!
>
>

v815> create table t (id integer, val varchar2(20));

Table created.

v815> create sequence s ;

Sequence created.

v815> create or replace trigger bi_t
  2 before insert on t
  3 for each row
  4 begin
  5 select s.nextval into :new.id from dual;   6 end;
  7 /

Trigger created.

v815> var id number
v815> insert into t (val) values ('my string')   2 returning id into :id;

1 row created.

v815> print id

        ID


         1

v815> insert into t (val) values ('my string2')   2 returning id into :id;

1 row created.

v815> print id

        ID


         2

v815> select * from t;

        ID VAL

---------- --------------------
         1 my string
         2 my string2

2 rows selected.

--
Hope this helps
Michel
Received on Tue Sep 04 2001 - 09:57:58 CDT

Original text of this message

HOME | ASK QUESTION | ADD INFO | SEARCH | E-MAIL US