使用@@身份而非scope

编程入门 行业动态 更新时间:2024-10-27 18:28:08
使用@@身份而非scope_identity的原因(Reason for using @@identity rather than scope_identity)

在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.

更多推荐

本文发布于:2023-07-28 23:22:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:而非   身份   scope

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!