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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I need to insert from one table to

Status
Not open for further replies.

ije

MIS
Jun 10, 2002
38
0
0
AU
I need to insert from one table to another, so long as the destination does not have a record with two fields in the row matching two fields in the source table.

If the source has 2 rows like:

Row 1: aa,bb,cc,dd,ee
Row 2: gg,hh,ii,jj

and the destination has

Row 1: aa,bb,tt,ss
Row 2: gg,mm,ii,jj

I would like to insert only Row 2 into the destination, as I have a match with field1 and field2 in source row1 and destination row 1.

I hope this makes sense, and someone can provide an example SQL statement for me!!

Many many thanks in advance [afro2]



 
Hi
try this

INSERT INTO Destination(f1, f2, f3, f4)
SELECT s.f1, s.f2, s.f3, s.f4
FROM Source s
LEFT JOIN Destination d ON s.f1 = d.f1 and s.f2 = d.f2
WHERE d.f1 IS NULL

HTH
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top