Solved

Delete Records from Multiple tables at once

Posted on 2011-09-08
5
255 Views
Last Modified: 2012-05-12
I have three tables in a MS SQL 2000 server:

1. [Orders]
2. [OrderList]
3. [OrderOptions]

There are over 70,000 Orders, and for each order there is between 1 to 10 items (records) in the OrderList table and for Every OrderList Record, there is anywhere from 1 to 20 records in the OrderOptions table.  They are all interconnected, however there is now close to a Million records in the OrderOptions table as a result.

[Orders] has a primary key field called OrderId  there is only one unique record with that orderId

[OrderList] has a OrderListID that is a primary key, but also has the OrderId field, however it may contain several records with the same OrderId. The two are linked and act as Items ordered tracked by the OrderID

 [OrderOptions] has the OrderOptionID that is its primary key, but also has the OrderListID which also can have several of the same OrderListId's but there is NO reference to the OrderId  This table hold the details about items in the OrderList table.

They are all interconnected but since the OrderOptions table does no have the OrderID it has to have a one to many Join from the OrderList table.  

All I want to be able to do is create a Query, that will delete Old Orders but also remove the old data from the Other two tables.  So for example, I want to delete all Orders where the OrderID is < 76750
0
Comment
Question by:upgraders
  • 3
  • 2
5 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 500 total points
ID: 36502954
Before you try anything, first backup your tables or database.

The following will do the trick:

delete oo
from OrderOptions oo
inner join OrderList ol on ol.OrderListID = oo.OrderListID
where ol.OrderID < 76750

delete OrderList 
where OrderID < 76750

delete Orders
where OrderID < 76750

Open in new window

0
 
LVL 1

Author Comment

by:upgraders
ID: 36503038
VERY Scary thought if I messed up..  I added the field OrderID to the OrderOptions table.  Can you give me an update query that would populate this new field, then I could have more control over deleting record :)
0
 
LVL 23

Assisted Solution

by:wdosanjos
wdosanjos earned 500 total points
ID: 36503094
The following query will populate the OrderOptions.OrderID column:

update oo
set OrderID = ol.OrderID
from OrderOptions oo
inner join OrderList ol on ol.OrderListID = oo.OrderListID

Open in new window


Something to keep in mind.  Adding OrderID to OrderOptions breaks the normalization.
0
 
LVL 1

Author Comment

by:upgraders
ID: 36503177
I get this error with that query:


Msg 208, Level 16, State 1, Line 1
Invalid object name 'OrderOptions'.
Msg 208, Level 16, State 1, Line 1
Invalid object name 'OrderList'.
0
 
LVL 1

Author Comment

by:upgraders
ID: 36503190
Nevermind....  forgot to set the Default Database....  it's running now :)  I'll report back
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

912 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

23 Experts available now in Live!

Get 1:1 Help Now