Oracle - return newly inserted key value

雨燕双飞 提交于 2021-02-07 05:43:21

问题


We have a table with a Primary Key that gets populated on insert by a trigger on the table - the trigger gets the next sequence number from a sequence we created for the table and uses that for the value of the key on insert. Now we would like to be able to return that value in our insert procedure (PL\SQL), similar to select @@scope_identity in SQL Server. I have been googling all day and basically come up with nothing - anyone been successful with this before?

Thanks


回答1:


I don't know if it works with triggers but the RETURNING clause may be what you're looking for:

INSERT INTO my_table (col_1, col_2)
  VALUES ('foo', 'bar')
  RETURNING pk_id INTO my_variable;



回答2:


I think you are looking for a Callable Statement. Here's javadoc if you are trying to get to it from Java.




回答3:


why not just return the_sequence.currval ?



来源:https://stackoverflow.com/questions/1145294/oracle-return-newly-inserted-key-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!