SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Deleting Grandfather, father, child records


Deleting Grandfather, father, child records

Author
Message
Charlottecb
Charlottecb
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1180 Visits: 239

Hi All.

Can someone help with the following please.

3 tables BudgetCategory, Budget and BudgetHolder in a Grandfather, Father child hierarchy

BudgetCategory - CategoryID, CategoryDesc

Budget - CategoryID, BudgetID, BudgetDesc

BudgetHolder - BudgetID, StaffName

If I delete a BudgetCategory record, I need to also delete all related Budget records and all BudgetHolder record related to each deleted Budget record.

How do I do this in T-SQL. Should I be using a trigger. If so what should I code.

Thanks for any help.

CCB





Wayne Lawton
Wayne Lawton
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1154 Visits: 22
There are several ways to accomplish what you are trying to do. Triggers are one option. Another option is to set up the tables as described in BOL. Do a search on "Cascading Referential Integrity Constraints". The quick version is that when you create the father and child tables you can set up the foreign key reference constraints to cascade when a row is deleted in the parent table. I have heard debates pro and con whether this is a good strategy to use.



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search