I have a situation similar to this SO question where I want to copy parent rows and their child rows from and into the same table.
The suggested answer using OUTPUT
and MERGE
is clean but only considers just one record.
Is there a way to modify it to handle multiple parent rows?
EDIT
I tried to modify the SQL statement and came up with this:
DECLARE @fromId int, @toId int;
SET @fromId = 1;
SET @toId = 2;
DECLARE @mapping TABLE (old_id int, new_id int);
INSERT INTO parent_table (col1, col2, col3)
SELECT @toId, col2, col3
FROM parent_table
WHERE col1 = @fromId;
MERGE child_table tgt
USING (
SELECT t.parent_id, t.col2, t.col3
FROM child_table t
inner join parent_table p on p.id = t.parent_id
WHERE p.col1 = @toId
) src
ON 0 = 1
WHEN NOT MATCHED THEN
INSERT (parent_id, col2, col3) VALUES (src.parent_id, src.col2, src.col3)
OUTPUT src.parent_id, INSERTED.parent_id INTO @mapping (old_id, new_id);
The first INSERT
of the parent rows works. However, the second insert does insert any child rows. What am I missing?