Searching Stored Procedures Source Code | Database Journal

Searching Stored Procedures Source Code

Written By
Alan Enderby
Alan Enderby
Oct 7, 2000
1 minute read

Want to find that stored procedure that deletes those stock items.

This
simple (& crude) script will scan syscomments for a given string
and
display the stored procedure and the section of code.

if exists
(select * from sysobjects where id =
object_id(‘dbo.sp_findsp’) and sysstat
& 0xf = 4)
drop procedure dbo.sp_findsp
GO

create proc
sp_findsp @s varchar(255) as
DECLARE @msg varchar(255) ,@ul
varchar(255)
select @s=’%’ + @s + ‘%’
select ‘SP Name’=upper(o.name),
Seq=colid ,’SP
Line’=substring(text,patindex(@s,text)-5, 30)
from
syscomments c , sysobjects o
where o.id=c.id
and patindex(@s,text) > 0
order by name
SELECT @msg=’* Stored procedures containing string "’
+ @s + ‘=’ +
convert(varchar(8),@@rowcount) + ‘ *’
SELECT
@ul=replicate(‘*’,datalength(@msg))
Print ‘ ‘
PRINT @ul
PRINT
@msg
Print @ul

GO

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.