码迷,mamicode.com
首页 > 其他好文 > 详细

08 Scop_Identity(),@@Identity,Ident_Current()的区别(What is the difference between Scope_Identity(),@@Identity, and Ident_Current?)

时间:2016-01-16 14:04:40      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

在StackOverflow上有一个帖子很好的解释了这三者之间的区别,本文将其大致意思翻译一下。读者也可以直接访问下面的链接查看原帖:http://stackoverflow.com/questions/1920558/what-is-the-difference-between-scope-identity-identity-identity-and-ide

  • Scop_Identity()函数返回的是同一个session 中的同一个scope的最后一个identity值。
  • @@Identity是一个变量,它返回的是同一个session中的最后一个identity值。
  • Ident_Current(‘tableName‘)函数返回的是所有session中的所有scope的最后一个identity值。

也就是说,Scop_Identity(),@@Identity,Ident_Current(‘tableName‘)的取值范围逐个增大的。

上面提到的session实际上就是一个数据库链接,scope就是当前查询(query)或者当前存储过程(stored procedure).

当你表中有一个触发器(trigger)的时候,在插入一条数据的时候,该触发器同时去插入另外一张表的数据时,Scop_Identity()和@@Session返回的值是不一样的。Scope_Identity()返回的是当前插入语句所产生的identity值,而@@Identity返回的是触发器插入数据时产生的identity值。

所以,大多数情况下我们使用的是Scop_Identity()函数。

08 Scop_Identity(),@@Identity,Ident_Current()的区别(What is the difference between Scope_Identity(),@@Identity, and Ident_Current?)

标签:

原文地址:http://www.cnblogs.com/kuillldan/p/5135246.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!