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

  • SEARCH
  • Home
  • 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 1052503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:06:13+00:00 2026-05-16T17:06:13+00:00

I am creating a SQL 2008 R2 stored procedure to duplicate a row and

  • 0

I am creating a SQL 2008 R2 stored procedure to duplicate a row and all it’s children.

It’s a 3-tiered setup with a Parent, Child and Sub-Child
Given the ID of the parent I need to create a duplicate.

I have solved it using a fast_forward cursor.

I know I can also do it with a while loop through rows but I do not believe that will be faster than this cursor method. What are your thoughts?

Is there a better way to accomplish this task without using cursors?

EDIT: Another option I considered was creating a temp table holding the old / new PKID’s of the TBLACStages records.

TBLACStages may have anywhere from 1 to 20 corresponding rows (and TBLACUpgrade will likely have 3 rows per TBLACStages row)

CREATE PROCEDURE [dbo].[spDuplicateACUnit]
@pACUnitID bigint = 0 
AS BEGIN
SET NOCOUNT ON;

DECLARE @NewACUnitID bigint = 0

INSERT INTO TBLACUnits ([col1] ,[col2] ,[...] ,[coln]) SELECT [col1] ,[col2] ,[...] ,[coln] FROM TBLACUnits WHERE ACUnitID = @pACUnitID

SELECT @NewACUnitID = SCOPE_IDENTITY()

DECLARE @ACStageID bigint = 0 
    DECLARE @NewACStageID bigint = 0

DECLARE @ACUnitCursor CURSOR

SET @ACUnitCursor = CURSOR LOCAL FAST_FORWARD FOR SELECT ACStageID FROM TBLACStages WHERE TBLACStages.ACUnitID = @pACUnitID

OPEN @ACUnitCursor

FETCH NEXT FROM @ACUnitCursor INTO @ACStageID

WHILE @@FETCH_STATUS = 0 
BEGIN

INSERT INTO TBLACStages ([ACUnitID] ,[col1] ,[col2] ,[...] ,[coln]) SELECT @NewACUnitID ,[col1] ,[col2] ,[...] ,[coln] FROM TBLACStages WHERE TBLACStages.ACStageID = @ACStageID

SELECT @NewACStageID = SCOPE_IDENTITY()

INSERT INTO TBLACUpgrade ([ACStageID] ,[col1] ,[col2] ,[...] ,[coln]) SELECT @NewACStageID ,[col1] ,[col2] ,[...] ,[coln] FROM TBLACUpgrade WHERE TBLACUpgrade.[ACStageID] = @ACStageID

FETCH NEXT FROM @ACUnitCursor INTO @ACStageID 
END

CLOSE @ACUnitCursor DEALLOCATE @ACUnitCursor

END

GO
  • 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-05-16T17:06:14+00:00Added an answer on May 16, 2026 at 5:06 pm

    This should give you the idea:

    CREATE TABLE t_parent (id INT NOT NULL PRIMARY KEY IDENTITY, value VARCHAR(100))
    CREATE TABLE t_child (id INT NOT NULL PRIMARY KEY IDENTITY, parent INT NOT NULL, value VARCHAR(100))
    CREATE TABLE t_grandchild (id INT NOT NULL PRIMARY KEY IDENTITY, child INT NOT NULL, value VARCHAR(100))
    
    INSERT
    INTO    t_parent (value)
    VALUES  ('Parent 1')
    
    INSERT
    INTO    t_parent (value)
    VALUES  ('Parent 2')
    
    INSERT
    INTO    t_child (parent, value)
    VALUES  (1, 'Child 2')
    
    INSERT
    INTO    t_child (parent, value)
    VALUES  (2, 'Child 2')
    
    INSERT
    INTO    t_grandchild (child, value)
    VALUES  (1, 'Grandchild 1')
    
    INSERT
    INTO    t_grandchild (child, value)
    VALUES  (1, 'Grandchild 2')
    
    INSERT
    INTO    t_grandchild (child, value)
    VALUES  (2, 'Grandchild 3')
    
    DECLARE @parent TABLE (oid INT, nid INT)
    DECLARE @child TABLE (oid INT, nid INT)
    
    MERGE
    INTO    t_parent
    USING   (
            SELECT  id, value
            FROM    t_parent
            ) p
    ON      1 = 0
    WHEN NOT MATCHED THEN
    INSERT  (value)
    VALUES  (value)
    OUTPUT  p.id, INSERTED.id
    INTO    @parent;
    SELECT  *
    FROM    @parent
    MERGE
    INTO    t_child
    USING   (
            SELECT  c.id, p.nid, c.value
            FROM    @parent p
            JOIN    t_child c
            ON      c.parent = p.oid
            ) c
    ON      1 = 0
    WHEN NOT MATCHED THEN
    INSERT  (parent, value)
    VALUES  (nid, value)
    OUTPUT  c.id, INSERTED.id
    INTO    @child;
    SELECT  *
    FROM    @child;
    INSERT
    INTO    t_grandchild (child, value)
    SELECT  c.nid, gc.value
    FROM    @child c
    JOIN    t_grandchild gc
    ON      gc.child = c.oid
    SELECT  *
    FROM    t_grandchild
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.