Scope_Identity()、Identity()、@@Identity 和 Ident_Current() 之间有什么区别?
我知道 Scope_Identity()
、Identity()
、@@Identity
和 Ident_Current()
都获得了值身份栏,但我很想知道其中的区别。
我遇到的部分争议是,应用于上述这些函数的范围是什么意思?
我还想要一个使用它们的不同场景的简单示例?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
@@identity
函数返回在同一会话中创建的最后一个身份。scope_identity()
函数返回在同一会话和同一范围内创建的最后一个身份。ident_current(name)
返回为任何会话中的特定表或视图创建的最后一个标识。identity()
函数不用于获取身份,它用于在select...into
查询中创建身份。会话是数据库连接。范围是当前查询或当前存储过程。
scope_identity()
和@@identity
函数不同的情况是,如果表上有触发器。如果您有一个插入记录的查询,导致触发器在某处插入另一条记录,则scope_identity()
函数将返回查询创建的标识,而@@identity
code> 函数将返回触发器创建的标识。因此,通常您会使用
scope_identity()
函数。@@identity
function returns the last identity created in the same session.scope_identity()
function returns the last identity created in the same session and the same scope.ident_current(name)
returns the last identity created for a specific table or view in any session.identity()
function is not used to get an identity, it's used to create an identity in aselect...into
query.The session is the database connection. The scope is the current query or the current stored procedure.
A situation where the
scope_identity()
and the@@identity
functions differ, is if you have a trigger on the table. If you have a query that inserts a record, causing the trigger to insert another record somewhere, thescope_identity()
function will return the identity created by the query, while the@@identity
function will return the identity created by the trigger.So, normally you would use the
scope_identity()
function.好问题。
@@IDENTITY
:返回 SQL 连接 (SPID) 上生成的最后一个标识值。大多数情况下,它会是您想要的,但有时并非如此(例如,当触发触发器以响应INSERT
时,触发器会执行另一个INSERT
语句)。SCOPE_IDENTITY()
:返回当前作用域(即存储过程、触发器、函数等)中生成的最后一个标识值。IDENT_CURRENT()
:返回特定表的最后一个标识值。不要使用它从INSERT
获取标识值,它会受到竞争条件的影响(即多个连接在同一个表上插入行)。IDENTITY()
:在将表中的列声明为标识列时使用。有关更多参考,请参阅:http://msdn.microsoft.com/en-us /library/ms187342.aspx。
总结一下:如果您正在插入行,并且想要了解刚刚插入的行的标识列的值,请始终使用
SCOPE_IDENTITY()
。Good question.
@@IDENTITY
: returns the last identity value generated on your SQL connection (SPID). Most of the time it will be what you want, but sometimes it isn't (like when a trigger is fired in response to anINSERT
, and the trigger executes anotherINSERT
statement).SCOPE_IDENTITY()
: returns the last identity value generated in the current scope (i.e. stored procedure, trigger, function, etc).IDENT_CURRENT()
: returns the last identity value for a specific table. Don't use this to get the identity value from anINSERT
, it's subject to race conditions (i.e. multiple connections inserting rows on the same table).IDENTITY()
: used when declaring a column in a table as an identity column.For more reference, see: http://msdn.microsoft.com/en-us/library/ms187342.aspx.
To summarize: if you are inserting rows, and you want to know the value of the identity column for the row you just inserted, always use
SCOPE_IDENTITY()
.如果您了解范围和会话之间的区别,那么理解这些方法就会很容易。
一个非常好的 博客文章描述了这种差异:
由此可见,三种身份检索方法的区别如下:
If you understand the difference between scope and session then it will be very easy to understand these methods.
A very nice blog post by Adam Anderson describes this difference:
Thus the differences between the three identity retrieval methods are as follows:
范围是指执行
INSERT
语句SCOPE_IDENTITY()
的代码上下文,而不是@@IDENTITY
的全局范围。给出不同的结果。
Scope means the code context that performs the
INSERT
statementSCOPE_IDENTITY()
, as opposed to the global scope of@@IDENTITY
.Gives different results.
为了澄清
@@Identity
的问题:例如,如果您插入一个表并且该表具有执行插入的触发器,
@@Identity
将返回插入中的 id触发器(log_id
或其他东西),而scope_identity()
将从原始表中的插入返回 id。因此,如果您没有任何触发器,
scope_identity()
和@@identity
将返回相同的值。如果您有触发器,您需要考虑您想要什么值。To clarify the problem with
@@Identity
:For instance, if you insert a table and that table has triggers doing inserts,
@@Identity
will return the id from the insert in the trigger (alog_id
or something), whilescope_identity()
will return the id from the insert in the original table.So if you don't have any triggers,
scope_identity()
and@@identity
will return the same value. If you have triggers, you need to think about what value you'd like.范围标识
:正在执行的存储过程中添加的最后一条记录的标识。@@Identity
:在查询批处理中添加的最后一条记录的标识,或者作为查询的结果,例如执行插入的过程,然后触发一个触发器,然后插入一条记录将返回标识从触发器插入的记录。IdentCurrent
:为表分配的最后一个标识。Scope Identity
: Identity of last record added within the stored procedure being executed.@@Identity
: Identity of last record added within the query batch, or as a result of the query e.g. a procedure that performs an insert, the then fires a trigger that then inserts a record will return the identity of the inserted record from the trigger.IdentCurrent
: The last identity allocated for the table.这是这本书的另一个很好的解释:
Here is another good explanation from the book: