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

The Archive Base Latest Questions

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

I have 2 tables (srcTable1 & destTable) that have identical schemas. I am trying

  • 0

I have 2 tables (srcTable1 & destTable) that have identical schemas. I am trying to copy all rows from srcTable to destTable and ignore the duplicates. I thought I could just add a WHERE clause with a subquery that would give me only the rows that aren’t duplicates. However, it doesn’t seem to work. I don’t get any rows inserted or selected.

INSERT INTO destTable SELECT * FROM srcTable WHERE NOT EXISTS(SELECT * FROM destTable) 

I realize I could do something like this:

INSERT INTO destTable SELECT * FROM srcTable WHERE MyKey IN (SELECT MyKey FROM destTable) 

However, my tables have multiple keys and I can’t think of how you could do this with multiple keys.

Any idea what I’m doing wrong or do you have any better ideas?

  • 1 1 Answer
  • 3 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:51:35+00:00Added an answer on May 11, 2026 at 10:51 am

    Your problem is that you need another where clause in the subquery that identifies what makes a duplicate:

    INSERT INTO destTable SELECT Field1,Field2,Field3,...  FROM srcTable WHERE NOT EXISTS(SELECT *                   FROM destTable                   WHERE (srcTable.Field1=destTable.Field1 and                        SrcTable.Field2=DestTable.Field2...etc.)                  ) 

    As noted by another answerer, an outer join is probably a more concise approach. My above example was just an attempt to explain using your current query to be more understandible. Either approach could technically work.

    INSERT INTO destTable SELECT s.field1,s.field2,s.field3,...  FROM srcTable s         LEFT JOIN destTable d ON (d.Key1 = s.Key1 AND d.Key2 = s.Key2 AND...) WHERE d.Key1 IS NULL 

    Both of the above approaches assume you are woried about inserting rows from source that might already be in destination. If you are instead concerned about the possibility that source has duplicate rows you should try something like.

    INSERT INTO destTable SELECT Distinct field1,field2,field3,...  FROM srcTable   

    One more thing. I’d also suggest listing the specific fields on your insert statement instead of using SELECT *.

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

Sidebar

Related Questions

hey all I have tables with millions of rows in them and some of
I have tables with listings, categories and one that maps them to each other.
I have tables & data like this: venues table contains : id +----+---------+ |
i have tables driver, cars and trailers they are all in relationship. and main
I have tables for dogs, cats , horses containing rows of information about them
I have tables in a local database I've built in Visual Studio 2008 that
In my database I have tables that define types for example Table: Publication Types
When I have tables in my database that have PK/FK relationships (int) and when
In my database I have tables that will grow at different speeds: fairly static
I have a vb program that reads data from an excel sheet and displays

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.