I need to insert data into a table and I need to output the newly inserted ids from the destination table mapped alongside the Ids from the source table. I have the following query:
DECLARE @mapping TABLE(
DestId int,
SourceId int);
INSERT INTO dest_table (column1, column2)
OUTPUT INSERTED.Id as DestId, src_table.Id as SourceId INTO @mapping
SELECT
src_table.column1,
src_table.column2
FROM src_table
Which gives the following error:
The multi-part identifier "src_table.Id" could not be bound.
Normally, I would use a MERGE statement. However in this case it has to run on SQL Server 2005!!!
Is there any way of achieving this without resolving to a cursor and inserting values one by one?