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

Delete using Join

Status
Not open for further replies.

bsal81

Programmer
Jul 26, 2007
8
US
I'm having a little trouble getting this SQL Statement right. I'm fairly new to SQL so my apologies if I made an obvious mistake. I have two tables we'll call them Table1 and Table2. Both tables will have an ID and Type column that will be the key fields. I want to delete records from Table1 where there is not a corresponding ID and Type entry in Table2. And to throw a little more complexity into the mix I don't want to delete entries in Table1 where the ID = -99.

I have the following SQL Statement but it does not delete anything.

Delete Table1
From Table1 Right Outer Join Table2
On Table1.Type = Table2.Type AND Table1.ID = Table2.ID
Where Table2.Type Is Null AND
Table2.ID Is Null AND
Table1.ID <> -99

Can anyone help?
 
You are joining on Type and ID but your criteria is that they be NULL. NULL <> NULL so your conditions would never be true. Try a LEFT JOIN and see if that works (off the top of my head)...

------------------------------------------------------------------------------------------------------------------------
"Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."
- Winston Churchill
 
Looks like that worked.
I knew I was making a stupid mistake.
Thanks!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top