【问题标题】:What is Oracle's counterpart to SQL Server's @@IDENTITY?Oracle 与 SQL Server 的@@IDENTITY 对应的是什么?
【发布时间】:2013-03-24 15:31:12
【问题描述】:

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

【问题讨论】:

  • 由于Oracle 确实没有IDENTITY 列的概念,因此它没有等价物。在 Oracle 中,您通常使用SEQUENCE 来获取连续的、唯一的数字,以便在您的表中使用。
  • @marc_s - 好的,谢谢...
  • answer stackoverflow.com/a/51051121/3104267 帮助我解决了我的要求,以解决等效问题。

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-01-13
    • 2010-12-01
    • 1970-01-01
    • 2011-04-01
    • 1970-01-01
    • 2012-05-30
    • 1970-01-01
    相关资源
    最近更新 更多