简体   繁体   中英

Insert into table based on comparison of two other tables

I currently have a table of 15 million old-records ( table1 ), another table of 11-million new records ( table2 ) and an empty table ( table3 ).

Both table1 and table2 share data - I would like find the records in table1 table that aren't present in table2 , and then insert these into table3 .

So far I have the following (which is returning an unknown column on table3.uniqueCode error):

INSERT INTO table3
SELECT * FROM table1
WHERE table1.uniqueCode != table2.uniqueCode

I should note that table1 and table2 do not have the same structure, but both have the field of uniqueCode, this is not a KEY but just a literal unique string code.

Any help would be very much appreciated, I've looked into using a JOIN statement but I also encountered errors with this approach.

Thanks!

Update - Answered

The answer was simple:

INSERT INTO table3
(SELECT * FROM table1
WHERE uniqueCode not in (Select uniqueCode from table2))

There are an abundance for varied answers below which tailor to slightly different situations. Please look at those too as they may solve a slightly varied question you may have like this. :)

if the tables have the same structure:

INSERT INTO table3
(SELECT * FROM table1
WHERE uniqueCode not in (Select uniqueCode from table2))

Can you try this

INSERT INTO table3 (uniqueCode)
SELECT t2.uniqueCode FROM table2 WHERE t2.uniqueCode 
NOT IN  
(SELECT t3.uniqueCode FROM table3 t3 WHERE t3.uniqueCode = t2.uniqueCode)

Edited

INSERT INTO table3 (uniqueCode)
SELECT t2.uniqueCode FROM table2 WHERE t2.uniqueCode 
NOT IN  
(SELECT t1.uniqueCode FROM table1 t1 WHERE t1.uniqueCode = t2.uniqueCode)

Use Merge statement in sql. See the example http://technet.microsoft.com/en-in/library/bb522522(v=sql.105).aspx

BEGIN TRAN;
MERGE Target AS T
USING Source AS S
ON (T.EmployeeID = S.EmployeeID) 
WHEN NOT MATCHED BY TARGET AND S.EmployeeName LIKE 'S%' 
    THEN INSERT(EmployeeID, EmployeeName) VALUES(S.EmployeeID, S.EmployeeName)
WHEN MATCHED 
    THEN UPDATE SET T.EmployeeName = S.EmployeeName
WHEN NOT MATCHED BY SOURCE AND T.EmployeeName LIKE 'S%'
    THEN DELETE 
OUTPUT $action, inserted.*, deleted.*;
ROLLBACK TRAN;

Step by step:
first we should to select records in table1 that not exists in table2 (according to your question)
if uniqueCode is a unique code (KEY) then:

SELECT * FROM table1 WHERE uniqueCode NOT IN (SELECT uniqueCode FROM table2)

now we should to insert these result into table3 ,so:

INSERT INTO table3
SELECT * FROM table1 WHERE uniqueCode NOT IN (SELECT uniqueCode FROM table2)

and done...

Attention: all 3 tables must have same structure in this answer

If you need to compare rows and you have not got a unique column (you want to compare all columns) you can use EXCEPT clause like this:

INSERT INTO table3
SELECT *
FROM tabel1
EXCEPT
SELECT *
FROM table2

I think UNION will be the best option to use between two table. Like :

SELECT t1.* FROM table1 t1 UNION SELECT t2.* FROM table2 t2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM