Skip to content
Advertisement

Sql server – recursive delete

I’m trying to delete user’s data and all it’s related data that is located in different tables. All the tables have Foreign Keys but without cascade delete.

I investigated some options:

  1. Enable cascade delete on all FK, delete and remove the cascade delete.
  2. Delete from bottom UP, loop up for all the leaves delete and repeat this operation till Root.

Are there any more smart option or other Techniques?

I’m using Microsoft SQL Server 2012 (SP1)

Advertisement

Answer

Oracle solution:

How to generate DELETE statements in PL/SQL, based on the tables FK relations?

SQL Server solution:

Generate Delete Statement From Foreign Key Relationships in SQL 2008?

Hope it helps

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement