I will import many data rows from a csv file into a SQL Server database (through a web application). I need the auto generated id value back for the client.
If I do this in a loop, the performance is very bad (but I can use SCOPE_IDENTITY()
without any problems).
A more performant solution would be a way like this:
INSERT INTO [MyTable]
VALUES ('1'), ('2'), ('3')
SELECT SCOPE_IDENTITY()
Is there any way to get all generated IDs and not only the last generated id?
SCOPE_IDENTITY()
only gives you the one, latest insertedIDENTITY
value. But you could check out theOUTPUT
clause of SQL Server .... – CircumspectionsqlBuilder.Append("OUTPUT INSERTED.autoid ");
. No need to put the output in a table variable if you want the output to your client code. – Mendie