插入Oracle并检索生成的序列ID

发布于 2024-10-30 05:52:03 字数 703 浏览 2 评论 0原文

我有一些针对 SQL Server 的原始 SQL 查询,这些查询使用 SCOPE_IDENTITY 来检索特定 INSERT 生成的 ID,在该 INSERT 全部在一次执行中发生之后...

INSERT into Batch(
BatchName,
BatchType,
Source,
Area
) Values (
@strBatchName,
@strType,
@strSource,
@intArea
);

SELECT SCOPE_IDENTITY() BatchID;

问题是:

对于 Oracle 数据库来说,执行此操作的最佳方法是什么?

这可以通过标准 SQL 在 Oracle 上完成吗?或者我是否必须将其切换为使用存储过程并在存储过程的主体中放置类似的内容?

如果它必须是存储过程,那么检索最后生成的序列号的事实上的标准方法是什么,注意考虑多个线程上可能会重叠执行,因此该机制需要检索正确生成的 ID 和不一定是最后生成的绝对 ID。

如果两个同时执行,则每个都必须从各自的调用中返回正确生成的 ID。请注意,由于调用的多线程性质,我没有使用 SQL Server 的“@@IDENTITY”。

如果可能的话,我宁愿将其保留为原始 SQL,因为这对我来说更容易跨平台管理(包含每个平台的 SQL 块的单个文件,由 DBMS 标识标签分隔)。存储过程对我来说需要管理更多的工作,但如果这是唯一可能的方法,我可以这样做。

I have a handful of raw SQL queries for SQL Server which use SCOPE_IDENTITY to retrieve the generated ID for a specific INSERT immediately after that INSERT occurs all in one execution…

INSERT into Batch(
BatchName,
BatchType,
Source,
Area
) Values (
@strBatchName,
@strType,
@strSource,
@intArea
);

SELECT SCOPE_IDENTITY() BatchID;

The question is:

What’s the best way to do that for an Oracle database?

Can this be done on Oracle through standard SQL or do I have to switch this to use a stored procedure and place something similar in the body of the stored proc?

If it must be a stored proc, then what is the de-facto standard way for retrieving the last generated sequence number, taking care to consider there will likely be overlapping executions on multiple threads so this mechanism will need to retrieve the right generated ID and not necessarily the absolute last generated ID.

If two execute simultaneously then each must return the correct generated ID from each respective call. Notice I’m not using SQL Server's “@@IDENTITY” because of that multithreaded nature of the calls.

