Determine Which Objects Reference a Table in SQL Server

Tavousi picture Tavousi · Nov 26, 2012 · Viewed 68.4k times · Source

I work with SQL Server 2008 and I have a database that has more than 1500 columns and about 500 stored procedures and ... .

I want to rename a table that has several relations and is referenced in many stored procedure and views and ... .

How to I can get all Items in database that has a relation with this table?

Thanks.

Answer

Mitch Wheat picture Mitch Wheat · Nov 26, 2012

Using sys.dm_sql_referencing_entities:

SELECT 
    referencing_schema_name, referencing_entity_name, referencing_id, 
    referencing_class_desc, is_caller_dependent
FROM 
    sys.dm_sql_referencing_entities ('mySchemaName.myTableName', 'OBJECT');
GO

where 'mySchemaName.myTableName' is your schema.table, for example 'dbo.MyTable'