views:

1107

answers:

5

Is there an easy way to chase down table/stored procedure/function dependencies in Sql Server (I'm using 2k5)? I've inherited a giant application with lots of tables and even more stored procedures and functions that are long and interlinked.

At the end of the day is there a way to build a dependency tree? Ideally what I'm looking for goes in both directions:

For a table/procedure - what depends ON it?: Show me all the stored procedures that eventually reference it (ideally in a tree view such that sub procedures nest out to the bigger procedures that call them)

For a procedure - what does IT depend on?: Show me all the procedures and tables that a given procedure will (or could) touch when running.

It seems this tool shouldn't be that hard to make and would be incredibly useful for DB maintenance generally. Is anyone aware of such a thing? If this doesn't exist, why the heck not?

EDIT: The built in functionality in Management Studio is nice but the information does not appear to be complete at all.

A: 

I don't think it's a guaranteed-complete list, but in Management Studio you can right click on a table or stored procedure and choose the View Dependencies option.

Joel Coehoorn
+4  A: 

Red Gate has a pretty useful tool called SQL Dependency Tracker. We've successfully used it for the type of results you're wishing to obtain.

Scott Saad
Finally got around to using the free trial - this is a great tool.
Cory
A: 

The system table that attempt to keep track of dependencies is usually wrong, so any answer you get from that, you will have to re-confirm by other means, so why bother with it?

Commercial products exist, such as Redgate SQL Dependency Tracker.

A poor developer like myself, I use SQL Digger, which is free. By searching the DDL for an object name, you usually can find what first degree dependencies there are for an object in question.

The next level of dependency tracing is to trace what C# or VB.NET net objects depend on an object in SQL, but AFAIK, tools don't exist for that outside of global search.

MatthewMartin
+1  A: 

Here is a list of options if you're low on the budget:

http://www.mssqltips.com/tip.asp?tip=1294

You could also run a trace and see what management studio is actually doing when you click 'view dependencies'. Grab that code and see if you can modify it for your own use. This is a good technique for figuring out how to automate various things that you normally do through the UI.

Sam
How would one "Grab that code"?
Cory
Using profiler you should be able to see what code is running against the server.
Tom H.
+7  A: 

Hopefully I'm not too late with this:

If your SQL Login has access to the sys schema in a particular database, you can use the sys.dependencies view to find all of an object's dependencies in one shot:

SELECT o.name, o.type_desc, p.name, p.type_desc
FROM sys.sql_dependencies d
INNER JOIN sys.objects o
    ON d.object_id = o.object_id
INNER JOIN sys.objects p
    ON d.referenced_major_id = p.object_id

Using this as a starting point you could probably build a decent tool to create a dependency tree. There are also type specific views (e.g. sys.columns) that give more in depth information regarding each specific database object type; these could be used to provide contextual information on an object if necessary.

Jeremy Seghi
Does this work for SQL Server 2000?
Jonathon Watney
I believe the Object Catalog views that the `sys.objects` table belongs to was introduced in SQL 2005, but a similar query can be written using the Information Schema views. They are a little bit trickier, but you can find documentation on them at http://msdn.microsoft.com/en-us/library/aa933204(SQL.80).aspx
Jeremy Seghi