MS SQL Server 最后插入的 ID

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

MS SQL Server Last Inserted ID

sqlsql-server

提问by user2345661

In my database all tables are using a common table for Sequence(ID_Table).

在我的数据库中,所有表都使用一个通用表作为 Sequence(ID_Table)。

TABLE_ID has two fields (Common_ID, Table_Name).

TABLE_ID 有两个字段(Common_ID、Table_Name)。

If I insert any record in the table, I have to first insert a record in Table_ID(Auto-increment, Table_name) then use that Auto-increment value in my Other Table.

如果我在表中插入任何记录,我必须首先在 Table_ID(Auto-increment, Table_name) 中插入一条记录,然后在我的其他表中使用该自动增量值。

For example, I want to insert in Table_Products which has fields ID(Common_ID), Product_Name, Product_ID(Auto Increment)

例如,我想在具有字段 ID(Common_ID)、Product_Name、Product_ID(Auto Increment) 的 Table_Products 中插入

I want to do something like this:

我想做这样的事情:

INSERT INTO TABLE_ID (Table_NAME), Values (Table_Products)

Get the Inserted ID and use it in Table_Products:

获取插入的 ID 并在 Table_Products 中使用它:

INSERT INTO Table_Products (ID, Product_Name, Product_ID(Auto Increment) 
VALUES (ID from TABLE_ID, SomeProduct, Increment)

回答by Devart

Try this one -

试试这个——

DECLARE @ID BIGINT

INSERT INTO dbo.TABLE_ID (Table_NAME) 
SELECT 'Table_Products'

SELECT @ID = SCOPE_IDENTITY()

INSERT INTO dbo.Table_Products (ID, Product_Name)
SELECT @ID, 'SomeProduct'

回答by Andomar

You can use an insertstatement with the outputclause to generate a new Common_ID. Using insert ... select, you can specify that ID in an insert operation:

您可以使用insert带有output子句的语句来生成新的 Common_ID。使用insert ... select,您可以在插入操作中指定该 ID:

declare @Common_ID as table(ID int)

insert  Table_ID
        (Table_Name)
output  inserted.Common_ID into @Common_ID
values  ('Table_Products')

insert  Table_Products
        (ID, Product_Name)
select  ID
,       'Some Product'
from    @Common_ID 

回答by Maximus

Use SCOPE_IDENTITY() after ur insert statementto get the last inserted id.

在插入语句之后使用 SCOPE_IDENTITY() 来获取最后插入的 id。

DECLARE @Product_Id int

INSERT INTO TABLE_ID (Table_NAME) VALUES (Table_Products);
SELECT @Product_Id=SCOPE_IDENTITY();

Insert INTO Table_Products (ID, Product_Name)
VALUES (ID from TABLE_ID, 'SomeProduct')

回答by Developerzzz

Dear friend you have to select id of last record inserted and then pass it in another table so bellow code will help you very well

亲爱的朋友,您必须选择插入的最后一条记录的 id,然后将其传递到另一个表中,因此下面的代码将对您有所帮助

Insert INTO TABLE_ID (Table_NAME), Values (Table_Products)
DECLARE @ID int;
set @ID = SCOPE_IDENTITY(); 

Insert INTO Table_Products (ID, Product_Name) 
Values (@ID, SomeProduct)

this code will solve your problem i define @ID for your last record id and then insert it in your other table

此代码将解决您的问题

回答by Guy L

For MySql use select LAST_INSERT_ID();

对于 MySql 使用 select LAST_INSERT_ID();

回答by Jeff Puckett

All the other answers so far declare intermediary variables for SCOPE_IDENTITY(), but it could be simpler:

到目前为止,所有其他答案都为 声明了中间变量SCOPE_IDENTITY(),但它可能更简单:

INSERT INTO dbo.TABLE_ID (Table_NAME) VALUES 'Table_Products';

INSERT INTO dbo.Table_Products (ID, Product_Name) VALUES (SCOPE_IDENTITY(),'SomeProduct');

回答by Edper

You could be also more table-specific using IDENT_CURRENT()

您还可以使用 IDENT_CURRENT() 更特定于表

Insert INTO TABLE_ID (Table_NAME), Values (Table_Products)

select @NewID=IDENT_CURRENT('TABLE_ID')

Insert INTO Table_Products (ID, Product_Name, Product_ID
Values (@NewID, SomeProduct, Increment)