Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 Script Library
 Search all code for specific keyword

Author  Topic 

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-14 : 09:09:51
This will work on SQL Server 2005 and later.
Since the code is building an XML string, keywords overlapping the magic 4000 character limit are fetched!
SELECT	p.RoutineName, 
'EXEC sp_helptext ' + QUOTENAME(p.RoutineName) AS [Exec]
FROM (
SELECT OBJECT_NAME(so.ID) AS RoutineName,
(SELECT TOP 100 PERCENT '' + sc.TEXT FROM SYSCOMMENTS AS sc WHERE sc.ID = so.ID ORDER BY sc.COLID FOR XML PATH('')) AS Body
FROM SYSOBJECTS AS so
WHERE so.TYPE IN ('C', 'D', 'FN', 'IF', 'P', 'R', 'RF', 'TF', 'TR', 'V', 'X')
) AS p
WHERE p.Body LIKE '%YourKeyWordHere%'

E 12°55'05.25"
N 56°04'39.16"

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2008-03-14 : 09:33:50
I like the output -- very nice idea!

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-14 : 11:54:34
The types are
C = CHECK constraint 
D = Default or DEFAULT constraint
FN = Scalar function
IF = In-lined table-function
P = Stored procedure
R = Rule
RF = Replication filter stored procedure
TF = Table function
TR = Trigger
V = View
X = Extended stored procedure

I were suggested to use sys.sql_modules instead but then types C and X are not shown.


E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2008-03-14 : 15:23:36
I really like this script.

As a suggestion, you may want to include the schema in the object name, in case the object is not dbo.


select
--p.RoutineName,
'exec sp_helptext ''' +p.RoutineName+'''' AS [Exec]
from (
select
quotename(user_name( so.uid ))+'.'+
quotename(object_name(so.id)) as RoutineName,
(
select top 100 percent
'' + sc.text
from
syscomments as sc
where
sc.id = so.id
order by
sc.colid
for xml path('')
) as body
from
sysobjects as so
where
so.type in
('C','D','FN','IF','P','R','RF','TF','TR','V','X')
) as p
where
-- String to search for
p.Body LIKE '%##%'


Results:
Exec
-------------------------------------------------------
exec sp_helptext '[DevGuy].[MyBadlyCodedProc]'
exec sp_helptext '[dbo].[MyBadlyCodedFunction]'



CODO ERGO SUM
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-14 : 18:22:40
Excellent. Thank you both.
Throw in an ORDER BY p.RoutineName also and I am happy.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-03-15 : 04:48:40
In previous versions, script out to text file and do search
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/12/13/script-out-procedures-and-functions-part-2.aspx


Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -