Open In App

Check the Dependencies of a Table in SQL Server

Last Updated : 21 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Sometimes there is a need to find the dependencies of a table in SQL Server using SQL Server Management Studio or SQL Query. It is useful to have information about the dependencies while altering or dropping any table.

In this article, we will learn how to find table dependencies in SQL Server using SQL Server Management Studio and SQL queries

Find Dependencies of a Table Using SQL Server Management Studio

Follow the steps below, to check dependencies of a table using SQL Server Management Studio.

Step-1: Expand Database, Expand Tables, Right click on the table name. Expand Tables

Step-2: Click on View Dependencies.  

View DependenciesFind Table Dependencies in SQL Server Using SQL Queries

We can also check table dependencies in SQL Server using SQL Queries.

Approach 1: Using the SP_DEPENDS stored procedure

It will return all the dependencies on the specified Object, includes Tables, Views, Stored Procedures, Constraints, etc.

Query:

Use DatabaseName ;
EXEC sp_depends @objname = N'ObjectName' ;

Example:

Use SQL_DBA ;
EXEC sp_depends @objname = N'[dbo].[tbl_Errors_Stats]' ;

Output:

nametype
dbo.usp_FetchStatisticsstored procedure
dbo.usp_PostStatistics_Updatestored procedure
dbo.usp_Update_theStatisticsstored procedure

Approach 2: Using sys.dm_sql_referencing_entities

Query:

Use DatabaseName ;
SELECT * FROM sys.dm_sql_referencing_entities('ObjectName',
'OBJECT') ;

Example

use SQL_DBA ;
SELECT * FROM sys.dm_sql_referencing_entities('[dbo].[tbl_Errors_Stats]',
'OBJECT') ;

Output :

referencing _schema_namereferencing _entity_namereferencing _idreferencing _classreferencing _class_descis_caller _dependent
dbousp_FetchStatistics5975771671OBJECT_OR_COLUMN0
dbousp_PostStatistics _Update5815771101OBJECT_OR_COLUMN0
dbousp_Update _theStatistics5655770531OBJECT_OR _COLUMN0

Approach 3: Using INFORMATION_SCHEMA.ROUTINES

Query:

SELECT ROUTINE_SCHEMA,
ROUTINE_NAME,
ROUTINE_TYPE,
ROUTINE_DEFINITION
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%ObjectName%'

Example:

use SQL_DBA

SELECT ROUTINE_SCHEMA,
ROUTINE_NAME,
ROUTINE_TYPE
FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%tbl_Errors_Stats%'

Output :

ROUTINE_SCHEMAROUTINE_NAMEROUTINE_TYPE
dbousp_Update_theStatisticsPROCEDURE
dbousp_PostStatistics_UpdatePROCEDURE
dbousp_FetchStatisticsPROCEDURE

Approach 4: Using sys.sql_expression_dependencies

Query:

SELECT *
FROM sys.sql_expression_dependencies A, sys.objects B
WHERE referenced_id = OBJECT_ID(N'ObjectName') AND
A.referencing_id = B.object_id

GO

Example:

use SQL_DBA

SELECT referenced_id, referenced_database_name, referenced_schema_name, name
FROM sys.sql_expression_dependencies A, sys.objects B
WHERE referenced_id = OBJECT_ID(N'tbl_Errors_Stats') AND
A.referencing_id = B.object_id

GO

Output :

referenced_idreferenced_database_namereferenced_schema_namename
613577224SQL_DBAdbousp_Update_theStatistics
613577224SQL_DBAdbousp_PostStatistics_Update
613577224SQL_DBAdbousp_FetchStatistics

Next Article
Article Tags :

Similar Reads