使用 @@identity 而不是 scope_identity 的原因 [英] Reason for using @@identity rather than scope_identity
问题描述
在 SQL Server 2005 数据库上,我们的一位远程开发人员刚刚签入了对存储过程的更改,该存储过程将select scope_identity"更改为select @@identity".您知道为什么要使用 @@identity 而不是 scope_identity 的任何原因吗?
On a SQL Server 2005 database, one of our remote developers just checked in a change to a stored procedure that changed a "select scope_identity" to "select @@identity". Do you know of any reasons why you'd use @@identity over scope_identity?
推荐答案
@@IDENTITY
将返回当前会话发出的最后一个标识值.SCOPE_IDENTITY()
返回当前会话和相同作用域中的最后一个标识值.它们通常是相同的,但假设调用了一个触发器,它在当前语句之前的某处插入了一些东西.@@IDENTITY
将通过触发器的INSERT
语句返回标识值,而不是块的插入语句.除非他知道自己在做什么,否则这通常是错误的.
@@IDENTITY
will return the last identity value issued by the current session. SCOPE_IDENTITY()
returns the last identity value in the current session and same scope. They are usually the same, but assume a trigger is called which inserted something somewhere just before the current statement. @@IDENTITY
will return the identity value by the INSERT
statement of the trigger, not the insert statement of the block. It's usually a mistake unless he knows what he's doing.
这篇关于使用 @@identity 而不是 scope_identity 的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!