?
Solved

SQL delete command

Posted on 2011-03-03
4
Medium Priority
?
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Ephraim Wangoya
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 1000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

801 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