Delete Records from Multiple tables at once
Posted on 2011-09-08
I have three tables in a MS SQL 2000 server:
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