SQLServer - How to find dependent tables on my table?

The way to get ONLY TABLE references (i.e. tables that uses given table as a foreign key and tables that given table uses the same way) you can use this code snippet:

declare @tableName varchar(64);
set @tableName = 'TABLE';

select
SO_P.name as [parent table]
,SC_P.name as [parent column]
,'is a foreign key of' as [direction]
,SO_R.name as [referenced table]
,SC_R.name as [referenced column]
,*
from sys.foreign_key_columns FKC
inner join sys.objects SO_P on SO_P.object_id = FKC.parent_object_id
inner join sys.columns SC_P on (SC_P.object_id = FKC.parent_object_id) AND (SC_P.column_id = FKC.parent_column_id)
inner join sys.objects SO_R on SO_R.object_id = FKC.referenced_object_id
inner join sys.columns SC_R on (SC_R.object_id = FKC.referenced_object_id) AND (SC_R.column_id = FKC.referenced_column_id)
where
    ((SO_P.name = @tableName) AND (SO_P.type = 'U'))
    OR
    ((SO_R.name = @tableName) AND (SO_R.type = 'U'))

In SQL server management studio, you can right click your table in the object explorer, and then select 'View Dependencies'. This will open a new window in which you can see all other objects (not just tables) that depend on your table, and on which your table depends.


Here is a stored procedure I put together based in part on the above answer.

-- =============================================
-- Author:      R. Mycroft
-- Create date: 2012-08-08
-- Description: Lists foreign keys to & from a named table.  
-- (Have yet to find this one via Google!)
-- =============================================
alter procedure usp_ListTableForeignKeys 
    @tableName varchar(300) = ''
as
begin
set nocount on;

select 
    object_name(parent_object_id) as childObjectName
    , object_name(referenced_object_id) as parentObjectName
    , name, type_desc, create_date
from sys.foreign_keys
where object_name(parent_object_id) = @tableName
or object_name(referenced_object_id) = @tableName
end