I have a parent/child table (simple tree) table structure (ID, ParentID), where I want to delete (and get the ID of) all children for a given parent ID – similar to this post sql server, cascade delete and parent/child table .
During the loop, where I’ve got the current ID, I will also be performing other actions with this ID.
Can someone give me an example of the SPROC to accomplish this?
Thanks
Assuming, you’re on SQL SERVER 2005, here is the example of stored procedure to accomplish this (by using CTE):
This removes a row defined by
@idparameter with all child nodes from the table and returns the deleted values to the processing application in a single action.You also could return deleted rows into a table variable (should be defined before CTE):
and then