65.9K
CodeProject is changing. Read more.
Home

Searching within all the Stored Procedures in a database

starIconstarIconstarIconemptyStarIconemptyStarIcon

3.00/5 (1 vote)

Mar 15, 2012

CPOL
viewsIcon

13820

You can search for any string with in the TSQL scripts of all the Stored Procedures in a database using the syscomments table.

Introduction

Sometimes we may want to find out which procedure updates a particular column to a given status, which all procedures are joining two given tables etc. If we can search with in the stored procedure text it will enable us to isolate the problem and rectify bugs faster.

Using the code

Just query the syscomments table that stores all the TSQL scripts of the procedures. The below query also joins sys.objects also to find out the created and last modified time of the stored procedures.

SELECT so.name ProcedureName, TEXT
, so.create_date [Created Date]
, so.modify_date [Modified Date]
FROM SysComments sc
JOIN Sys.Objects so ON OBJECT_NAME(sc.id) = so.name
WHERE Text LIKE '%Sample%'

Note that this script works only with SQL Server