简体   繁体   English

使用@@ identity检索PK

[英]Retrieve the PK using @@identity

I'm building a website using ASP.NET and SQL Server, and I use 我正在使用ASP.NET和SQL Server构建一个网站,我使用

SELECT PK FROM Table WHERE PK = @@identity

My question is which is better and more reliable to retrieve the last inserted PK for multiuser website, using @@identity or using this: 我的问题是使用@@identity或使用以下内容检索多用户网站的最后插入的PK更好更可靠:

SELECT MAX(PK) FROM Table WHERE PK = Session ("UserID")

I'm not sure exactly what you want to achieve, but the recommended way to retrieve the primary key value of the last statement on a connection is to use SCOPE_IDENTITY() 我不确定你想要实现什么,但是检索连接上最后一个语句的主键值的推荐方法是使用SCOPE_IDENTITY()

@@Identity is particularly risky where you are using triggers, since it returns the last generated identity value, including those generated by triggers flowing on from a statement. @@ Identity在使用触发器时特别危险,因为它返回最后生成的标识值,包括从语句流入的触发器生成的标识值。

MSDN has the following to say: MSDN有如下说法:

SCOPE_IDENTITY and @@IDENTITY return the last identity values that are generated in any table in the current session. SCOPE_IDENTITY和@@ IDENTITY返回当前会话中任何表中生成的最后一个标识值。 However, SCOPE_IDENTITY returns values inserted only within the current scope; 但是,SCOPE_IDENTITY返回仅在当前范围内插入的值; @@IDENTITY is not limited to a specific scope. @@ IDENTITY不限于特定范围。

You should certainly use SCOPE_IDENTITY() in favour of the MAX(PK) approach - any number of possible future changes could invalidate this method. 您当然应该使用SCOPE_IDENTITY()来支持MAX(PK)方法 - 任何可能的未来更改都可能使此方法无效。

For SQL Server 2005 and above... 对于SQL Server 2005及更高版本...

You can do the INSERT and SELECT in one call using the OUTPUT clause... 您可以使用OUTPUT子句在一次调用中执行INSERT和SELECT ...

INSERT MyTable (col1, col2, ..., coln)
OUTPUT INSERTED.keycol, INSERTED.col1, INSERTED.col2, ..., INSERTED.coln
VALUES (val1, val2, ..., valn)

Otherwise, you only use SCOPE_IDENTITY() 否则,您只使用SCOPE_IDENTITY()

As mentioned by @David Hall the @@IDENTITY keyword returns the most recently created identity for your current connection, not always the identity for the recently added record in your query and may return an incorrect value. 正如@David Hall所提到的, @@IDENTITY关键字返回当前连接最近创建的标识,并不总是查询中最近添加的记录的标识,并且可能返回不正确的值。 Using MAX(PK) there is a higher chance for an incorrect value and I'd strongly recommend against using it. 使用MAX(PK)值的可能性更大,我强烈建议不要使用它。 To avoid the any race conditions I'd suggest that you use SCOPE_IDENTITY() to return the identity of the recently added record in your INSERT SQL Statement or Stored Procedure. 为了避免任何竞争条件,我建议您使用SCOPE_IDENTITY()在INSERT SQL语句或存储过程中返回最近添加的记录的标识。

Depends on what you're trying to accomplish. 取决于你想要完成的事情。 If you want to return the just-generated ID to the ASP.NET code (a typical scenario), then @@identity is your friend. 如果要将刚刚生成的ID返回到ASP.NET代码(典型场景),那么@@ identity就是您的朋友。 In a high-concurrency situation, mak(PK) is not even guaranteed to be the PK you're after. 在高并发的情况下,mak(PK)甚至不能保证你所追求的PK。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM