Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8961769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:58:15+00:00 2026-06-15T15:58:15+00:00

I have following code in my SQL Server 2008 R2 stored procedure. In that

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:58:16+00:00Added an answer on June 15, 2026 at 3:58 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code in one of my Sql (2008) Stored Procs which
I have a T-SQL stored procedure in a SQL Server 2008 database that I
I have following tsql code in sql server 2008: declare @ID INT SET @ID
I have the following code to connect to a sql server compact edition 2008:
How to convert Varchar to Int in sql server 2008. i have following code
I have created an stored procedure in SQL Server 2008 and it contains the
I am having following stored procedure in my SQL Server 2008 R2 database ALTER
I have a SQL Server 2008 procedure that sends email via sp_send_dbmail. I'm using
On SQL Server 2008 R2, I have following T-SQL code: SELECT CAST(GETDATE() AS DATETIMEOFFSET);
I have the following code in a cursor in SQL Server 2008 r2. begin

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.