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

The Archive Base Latest Questions

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

Let’s say I have a temp table of data I want to put into

  • 0

Let’s say I have a temp table of data I want to put into another table, but before doing so, I want to remove any rows in the temp table that already exist in the destination table.

I’m thinking of using a left outer join to identify which rows are duplicates, but then how can I go and delete these rows from my temp table?

Am I taking the right approach, or would it be better to remove duplicates after transferring the data?

  • 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-29T10:55:09+00:00Added an answer on May 29, 2026 at 10:55 am

    Assuming there is an index on col1/col2, this may perform better than trying to weed out duplicates in the process of the insert:

    DELETE t
    FROM #temp AS t
    WHERE NOT EXISTS
    (
      SELECT 1 FROM dbo.table
        WHERE col1 = t.col1
        AND col2 = t.col2
    );
    

    An equivalent to Jack’s approach would be:

    INSERT dbo.table(col1, col2)
    SELECT col1, col2
    FROM #temp AS t
    WHERE NOT EXISTS
    (
      SELECT 1 FROM dbo.table
        WHERE col1 = t.col1
        AND col2 = t.col2
    );
    

    I’m going to assume you have a unique index on the #temp table so you don’t need DISTINCT or GROUP BY. You can also delete duplicates from the #temp table as a separate step first, e.g.

    ;WITH t AS 
    (
      SELECT col1, col2, 
        rn = ROW_NUMBER() OVER (PARTITION BY col1, col2 ORDER BY col1)
    )
    DELETE t WHERE rn > 1;
    

    Whether you’re doing the right thing or not, no idea. Will require a lot more information and testing to determine which approach is best. You’re going to have to scan 2+ million rows for duplicates no matter which method you end up using.

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

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Let's say I have a table with a Color column. Color can have various
Let's say I have thousands of users and I want to make the passwords
I have a French site that I want to parse, but am running into
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have such MYSQL table for logins: id---name---location---login_date ---------------------------------- 1----mike----usa-----21.08.2012 2----tony----uk------22.08.2012 3----tony----uk------23.08.2012
Let's say I have some data in a CouchDB database. The overall size is
Let's say I'm building a data access layer for an application. Typically I have
Let's say I have a drive such as C:\ , and I want to
Let's say I have an Instant Messenger server using SignalR. I want to broadcast

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.