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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:51:02+00:00 2026-05-27T20:51:02+00:00

I am importing data from a flat file into a normalized table structure. I

  • 0

I am importing data from a flat file into a normalized table structure. I am currently using cursors to do inserts into related tables so I have the primary keys to insert into the join table. Can I do this in a set based way in SQL Server 2008 R2?

I have 3 tables: contacts, phones, and contactPhones. After running the import I’d like there to be 2 contacts in the contact table, 2 in the phones table, and 2 in the contactPhones table. The real import is considerably more complicated, but getting this to work will let me migrate the real import from cursors to a set based solution.

It seems like the merge or output keywords should be able to do what I want but I haven’t been able to get the syntax to work.

Here is a code sample trying it with OUTPUT. I got this to almost work, except I couldn’t reference import.contactId.

create table import(contactId int  identity, phone varchar(50), name varchar(10))
create table contacts (contactId int identity, name varchar(50))
create table contactPhone (contactId int, phoneId int)
create table Phones (phoneId int identity, number varchar(10))

go
insert into import (phone, name)
    select '1872', 'dave'
    union (select '9110', 'Jordan')

insert into contacts
    select name from import
insert into Phones (number)
    OUTPUT import.contactId, INSERTED.phoneId into contactPhone
    select phone from import

select * from contactPhone

Here is a code sample trying it with merge:

create table import(contactId int  identity, phone varchar(50), name varchar(10))
create table contacts (contactId int identity, name varchar(50))
create table contactPhone (contactId int, phoneId int)
create table Phones (phoneId int identity, number varchar(10))

go
insert into import (phone, name)
    select '1872', 'dave'
    union (select '9110', 'Jordan')

insert into contacts
    select name from import

MERGE phones target
    USING (select import.contactId, import.phone, import.name 
            from import join contacts on import.contactId = contacts.contactId) as source
    ON (target.contactId = source.contactId)
    WHEN MATCHED THEN 
        insert into Phones (number)
            OUTPUT import.contactId, INSERTED.phoneId into contactPhone
            select phone from import
    WHEN NOT MATCHED THEN   
        INSERT (name)
        VALUES (source.Name)
        OUTPUT INSERTED.*;



select * from contactPhone
  • 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-27T20:51:03+00:00Added an answer on May 27, 2026 at 8:51 pm

    Use merge on contacts and Phones and store the output in a table variable to be used when you insert into contactPhone.

    insert into import (phone, name)
    select '1872', 'dave' union all
    select '9110', 'Jordan'
    
    declare @ContactIDs table(SourceID int primary key, TargetID int)
    declare @PhoneIDs table (SourceID int primary key, TargetID int)
    
    merge contacts as c
    using import as i
    on 0 = 1
    when not matched then
      insert (name) values (i.name)
    output i.contactId, inserted.contactId into @ContactIDs;
    
    merge Phones as p
    using import as i
    on 0 = 1
    when not matched then
      insert (number) values (i.phone)
    output i.contactId, inserted.phoneId into @PhoneIDs;
    
    insert into contactPhone(contactId, phoneId)
    select c.TargetID, p.TargetID
    from import as i
      inner join @ContactIDs as c
        on i.contactID = c.SourceID
      inner join @PhoneIDs as p
        on i.contactID = p.SourceID  
    

    Using merge..output to get mapping between source.id and target.id

    https://data.stackexchange.com/stackoverflow/qt/122662/

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

Sidebar

Related Questions

I'm importing data from youtube into a textarea using Javascript. If you I simply
While importing data from a flat file, I noticed that some of lines have
I'm importing data from an Excel file in ASP.NET using OleDB. After finishing the
I'm importing some data from a CSV file, and numbers that are larger than
I am importing massive amounts of data from Excel that have various table layouts.
My application is storing location data from GPS inputs. When importing a GPX file,
I am importing data from a CSV file. One of the fields has an
I'm importing data in a SQL Server 2008 database from excel file where the
I'm importing data from a CSV file that comes from excel, but i can't
I have a web form used for importing data from a CSV file. It

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.