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
 Database Design and Application Architecture
 How To Chart Stored Procedures or UDFs

Author  Topic 

YSLGuru
Starting Member

2 Posts

Posted - 2010-04-14 : 14:01:46
First off let me say that I may be wording this incorrectly so if anything is unclear please reply back and let me know. I realize I may not be using the correct terms or wording and so that may be why I have had so much trouble finding an answer to this searching on the internet for quite some time already.

Is there a way be it a software tool or anyting that one can chart a STored procedure or UDF code call/execution? For example if you have a Stored Procedure that contains calls to other STored procedures that may also contain calls to other STored Procedures, is there anything out there that can create a visual representation of these code calls like a flow chart does for similiar flows?

I've been searching high and low and it looks like Stored Procedures and UDFs are the red-headed step children of the charting world. I looked at UML hopin I might have some look there but I can't find a diagram that fits the profile of visually charting SP/UDF code calls.

I'm researching a problem that involves a deeply nested set of Stored procedure calls (the SP's are stock with the app we have and so I can only investugate and not change them) and I've been manually drawing this out so far but I really need a more sharable and professional way of doing this other then pen & paper.

Am I crazy or is it that this kind of thing just does not exist?

BTW - I have boith SQL Server 2005 & 2008 on the client side but all of our serves are still running the live DB's under 2005 so I have to go with whatever is 2005 complaint even though I do use SSMS 2008 for the client/design work.

I also have VS2008 Team System Edit but I can't see where it offers anything to help with this either.

Lastly, I do have access to Visio 2007 but if it ends up being that Visio is the best way tod o thjis then can someone rpovide some guidence or suggestion on which template or chart/diagram in Visio to use? There are so many and its unclear which you would use for this kind of charting.

Thanks

Kristen
Test

22859 Posts

