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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:59:37+00:00 2026-06-09T21:59:37+00:00

I am trying to insert some data from one table into another but I

  • 0

I am trying to insert some data from one table into another but I would like to prevent the insertion of duplicate rows. I have currently the following query:

INSERT INTO Table1
(
    Table1Col1,
    Table1Col2,
    Table1Col3,
    Table1Col4,
    Table1Col5
)
SELECT
    Table2Col1,
    Table2Col2 = constant1,
    Table2Col3 = constant2,
    Table2Col4 = constant3,
    Table2Col5 = constant4
FROM Table2
WHERE
    Condition1 = constant5
AND
    Condition2 = constant6
AND
    Condition3 = constant7
AND
    Condition4 LIKE '%constant8%'

What I do not know is that the row I am trying to insert from Table2 into Table1 might already exist and I would like to prevent this possible duplication from happening and skip the insertion and just move onto inserting the next unique row.

I have seen that I can use a WHERE NOT EXISTS clause and use of the INTERSECT keyword but I did not fully understand how to apply it to my particular query as I only want to use some of the selected data from Table2 and then some constant values to insert into Table1.

EDIT:

I should add that the columns TableCol2 through to TableCol5 don’t actually exist in the result set and I am just populating these columns alongside Table2Col1 that is returned.

  • 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-06-09T21:59:39+00:00Added an answer on June 9, 2026 at 9:59 pm

    Intersect (minus in Sql Server’s terms) is out of question because it compares whole row. Other two options are not in/not exists/left join and merge. Not In is for single-column prinary key only, so it is out of question in this instance. In/Exists/Left join should have the same performance in Sql Server, so I’ll just use exists:

    INSERT INTO Table1
    (
        Table1Col1,
        Table1Col2,
        Table1Col3,
        Table1Col4,
        Table1Col5
    )
    SELECT
        Table2Col1,
        Table2Col2 = constant1,
        Table2Col3 = constant2,
        Table2Col4 = constant3,
        Table2Col5 = constant4
    FROM Table2
    WHERE
        Condition1 = constant5
    AND
        Condition2 = constant6
    AND
        Condition3 = constant7
    AND
        Condition4 LIKE '%constant8%'
    AND NOT EXISTS
        (
           SELECT *
             FROM Table1 target
            WHERE target.Table1Col1 = Table2.Table2Col1
              AND target.Table1Col2 = Table2.Table2Col2
              AND target.Table1Col3 = Table2.Table2Col3
        )
    

    Merge is used to sync two tables; it has ability to insert, update and delete records from target table.

    merge into table1 as target
    using table2 as source
       on target.Table1Col1 = source.Table2Col1
      AND target.Table1Col2 = source.Table2Col2
      AND target.Table1Col3 = source.Table2Col3
    when not matched by target then
      insert (Table1Col1,
              Table1Col2,
              Table1Col3,
              Table1Col4,
              Table1Col5)
      values (Table2Col1,
              Table2Col2,
              Table2Col3,
              Table2Col4,
              Table2Col5);
    

    If columns from table2 are computed during transfer, in not exists() case you might use derived table in place of table2, and the same applies to merge example – just place your query in place of reference to table2.

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

Sidebar

Related Questions

I am trying to insert data from one of my existing table into another
I need to copy some data from one table to another but I need
Trying to archive some data from one database table to another over linked server
I am trying to insert some data into one table and after that is
I am trying to insert some data from excel file into a table. I
I am trying to insert some data into a pysqlite database but even tho
I am trying to insert some text data into a table in SQL Server
I am trying to take data from one mysql table and insert it in
I am trying to syphon data over from one table to another. The problem
So I am trying to insert multiples rows of data from one table to

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.