Solved

SQL delete command

Posted on 2011-03-03
4
328 Views
Last Modified: 2012-05-11
How can I check if parts from Table1 exists in Table2 and Table3 before delete record from Table1?

Table1
partno partname


Table2
partno iQty

Table3
partno oQty

How can I write a single sql statment to check the existence of table1's partno in table2 and table2 and if not exists delete record from Table1.

thanks

ayha
0
Comment
Question by:ayha1999
4 Comments
 
LVL 15

Expert Comment

by:derekkromm
ID: 35030710
delete t1
from table1 t1
left join table2 t2
on t1.partno = t2.partno
left join table3 t3
on t1.partno = t3.partno
where t2.partno is null and t3.partno is null
0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 35030725
delete fom table1 t1 wherenot exists (select 1 from (
select partno from table2 union select partno from tale3 ) t on t1.patno = t.partno)
0
 
LVL 32

Expert Comment

by:ewangoya
ID: 35030728

delete from Tbale1
where partno = your number
and not exists (select 1 from Table2 B
                       where B.partno = A.partno)

0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 250 total points
ID: 35033778
To delete existing records in Table2 and Table3

delete from Table1
whete Table1.partno in
( Select partno from table2 union Select partno from table3)

To delete not existing records in Table2 and Table3

delete from Table1
whete Table1.partno not in
( Select partno from table2 union Select partno from table3)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now