Click here to Skip to main content
15,845,681 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello

I need some assistance copying one table to another. I Would like to copy the data from table1 to table 2 if the item doesn't exist in table 2.

Thank you very much.

Steven Andler
Posted

Hi,
You can use this same logic with the help of EXITS /NOT EXITS keyword to remove 'NOT IN' condition in WHERE clause

Click here for more on SQL: EXISTS Condition
 
Share this answer
 
v2
Replace the fields (field1, field2 etc) with your actual field names

This is assuming Field1 is your primary key

SQL
INSERT INTO
    TABLE2 (Field1, Field2, Field3, Field4, Field5)
SELECT
    Field1, Field2, Field3, Field4, Field5
FROM
    TABLE1
WHERE
    Field1 --Obviously, replace this with whatever your PK is in your tables
NOT IN
(
    SELECT Field1 FROM TABLE2
)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900