我有一个将记录插入表中的程序。我还有一个触发器,可以在
BEFORE INSERT
上自动递增序列(也有身份类型)。我想对其结果进行操作,例如例如,突出显示网站表格中的新记录。将 RETURNS
与 GEN_ID(<sequence>, 0)
一起使用是唯一的方法吗?如果是这样,据我所知,就存在竞争条件,不是吗?如果有人先交易成功,我的ID可能就不是我的了。
还有一个
RETURNING
子句,但据我所知,它可以在 DML 查询中使用,例如在执行块或动态语句中,如 INSERT INTO ... RETURNING ID
?
RETURNING
语句中使用 INSERT
子句,并将其分配给存储过程的输出变量之一。
举个简单的例子:
create table example_table (
id integer generated by default as identity constraint pk_example_table primary key,
val varchar(50)
);
set term #;
create procedure add_to_example(new_val type of column example_table.val)
returns (new_id type of column example_table.id)
as
begin
insert into example_table (val) values (:new_val) returning id into new_id;
end#
set term ;#
然后您可以执行以下操作:
execute procedure add_to_example('abc');
例如:
SQL> execute procedure add_to_example('abc');
NEW_ID
============
1
SQL> execute procedure add_to_example('bce');
NEW_ID
============
2
SQL>