![](/img/trans.png)
[英]2 rows inserted instead of 1 when using @@IDENTITY / scope_identity()
[英]Consistency when using SCOPE_IDENTITY() in output
我想執行一個SQL查詢,該查詢將返回添加到表中的行的當前標識ID:
DECLARE @id int
DECLARE @tblOutput table (id int)
INSERT INTO tblStudent(Name, Family, age, test)
OUTPUT inserted.id into @tblOutput
VALUES('ashghar', 'farhadi', 321, 135)
SELECT @id = id from @tblOutput
現在我的問題是我當前插入行的返回id還是最后插入行的id?
我的意思是我可以相信它作為外鍵使用嗎?
output inserted
和SCOPE_IDENTITY
將為您提供上一個語句剛插入的行的ID。 所以是的,你可以將它用作外鍵。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.