Tsql – Search Stored Proceedures and Views for text

Posted: 2012-12-07 in Sql
Tags: ,

Ms Sql Tsql query to search stored procedures and views for text (the create statements not the outputs)


DECLARE @searchString varchar(50)
set @searchString = '%searchString%'

select object_name(m.object_id) as ObjectName,
    m.definition as QueryText,
    case WHEN p.object_id is null THEN 'v' ELSE 'p' end as [ObjectType]
from sys.sql_modules m
       left join sys.procedures p on m.object_id = p.object_id
where --p.object_id is not null -- only views
    definition like '%'+ @searchString + '%'
order by object_name(m.object_id)

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s