I have a table with two columns:
CREATE TABLE MyTable(
Id int IDENTITY(1,1) NOT NULL,
Name nvarchar(100) NOT NULL);
I want to duplicate data using the SELECT INSERT statement:
INSERT INTO MyTable (Name)
SELECT Name FROM MyTable
and here is part of the trick - I want to get a mapping table between the original identifier and the new identifier:
DECLARE @idsMap TABLE (OriginalId int, NewId int)
I know, I suppose to use the OUTPUT clause , but for some reason it does not work:
INSERT INTO MyTable (Name)
OUTPUT t.Id, INSERTED.Id INTO @idsMap (OriginalId, NewId)
SELECT Name FROM MyTable t
Related questions:
Can SQL be inserted using select return multiple identity?
Can I insert with a table parameter and also get identifier values?
source
share