本文介紹了如何插入表并取回主鍵值?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個主鍵設置為自動遞增.
I have a primary key set up to auto increment.
我正在執行多個查詢,我需要檢索該主鍵值以用作另一個表中的外鍵 (IsIdentity = TRUE
).
I am doing multiple queries and I need to retrieve that primary key value to use as a foreign key in another table (IsIdentity = TRUE
).
當我執行插入查詢時,有沒有什么優雅的方法來取回主鍵值?現在我正在重新查詢并獲得該列中的最高值,這似乎非常糟糕.
Is there any elegant way to get back the primary key value when I do an insert query? Right now I am requerying and getting the highest value in that column which seems really hacky.
有什么建議嗎?
推薦答案
insert into YourTable values (...)
使用 scope_identity() 獲取新的 PK
get the new PK with scope_identity()
select scope_identity()
這篇關于如何插入表并取回主鍵值?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!