Thursday, October 15, 2015

How to find dependent table in Sql server?

select    s1.name as from_schema
,        s1.name+'.'+o1.Name as from_table
,        s2.name as to_schema
,        s2.name+'.'+o2.Name as to_table
from    sys.foreign_keys fk
inner    join sys.objects o1
on        fk.parent_object_id = o1.object_id
inner    join sys.schemas s1
on        o1.schema_id = s1.schema_id
inner    join sys.objects o2
on        fk.referenced_object_id = o2.object_id
inner    join sys.schemas s2
on        o2.schema_id = s2.schema_id

where    not    (    s1.name = s2.name
            and    o1.name = o2.name) and  o2.name='Your table name'