Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Apples
Posting Yak Master
146 Posts |
Posted - 2008-06-09 : 14:03:12
|
| OK, this is a big problem, with multiple tables, but here goes. Here's my schema:--------------------------------------------------Events--------------------------------------------------ID | E_Title----------------------------------------------------------------------------------------------------EventOptionGroups--------------------------------------------------ID | EOG_EventID | EOG_OptionGroupID----------------------------------------------------------------------------------------------------OptionGroups--------------------------------------------------ID | OG_Title----------------------------------------------------------------------------------------------------Options--------------------------------------------------ID | O_OptionGroupID----------------------------------------------------------------------------------------------------EventRegistration--------------------------------------------------ID | ER_EventID----------------------------------------------------------------------------------------------------RegistrantOptions--------------------------------------------------ID | RO_EventRegistrationID | RO_OptionGroupID--------------------------------------------------OK, what I'm trying to do is, when I delete an event, I need to delete all the data associated with that event. So here's the thought process.Delete Event based on IDDelete all EventRegistration where ER_EventID = Event.IDDelete all RegistrantOptions where RO_EventRegistrationID = EventRegistration.IDDelete all EventOptionGroups where EOG_EventID = Event.IDDelete all OptionGroups where OptionGroups.ID = EOG_OptionGroupIDDelete all Options where O_OptionGroupID = OptionGroups.IDSorry that it's so complicated, by I need help. There are foreign key constraints on the tables as well, so you have to work from the bottom back up. |
|
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-06-09 : 14:13:35
|
| DECLARE @id intSET @id = 1234DELETE ct1FROM ChildTable1 ct1INNER JOIN ParentTable ptON ct1.SomeColumn = pt.SomeColumnWHERE pt.A = @idDELETE ct2FROM ChildTable2 ct2INNER JOIN ParentTable ptON ct2.SomeColumn = pt.SomeColumnWHERE pt.A = @idDELETE ct3FROM ChildTable3 ct3INNER JOIN ChildTable4 ct4ON ct3.SomeColumn = ct4.SomeColumnINNER JOIN ParentTable ptON ct4.SomeColumn = pt.SomeColumnWHERE pt.A = @idDELETE ct4FROM ChildTable4 ct4INNER JOIN ParentTable ptON ct4.SomeColumn = pt.SomeColumnWHERE pt.A = @idTara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-06-09 : 14:17:28
|
| Or If you have control over schema, you could simply modify all foreign key definition with ON DELETE CASCADE feature. When you specify this while creating a foreign key, any record deletion in the parent table will automatically remove the related records in the child tables. |
 |
|
|
|
|
|