65.9K
CodeProject is changing. Read more.
Home

How to find a specific string from stored procedures, triggers and functions in database

emptyStarIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

0/5 (0 vote)

Feb 7, 2012

CPOL
viewsIcon

6610

Simply add your search phrase and run to find jobs, Stored Procedures, Keys, Tables, Views, Indexes, Defaults, and Functions.set nocount on declare @Keyword as varchar(200)set @Keyword = 'Search prase'--<---- enter keyword here--Search jobsselect name Job, enabled , description, ...

Simply add your search phrase and run to find jobs, Stored Procedures, Keys, Tables, Views, Indexes, Defaults, and Functions.

set nocount on 
declare @Keyword as varchar(200)
set @Keyword = 'Search prase'--<---- enter keyword here

--Search jobs
select name Job, enabled , description,  date_created, date_modified ,step_id,  step_name ,subsystem,
	command, last_run_date
	from msdb.dbo.sysjobs j with (nolock)
	join msdb.dbo.sysjobsteps s on j.job_id = s.job_id
	where command like'%'+ @Keyword +'%'

/*
Search: 
stored procedures
Keys
Tables
Views
Indexes
Defaults
Functions
*/

SELECT DISTINCT
	name, 
	CASE o.xtype 
		WHEN 'S' THEN 'System Stored Proc'
		WHEN 'F' THEN 'Foreign Key'
		WHEN 'U' THEN 'Table'
		WHEN 'V' THEN 'View'
		WHEN 'P' THEN 'Stored Proc'
		WHEN 'PK' THEN 'Primary Key'
		WHEN 'TR' THEN 'Trigger'
		WHEN 'UQ' THEN 'Unique Index'
		WHEN 'D' THEN 'Default'
		WHEN 'FN' THEN 'User Defined Function'
		ELSE 'Unknown'
	END, 
	text 
FROM
	sysobjects o left join syscomments c ON c.id = o.id
WHERE
	text LIKE '%' + @Keyword + '%' 
OR	name LIKE '%' + @Keyword + '%'