如何在将oracle nextval分配给数据库中的某一行后立即将其检索到变量中 [英] How to retrieve oracle nextval into variable right after it is assigned to some row in database
问题描述
大家好,
我要做的是为用户创建的查询生成唯一ID。
我使用oracle序列来分配查询ID,这是表中的主键。
创建查询后,我想显示查询ID已创建,基本上是序列号。
谢谢.. !!
< b>我尝试了什么:
首先,显示sequence.currval并不能保证正确的输出,因为在那段时间内其他用户可以将查询插入到数据库中。
其次,如果我选择x用户正确创建的id,那可能会有效。但这需要解雇另一个查询。
还有其他办法吗?
Hi Everyone,
What I am trying to do is to generate unique ids for queries created by users.
I am using oracle sequence to assign the query id, which is primary key in the table.
After creating the query, I want to display the query id created, which is basically the sequence no.
Thank you..!!
What I have tried:
Firstly, displaying the sequence.currval will not guarantee right outpput because within that time other user can insert query into database.
Secondly, if I select the id that is created by x user exactly at y time, that might work. But that requires another query to be fired.
Is there any alternative way?
推荐答案
您可以使用return子句来做到这一点。
示例:
Hi,
You can use the returning clause to do that.
Example:
INSERT INTO {query_table} (query_id, query)
VALUES (sequence.nextval, query_From_User)
RETURNING query_id into {a_defined_variable}
这将为您提供插入记录的ID。
This will give you the id of the inserted record.
这篇关于如何在将oracle nextval分配给数据库中的某一行后立即将其检索到变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!