Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQL Insert

Status
Not open for further replies.

Injun

IS-IT--Management
Dec 17, 2002
30
US
I need to Insert TableB from TableA with the records that doesn’t exist in TableB

INSERT INTO TableB
(SELECT A,B,C,D
FROM TableA
WHERE NOT EXISTS
(SELECT A,B,C,D
FROM TableB ))

Please help .. I am not able to work this out !
 
Try this....

Code:
INSERT 
INTO TableB(A,B,C,D)
SELECT A,B,C,D
FROM TableA
     Left Join TableB
       On  TableA.A = TableB.A
       And TableA.B = TableB.B
       And TableA.C = TableB.C
       And TableA.D = TableB.D
WHERE Coalesce(TableB.A, TableB.B, TableB.C, TableB.D) Is NULL

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
of course it's simpler if you have some sort of identity field or other identifier field to join on.

"NOTHING is more important in a database than integrity." ESquared
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top