在 SQL 中检索插入的行 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4734589/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 08:57:11  来源:igfitidea点击:

Retrieve inserted row ID in SQL

sql

提问by charuka

How do I retrieve the ID of an inserted row in SQL?

如何在 SQL 中检索插入行的 ID?

Users Table:

用户表:

Column  | Type
--------|--------------------------------
ID      | * Auto-incrementing primary key
Name    | 
Age     | 

Query Sample:

查询示例:

insert into users (Name, Age) values ('charuka',12)

回答by RichardTheKiwi

In MySQL:

在 MySQL 中:

SELECT LAST_INSERT_ID();

In SQL Server:

在 SQL Server 中:

SELECT SCOPE_IDENTITY();

In Oracle:

在甲骨文中:

SELECT SEQNAME.CURRVAL FROM DUAL;

In PostgreSQL:

在 PostgreSQL 中:

SELECT lastval();

(edited: lastval is any, currval requires a named sequence) Note: lastval() returns the latest sequence value assigned by your session, independently of what is happening in other sessions.

(编辑:lastval 是任何,currval 需要一个命名序列) 注意:lastval() 返回由您的会话分配的最新序列值,与其他会话中发生的情况无关。

回答by marc_s

In SQL Server, you can do (in addition to the other solutions already present):

在 SQL Server 中,您可以执行以下操作(除了已经存在的其他解决方案):

INSERT INTO dbo.Users(Name, Age) 
OUTPUT INSERTED.ID AS 'New User ID'
VALUES('charuka', 12)

The OUTPUTclause is very handy when doing inserts, updates, deletes, and you can return any of the columns - not just the auto-incremented IDcolumn.

OUTPUT子句在执行插入、更新、删除时非常方便,您可以返回任何列 - 而不仅仅是自动递增的ID列。

Read more about the OUTPUT clause in the SQL Server Books Online.

SQL Server 联机丛书中阅读有关 OUTPUT 子句的更多信息

回答by a_horse_with_no_name

In Oracle and PostgreSQL you can do this:

在 Oracle 和 PostgreSQL 中,您可以这样做:

INSERT INTO some_table (name, age)
VALUES
('charuka', 12)
RETURNING ID

When doing this through JDBC you can also do that in a cross-DBMS manner (without the need for RETURNING) by calling getGeneratedKeys() after running the INSERT

通过 JDBC 执行此操作时,您还可以通过在运行 INSERT 后调用 getGeneratedKeys() 以跨 DBMS 方式(无需返回)执行此操作

回答by Sim2K

I had the same need and found this answer ..

我有同样的需求并找到了这个答案..

This creates a record in the company table (comp), it the grabs the auto ID created on the company table and drops that into a Staff table (staff) so the 2 tables can be linked, MANY staff to ONE company. It works on my SQL 2008 DB, should work on SQL 2005 and above.

这会在公司表 (comp) 中创建一条记录,它会获取在公司表上创建的自动 ID 并将其放入员工表 (staff) 中,以便可以将 2 个表链接到一个公司,许多员工。它适用于我的 SQL 2008 DB,应该适用于 SQL 2005 及更高版本。

===========================

============================

CREATE PROCEDURE [dbo].[InsertNewCompanyAndStaffDetails]

 @comp_name varchar(55) = 'Big Company',

 @comp_regno nchar(8) = '12345678',

 @comp_email nvarchar(50) = '[email protected]',

 @recID INT OUTPUT

-- The '@recID'is used to hold the Company auto generated ID number that we are about to grab

-- ' @recID'用于保存我们将要抓取的公司自动生成的 ID 号

AS
 Begin

  SET NOCOUNT ON

  DECLARE @tableVar TABLE (tempID INT)

-- The line above is used to create a tempory table to hold the auto generated ID number for later use. It has only one field 'tempID' and its type INTis the same as the '@recID'.

-- 上面的行用于创建一个临时表来保存自动生成的 ID 号供以后使用。它只有一个字段 'tempID' 并且其类型INT'@recID'相同。

  INSERT INTO comp(comp_name, comp_regno, comp_email) 

  OUTPUT inserted.comp_id INTO @tableVar

-- The 'OUTPUT inserted.' line above is used to grab data out of any field in the record it is creating right now. This data we want is the ID autonumber. So make sure it says the correct field name for your table, mine is 'comp_id'. This is then dropped into the tempory table we created earlier.

-- ' OUTPUT 插入。' 上面的行用于从它正在创建的记录中的任何字段中获取数据。我们想要的数据是 ID 自动编号。因此,请确保它为您的表显示正确的字段名称,我的是'comp_id'。然后将其放入我们之前创建的临时表中。

  VALUES (@comp_name, @comp_regno, @comp_email)

  SET @recID = (SELECT tempID FROM @tableVar)

-- The line above is used to search the tempory table we created earlier where the ID we need is saved. Since there is only one record in this tempory table, and only one field, it will only select the ID number you need and drop it into '@recID'. '@recID' now has the ID number you want and you can use it how you want like i have used it below.

-- 上一行用于搜索我们之前创建的临时表,其中保存了我们需要的ID。由于这个临时表中只有一条记录,而且只有一个字段,因此它只会选择您需要的 ID 号并将其放入“ @recID”中。“ @recID”现在有了您想要的 ID 号,您可以像我在下面使用的那样随意使用它。

  INSERT INTO staff(Staff_comp_id) 
  VALUES (@recID)

 End

-- So there you go. I was looking for something like this for ages, with this detailed break down, I hope this helps.

——所以你去。多年来,我一直在寻找这样的东西,有了这个详细的分解,我希望这会有所帮助。