Check the Dependencies of a Table in SQL Server
Last Updated :
21 Jun, 2024
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. 
Step-2: Click on View Dependencies.
Find 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:
name | type |
---|
dbo.usp_FetchStatistics | stored procedure |
dbo.usp_PostStatistics_Update | stored procedure |
dbo.usp_Update_theStatistics | stored 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_name | referencing _entity_name | referencing _id | referencing _class | referencing _class_desc | is_caller _dependent |
---|
dbo | usp_FetchStatistics | 597577167 | 1 | OBJECT_OR_COLUMN | 0 |
dbo | usp_PostStatistics _Update | 581577110 | 1 | OBJECT_OR_COLUMN | 0 |
dbo | usp_Update _theStatistics | 565577053 | 1 | OBJECT_OR _COLUMN | 0 |
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_SCHEMA | ROUTINE_NAME | ROUTINE_TYPE |
---|
dbo | usp_Update_theStatistics | PROCEDURE |
dbo | usp_PostStatistics_Update | PROCEDURE |
dbo | usp_FetchStatistics | PROCEDURE |
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_id | referenced_database_name | referenced_schema_name | name |
---|
613577224 | SQL_DBA | dbo | usp_Update_theStatistics |
613577224 | SQL_DBA | dbo | usp_PostStatistics_Update |
613577224 | SQL_DBA | dbo | usp_FetchStatistics |
Similar Reads
Check whether a Table exists in SQL Server database or not
Before creating a table, it is always advisable to check whether the table exists in the SQL Server database or not. Checking for table existence before creation helps in avoiding duplication errors, ensures data integrity, and enables efficient database management. There are multiple methods in SQL
3 min read
How to Check if a Column Exists in a SQL Server Table?
In this article, we will look at how to check if a particular column exists in a database table or not. For checking the existence of a column we need to create the table first. So, let us create a table with some columns and data. Creating table: Syntax: CREATE TABLE table_name ( column1 datatype,
2 min read
SQL Query to Find the Number of Columns in a Table
SQL stands for a structure query language, which is used in the database to retrieve data, update and modify data in relational databases like MySql, Oracle, etc. And a query is a question or request for data from the database, that is if we ask someone any question then the question is the query. S
4 min read
Copy Tables Between Databases In SQL Server
Copying tables between databases in SQL Server can be crucial for data migration, backups, or setting up test environments. One effective method to achieve this is by generating scripts using SQL Server Management Studio (SSMS).In this article, we will learn how to Copy Tables Between Databases In S
3 min read
Create a Table if it Doesn't Exist in SQL
Creating tables is a fundamental part of database management and sometimes it is important to ensure a table doesnât already exist before creating it. The CREATE TABLE IF NOT EXISTS statement in SQL provides a simple and effective way to handle such situations prevent errors and simplify database ma
4 min read
Table operations in MS SQL Server
In a relational database, the data is stored in the form of tables and each table is referred to as a relation. A table can store a maximum of 1000 rows. Tables are a preferred choice as: Tables are arranged in an organized manner. We can segregate the data according to our preferences in the form o
2 min read
Check Constraint in MS SQL Server
Check Constraint : It is used alongside relational operators to check whether a value satisfies the condition or not (boolean). If the condition is satisfied, the boolean expression sets to True otherwise False. The check constraint does not have a specific syntax. It is used along with the create t
2 min read
Delete Action in MS SQL Server
Prerequisite - Foreign key in MS SQL Server A column can be inserted in a child table only if it is a part of the parent table in case of a foreign key. The foreign key allows us to perform other actions called Referential Actions. Referential actions allow to update or delete a row in case of the p
2 min read
SQL Query to Check Given Format of a Date
Date validation is a common requirement when working with databases. In SQL, ensuring that a date adheres to a specific format is important for maintaining data consistency and preventing errors during analysis or processing. This article will guide us through the process of using SQL queries to che
4 min read
SQL Query to Display All the Existing Constraints on a Table
In SQL, we sometimes need to display all the currently existing constraints on a table. The whole process for doing the same is demonstrated below. For this article, we will be using the Microsoft SQL Server as our database. Step 1: Create a Database. For this use the below command to create a datab
3 min read