Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Which Objects Reference a Table in SQL Server

I need to rename a table that has many columns and stored procedures that process against that table. How can one get all Items in database that have a relation to a table in such a scenario?

like image 542
Tavousi Avatar asked Nov 26 '12 10:11

Tavousi


2 Answers

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'

like image 107
Mitch Wheat Avatar answered Oct 22 '22 04:10

Mitch Wheat


If you need to find database objects (e.g. tables, columns, triggers) by name - have a look at the FREE Red-Gate tool called SQL Search which does this - it searches your entire database for any kind of string(s).

enter image description here

enter image description here

It's a great must-have tool for any DBA or database developer - did I already mention it's absolutely FREE to use for any kind of use??

like image 33
marc_s Avatar answered Oct 22 '22 06:10

marc_s