Solved

Delete Records from Multiple tables at once

Posted on 2011-09-08
5
254 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Numeric sequence in SQL 14 38
Stored Procedure 2 10
In or Between 2 0
Error in query 3 0
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

744 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

12 Experts available now in Live!

Get 1:1 Help Now