插入Oracle并检索生成的序列ID
我有一些针对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
扩展一下@Guru和@Ronnis的答案,您可以隐藏序列并使其看起来更像使用触发器的自动增量,并有一个过程为您执行插入并将生成的ID作为输出返回范围。
然后,您可以调用该过程而不是执行普通插入,例如从匿名块:
您可以在没有显式匿名块的情况下进行调用,例如从 SQL*Plus:
... 并使用绑定变量
:l_batchid< /code> 引用之后生成的值:
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.
You can then call the procedure instead of doing a plain insert, e.g. from an anoymous block:
You can make the call without an explicit anonymous block, e.g. from SQL*Plus:
... and use the bind variable
:l_batchid
to refer to the generated value afterwards:Oracle 中的列没有自动递增功能。您需要创建一个 SEQUENCE 对象。您可以使用如下序列:
...返回下一个数字。要找出最后创建的序列号(在您的会话中),您可以使用:
此站点有几个关于如何使用序列的完整示例。
编辑:写完这个答案两年后,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:
...to return the next number. To find out the last created sequence nr (in your session), you would use:
This site has several complete examples on how to use sequences.
Edit: Two years after this answer was written Oracle introduced "identity columns".
您可以使用以下语句将插入的 Id 获取到类似变量的内容。
现在您可以使用以下语句检索该值
You can use the below statement to get the inserted Id to a variable-like thing.
Now you can retrieve the value using the below statement
将其作为存储过程确实有很多优点。您可以使用语法
insert into table_name value returned
获取插入到表中的序列。比如:
或者直接返回
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:
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 ofreturning into
effectively.您可以使用单个语句来完成此操作 - 假设您从具有输入/输出参数功能的类似 JDBC 的连接器调用它:
或者作为 pl-sql 脚本:
You can do this with a single statement - assuming you are calling it from a JDBC-like connector with in/out parameters functionality:
or, as a pl-sql script:
一个更优雅的解决方案可以使用 RETURNING 语句:
我希望它能帮助你
A more elegant solution could be using RETURNING statement:
I hope it help you