SQL Clone
SQLServerCentral is supported by Redgate
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: 996 | Views in the last 30 days: 1
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...