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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T10:08:24+00:00 2026-05-11T10:08:24+00:00

I have a situation in which a CSV is uploaded to my application and

  • 0

I have a situation in which a CSV is uploaded to my application and each line essentially needs to be put into the database. I read each line and build a data table and then SqlBulkCopy that table up to a staging table.

The staging table looks like this:

UserID, GroupID, FirstName, LastName, EmailAddress 

I have three other relevant tables in the database. A contacts table, a groups table and a contacts to groups mapping table.

Contacts: ID, UserID, FirstName, LastName, EmailAddress  Groups ID, UserID, Name, Description  ContactGroupMapping ID, ContactID, GroupID 

The ContactGroupMapping table simply maps contacts to groups. Hopefully the staging table now makes sense, it holds the details of each imported contact plus the group they should also be mapped to.

My plan was to run a query against the database after the SqlBulkCopy to move the data from the staging table to the Contacts and ContactGroupMapping tables. Currently, I have a query looking something like this:

INSERT INTO Contacts (UserID, FirstName, LastName, EmailAddress)  SELECT DISTINCT [t1].UserID, [t1].EmailAddress, [t1].FirstName, [t1].LastName FROM ContactImportStaging as [t1] WHERE NOT EXISTS  (     SELECT UserID, EmailAddress, FirstName, LastName FROM Contacts     WHERE UserID = [t1].UserID AND EmailAddress = [t1].EmailAddress AND FirstName = [t1].FirstName AND LastName = [t1].LastName ) 

So, my problem is that while this inserts all the distinct contacts into my contacts table, I then have no way to add the associated row to the mapping table for each newly inserted contact.

The only solution (probably because I suck at SQL) I can come up with is to have an extra nullable field in the contacts table identifying the group that the contact is to be associated with and insert this too. Then I could run a second query to select all contacts with a value in this column and insert into the mappings table.

Any thoughts on how this sort of thing should be achieved most efficiently?

Thanks.

Edit: To elaborate on the object model: There are any number of Contacts and any number of Groups. A contact can be in a group by way of an entry in the ContactGroupMapping table. One contact can be in any number of groups. At the database level, this model is the concern of the three tables Contacts, Groups and ContactGroupMapping. I’m needing to move one row from the staging table and create two rows; one in the Contacts table and one in the ContactGroupMapping table.

  • 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. 2026-05-11T10:08:25+00:00Added an answer on May 11, 2026 at 10:08 am

    I’d just bulk copy the CSV into a staging database table. You can delete the table later, if space is a concern.

    After copying the CSV into, say, StagingTable, the trick is to split it into the other tables. Since your CSV can now be referenced in the database, you can join the other tables to retrieve the new table’s ID:

    insert into ContactGroupMapping select cont.ID, grp.ID from StagingTable stag inner join Contacts cont on cont.UserID = stag.UserID inner join Groups grp on grp.GroupID = stag.GroupID 

    An even easier solution would be to keep the original GroupID and UserID. Filling the GroupContactMapping would then be as easy as:

    insert into ContactGroupMapping select UserID, GroupID from StagingTable stag 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer have you tried using another ADO.NET provider to see if… May 12, 2026 at 12:59 am
  • Editorial Team
    Editorial Team added an answer What I eventually found was as follows: Firefox and IE… May 12, 2026 at 12:59 am
  • Editorial Team
    Editorial Team added an answer You can get the effect you need by using the… May 12, 2026 at 12:59 am

Related Questions

I have a PHP script which reads a large CSV and performs certain actions,
I currently have a table in MySQL that stores values normally, but I want
I have a situation in which a managed DLL calls some unmanaged DLL. I
Customers customer_id Orders order_id customer_id fk If I have two tables and define a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.