user160820
user160820

Reputation: 15200

getting last inserted ids

I am using SQL statement like

INSERT INTO TableName (col1, col2, col3)
VALUES
(val1, val2, val3),
(val1, val2, val3),
(val1, val2, val3),
(val1, val2, val3)

Is there a way to get the list of all newly inserted rows?

Upvotes: 1

Views: 101

Answers (2)

marc_s
marc_s

Reputation: 754258

Yes - if you're on SQL Server 2005 or newer - use the OUTPUT clause!

INSERT INTO TableName (col1, col2, col3)
OUTPUT Inserted.ID, Inserted.col1
VALUES
(val1, val2, val3),
(val1, val2, val3),
(val1, val2, val3),
(val1, val2, val3)

This will output the ID and col1 column for all new rows inserted.

Upvotes: 2

Related Questions