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

时间:2013-04-03 05:13:07

标签: plsql sql-server-2008-r2 oracle11g

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

1 个答案:

答案 0 :(得分:2)

鉴于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