Tuesday, June 11, 2013

Find which tables are referring to a table

Sometimes there is a situation when we want to know which tables are referring to a particular table.
Most common place where we need this information is, when you don't delete the record physically but just flag it for deletion.

Here is the query to find out:

select a.name as ConstraintName, f.name as FromTable
from sysobjects a, sysobjects f, sysobjects t, sysforeignkeys b
where a.id=b.constid and f.id=b.fkeyid and t.id=b.rkeyid and t.name= 'TableName'


No comments: