Scope_Identity()、Identity()、@@Identity 和 Ident_Current() 之间有什么区别?

发布于 2024-08-15 14:33:38 字数 208 浏览 5 评论 0 原文

我知道 Scope_Identity()Identity()@@IdentityIdent_Current() 都获得了值身份栏,但我很想知道其中的区别。

我遇到的部分争议是,应用于上述这些函数的范围是什么意思?

我还想要一个使用它们的不同场景的简单示例?

I know Scope_Identity(), Identity(), @@Identity, and Ident_Current() all get the value of the identity column, but I would love to know the difference.

Part of the controversy I'm having is what do they mean by scope as applied to these functions above?

I would also love a simple example of different scenarios of using them?

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

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

发布评论

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

评论(7

戏剧牡丹亭 2024-08-22 14:33:38
  • @@identity 函数返回在同一会话中创建的最后一个身份。
  • scope_identity() 函数返回在同一会话和同一范围内创建的最后一个身份。
  • ident_current(name) 返回为任何会话中的特定表或视图创建的最后一个标识。
  • identity() 函数不用于获取身份,它用于在 select...into 查询中创建身份。

会话是数据库连接。范围是当前查询或当前存储过程。

scope_identity()@@identity 函数不同的情况是,如果表上有触发器。如果您有一个插入记录的查询,导致触发器在某处插入另一条记录,则 scope_identity() 函数将返回查询创建的标识,而 @@identity code> 函数将返回触发器创建的标识。

因此,通常您会使用 scope_identity() 函数。

  • The @@identity function returns the last identity created in the same session.
  • The scope_identity() function returns the last identity created in the same session and the same scope.
  • The ident_current(name) returns the last identity created for a specific table or view in any session.
  • The identity() function is not used to get an identity, it's used to create an identity in a select...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, the scope_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.

长不大的小祸害 2024-08-22 14:33:38

好问题。

  • @@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 an INSERT, and the trigger executes another INSERT 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 an INSERT, 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().

莳間冲淡了誓言ζ 2024-08-22 14:33:38

如果您了解范围和会话之间的区别,那么理解这些方法就会很容易。

一个非常好的 博客文章描述了这种差异:

会话表示正在执行命令的当前连接。

范围表示命令的直接上下文。每个存储过程调用都在其自己的作用域中执行,嵌套调用在调用过程作用域内的嵌套作用域中执行。同样,从应用程序或 SSMS 执行的 SQL 命令在其自己的范围内执行,如果该命令触发任何触发器,则每个触发器都会在其自己的嵌套范围内执行。

由此可见,三种身份检索方法的区别如下:

@@identity 返回会话中生成的最后一个身份值,但任何范围。

scope_identity() 返回会话和范围中生成的最后一个身份值。

ident_current() 返回为任何会话和任何范围中的特定表生成的最后一个标识值。

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:

Session means the current connection that's executing the command.

Scope means the immediate context of a command. Every stored procedure call executes in its own scope, and nested calls execute in a nested scope within the calling procedure's scope. Likewise, a SQL command executed from an application or SSMS executes in its own scope, and if that command fires any triggers, each trigger executes within its own nested scope.

Thus the differences between the three identity retrieval methods are as follows:

@@identity returns the last identity value generated in this session but any scope.

scope_identity() returns the last identity value generated in this session and this scope.

ident_current() returns the last identity value generated for a particular table in any session and any scope.

酒中人 2024-08-22 14:33:38

范围是指执行 INSERT 语句 SCOPE_IDENTITY() 的代码上下文,而不是 @@IDENTITY 的全局范围。

CREATE TABLE Foo(
  ID INT IDENTITY(1,1),
  Dummy VARCHAR(100)
)

CREATE TABLE FooLog(
  ID INT IDENTITY(2,2),
  LogText VARCHAR(100)
)
go
CREATE TRIGGER InsertFoo ON Foo AFTER INSERT AS
BEGIN
  INSERT INTO FooLog (LogText) VALUES ('inserted Foo')
  INSERT INTO FooLog (LogText) SELECT Dummy FROM inserted
END

INSERT INTO Foo (Dummy) VALUES ('x')
SELECT SCOPE_IDENTITY(), @@IDENTITY 

给出不同的结果。

Scope means the code context that performs the INSERT statement SCOPE_IDENTITY(), as opposed to the global scope of @@IDENTITY.

CREATE TABLE Foo(
  ID INT IDENTITY(1,1),
  Dummy VARCHAR(100)
)

CREATE TABLE FooLog(
  ID INT IDENTITY(2,2),
  LogText VARCHAR(100)
)
go
CREATE TRIGGER InsertFoo ON Foo AFTER INSERT AS
BEGIN
  INSERT INTO FooLog (LogText) VALUES ('inserted Foo')
  INSERT INTO FooLog (LogText) SELECT Dummy FROM inserted
END

INSERT INTO Foo (Dummy) VALUES ('x')
SELECT SCOPE_IDENTITY(), @@IDENTITY 

Gives different results.

转角预定愛 2024-08-22 14:33:38

为了澄清 @@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 (a log_id or something), while scope_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.

不必在意 2024-08-22 14:33:38

范围标识:正在执行的存储过程中添加的最后一条记录的标识。

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

海风掠过北极光 2024-08-22 14:33:38

这是这本书的另一个很好的解释:

至于 SCOPE_IDENTITY 和 @@IDENTITY 之间的区别,假设您有一个存储过程 P1,其中包含三个语句:
- 生成新身份值的 INSERT
- 对存储过程 P2 的调用,该存储过程还具有生成新标识值的 INSERT 语句
- 查询函数 SCOPE_IDENTITY 和 @@IDENTITY 的语句
SCOPE_IDENTITY 函数将返回 P1 生成的值(相同会话和范围)。 @@IDENTITY 函数将返回 P2 生成的值(同一会话,无论范围如何)。

Here is another good explanation from the book:

As for the difference between SCOPE_IDENTITY and @@IDENTITY, suppose that you have a stored procedure P1 with three statements:
- An INSERT that generates a new identity value
- A call to a stored procedure P2 that also has an INSERT statement that generates a new identity value
- A statement that queries the functions SCOPE_IDENTITY and @@IDENTITY
The SCOPE_IDENTITY function will return the value generated by P1 (same session and scope). The @@IDENTITY function will return the value generated by P2 (same session irrespective of scope).

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