Solved

how to delete records that contain no child records

Posted on 2011-02-25
5
282 Views
Last Modified: 2012-06-27
I need to delete all order records that do not container order items.

the tables are linked by the orderId on the orderItems table. So i can do the following

select * from orders o
inner join orderitems oi on o.id = oi.orderid

How can i detect which order records contain no child orderItems
0
Comment
Question by:frosty1
5 Comments
 
LVL 13

Expert Comment

by:devlab2012
ID: 34981543
delete from orders o where not exists(select * from orderitems oi where oi.orderid = o.orderid)
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 34981566
backup your db before tyr
delete from orders where id not in
 (select orderid from orderitems )

Open in new window

0
 
LVL 3

Accepted Solution

by:
jmro20 earned 500 total points
ID: 34981586
Delete From Orders
          Left Join OrderItems On Orders.OrderId = OrderItems.OrderId
Where OrderItems.OrderId IS NULL      
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 34982526
I would recommend using jmro20's answer.  Among other points in its favor is the fact that it is probably going to provide the best performance.
0
 
LVL 3

Expert Comment

by:jmro20
ID: 34997724
Did you solved your problem?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
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 will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

920 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

19 Experts available now in Live!

Get 1:1 Help Now