SQL 获取最后插入的行ID(用SQL语句)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9477502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get the last inserted row ID (with SQL statement)
提问by Snake Eyes
I want to get the new created ID when you insert a new record in table.
当您在表中插入新记录时,我想获取新创建的 ID。
I read this: http://msdn.microsoft.com/en-us/library/ms177564.aspxbut it needs to create temporary table.
我读到这个:http: //msdn.microsoft.com/en-us/library/ms177564.aspx但它需要创建临时表。
I want to return the ID after executing INSERT statement (assuming executing just one INSERT).
我想在执行 INSERT 语句后返回 ID(假设只执行一个 INSERT)。
Example:
例子:
1 Joe Joe
2 Michael Mike
3 Zoe Zoe
When executing an INSERT statement, I want to return the created ID, means 4.
执行INSERT语句时,我想返回创建的ID,意思是4。
Can tell me how to do that using SQL statement or it is not possible ?
可以告诉我如何使用 SQL 语句来做到这一点,或者这是不可能的吗?
回答by marc_s
If your SQL Server table has a column of type INT IDENTITY
(or BIGINT IDENTITY
), then you can get the latest inserted value using:
如果您的 SQL Server 表有一个类型为INT IDENTITY
(或BIGINT IDENTITY
)的列,那么您可以使用以下方法获取最新插入的值:
INSERT INTO dbo.YourTable(columns....)
VALUES(..........)
SELECT SCOPE_IDENTITY()
This works as long as you haven't inserted another row - it just returns the last IDENTITY
value handed out in this scope here.
只要您没有插入另一行,它就可以工作 - 它只返回IDENTITY
在此范围内分发的最后一个值。
There are at least two more options - @@IDENTITY
and IDENT_CURRENT
- read more about how they works and in what way they're different (and might give you unexpected results) in this excellent blog post by Pinal Dave here.
至少还有两个选项 -@@IDENTITY
并且IDENT_CURRENT
- 在此处由 Pinal Dave 撰写的这篇出色的博客文章中阅读更多关于它们如何工作以及它们以什么方式不同(并且可能会给您带来意想不到的结果)的信息。
回答by Aaron Bertrand
Assuming a simple table:
假设一个简单的表:
CREATE TABLE dbo.foo(ID INT IDENTITY(1,1), name SYSNAME);
We can capture IDENTITY
values in a table variable for further consumption.
我们可以IDENTITY
在表变量中捕获值以供进一步使用。
DECLARE @IDs TABLE(ID INT);
-- minor change to INSERT statement; add an OUTPUT clause:
INSERT dbo.foo(name)
OUTPUT inserted.ID INTO @IDs(ID)
SELECT N'Fred'
UNION ALL
SELECT N'Bob';
SELECT ID FROM @IDs;
The nice thing about this method is (a) it handles multi-row inserts (SCOPE_IDENTITY()
only returns the last value) and (b) it avoids this parallelism bug, which can lead to wrong results, but so far is only fixed in SQL Server 2008 R2 SP1 CU5.
这种方法的好处是(a)它处理多行插入(SCOPE_IDENTITY()
只返回最后一个值)和(b)它避免了这个并行错误,这可能导致错误的结果,但到目前为止只在 SQL Server 2008 中得到修复R2 SP1 CU5。
回答by MaxRecursion
You can use:
您可以使用:
SELECT IDENT_CURRENT('tablename')
to access the latest identity for a perticular table.
访问特定表的最新标识。
e.g. Considering following code:
例如考虑以下代码:
INSERT INTO dbo.MyTable(columns....) VALUES(..........)
INSERT INTO dbo.YourTable(columns....) VALUES(..........)
SELECT IDENT_CURRENT('MyTable')
SELECT IDENT_CURRENT('YourTable')
This would yield to correct value for corresponding tables.
这将产生相应表的正确值。
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.
它返回IDENTITY
表中生成的最后一个值,无论创建该值的连接如何,也无论生成该值的语句的范围如何。
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.
IDENT_CURRENT
不受范围和会话的限制;它仅限于指定的表。IDENT_CURRENT
返回为任何会话和任何范围内的特定表生成的标识值。