I would rather keep it as raw SQL if possible since that’s much easier for me to manage across platforms (single file containing each platform's SQL block separated by DBMS identifying tags). Stored procs are a bit more work for me to manage, but I can go that way if it's the only way possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

梦里寻她 2024-11-06 05:52:03

扩展一下@Guru和@Ronnis的答案,您可以隐藏序列并使其看起来更像使用触发器的自动增量,并有一个过程为您执行插入并将生成的ID作为输出返回范围。

create table batch(batchid number,
    batchname varchar2(30),
    batchtype char(1),
    source char(1),
    intarea number)
/

create sequence batch_seq start with 1
/

create trigger batch_bi
before insert on batch
for each row
begin
    select batch_seq.nextval into :new.batchid from dual;
end;
/

create procedure insert_batch(v_batchname batch.batchname%TYPE,
    v_batchtype batch.batchtype%TYPE,
    v_source batch.source%TYPE,
    v_intarea batch.intarea%TYPE,
    v_batchid out batch.batchid%TYPE)
as
begin
    insert into batch(batchname, batchtype, source, intarea)
    values(v_batchname, v_batchtype, v_source, v_intarea)
    returning batchid into v_batchid;
end;
/

然后,您可以调用该过程而不是执行普通插入,例如从匿名块:

declare
    l_batchid batch.batchid%TYPE;
begin
    insert_batch(v_batchname => 'Batch 1',
        v_batchtype => 'A',
        v_source => 'Z',
        v_intarea => 1,
        v_batchid => l_batchid);
    dbms_output.put_line('Generated id: ' || l_batchid);

    insert_batch(v_batchname => 'Batch 99',
        v_batchtype => 'B',
        v_source => 'Y',
        v_intarea => 9,
        v_batchid => l_batchid);
    dbms_output.put_line('Generated id: ' || l_batchid);
end;
/

Generated id: 1
Generated id: 2

您可以在没有显式匿名块的情况下进行调用,例如从 SQL*Plus:

variable l_batchid number;
exec insert_batch('Batch 21', 'C', 'X', 7, :l_batchid);

... 并使用绑定变量 :l_batchid< /code> 引用之后生成的值:

print l_batchid;
insert into some_table values(:l_batch_id, ...);

Expanding a bit on the answers from @Guru and @Ronnis, you can hide the sequence and make it look more like an auto-increment using a trigger, and have a procedure that does the insert for you and returns the generated ID as an out parameter.

create table batch(batchid number,
    batchname varchar2(30),
    batchtype char(1),
    source char(1),
    intarea number)
/

create sequence batch_seq start with 1
/

create trigger batch_bi
before insert on batch
for each row
begin
    select batch_seq.nextval into :new.batchid from dual;
end;
/

create procedure insert_batch(v_batchname batch.batchname%TYPE,
    v_batchtype batch.batchtype%TYPE,
    v_source batch.source%TYPE,
    v_intarea batch.intarea%TYPE,
    v_batchid out batch.batchid%TYPE)
as
begin
    insert into batch(batchname, batchtype, source, intarea)
    values(v_batchname, v_batchtype, v_source, v_intarea)
    returning batchid into v_batchid;
end;
/

You can then call the procedure instead of doing a plain insert, e.g. from an anoymous block:

declare
    l_batchid batch.batchid%TYPE;
begin
    insert_batch(v_batchname => 'Batch 1',
        v_batchtype => 'A',
        v_source => 'Z',
        v_intarea => 1,
        v_batchid => l_batchid);
    dbms_output.put_line('Generated id: ' || l_batchid);

    insert_batch(v_batchname => 'Batch 99',
        v_batchtype => 'B',
        v_source => 'Y',
        v_intarea => 9,
        v_batchid => l_batchid);
    dbms_output.put_line('Generated id: ' || l_batchid);
end;
/

Generated id: 1
Generated id: 2

You can make the call without an explicit anonymous block, e.g. from SQL*Plus:

variable l_batchid number;
exec insert_batch('Batch 21', 'C', 'X', 7, :l_batchid);

... and use the bind variable :l_batchid to refer to the generated value afterwards:

print l_batchid;
insert into some_table values(:l_batch_id, ...);
歌入人心 2024-11-06 05:52:03

Oracle 中的列没有自动递增功能。您需要创建一个 SEQUENCE 对象。您可以使用如下序列:

insert into table(batch_id, ...) values(my_sequence.nextval, ...)

...返回下一个数字。要找出最后创建的序列号(在您的会话中),您可以使用:

my_sequence.currval

此站点有几个关于如何使用序列的完整示例。

编辑:写完这个答案两年后,Oracle 引入了“身份列”。

There are no auto incrementing features in Oracle for a column. You need to create a SEQUENCE object. You can use the sequence like:

insert into table(batch_id, ...) values(my_sequence.nextval, ...)

...to return the next number. To find out the last created sequence nr (in your session), you would use:

my_sequence.currval

This site has several complete examples on how to use sequences.

Edit: Two years after this answer was written Oracle introduced "identity columns".

国际总奸 2024-11-06 05:52:03

您可以使用以下语句将插入的 Id 获取到类似变量的内容。

INSERT INTO  YOUR_TABLE(ID) VALUES ('10') returning ID into :Inserted_Value;

现在您可以使用以下语句检索该值

SELECT :Inserted_Value FROM DUAL;

You can use the below statement to get the inserted Id to a variable-like thing.

INSERT INTO  YOUR_TABLE(ID) VALUES ('10') returning ID into :Inserted_Value;

Now you can retrieve the value using the below statement

SELECT :Inserted_Value FROM DUAL;
烟柳画桥 2024-11-06 05:52:03

将其作为存储过程确实有很多优点。您可以使用语法 insert into table_name value returned 获取插入到表中的序列。

比如:

declare
some_seq_val  number;
lv_seq        number;
begin
some_seq_val := your_seq.nextval;
insert into your_tab (col1, col2, col3) 
values (some_seq_val, val2, val3) returning some_seq_val into lv_seq;

dbms_output.put_line('The inserted sequence is: '||to_char(lv_seq));
end;
/

或者直接返回some_seq_val。如果您不使用 SEQUENCE,并且通过某些计算得出序列,则可以有效地使用返回

Doing it as a stored procedure does have lot of advantages. You can get the sequence that is inserted into the table using syntax insert into table_name values returning.

Like:

declare
some_seq_val  number;
lv_seq        number;
begin
some_seq_val := your_seq.nextval;
insert into your_tab (col1, col2, col3) 
values (some_seq_val, val2, val3) returning some_seq_val into lv_seq;

dbms_output.put_line('The inserted sequence is: '||to_char(lv_seq));
end;
/

Or just return some_seq_val. In case you are not making use of SEQUENCE, and arriving the sequence on some calculation, you can make use of returning into effectively.

貪欢 2024-11-06 05:52:03

您可以使用单个语句来完成此操作 - 假设您从具有输入/输出参数功能的类似 JDBC 的连接器调用它:

insert into batch(batchid, batchname) 
values (batch_seq.nextval, 'new batch')
returning batchid into :l_batchid;

或者作为 pl-sql 脚本:

variable l_batchid number;

insert into batch(batchid, batchname) 
values (batch_seq.nextval, 'new batch')
returning batchid into :l_batchid;

select :l_batchid from dual;

You can do this with a single statement - assuming you are calling it from a JDBC-like connector with in/out parameters functionality:

insert into batch(batchid, batchname) 
values (batch_seq.nextval, 'new batch')
returning batchid into :l_batchid;

or, as a pl-sql script:

variable l_batchid number;

insert into batch(batchid, batchname) 
values (batch_seq.nextval, 'new batch')
returning batchid into :l_batchid;

select :l_batchid from dual;
野の 2024-11-06 05:52:03

一个更优雅的解决方案可以使用 RETURNING 语句:

DECLARE
V_ID_RETURN NUMBER;
BEGIN

INSERT into Batch(
BatchName,
BatchType,
Source,
Area
) Values (
@strBatchName,
@strType,
@strSource,
@intArea
)
RETURNING BatchID INTO V_ID_RETURN;

DBMS_OUTPUT.PUT_LINE('V_ID_RETURN:'||V_ID_RETURN);

END;

我希望它能帮助你

A more elegant solution could be using RETURNING statement:

DECLARE
V_ID_RETURN NUMBER;
BEGIN

INSERT into Batch(
BatchName,
BatchType,
Source,
Area
) Values (
@strBatchName,
@strType,
@strSource,
@intArea
)
RETURNING BatchID INTO V_ID_RETURN;

DBMS_OUTPUT.PUT_LINE('V_ID_RETURN:'||V_ID_RETURN);

END;

I hope it help you

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文