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 1111937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:37:43+00:00 2026-05-17T02:37:43+00:00

My database looks like this: Questionnaire Id Description Category id description QuestionnaireId (FK) Question

  • 0

My database looks like this:

Questionnaire 
Id 
Description

Category
id
description
QuestionnaireId (FK)    

Question
id
CategoryId (FK)
field

When I copy a questionnaire, I’d like to copy all the underlying tables. So this means that the table Questionnaire gets a new Id. Then, all the belonging categories of the questionnaire must also be copied. So the newly inserted categories must get the new questionnaire Id. After the categories, the questions must be copied. But the categoryId must be updated to the newly inserted category.

How can I do this using t-sql?

  • 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-17T02:37:44+00:00Added an answer on May 17, 2026 at 2:37 am

    This is pretty easy to accomplish, but you have to keep track of everything as you go. I would generally create a single SP for this, which takes as an input the questionnaire to copy.

      DECLARE @newQuestionnaireId INT
      INSERT INTO Questionnaire
      (Id,Description)
      SELECT Id, Description 
      FROM Questionnaire
      WHERE ID = @sourceQuestionnaireID
      SET @newquestionnaireId = SCOPE_IDENTITY()
    

    At this point you have a new header record, and the newly generated Id for the copy. The next step is to load the categories into a temp table which has an extra field for the new Id

    DECLARE @tempCategories TABLE (id INT, description VARCHAR(50),newId INT)
    INSERT INTO @tempCategories(id,description)
    SELECT id, description FROM Category 
    WHERE questionnaireId = @sourceQuestionnaireId
    

    Now, you have a temp table with all the categories to insert, along with a field to backfill the new ID for this category. Use a cursor to go over the list inserting the new record, and use a similar SCOPE_IDENTITY call to backfill the new Id.

    DECLARE cuCategory CURSOR FOR SELECT Id, Description FROM @tempCategories
    DECLARE @catId INT, @catDescription, @newCatId INT
    OPEN cuCategory
    FETCH NEXT FROM cuCategory INTO @catId,@catDescription
    WHILE @@FETCH_STATUS<>0
    BEGIN
      INSERT INTO Category(description,questionnaireId)
      VALUES(@catDescription,@newQuestionnaireId)
      SET @newCatId = SCOPE_IDENTITY()
    
      UPDATE @tempCategories SET newCatId=@newCatId
      WHERE id=@catId
      FETCH NEXT FROM cuCategory INTO @catId,@catDescription
    END
    CLOSE cuCategory
    DEALLOCATE cuCategory
    

    At this point you now have a temp table which maps the catId from the original questionnaire to the catId for the new questionnaire. This can be used to fill the final table in much the same way – which i’ll leave as an excercise for you, but feel free to post back here if you have difficulty.

    Finally, I would suggest that this whole operation is carried out within a transaction to save you from half completed copies when something goes wrong.

    A couple of disclaimers: The above was all typed quickly, dont expect it to work off the bat. Second, Ive assumed that all your PK’s are identity fields, which they should be! If they’re not just replace the SCOPE_IDENTITY() calls with the appropriate logic to generate the next ID.

    Edit: documentation for Cursor operations can be foundhere

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

Sidebar

Related Questions

say i have a nvarchar field in my database that looks like this 1,
I have a database that looks like this: ID parent ticket category _record_status _log_user
I have a database that looks like this: ID parent name description _record_status _log_user
For an access database that looks like this: (All text fields) Co1 Co2 Co3
I have two tables in a database that looks like this members table1 id
I have a script to delete all tables in my database that looks like
My Table 'Levels' in a SQL Server 2005 database looks like this: LevelId Description
My database looks like this: Book,Chapter,Verse,Scripture 1,1,1,1text1 1,1,2,1text2 1,1,3,1text3 1,1,4,1text4 1,2,1,2text1 1,2,2,2text2 1,2,3,2text3 I
I have data in an MYSQL database that looks like this: Project Date Time
I have a table in a MSSQL database that looks like this: Timestamp (datetime)

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.