Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Bradley Schacht

Bradley is a consultant at Pragmatic Works in Jacksonville, FL. He was an author on the book SharePoint 2010 Business Intelligence 24-Hour Trainer and tech edited the SQL 2011 Bible. His experience on the Microsoft BI platform includes DTS, SSIS, SSRS, SSAS and migrations and conversions. He has helped numerous companies in successfully developing and implementing new business intelligence solutions into their organizations. Bradley also participates as a speaker in community events like SQL Saturday, Code Camp, SQL Lunch and SQL Server User Groups. He is a contributor on sites such as BIDN.com and SQL Server Central as well as an active member of the Jacksonville SQL Server User Group (JSSUG).

Search Stored Procedure Text

There will come a day, if it has not already come and gone, when you will want to search through your stored procedures because you don’t remember which one did what you want to do.  Did that make sense?  Because I don’t know if it did.  Anyway, here is what we have…

  1. You
  2. A bunch of stored procedures
  3. Something you are looking for inside one or more of your stored procedures
  4. A long night ahead opening each one and using CTRL + F

Luckily there are a couple of super easy solutions.  Below are two such solutions.  The first one is real simple, however, it may not produce the correct results on larger procedures.

SELECT * FROM INFORMATION_SCHEMA.ROUTINES
WHERE ROUTINE_DEFINITION LIKE '%Text%'

The problem with this method is that it only stores the first 8000 characters of the stored procedure so you may miss what you are looking for.  Another way to go about searching is by using the following query.

SELECT
name AS ProcedureName,
OBJECT_DEFINITION(OBJECT_ID) AS ProcedureText
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%Text%'

This should get you everything you need without worrying about the procedures that are over 8000 characters long.  There is also another way to query the stored procedures as well as functions.  So I created the little query below that will allow you to search those objects in addition to stored procedures. If you really want to toss a little cursor or while loop around this and iterate through all stored procedures and functions across all databases.

SELECT
DB_NAME() AS DatabaseName,
CASE WHEN OBJECTPROPERTY(object_id, 'IsProcedure') = 1 THEN 'Stored Procedure'
WHEN OBJECTPROPERTY(object_id, 'IsTableFunction') = 1 THEN 'Table Function'
WHEN OBJECTPROPERTY(object_id, 'IsScalarFunction') = 1 THEN 'Scalar Function'
WHEN OBJECTPROPERTY(object_id, 'IsInlineFunction') = 1 THEN 'Inline Function'
ELSE NULL END AS ObjectType,
OBJECT_SCHEMA_NAME(object_id) AS ObjectSchema,
OBJECT_NAME(object_id) ObjectName,
definition AS ObjectText
FROM sys.sql_modules
WHERE OBJECTPROPERTY(object_id, 'IsProcedure') = 1
OR OBJECTPROPERTY(object_id, 'IsTableFunction') = 1
OR OBJECTPROPERTY(object_id, 'IsScalarFunction') = 1
OR OBJECTPROPERTY(object_id, 'IsInlineFunction') = 1
AND definition LIKE '%Text%'

Hope you enjoy!

Comments

No comments.

Leave a Comment

Please register or log in to leave a comment.