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

  • Home
  • SEARCH
  • 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 682301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T01:34:49+00:00 2026-05-14T01:34:49+00:00

I have the following two tables: Table1 ———- ID Name 1 A 2 B

  • 0

I have the following two tables:

Table1
----------
ID   Name
1    A
2    B
3    C

Table2
----------
ID   Name
1    Z

I need to insert data from Table1 to Table2. I can use the following syntax:

INSERT INTO Table2(Id, Name) SELECT Id, Name FROM Table1

However, in my case, duplicate IDs might exist in Table2 (in my case, it’s just “1“) and I don’t want to copy that again as that would throw an error.

I can write something like this:

IF NOT EXISTS(SELECT 1 FROM Table2 WHERE Id=1)
INSERT INTO Table2 (Id, name) SELECT Id, name FROM Table1 
ELSE
INSERT INTO Table2 (Id, name) SELECT Id, name FROM Table1 WHERE Table1.Id<>1

Is there a better way to do this without using IF - ELSE? I want to avoid two INSERT INTO-SELECT statements based on some condition.

  • 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-14T01:34:50+00:00Added an answer on May 14, 2026 at 1:34 am

    Using NOT EXISTS:

    INSERT INTO TABLE_2
      (id, name)
    SELECT t1.id,
           t1.name
      FROM TABLE_1 t1
     WHERE NOT EXISTS(SELECT id
                        FROM TABLE_2 t2
                       WHERE t2.id = t1.id)
    

    Using NOT IN:

    INSERT INTO TABLE_2
      (id, name)
    SELECT t1.id,
           t1.name
      FROM TABLE_1 t1
     WHERE t1.id NOT IN (SELECT id
                           FROM TABLE_2)
    

    Using LEFT JOIN/IS NULL:

    INSERT INTO TABLE_2
      (id, name)
       SELECT t1.id,
              t1.name
         FROM TABLE_1 t1
    LEFT JOIN TABLE_2 t2 ON t2.id = t1.id
        WHERE t2.id IS NULL
    

    Of the three options, the LEFT JOIN/IS NULL is less efficient. See this link for more details.

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

Sidebar

Related Questions

Suppose I have two tables Table1 and Table2 with the following data. Column1 Column2
I have two tables and the following query: table1 --------- table1Id(pk) fromdate, todate, name,
I have two tables that contain data from different temperature sensors: Table1 _TimeStamp Temperature
I have two tables that have following rows: Table1 ID Name Number ===================== 1
If I have two tables with following columns: Table1: [id,value] Table2: [id,comment] where id
I have two tables with the following columns: table1: id, agent_name, ticket_id, category, date_logged
I have the following two tables: Customer { int Id int Name } Bills
I have two tables: Contact (id,name) Link (id, contact_id, source_id) I have the following
I have the following query where I combine two fields from two tables: <cfquery
In SQL server 2008: Suppose I have two tables. Table1 has 3 fields: Name,

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.