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 PowerShell MVP, Doug Finke (Blog|Twitter). Since then Doug has created an alternative solution based on the new Microsoft Automatic Graph Layout features in Visual Studio 2010. The approach Doug takes builds a DGML XML file using PowerShell which then can be opened in Visual Studio 2010.

I thought it would be interesting to create an updated version of the SQL Server Object Dependency Viewer using DGML. Rather than simply running Doug’s script as-is I decided to develop an alternative solution targeted for SQL Server. Because the new solution only requires creating a DGML XML file and SQL Server can emit XML natively I used the following T-SQL/XQuery (no PowerShell required! Nonetheless I included a one-liner at the end of this post)

Requirements:

  • SQL Server 2008 or higher database
  • Visual Studio 2010

Run the following script from SQL Server Management Studio

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
;WITH
xmlnamespaces
(
DEFAULT 'http://schemas.microsoft.com/vs/2009/dgml'
)
,Links AS
(SELECT 1 AS 'DirectedGraph')
,Link AS
(SELECT DISTINCT
OBJECT_NAME(referencing_id) AS [Source],
COALESCE(referenced_server_name + '.','') + COALESCE(referenced_database_name + '.','')
+ COALESCE(referenced_schema_name + '.','') + referenced_entity_name AS [Target],
o.type_desc AS SourceType
FROM sys.sql_expression_dependencies AS sed
INNER JOIN sys.objects AS o ON sed.referencing_id = o.object_id
AND o.type_desc != 'CHECK_CONSTRAINT')
 
SELECT
(
	SELECT [Source] AS "@Source", [Target] AS "@Target"
	FROM Link for xml path('Link'), type
)
FROM Links for xml AUTO, root('DirectedGraph'), type

Save the output as a dgml file, for example AdventureWorksLT.dgml. Next double-click to open the file in Visual Studio. You should see a dependency graph similar to this:

vsdepends1

If you want to automate a the steps of saving and opening the DGML file. Save the T-SQL script above as dgml.sql and create a PowerShell script you can then call from sqlps host:

1
2
3
$fileName = "C:\Users\u00\Desktop\AdventureWorks.dgml"
Invoke-Sqlcmd -ServerInstance "Z003\R2" -Database "AdventureWorksLT" -InputFile "C:\Users\u00\Desktop\dgml.sql" -MaxCharLength 8000 | Select -ExpandProperty Column1 | Set-Content $fileName
invoke-item $fileName
This work, unless otherwise expressly stated, is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
One Response to SQL Server Object Dependency Viewer Revisited
  1. [...] SQL Server Object Dependency Viewer Revisited (blog de Chad Miller, via Greg Duncan) Haaaa, les dépendances entres objets sous SQL Server. Chad Miller donne un exemple de requête permettant de générer du DGML à partir de la vue sys.sql_expression_dependencies, à ne pas confondre avec sys.sql_dependencies ou encore sys.sysdepends. Cet exemple nécessite Visual Studio 2010 (pour la visualisation du graphe DGML) et SQL Server 2008 minimum (pour la vue sys.sql_expression_dependencies). Ce post vous a plu ? Ajoutez le dans vos favoris pour ne pas perdre de temps à le retrouver le jour où vous en aurez besoin : addthis_url=location.href;addthis_title=document.title;addthis_pub='Nix'; Posted: dimanche 15 août 2010 23:46 par coq Classé sous : .NET, SQL Server, ASP.NET, Outils, Performance, Debug, Trucs intéressants, Event Tracing for Windows (ETW) [...]

Leave a Reply

Wanting to leave an <em>phasis on your comment?

Trackback URL http://sev17.com/2010/08/sql-server-object-dependency-viewer-revisited/trackback/