Quantcast
Channel: SQL: Remove rows whose associations are broken (orphaned data) - Stack Overflow
Viewing all articles
Browse latest Browse all 6

Answer by K0D4 for SQL: Remove rows whose associations are broken (orphaned data)

$
0
0

I've found in SQL 2008 R2, if your "in" clause contains a null value (perhaps from a table who has a reference to this key that is nullable), no records will be returned! To correct, just add a clause to your selects in the union part:

delete from SomeTable where Key not in (  select SomeTableKey from TableB where SomeTableKey is not null  union  select SomeTableKey from TableC where SomeTableKey is not null)

Viewing all articles
Browse latest Browse all 6

Trending Articles