Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Finding Dynamic SQL's Dependencies

By Arthur Gimpel,

 In order to use this script, you should have a table named dbo.CommandList. In my example, this table contains:

  • ServerName: the name of the linked server in which the query might run.
  • DBName: the Database in which the query might run.
  • SQLCommand: the query for which the script will find the dependencies.

What the script basically does is accessing the server (assuming the linked server works) and creates temporary stored procedure for the dynamic SQL. After creation, it finds its dependencies through sys.dm_sql_referenced_entities DMF.

I've tried several techniques to retrieve dynamic SQL's dependencies, and this was the only one which actually worked.

Feel free to modify the script to work with Dynamix SQL in variables and not tables, and keep in mind that sys.dm_sql_referenced_entities works only in SQL Server 2008 +. For older versions you should use other DMV's.

Total article views: 958 | Views in the last 30 days: 2
Related Articles

Can query performance be dependent on the client?

This question sounds really easy! The query performance depends essentially on the execution plan, w...


Routine Dependency Visualizer

This article contains a T-SQL script that can show you the dependency of all objects in your SQL Ser...


SQL Server Object Dependency Viewer Revisited

A year ago I blogged about building a SQL Server 2008 Object Dependency Viewer based on a script by ...


Get Top Queries Taxing SQL Server CPUs

Script which uses SQL Server DMOs to gather top CPU taxing queries which can benifit most from tunin...


Finding Column Dependencies

Most experienced DBAs know that sysdepends is not very reliable, so how can you find the dependencie...


Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones