tags:

views:

22

answers:

3

Say I have a stored procedure, ProcA. It calls ProcB some of the time, ProcC all of the time and ProcD if it hits an error. ProcB calls ProcE every time. ProcC calls ProcF if it hits an error.

How could I automate getting the text of ProcA along with the text of all the procs that it calls and regress all the way down the tree (A down to F)? This would help a lot with finding errors in complex sql procs.

My first thought here is get the text of ProcA, regex through it and find any calls to other procs, wash rinse repeat, at the end spit out a text (file or to UI) that looks like this:

ProcA definition
... 
ProcB definition
...
... 
ProcF definition

But I'm open to suggestion, perhaps there's an easier way. If there's a tool that does this already lemme know. I have no idea what to put into google on this one.

+2  A: 

In SQL Server you have sys.sysdepends and sys.syscomments.

INFORMATION_SCHEMA.ROUTINES can be useful in the general SQL case, but it has a limit to the size of the text returned on SQL Server, so I avoid it.

Cade Roux
you can go to sys.comments to get full text of a proc if memory serves me right
jcollum
sorry, sys.syscomments instead of INFORMATION_SCHEMA.ROUTINES, but there's some trickery you have to do to get full text for long procs; many companies have a proc or script for it around already
jcollum
SELECT o.type_desc AS ROUTINE_TYPE ,s.[name] AS [SCHEMA_NAME] ,o.[name] AS ROUTINE_NAME ,m.definition AS ROUTINE_DEFINITION FROM sys.sql_modules AS m WITH (NOLOCK) INNER JOIN sys.objects AS o WITH (NOLOCK) ON m.[object_id] = o.[OBJECT_ID] INNER JOIN sys.schemas AS s WITH (NOLOCK) ON s.[schema_id] = o.[schema_id]
Cade Roux
A: 

this is highly database dependent, because the best way (if possible) would be to query the database's internal tables.

KM
good point. belongs in comments though. I've modified the tags on the question
jcollum
A: 

If you're using MS SQL in SQL 2000 Enterprise Manager right click on your stored procedure > All Tasks > Display Dependencies. In SQL 2005 Management Studio, right click on your stored procedure > View Dependencies.

It won't show you any code for the other objects that this proc id dependent on, but it will list the objects which you can then "wash, rinse, repeat"

Eppz