Home » Find object dependencies in SQL Server

Find object dependencies in SQL Server

by Vlad Drumea
2 comments

This is a brief post containing a query that lists object dependencies in SQL Server for either a specific object or for the entire database.

Intro

Figuring out object dependencies in a SQL Server database, especially one you’re not familiar with, can be a challenge.
Luckily, Microsoft provides the sys.sql_expression_dependencies system catalog view to help DBAs and developers track down various object dependencies.

I leveraged this catalog view in a query that returns the necessary info in a more user-friendly way, while also allowing easy filtering for referenced or referencing object.

The query

To search for a specific object, set the value of the @ObjectName parameter to the name of the object.
The object name can be schema-qualified and even wrapped in square brackets.
The query will return all objects that depend on specified object as well as the objects it depends on.

If the @ObjectName parameter is left empty, then the dependencies for the entire database are returned.


You can also find it in my SQL Server Scripts repo as GetObjectDependencies.sql.

Object dependencies result set

The result set is fairly self-explanatory. Unfortunately, it’s a bit wider than what I can fit in a decent screenshot.

One thing to note is, that for computed columns, the referenceing_object_name column will also contain the name of that column as part of the fully qualified object name.


Note

Due to the way that sys.sql_expression_dependencies works, the referencing_database_name column can contain CROSS APPLY alias names, or, in the case of XML data type methods, aliases, CTE names or the inserted/deleted tables.

This is not due to the query itself, but due to the information returned from sys.sql_expression_dependencies.

Here’s an example from the AdventureWorks2019 database, where the Person.iuPerson trigger appears to reference a database named inserted.


But looking ad the trigger’s DDL shows that’s not the case, and it is, in fact, using the exist() XML method on the inserted.Demographics column.


The relevant portion of the code being:


Conclusion

That’s pretty much it.
Just a query that should help you when you’re digging through database object dependencies in SQL Server.

If you have more digging to do in unfamiliar databases, you might also want to give this post a read:

You may also like

2 comments

George Walkey April 16, 2025 - 14:48

Hey Vlad
your script is picking up Cross Apply Aliases as Database names but a nice script overall

Reply
Vlad Drumea April 16, 2025 - 15:21

Hey George
Thank you for the feedback!
Unfortunately, that is a limitation of sys.sql_expression_dependencies.
If you run
SELECT * FROM sys.sql_expression_dependencies;
you’ll notice that the referenced_database_name column can contain things that aren’t databases, such as aliases, CTE names, etc.
One example is the Person.iuPerson trigger in the AdventureWorks2019 database where using an XML method ( inserted.[Demographics].exist() ) causes inserted to show up as a referenced database.

I’ll make a clarifying note of this in the post to help avoid confusion.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.