Query the contents of stored procedures on SQL Server

Ben Aston picture Ben Aston · Oct 7, 2011 · Viewed 43k times · Source

I am exploring a legacy database system and have very little knowledge of its internals. I would like to find all the stored procedures that invoke another stored procedure A.

How best to do this?

Can I write something like this pseudocode:

select name from AllStoredProcedures as Asp where Asp.TextualContent contains 'A'

Asp.TextualContent means the actual SQL contained in the SP.

Answer

Martin Smith picture Martin Smith · Oct 7, 2011
SELECT OBJECT_NAME(object_id),
       definition
FROM sys.sql_modules
WHERE objectproperty(object_id,'IsProcedure') = 1
  AND definition    like '%Foo%'