Click here to Skip to main content
12,829,115 members (43,284 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked
Posted 6 Jun 2013

Find text in Stored Procedures, View, Trigger, and Function

, 6 Jun 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
Find text in Stored Procedures, View, Trigger, and Function.


I have often needed to find a stored procedure, view, trigger, and function that contains a certain piece of text, such as a text in a subject line.  

Typically, people are trying to find all the stored procedures that reference a specific object. The best place to do this kind of searching is through your source control tool (you do keep your database objects in source control, don't you?), there are certainly some ways to do this in the database. 

Using the code

Let's say you are searching for 'foobar' in all your stored procedures, view, trigger, and function. You can do this using syscomments:  

DECLARE @SEARCHSTRING VARCHAR(255), @notcontain Varchar(255)

SELECT @SEARCHSTRING = 'foobar', @notcontain = ''

case when sysobjects.xtype = 'P' then 'Stored Proc'
when sysobjects.xtype = 'TF' then 'Function'
when sysobjects.xtype = 'TR' then 'Trigger'
when sysobjects.xtype = 'V' then 'View'
end as [Object Type]
FROM sysobjects,syscomments
AND sysobjects.type in ('P','TF','TR','V')
AND sysobjects.category = 0
AND ((CHARINDEX(@notcontain,syscomments.text)=0 
or CHARINDEX(@notcontain,syscomments.text)<>0))  

If you want to remove some string from searching you can always change the variable value "@notcontain" to the corresponding value, example (remove word "comment" in the search above): 

SELECT @SEARCHSTRING = 'foobar', @notcontain = 'comment'


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nelson Kosta Souto
Portugal Portugal
My name is Nelson Souto, I develop software for fun and profit since 1992.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Hugo Durana6-Jun-13 12:11
memberHugo Durana6-Jun-13 12:11 
QuestionAlternative Pin
Mika Wendelius6-Jun-13 4:28
mvpMika Wendelius6-Jun-13 4:28 
AnswerRe: Alternative Pin
Davinder Singh21-Aug-13 5:44
memberDavinder Singh21-Aug-13 5:44 
GeneralRe: Alternative Pin
Nelson Kosta Souto2-Sep-13 1:25
memberNelson Kosta Souto2-Sep-13 1:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170326.1 | Last Updated 6 Jun 2013
Article Copyright 2013 by Nelson Kosta Souto
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid