开发者

Multiple insert identity get

开发者 https://www.devze.com 2023-01-26 06:20 出处:网络
My query in Stored procedure looks like this. insert into a(name) select name from b. I want to get all the new identity 开发者_如何学运维value from a.

My query in Stored procedure looks like this.

insert into a(name) select name from b.

I want to get all the new identity 开发者_如何学运维value from a. How can i do this?

Thanks in advance.


insert into a(name)
OUTPUT INSERTED.IDColumn  --this line for >1 row
select name from b.

The OUTPUT Clause


Have a look at OUTPUT Clause (Transact-SQL)

Something like

DECLARE @a TABLE(
        ID INT IDENTITY (1,1),
        Name VARCHAR(250)
)

DECLARE @b TABLE(
        Name VARCHAR(250)
)
INSERT INTO @b SELECT 'a'
INSERT INTO @b SELECT 'b'
INSERT INTO @b SELECT 'c'
INSERT INTO @b SELECT 'd'

INSERT INTO @a(name) 
OUTPUT INSERTED.ID
SELECT name FROM @b
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号