Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Object Execution Dependencies

Author  Topic 

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-29 : 07:43:44
I recently had to reverse engineer a third party schema that was playing up and part of this was to find out what procedures and functions could be called by a particular object. I've written this recursive report that can tell you.

It's a little rough and ready but if you need something like this you can customise it.

It won't help you track sub executions that are the result of dynamic SQL so beware of that.

The only parameter is the @objectName -- you can either set this to a procedure or function name or leave NULL to get all the objects.

Here it is

/*** Generate Procedure and Function Dependancy Chart *************************
*
* Makes a tree list of object(s) dependancies. Set @object to the desired
* object name or leave NULL to bring back all objects.
*
* Charlie (2010-04-15)
*
******************************************************************************/

DECLARE @object VARCHAR(255) SET @object = NULL

/*****************************************************************************/

; WITH ObjectTree AS (
SELECT
o.[name] AS [Base Object]
, CAST('' AS SYSNAME) AS [Dependant Object]
, o.[xtype] AS [Object Type]
, o.[ID] AS [Object_ID]
, CAST('' AS VARCHAR(MAX)) AS [Execution Path]
, 0 AS [Level]
FROM
sys.sysObjects o
WHERE
o.xtype IN ('P', 'FN')
AND (o.[name] = @object OR @object IS NULL)
UNION ALL SELECT
ot.[Base Object]
, o.[name] AS [Dependant Object]
, o.[xType] AS [Object Type]
, o.[ID] AS [Object_ID]
, ot.[Execution Path] + CAST(
CASE ot.[Execution Path] WHEN '' THEN o.[name]
ELSE ' -> ' + o.[name]
END AS VARCHAR(MAX)) AS [Execution Path]
, ot.[level] + 1
FROM
ObjectTree ot
JOIN sys.sysDepends sd ON sd.[ID] = ot.[object_ID]
JOIN sys.sysObjects o ON o.[ID] = sd.[depID]
WHERE
o.xtype NOT IN ('U', 'V')
AND ot.[Execution Path] NOT LIKE '%' + o.[name] + '%'
)

SELECT
[Base Object]
, [Dependant Object]
, [Object Type]
, [Execution Path]
FROM
ObjectTree
ORDER BY
[Base Object]
, [Execution Path]

OPTION (MAXRECURSION 50);

Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-29 : 08:06:19
Might I suggest:
WHERE [Base Object] != [Object]


------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-29 : 08:10:03
this piece

AND ot.[Execution Path] NOT LIKE '%' + o.[name] + '%'

Should take of circular reference on its own.

If its broken for you though can you post a little test case?

Cheers.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

DBA in the making
Aged Yak Warrior

638 Posts

Posted - 2010-04-29 : 08:17:09
I ran it on the AdventureWorks database (SQL Server 2005), and every object in the database was listed as a dependency of itself, which seems a little redundant.

Also, it doesn't list the schema of the objects, which would be handy.

------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-29 : 10:38:39
thanks for the feeback -- I've ammended it slightly.

re the schema name -- good idea but I'll have to change the sys tables from sys.sysObjects to sys.objects. Will do that at some point.


Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page
   

- Advertisement -