In SQL Server, making changes to any existing database objects like deleting a table or changing the name of a table or a table column can affect other database objects like views, procedures, and functions where the updated or deleted database object was referenced. So before updating or deleting a database object, it is best to view the dependencies of the current object being deleted or edited. So we can use the SQL Server system Stored Procedure 'sp_depends' to check for object dependencies. In this article, we will see about 'sp_depends', its syntax, and usage in different scenarios.
Syntax:
sp_depends is a SQL Server system stored procedure and below is the syntax of its usage.
sp_depends [ @objname = ] '<object>' <object> ::= { [ database_name. [ schema_name ] . | schema_name. object_name }
Explanation of the syntax:
- @objectname - is the variable where the dependencies are assigned.
- <object> - is the name of the object for which the dependency is being checked.
Arguments:
- database_name: Replace here with the name of the database being used.
- schema_name: Replace with the name of the schema to which the object belongs.
- object_name: This refers to the database object, to check for dependencies. The object_name can be a table, view, stored procedure, user-defined function, or trigger. object_name data type is nvarchar(776), with no default value.
Example: How the 'sp_depends' is Used
Let us see a few examples of how the 'sp_depends' is used and the information we can view using this command.
The table used in the below examples:
CREATE TABLE [dbo].[Students](
[Student_ID] [int] NULL,
[Student_Name] [varchar](50) NOT NULL,
[Grade] [char](1) NULL,
[Grade_Level] [varchar](50) NULL,
[Course] [varchar](80) NULL,
[Exam_Name] [varchar](50) NULL,
[Student_Age] [tinyint] NULL
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Students] WITH CHECK ADD CONSTRAINT [CK_StudentAge] CHECK (([Student_Age]>=(3)))
GO
ALTER TABLE [dbo].[Students] CHECK CONSTRAINT [CK_StudentAge]
GO
In addition to the table, there are 2 Stored Procedures (spStudentsDemo1 and spStudentsDemo2), View named 'StudentRefView', and a scalar function named 'StudentTotalMarks' are there in the database. These are references to objects used in the examples given below
Example 1
EXEC sp_depends @objname=N'Students'
Below is the output received:
Result for sp_depends example 1The output shows the objects which are dependent on the table 'Students', meaning it displays the places where the table 'Students' is used or referenced. So if any change done in the table 'Students' can affect these objects.
In the Output, the column 'name' shows the objects where the table 'Students' is referenced and the type of each object is given under the column 'type'. The types referenced here are check constraint (check cns), stored procedure, view, and scalar function as displayed above.
Example 2
There is another result set displayed when an object is dependent on other objects like the View object 'StudentRefView'as given below:
exec sp_depends @objname=N'StudentRefView'
In this example, the object 'StudentRefView' is a 'View', used to check the dependencies
Below is the output received with 2 result sets:
Result sets for sp_depends example 2In the above output, there are 2 result sets. The first result set has 5 columns and as explained below, shows the objects, referenced by the current object 'StudentRefView' given in this example:
- name – Shows the name of the dependent object
- type – Displays the type of the dependent object (e.g. table)
- updated – This column shows whether the object is updated or not
- selected – object is used in a SELECT statement in the current object
- column – Dependent Column name from the object name specified
The second result set shows the name of the dependent objects and the type of the dependent objects used for creating the object 'StudentRefView'.
Conclusion
The sp_depends is a very useful and efficient tool when we have a large database and have multiple developers working on it so that before making changes to a database object, development team members can check and verify all the dependencies the database objects are not broken.
Similar Reads
sp_columns - SQL Server
In SQL Server, managing and understanding database schemas is crucial for effective database administration and development. The sp_columns stored procedure is a valuable tool for retrieving detailed metadata about the columns of a specified table or view. In this article, We will learn about sp_col
6 min read
Dynamic SQL in SQL Server
In SQL Server, at times the SQL Queries need to be dynamic and not static, meaning the complete SQL query may be built dynamically at run time as a string using the user inputs and any specific application logic. This can be done in queries run from back-end applications or inside stored procedures.
6 min read
SQL Server - Database Objects
In SQL Server, database objects are essential components that allow to store and manage data effectively. These objects can range from tables and views to stored procedures and indexes. Understanding the various types of database objects is important for database design, management, and optimization
5 min read
SQL Server Describe Table
SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. When working with databases in SQL Server it is essential to understand the schema of the tables present in the database. Describing a table means getting informati
4 min read
EXEC vs SP_EXECUTESQL in SQL Server
In SQL Server, dynamic SQL is a method to construct a SQL query at run time. For executing dynamic SQL queries there is a command called EXEC or EXECUTE and then the built-in stored procedure called sp_executesql. Both are used in running dynamic SQL queries in string format or using SQL variables.
4 min read
CREATE TABLE in SQL Server
SQL Server provides a variety of data management tools such as querying, indexing, and transaction processing. It supports multiple programming languages and platforms, making it a versatile RDBMS for various applications. With its robust features and reliability, SQL Server is a popular choice for
4 min read
SQL Server Architecture
Microsoft SQL Server is a widely used relational database management system (RDBMS) that organizations around the world rely on for managing and processing their data. It provides a scalable and reliable platform for managing large volumes of data, supporting a wide range of applications from small-
5 min read
SQL Server Interview Questions
Data is growing every day, and it plays a big part in making important decisions. To manage this data, we need reliable databases, and SQL Server is one of the most popular options out there. Learning SQL Server can lead to exciting careers as an SQL Specialist or SQL Developer. If youâre preparing
15+ min read
Nested Queries in SQL
Nested queries, also known as subqueries, are an essential tool in SQL for performing complex data retrieval tasks. They allow us to embed one query within another, enabling us to filter, aggregate, and perform sophisticated calculations. Whether we're handling large datasets or performing advanced
7 min read
List All Databases in SQL Server
In SQL Server, databases are crucial for storing and managing data efficiently. Whether we are managing a large enterprise system or a small application, understanding how to list all the databases on our SQL Server is essential. In this article, we will write SQL queries that help us to retrieve al
3 min read