Re: How to retrieve an auto-increment ID of a row

From: Michel Cadot <micadot_at_netcourrier.com>
Date: Tue, 4 Sep 2001 17:07:16 +0200
Message-ID: <9n2qn4$ges$1_at_s1.read.news.oleane.net>


"David Lee" <pritino2020_at_yahoo.com.tw> a écrit dans le message news: 9n2i8l$2jsjf$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 - 17:07:16 CEST

Original text of this message