Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query the contents of stored procedures on SQL Server

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.

like image 923
Ben Aston Avatar asked Oct 07 '11 11:10

Ben Aston


People also ask

How do I get the content of a stored procedure in SQL Server?

Expand Databases, expand the database in which the procedure belongs, and then expand Programmability. Expand Stored Procedures, right-click the procedure and then select Script Stored Procedure as, and then select one of the following: Create To, Alter To, or Drop and Create To. Select New Query Editor Window.


2 Answers

SELECT OBJECT_NAME(object_id),        definition FROM sys.sql_modules WHERE objectproperty(object_id,'IsProcedure') = 1   AND definition    like '%Foo%'  
like image 50
Martin Smith Avatar answered Nov 03 '22 23:11

Martin Smith


For SQL Server 2005/2008:

SELECT  s.name SchemaName         ,o.name RoutineName         ,o.[type] RoutineType         ,procs.* FROM    sys.sql_modules procs INNER JOIN sys.objects o ON procs.object_id = o.object_id  INNER JOIN sys.schemas s ON o.schema_id = s.schema_id WHERE   procs.[definition] LIKE '%A%' --AND       o.[type] = 'P' --'P' for stored procedures 
like image 33
Bogdan Sahlean Avatar answered Nov 03 '22 23:11

Bogdan Sahlean