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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:39:32+00:00 2026-05-23T19:39:32+00:00

I found this thread here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=16836 I have exactly the same problem. Quote: Rob

  • 0

I found this thread here: http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=16836
I have exactly the same problem. Quote:

Rob Pearmain writes “I have 3 tables that hold questions.

Table 1 : Question

Field : ID (Unique) Field : Name (Text)

Table 2 : Question Text (References Table1-ID)

Field : ID (Unique) Field : QuestionID (integer ref to Table1 ID)
Field : Text

Table 3 : Options

Field : ID (Unique) Field : QuestionTextID (integer ref to Table2 ID)
Field : Text

Say for example, I create a question with 2 Question text records and
5 option records. If I wanted to duplicate that question to a new
question, and copy over the Question Text records to new ID’s, and all
the related options, how can I do this easily (As the duplicate
question will have a new ID, each of the duplicated question text’s
will have new ID’s as will each of the options).”

The suggested solution is:

create procedure CopyQuestion
@idtocopy int
AS
declare @tempquestionid
declare @tempquestiontextid
declare @questiontextid

insert into question (name)
     select name from question where id = @idtocopy

select @tempquestionid = @@identity

declare question_cursor cursor for
     select id from [question text] where id = @idtocopy
open question_cursor
fetch next from question_cursor into @questiontextid
while @@fetch_status = 0
begin
   insert into [question text] (questionid, text)
      select @tempquestionid, text from [question text] where id = @questiontextid
   select @tempquestiontextid = @@identity
      insert into [options] (questiontextid, text)
   select @tempquestiontextid, text from [options] where questiontextid = @questiontextid
   fetch next from question_cursor into @questiontextid
end
close question_cursor
deallocate question_cursor

Is there a better solution to this problem? I will use an insert trigger.
Thanks!

  • 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-23T19:39:32+00:00Added an answer on May 23, 2026 at 7:39 pm

    You can use the merge statement with the output clause to get a match between the old and new id in questionText. This is described in this question Using merge..output to get mapping between source.id and target.id.

    In your case the code would look something like this. The code is not tested so there might be any number of typos in there but it shows what you can do.

    create procedure CopyQuestion
      @idtocopy int
    as
    
    declare @QuestionID int
    
    insert into question
    select Name 
    from question 
    where ID = @idtocopy
    
    select @QuestionID = scope_identity() 
    
    declare @IDs table (NewQID int, OldQID int)
    
    merge questionText as T
    using (select ID, @QuestionID as QuestionID, Field
           from questionText
           where QuestionID = @idtocopy) as S
    on 0=1
    when not matched then
      insert (QuestionID, Field) values (QuestionID, Field)
    output inserted.ID, S.ID   into @IDs;       
    
    insert into options
    select 
        I.NewQID,
        O.Field
    from options O
      inner join @IDs as I
        on O.QuestionTextID = I.OldQID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here: http://download.oracle.com/docs/html/A95907_01/diff_uni.htm#1077398 I found that on Windows Oracle is thread based, while on Unix
I found this: http://www.evolt.org/failover-database-connection-with-php-mysql and similar examples. But is there a better way? I
I found this via google: http://www.mvps.org/access/api/api0008.htm '******************** Code Start ************************** ' This code was
I found this link http://artis.imag.fr/~Xavier.Decoret/resources/glsl-mode/ , but there isn't a lot of description around
I'm trying to select an select element by name I found this thread showing
Integrating SimpleModal with ASP.NET and MasterPages This is a continuation of a previous thread
I found this PECL package called threads , but there is not a release
Reading this question I found this as (note the quotation marks) code to solve
I found this open-source library that I want to use in my Java application.
I found this in an article on Multithreaded Apartments, but can’t find a definition

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.