A user recently asked about an interesting scenario involving tables with foreign key relationships. They wanted to generate the DELETE statements that would allow them to manually delete from child tables first (perhaps in stages), based on criteria they define, and tell them – before performing the deletes – how many rows the operation would affect in each table. They wanted output like this:
DELETE dbo.ChildTable1 WHERE ParentID < <some constant>; -- This would delete 47 row(s).
DELETE dbo.ChildTable2 WHERE ParentID < <some constant>; -- This would delete 14 row(s).
DELETE dbo.ParentTable WHERE ID < <some constant>; -- This would delete 11 row(s).
Click through for the solution as well as several caveats.