1

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?

user160820
  • 14,866
  • 22
  • 67
  • 94
  • I think you mean INSERT rather than INSET. Are you using a programming language, or are you doing this directly on the db? What variety of SQL? MSSQL? MySQL? – DiMono Mar 05 '13 at 15:37
  • Thank for your comment It is INSERT statement for MSSQL – user160820 Mar 05 '13 at 15:38
  • http://stackoverflow.com/questions/5323528/get-the-id-of-last-inserted-records – hsan Mar 05 '13 at 15:38

2 Answers2

2

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
1

Can you use an OUTPUT clause in your INSERT?

http://blog.sqlauthority.com/2007/10/01/sql-server-2005-output-clause-example-and-explanation-with-insert-update-delete/

PM 77-1
  • 12,933
  • 21
  • 68
  • 111