TSQL 中“IDENTITY”的正确使用

发布于 2024-10-06 23:24:56 字数 341 浏览 5 评论 0原文

可能的重复:
获取插入行标识的最佳方法?

什么时候合适,并且应该如何在 T-SQL 中使用不同的 IDENTITY 关键字?

  • SELECT @@IDENTITY,
  • SELECT SCOPE_IDENTITY()
  • SELECT IDENT_CURRENT('表名')

Possible Duplicate:
Best way to get identity of inserted row?

When is it appropriate and how should someone use the different IDENTITY key word in T-SQL?

  • SELECT @@IDENTITY,
  • SELECT SCOPE_IDENTITY()
  • SELECT IDENT_CURRENT(‘tablename’)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦中楼上月下 2024-10-13 23:24:56

看一下 SQL SERVER – @@IDENTITY 与 SCOPE_IDENTITY() 与 IDENT_CURRENT – 检索最后插入的记录标识

摘自文章

选择@@IDENTITY

它返回最后一个 IDENTITY 值
无论如何,在连接上生成
产生该值的表,
并且无论范围如何
产生该值的语句。
@@IDENTITY 将返回最后一个
输入到表中的标识值
您当前的会话。而@@IDENTITY
仅限于当前会话,它
不限于当前范围。
如果表上有一个触发器
导致创建一个身份
另一张桌子,你会得到
最后创建的身份,甚至
如果是创建它的触发器。

选择 SCOPE_IDENTITY()

它返回最后一个 IDENTITY 值
在连接上产生并由
同一范围内的声明,
无论生成的表如何
值。 SCOPE_IDENTITY(),例如
@@IDENTITY,将返回最后一个
当前创造的身份价值
会话,但它也会将其限制为
您当前的范围也是如此。在其他方面
的话,它将返回最后一个
您明确表示的身份值
创建的,而不是任何身份
由触发器或用户创建
定义的函数。

SELECT IDENT_CURRENT('表名')

它返回最后一个 IDENTITY 值
生成在表中,无论
创造价值的连接,以及
无论范围如何
产生该值的语句。
IDENT_CURRENT不受范围限制
和会议;它仅限于一个
指定表。 IDENT_CURRENT 返回
为 a 生成的身份值
任何会话中的特定表和任何
范围。

Have a look at SQL SERVER – @@IDENTITY vs SCOPE_IDENTITY() vs IDENT_CURRENT – Retrieve Last Inserted Identity of Record

From the article

SELECT @@IDENTITY

It returns the last IDENTITY value
produced on a connection, regardless
of the table that produced the value,
and regardless of the scope of the
statement that produced the value.
@@IDENTITY will return the last
identity value entered into a table in
your current session. While @@IDENTITY
is limited to the current session, it
is not limited to the current scope.
If you have a trigger on a table that
causes an identity to be created in
another table, you will get the
identity that was created last, even
if it was the trigger that created it.

SELECT SCOPE_IDENTITY()

It returns the last IDENTITY value
produced on a connection and by a
statement in the same scope,
regardless of the table that produced
the value. SCOPE_IDENTITY(), like
@@IDENTITY, will return the last
identity value created in the current
session, but it will also limit it to
your current scope as well. In other
words, it will return the last
identity value that you explicitly
created, rather than any identity that
was created by a trigger or a user
defined function.

SELECT IDENT_CURRENT(‘tablename’)

It returns the last IDENTITY value
produced in a table, regardless of the
connection that created the value, and
regardless of the scope of the
statement that produced the value.
IDENT_CURRENT is not limited by scope
and session; it is limited to a
specified table. IDENT_CURRENT returns
the identity value generated for a
specific table in any session and any
scope.

护你周全 2024-10-13 23:24:56

MSDN对此有很好的参考。

简而言之,@@IDENTITY 的作用域是服务器上的当前会话(如果例如表上有一个触发器也将 IDENTITY 列添加到表中,那么在 INSERT 后您最终可能会检索到错误的 IDENTITY 值)。

SCOPE_IDENTITY() 将返回当前范围的最后插入的 ID,即不会出现上述问题。

IDENT_CURRENT 不限于任何会话,它返回表级别的信息(跨任何会话为该表生成的最后一个 id)

MSDN has a good reference on this.

In a nutshell, @@IDENTITY's scope is the current session on the server (you could end up retrieving the wrong IDENTITY value after an INSERT if e.g. there is a trigger on the table that also adds to a table with an IDENTITY column).

SCOPE_IDENTITY() will return the last inserted ID for the current scope, i.e. will not give you the problem as outlined above.

IDENT_CURRENT is not limited to any session, it returns info at the table-level (the last id generated for that table across any session)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文