0

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

4

1 回答 1

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
于 2013-04-03T08:09:56.120 回答