Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database

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

0.00/5 (No votes)
7 Feb 2012CPOL 6.6K  
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.


SQL
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 + '%'

License

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