Solved

Delete Records from Multiple tables at once

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Querying data from 3 SQL tables 2 32
SQL - Copy data from one database to another 6 19
SQL view 2 27
New to SSRS, extremely slow running report. 8 20
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 …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

773 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