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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:31:20+00:00 2026-06-05T07:31:20+00:00

I’ve made some modifications to my database and I need to migrate the old

  • 0

I’ve made some modifications to my database and I need to migrate the old data to the new tables. For that, I need to fill a table (ReportOptions) taking the data from the original table (Practice), and fill a second intermediate table (PracticeReportOption).

ReportOption (
    ReportOptionId int PK, 
    field1, field2...
)
Practice (
    PracticeId int PK, 
    field1, field2...
)
PracticeReportOption (
    PracticeReportOptionId int PK, 
    PracticeId int FK, 
    ReportOptionId int FK, 
    field1, field2...
)

I made a query to get all the data I need to move from Practice to ReportOptions, but I’m having trouble filling the intermediate table.

--Auxiliary tables
DECLARE @ReportOption TABLE (
    PracticeId int, -- This field is not on the actual ReportOption table
    field1, field2...
)
DECLARE @PracticeReportOption TABLE (
    PracticeId int, 
    ReportOptionId int, 
    field1, field2
)

--First I get all the data I need to move
INSERT INTO @ReportOption
SELECT P.practiceId, field1, field2...
  FROM Practice P

--I insert it into the new table,
--but somehow I need to have the repation PracticeId / ReportOptionId
INSERT INTO ReportOption (field1, field2...)
OUTPUT @ReportOption.PracticeId, --> this is the field I don't know how to get
       inserted.ReportOptionId
  INTO @PracticeReportOption (PracticeId, ReportOptionId)
SELECT field1, field2
  FROM @ReportOption

-- This would insert the relationship, If I knew how to get it!
INSERT INTO @PracticeReportOption (PracticeId, ReportOptionId)
SELECT PracticeId, ReportOptionId
  FROM @ReportOption

If I could reference a field that is not from the destination table in the OUTPUT clause, that would be great (I think I can’t, but I don’t know for sure). Any ideas on how to accomplish my need?

  • 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-05T07:31:22+00:00Added an answer on June 5, 2026 at 7:31 am

    You can do this by using MERGE instead of INSERT.

    So replace this:

    INSERT INTO ReportOption (field1, field2...)
    OUTPUT @ReportOption.PracticeId, --> this is the field I don't know how to get
           inserted.ReportOptionId
      INTO @PracticeReportOption (PracticeId, ReportOptionId)
    SELECT field1, field2
      FROM @ReportOption
    

    with:

    MERGE INTO ReportOption USING @ReportOption AS temp ON 1 = 0
    WHEN NOT MATCHED THEN
        INSERT (field1, field2)
        VALUES (temp.Field1, temp.Field2)
        OUTPUT temp.PracticeId, inserted.ReportOptionId, inserted.Field1, inserted.Field2
        INTO @PracticeReportOption (PracticeId, ReportOptionId, Field1, Field2);
    

    The key is to use a predicate that will never be true (1 = 0) in the merge search condition, so you will always perform the insert, but have access to fields in both the source and destination tables.


    Here is the entire code I used to test it:

    CREATE TABLE ReportOption (
        ReportOptionID INT IDENTITY(1, 1), 
        Field1 INT, 
        Field2 INT
    )
    CREATE TABLE Practice (
        PracticeID INT IDENTITY(1, 1), 
        Field1 INT, 
        Field2 INT
    )
    CREATE TABLE PracticeReportOption (
        PracticeReportOptionID INT IDENTITY(1, 1), 
        PracticeID INT, 
        ReportOptionID INT, 
        Field1 INT, 
        Field2 INT
    )
    
    INSERT INTO Practice VALUES (1, 1), (2, 2), (3, 3), (4, 4)
    
    
    MERGE INTO ReportOption r USING Practice p ON 1 = 0
    WHEN NOT MATCHED THEN
        INSERT (field1, field2)
        VALUES (p.Field1, p.Field2)
        OUTPUT p.PracticeId, inserted.ReportOptionId, inserted.Field1, inserted.Field2
        INTO PracticeReportOption (PracticeId, ReportOptionId, Field1, Field2);
    
    SELECT *
    FROM PracticeReportOption
    
    DROP TABLE ReportOption
    DROP TABLE Practice
    DROP TABLE PracticeReportOption 
    

    More reading, and the source of all that I know on the subject is here.

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.