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 the data using SELECT INSERT statement:
INSERT INTO MyTable (Name)
SELECT Name FROM MyTable
and here is the trickey part - I want to retrieve a mapping table between the original identity and the new identity:
DECLARE @idsMap TABLE (OriginalId int, NewId int)
I know I suppose to use the OUTPUT clause, but for some reason it doesn't work:
INSERT INTO MyTable (Name)
OUTPUT t.Id, INSERTED.Id INTO @idsMap (OriginalId, NewId)
SELECT Name FROM MyTable t
-- Returns error The multi-part identifier "t.Id" could not be bound.
Related questions:
can SQL insert using select return multiple identities?
Possible to insert with a Table Parameter, and also retrieve identity values?
MERGE
trick ,easily found by looking at related questions on here. – AlejandraalejandrinaMERGE
you are talking about. Isn't it something resemblesUPSERT
? How can I apply it to this problem? – CoursonUPSERT
. You arrange for it to always actuallyINSERT
but you're allowed to access the source table in theOUTPUT
clause, unlike here with theINSERT
statement. – Alejandraalejandrina