I'm trying to duplicate some rows in a table, with a couple of changed values, and I also need to store an old (that will be lost) id to do further processing later. I'm trying to use the Output Clausule to store that information, but SQL Server is throwing the following error:
Msg 4104 <...> The multi-part identifier could not be bound.
This is the table I'm duplicating the data (slightly modified to reduce the number of columns):
Create Table Elements
(
id int Identity(0,1) not null, --PK
name varchar(50) not null,
modelID int not null, --FK
constraint PK_Elements primary key (id)
);
And this is my my query:
declare @outputTable table
(
oldElementID int,
id int,
name varchar(50),
modelID bigint
);
Insert into Elements
(name, modelID)
Output e.id as oldElementID,
Inserted.id,
Inserted.name,
Inserted.modelID into @outputTable
select e.name, @newModelID
from Elements as e
where e.modelID = @oldModelID
Note: @oldModelID and @newModelID are previously declared and set.
I'm not sure if my logic is wrong and I have to take a different approach (but I was sure it was possible to do it this way). Or if I simply have an error that I can't quite put my finger on it.
Any help would be appreciated.
Thanks!