Posted - 2010-04-14 : 14:45:19
Dunno if something like this would help? (Probably unreliable under SQL 2000 as SYSDEPENDS wasn't reliably updated, hopefully it is under SQL2005 onwards! plus under SQL2000 the system table schema is slightly different)

SELECT O1.xtype, O1.name, O2.xtype, O2.name, D.*
FROM sys.sysdepends AS D
JOIN sys.sysobjects AS O1
ON O1.id = D.id
JOIN sys.sysobjects AS O2
ON O2.id = D.depid
AND O2.xtype NOT IN ('U', 'V') -- Ignore U=User table, V=View
ORDER BY O1.name, O2.name, D.number, D.depnumber, D.depid, D.id
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-15 : 04:13:18
BTY Kristen's post is missing a SELECT at the top.

This sounds like an interesting CTE challenge. to produce a tree of execution. (and avoid circular reference)


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

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-15 : 04:30:27
You could try this. Assuming you have 2005 or greater:

; WITH CTE AS (
SELECT
o.[name] AS [Base Object]
, o.[name] AS [Object]
, o.[xtype] AS [Object Type]
, o.[ID] AS [Object_ID]
, CAST(o.[name] AS VARCHAR(MAX)) AS [Execution Path]
, 0 AS [Level]
FROM
sys.sysObjects o
WHERE
o.xtype NOT IN ('U', 'V')

UNION ALL SELECT
CTE.[Base Object]
, o.[name] AS [Object]
, o.[xType] AS [Object Type]
, o.[ID] AS [Object_ID]
, CTE.[Execution Path] + CAST(' -> ' + o.[name] AS VARCHAR(MAX)) AS [Execution Path]
, CTE.[level] + 1
FROM
CTE
JOIN sys.sysDepends sd ON sd.[ID] = CTE.[object_ID]
JOIN sys.sysObjects o ON o.[ID] = sd.[depID]
WHERE
o.xtype NOT IN ('U', 'V')
AND CTE.[Execution Path] NOT LIKE '%' + o.[name] + '%'
)
SELECT * FROM CTE
ORDER BY
CTE.[Base Object]
, CTE.[Execution Path]

OPTION (MAXRECURSION 50);


Gives output like this for me:(Note I've removed some columns for readability)
Execution Path                                                       
---------------------------------------------------------------------
activateHolidayYearRollover
activateHolidayYearRollover -> getProRatadAnnualHolidayEntitlement
activateHolidayYearRollover -> getProRatadAnnualHolidayEntitlement ->
activateHolidayYearRollover -> getTimestamp
activateHolidayYearRollover -> holidayRound
activateHolidayYearRollover -> saveCarryOverHolidaysConfiguration
addDefaultLifestyleEventTypeForCompanies
addEmployeeToTeam

NB -- Edited output to avoid 'wide screen'
Charlie
===============================================================
Msg 3903, Level 16, State 1, Line 1736
The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-15 : 04:49:30
A slightly nicer version

/*** 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]
, o.[name] AS [Object]
, o.[xtype] AS [Object Type]
, o.[ID] AS [Object_ID]
, CAST(o.[name] AS VARCHAR(MAX)) AS [Execution Path]
, 0 AS [Level]
FROM
sys.sysObjects o
WHERE
o.xtype NOT IN ('U', 'V')
AND (o.[name] = @object OR @object IS NULL)
UNION ALL SELECT
ot.[Base Object]
, o.[name] AS [Object]
, o.[xType] AS [Object Type]
, o.[ID] AS [Object_ID]
, ot.[Execution Path] + CAST(' -> ' + o.[name] 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]
, [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
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2010-04-15 : 05:11:04
Nice job! I think I'll post my half-baked answers more often!!

Maybe ignore XTYPE = 'C' (Check), 'D' (Default) too?

Here's the full list from BoL, dunno which could potentially be present (and worth ignoring)?

AF = Aggregate function (CLR)
C = CHECK constraint
D = Default or DEFAULT constraint
F = FOREIGN KEY constraint
L = Log
FN = Scalar function
FS = Assembly (CLR) scalar-function
FT = Assembly (CLR) table-valued function
IF = In-lined table-function
IT = Internal table
P = Stored procedure
PC = Assembly (CLR) stored-procedure
PK = PRIMARY KEY constraint (type is K)
RF = Replication filter stored procedure
S = System table
SN = Synonym
SQ = Service queue
TA = Assembly (CLR) DML triggerTF = Table function
TR = SQL DML Trigger
TT = Table type
U = User table
UQ = UNIQUE constraint (type is K)
V = View
X = Extended stored procedure

and why aren;t they in a join-able sys-table?
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-15 : 05:22:16
quote:
Originally posted by Kristen

Nice job! I think I'll post my half-baked answers more often!!

Maybe ignore XTYPE = 'C' (Check), 'D' (Default) too?


This I leave as an exercise for the reader. Cheers for the extra info.


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

Kristen
Test

22859 Posts

Posted - 2010-04-15 : 05:27:30
and I also have "TF" which isn't in that BoL list (another good reason for needing an FK Table as a Constraint / Documentation tool) which I presume is a table-function that is not Inline (IF).

On that basis may also need to exclude 'PK', 'UQ', 'SQ'
in fact in my code the only ones left (to keep) are 'FN', 'IF', 'IT', 'P', 'S', 'TF'

I think also worth ignoring self-referencing items. In my code I have self referencing items for F, FN, IF, IT, P, PK, S, SQ, TF, TR, UQ

I also have DUPs - so I have added a DISTINCT

Possibly add that to the final SELECT (so that it is easy to comment in/out)

SELECT DISTINCT
[Base Object]
, [Object]
, [Object Type]
, [Execution Path]
FROM ObjectTree
WHERE 1=1
-- AND ([Object Type] NOT IN ('TR', 'F') OR [Base Object] <> [Object]) -- Ignore specific self-referncing Triggers
AND ([Base Object] <> [Object]) -- Ignore ALL self-referncing Triggers
AND [Object Type] NOT IN ('PK', 'UQ', 'SQ') -- Usual ignore candidates
ORDER BY [Base Object]
, [Execution Path]
OPTION (MAXRECURSION 50);
Go to Top of Page

Transact Charlie
Master Smack Fu Yak Hacker

3451 Posts

Posted - 2010-04-15 : 05:34:31
quote:
Originally posted by Kristen

(TC: Results on your first post are forcing wide-screen ... you may want to reformat them?)


edited/


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

YSLGuru
Starting Member

2 Posts

Posted - 2010-05-04 : 11:35:43
Thanks for the replies guys but what I'm looking for is how to chart the flow or code calls between linked or connected stored procedures; to graphically show the flow wehn SP A calls B which Calls C which then Calls D and calls E in a Loop. There are templates in products like Visio for doing this kind of code flow for procedural or Object Oriented prgrammming dealing with classes and functions/subs but there is no template or anything in any product that I can find that does this for SQL Stored procedures and or SQL UDF's.

There are several posted methods on the internet on how to find what SP an SP calls and how to tale that and show nested SP calls but what I am trying to find is a tool/product that will take the results of that and graphically chart it.

Thanks
Go to Top of Page
   

- Advertisement -