How do You Identify Object Dependencies in SQL Server

By:   |   Comments (11)   |   Related: 1 | 2 | 3 | 4 | 5 | More > Comparison Data and Objects


Problem

When developing components for SQL Server one change may affect another database object.  Finding these dependent objects should be straightforward, but in most cases it is not as easy as you would think.  So what is the best way to find dependency information in SQL Server?

Solution

There are several methods of getting this information.  The first approach would be to use the SQL Server Management tools. 

In SSMS, right click on the table name and select "View Dependencies" as shown below we are looking at dependencies for the Employee table.

view dependencies

This will give you the following view so you can see objects that are dependent on the Employee table.

view dependencies

And this next screen shot shows you objects that table Employee depends upon.

view dependencies

To get this information, SQL Server does a lot of work to get.  To see the process that SQL Server uses to generate this data for this screen click here.

Although this is helpful to get the data via the GUI, what other approaches are there to get this data?

Method 1 - INFORMATION_SCHEMA.ROUTINES

This approach uses INFORMATION_SCHEMA.ROUTINES to search through the definition of the routine such as the stored procedure, trigger, etc...

SELECT routine_name, routine_type FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_DEFINITION LIKE '%Employee%'
query results

Method 2 - sp_depends

This approach uses the system stored procedure sp_depends.

EXEC sp_depends @objname = N'HumanResources.Employee';
query results

Method 3 - Using syscomments

This approach reads data from the syscomments table.  This is similar to method 1.

SELECT distinct so.name
FROM syscomments sc 
INNER JOIN sysobjects so ON sc.id = so.id 
WHERE charindex('Employee', text) > 0
query results

Method 4 - sp_MSdependencies

This approach uses the system stored procedure sp_MSdependencies.

-- Value 131527 shows objects that are dependent on the specified object
EXEC sp_MSdependencies N'HumanResources.[Employee]', null, 1315327
query results
-- Value 1053183 shows objects that the specified object is dependent on
EXEC sp_MSdependencies N'HumanResources.[Employee]', null, 1053183
query results

Summary

As you can see from these different methods each gives you a different part of the answer.  So to be safe none of these approaches is full proof.  To get a complete listing you really need to employ a few different methods to ensure you are not missing anything.

An Example with New Objects

To take this a step further let's take a quick look at creating some objects to see what these different methods return.  For this next example we will create two stored procedures that rely on each other.  This is probably not something you would do, but this helps illustrate the issue.

create proc a
as
exec b
go 
-- after creating proc a we get this error message
-- Cannot add rows to sysdepends for the current object because it depends on the missing object 'b'. 
-- The object will still be created. 
create proc b
as
exec a
go

Now let's see what each of these methods returns after these two stored procedures have been created.

Method 1 - INFORMATION_SCHEMA.ROUTINES

SELECT routine_name, routine_type FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_DEFINITION LIKE '%a%'

This returns the following showing both proc a and proc b.

query results

Method 2 - sp_depends

exec sp_depends a

This returns the following showing only that proc b depends on proc a.

query results

Method 3 - Using syscomments

SELECT distinct so.name 
FROM syscomments sc 
INNER JOIN sysobjects so ON sc.id = so.id 
WHERE charindex('a', text) > 0

This returns the following showing both proc a and proc b.

query results

Method 4 - sp_MSdependencies

EXEC sp_MSdependencies N'dbo.a', null, 1315327
EXEC sp_MSdependencies N'dbo.a', null, 1053183

This returns the following showing only that proc b depends on proc a.

query results

Management Studio

This returns the following showing only that proc b depends on proc a.

view dependencies
sp dep15

So from this test the only two processes that returned the results we were expecting are Method 1 and Method 3.

Since stored procedure "b" did not exist when we created procedure "a" this dependency data does not exist.  If we alter stored procedure "a" and save the procedure the dependencies should be updated correctly as shown below.  This would be true for both sp_depends, sp_MSdependencies and SQL Server Management Studio.

view dependencies
view dependencies

As you have seen there are some cases where this information is reliable and others where the data is not reliable.  Make sure you check all methods before determining whether you have a complete list of all your object dependencies.

Next Steps


sql server categories

sql server webinars

subscribe to mssqltips

sql server tutorials

sql server white papers

next tip



About the author
MSSQLTips author Greg Robidoux Greg Robidoux is the President and founder of Edgewood Solutions, a technology services company delivering services and solutions for Microsoft SQL Server. He is also one of the co-founders of MSSQLTips.com. Greg has been working with SQL Server since 1999, has authored numerous database-related articles, and delivered several presentations related to SQL Server. Before SQL Server, he worked on many data platforms such as DB2, Oracle, Sybase, and Informix.

This author pledges the content of this article is based on professional experience and not AI generated.

View all my tips



Comments For This Article




Tuesday, August 18, 2020 - 9:14:39 AM - bernard black Back To Top (86320)
Nice explanation. I didn't know of some of those queries. Thanks.

Wednesday, April 15, 2015 - 11:27:11 PM - cornstarch Back To Top (36950)

I was wondering if there is a method out there to get all objects cross database/server that referenced a table. 

Respectfully,

cornstarch

 

 

 


Saturday, December 7, 2013 - 2:04:36 AM - Pramod Sharma Back To Top (27726)

Great!!! thanks.


Sunday, December 1, 2013 - 1:09:52 AM - MOhammad Back To Top (27643)

Very good . thanks


Thursday, August 29, 2013 - 12:41:31 AM - Adam Back To Top (26518)

Great tips.

Although it's not always possible to find all dependencies quickly and efficiently.

I stumbled across SQL Negotiator Pro available from www.aphilen.com this application graphically draws all dependencies with the capability of adding notes and exporting. Very nice!

 

 


Monday, April 29, 2013 - 1:18:49 PM - mohamed Back To Top (23620)

thanks a lot,it's a great tips  


Thursday, January 31, 2013 - 7:29:40 PM - Lan Nguyen Back To Top (21854)

Thank you!


Thursday, July 5, 2012 - 2:35:57 AM - venushakamuri Back To Top (18351)

very good Post ...............................


Wednesday, July 4, 2012 - 7:49:45 AM - Aj Back To Top (18340)

Great!! Thanks


Monday, March 26, 2012 - 6:15:37 AM - Anna Back To Top (16609)

Awesome! Thanks a lot.


Wednesday, September 17, 2008 - 8:31:52 PM - jacobsebastian Back To Top (1832)

 Great post!

For SQL Server 2005, I wrote a table valued function that implements a recursive CTE to return the entire dependency chain of given object. The code is published here: http://www.sqlserverandxml.com/2008/09/find-dependent-objects-recursively.html

Regards

Jacob Sebastian















get free sql tips
agree to terms