[英]Consistency when using SCOPE_IDENTITY() in output
I want to execute a SQL query that would return the current identity id of the row that was added to the table: 我想执行一个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
Now my question is it the returned id for my current inserted row or is it the id of the last inserted row? 现在我的问题是我当前插入行的返回id还是最后插入行的id?
I mean can I trust it for using as foreign key? 我的意思是我可以相信它作为外键使用吗?
Both output inserted
and SCOPE_IDENTITY
will give you the id from the row just inserted by your last statement. output inserted
和SCOPE_IDENTITY
将为您提供上一个语句刚插入的行的ID。 So yes you can use it as a foreign key. 所以是的,你可以将它用作外键。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.