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

Help with Delete query 2

Status
Not open for further replies.

sonname

Programmer
May 18, 2001
115
US
I have 2 tables. Lets say TableA and TableB. They are joined by 2 fields, EffortID and SkuID, meaning TableA has an EffortID field that joins with TableB's EffortID and TableA has a field called SkuID and joins with TableB's SkuID. I want to form a query that will delete the records in TableB that are not in TableA.

Ex:
TableA
effortID = 300
SkuID = 200
effortID = 350
SkuID = 250


TableB
effortID = 300
SkuID = 200
effortID = 350
SkuID = 250
EffortID = 600
SkuID = 500

In this situation I would want to delete the record in TableB that has an effortID of 600 and a SkuID of 500

Thanks in advance

 
Hi,

delete TableB
where effortID not in (select effortID from TableA) and
SkuID not in (select SkuID from TableA)


Please try this.
 
I'm kinda new at SQL Server myself, but I think this will work....

DELETE FROM TableB
FROM TableB INNER JOIN TableA
ON TableB.EffortID = TableA.EffortID
AND TableB.SkuID = TableA.SkuID

Hope this helps...
 
I tried the following, you are on the right track, but the query found the records in TableB that are present in TableA and deleted those. What I want is the opposite, delete the records in TableB that are not present in TableA.

DELETE FROM TableB
FROM TableB INNER JOIN TableA
ON TableB.EffortID = TableA.EffortID
AND TableB.SkuID = TableA.SkuID

 
Oops..my bad...I guess I should have read the post a little closer.

Try this:

DELETE FROM TableB b
WHERE NOT EXISTS (SELECT * from TableA a
WHERE a.EffortID = b.EffortID
AND a.SkuID = b.SkuID)


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top