在SQL Server 2005数据库上,我们的一位远程开发人员刚刚检查了对存储过程的更改,该存储过程将“select scope_identity”更改为“select @@ 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语句返回标识值,而不是块的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.
更多推荐
发布评论