kick it on DotNetKicks.com   Shout it  

Search the source of SQL Server views and stored procedures


Ever have to work on a database with a large number of views and you needed to search the content of the objects? Well, here's a query which will allow you to search inside your view objects:


DECLARE @search VARCHAR(1000)
SET @search = '[text]'

SELECT c.[Text]
FROM dbo.sysobjects AS v
INNER JOIN dbo.syscomments c ON c.id = v.id
AND CASE WHEN c.Number > 1 THEN c.Number
ELSE 0 END = 0
WHERE v.type = 'V' AND c.[Text] LIKE '%' + @search + '%'



Just set the value of @search to the value you'd like to find.

This was very useful to me as I was trying to muddle through an application which used a lot of nested views and I needed to determine which views referenced the view I was working on.

As a bonus (and of course for my own benefit) here's a method to search the source of stored procedures:

DECLARE @find VARCHAR(1000)

SET @find = '{search text here}'

SELECT
sp.name,
ISNULL(smsp.definition, ssmsp.definition) AS [Definition]
FROM
sys.all_objects AS sp
LEFT OUTER JOIN sys.sql_modules AS smsp ON smsp.object_id = sp.object_id
LEFT OUTER JOIN sys.system_sql_modules AS ssmsp ON ssmsp.object_id = sp.object_id
WHERE
(sp.type = N'P' OR sp.type = N'RF' OR sp.type='PC')
AND ISNULL(smsp.definition, ssmsp.definition) LIKE '%' + @find + '%'

Tags: , ,

kick it on DotNetKicks.com   Shout it  

Feedback

# 

Gravatar Very handy. I needed to look through 129,000 views which appeared to be generated multiple times from 63,000 tables. I had to check they were simple copies of the tables before deleting the lot. The generate scripts function ran all day without finishing, then errored on the weekend. This code gave me a text file that I could search in 8 minutes flat.

BTW, I can delete them now, having searched for 'GROUP BY', 'JOIN' AND 'SUM(' and not found anything. Every last one of them is flat. Isn't notepad wonderful. 3/2/2009 9:28 PM | noreply@blogger.com (Raymond)

# re: Search the source of SQL Server views and stored procedures

Gravatar Thanks for the help. I found you can also use:

select text from dbname..syscomments where id = OBJECT_ID('<view_name>') 4/26/2010 10:10 AM | Devon

Post a comment





 

Please add 2 and 4 and type the answer here:

 

 

Copyright © Mark J. Miller