Oracle 与 SQL Server 的 @@IDENTITY 对应物是什么?



Oracle与SQL Server的@@IDENTITY对应的是什么?

考虑到Oracle没有标识列,您通常会为每个表创建一个序列,并使用该序列填充主键。假设您已经这样做了,您可以获得序列的currval,以获得当前会话最近生成的序列值。

SQL> create table foo(
  2    col1 number primary key,
  3    col2 varchar2(10)
  4  );
Table created.
SQL> create sequence foo_seq;
Sequence created.
SQL> ed
Wrote file afiedt.buf
  1  create or replace trigger foo_trg
  2    before insert on foo
  3    for each row
  4  begin
  5    :new.col1 := foo_seq.nextval;
  6* end;
SQL> /
Trigger created.
SQL> insert into foo( col2 )
  2    values( 'foo' );
1 row created.
SQL> insert into foo( col2 )
  2    values( 'bar' );
1 row created.
SQL> select foo_seq.currval
  2    from dual;
   CURRVAL
----------
         2

最新更新