Question

I have following code in my SQL Server 2008 R2 stored procedure. In that stored procedure, I am copying one city to another city with it's family and persons.

Here I maintain family's source and target id in @FamilyIdMap.

left column indicates the codes line no.

-- Copy Person
1>      DECLARE @PersonIdMap table (TargetId int, SourceId int)
2>      MERGE Person as PersonTargetTable
3>      USING (SELECT PersonID, FamilyID, PersonName, ParentID FROM Person
4>      WHERE FamilyID in (SELECT FamilyID from Family where FamilyName like '%DA%'))
5>      AS PersonSourceTable ON (0=1)
6>      WHEN NOT MATCHED THEN
7>      INSERT(FamilyID, PersonName, ParentID)
8>      VALUES
9>      ((SELECT TOP 1 TargetID from @FamilyIdMap WHERE SourceID=FamilyID),PersonName, 
10>     ParentID) OUTPUT
11>     INSERTED.PersonID, PersonSourceTable.PersonID
12>     INTO @PersonIdMap;

It gives the output like this:

Source Table

PersonID    FamilyID    PersonName  ParentID
1           1           ABC         Null
2           1           Son of ABC  1
3           1           Son of ABC  1
4           2           XYZ         NULL
5           2           Son of XYZ  4

Target Table (Copied from Source Table using above given code)

PersonID    FamilyID    PersonName  ParentID
6           1           ABC         Null
7           1           Son of ABC  1 <-- ParentID Remains as it is
8           1           Son of ABC  1 <--
9           2           XYZ         NULL
10          2           Son of XYZ  4 <--

Problem in above output is it doesn't update the parentID, I want the output to be this:

Expected Target Table

PersonID    FamilyID    PersonName  ParentID
6           1           ABC         Null
7           1           Son of ABC  6 <-- ParentID should be updated
8           1           Son of ABC  6 <--
9           2           XYZ         NULL
10          2           Son of XYZ  9 <--

I know problem is at line # 10 of code

10>     ParentID) OUTPUT

but what should I replace with ParentID to update it ? Thanks in advance.

Was it helpful?

Solution

What you are trying to do cannot be done in a single step in SQL Server 2008R2. Updating the ParentId has to be a second step, as you cannot access OUTPUT values in one row that where the result of the insert of another row. However, you are already collecting the information for the second step. So, you just need to add a simple update.

IF OBJECT_ID('dbo.Person') IS NOT NULL DROP TABLE dbo.Person;
IF OBJECT_ID('dbo.Family') IS NOT NULL DROP TABLE dbo.Family;

CREATE TABLE dbo.Family(FamilyID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, FamilyName NVARCHAR(60));
CREATE TABLE dbo.Person(PersonID INT IDENTITY(1,1) PRIMARY KEY CLUSTERED, FamilyID INT REFERENCES dbo.Family(FamilyID), PersonName NVARCHAR(60), ParentID INT);
INSERT INTO dbo.Family(FamilyName) VALUES
('DA1'),
('DA2');

INSERT INTO dbo.Person(FamilyID, PersonName, ParentID) VALUES
(1, 'ABC', NULL),
(1, 'Son of ABC', 1),
(1, 'Son of ABC', 1),
(2, 'XYZ', NULL),
(2, 'Son of XYZ', 4 );

DECLARE @FamilyIdMap table (TargetId int, SourceId int)
MERGE dbo.Family tf
USING (SELECT * FROM dbo.Family WHERE FamilyName like '%DA%') AS sf
ON 1=0
WHEN NOT MATCHED THEN
INSERT (FamilyName)
VALUES(sf.FamilyName)
OUTPUT INSERTED.FamilyID, sf.FamilyID
INTO @FamilyIdMap;

DECLARE @PersonIdMap table (TargetId int, SourceId int)

MERGE dbo.Person as tp
USING (SELECT p.PersonID, p.FamilyID, p.PersonName, p.ParentID, fm.SourceId,fm.TargetId FROM Person AS p
INNER JOIN @FamilyIdMap AS fm 
ON p.FamilyID = fm.SourceId) AS sp
ON (0=1)
WHEN NOT MATCHED THEN
INSERT(FamilyID, PersonName, ParentID)
VALUES
(sp.TargetId,PersonName, ParentID) OUTPUT
INSERTED.PersonID, sp.PersonID
INTO @PersonIdMap;

UPDATE p SET
  ParentID = pm.TargetId
FROM dbo.Person AS p
JOIN @PersonIdMap pm
ON pm.SourceId = p.ParentID
WHERE EXISTS(SELECT 1 FROM @PersonIdMap pmf WHERE pmf.TargetId = p.PersonID);

SELECT * FROM dbo.Family;
SELECT * FROM @FamilyIdMap;
SELECT * FROM dbo.Person;
SELECT * FROM @PersonIdMap;

I did add code to create and fill the @FamilyIdMap table. I also cleaned up your original MERGE a little. It is now using the @FamilyIdMap table as a means to select the rows instead of joining to the dbo.Family table again. If you run this only on a small subset of families this should be faster. If you have a lot of families and you copy them all, going against the dbo.Family table again might be faster.

The final UPDATE updates only new rows in the Person table (all newly created PersonIds can be found in the TargetId column of the @PersonIdMap table), changing old ParentId values to new ParentId values using the information in the @PersonIdMap table.

I did not include transaction management, but atleast the MERGE dbo.Person and the following UPDATE dbo.Person should be executed inside the same transaction.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top