Code generation – Generate Delete Statement From Foreign Key Relationships in SQL 2008? Stack Overflow

fk your diet

Generate Delete Statement From Foreign Key Relationships in SQL 2008?

Is it possible via script/tool to generate a delete statement based on the tables fk relations.

i.e. I have the table: DelMe(ID) and there are 30 tables with fk references to its ID that I need to delete first, is there some tool/script that I can run that will generate the 30 delete statements based on the FK relations for me ?

(btw I know about cascade delete on the relations, I can’t use it in this existing db)

I’m using Microsoft SQL Server 2008

5 Answers 5

DELETE statements generated for use in SP with parameter, and as ON DELETE triggers: (this variant supports single column FKs only)

Here is a script for cascading delete by Aasim Abdullah, works for me on MS SQL Server 2008:

Usage example 1

Note the use of the fully qualified column name in the example. It’s subtle but you must specify the table name for the generated SQL to execute properly.

Leave a Reply

Your email address will not be published. Required fields are marked *