Delete all records that have no foreign key constraints
Posted
by Rodney Burton
on Stack Overflow
See other posts from Stack Overflow
or by Rodney Burton
Published on 2010-05-06T23:45:32Z
Indexed on
2010/05/06
23:48 UTC
Read the original article
Hit count: 290
I have a SQL 2005 table with millions of rows in it that is being hit by users all day and night. This table is referenced by 20 or so other tables that have foreign key constraints. What I am needing to do on a regular basis is delete all records from this table where the "Active" field is set to false AND there are no other records in any of the child tables that reference the parent record. What is the most efficient way of doing this short of trying to delete each one at a time and letting it cause SQL errors on the ones that violate constraints? Also it is not an option to disable the constraints and I cannot cause locks on the parent table for any significant amount of time.
© Stack Overflow or respective owner