SELECT DISTINCTo.name AS Object_Name,o.type_descFROM sys.sql_modules m INNER JOIN sys.objects oON m.object_id = o.object_id WHERE m.definition Like '%[String]%';
SELECT DISTINCTA.NAME AS OBJECT_NAME,A.TYPE_DESCFROM SYS.SQL_MODULES MINNER JOIN SYS.OBJECTS A ON M.OBJECT_ID = A.OBJECT_IDWHERE M.DEFINITION LIKE '%['+@SEARCH_TEXT+']%'ORDER BY TYPE_DESC
/*SEARCH SPROCS & VIEWS
The following query will allow search within the definitionsof stored procedures and views.
It spits out the results as XML, with the full definitions,so you can browse them without having to script them individually.
*/
/*STEP 1: POPULATE SEARCH KEYS. (Set to NULL to ignore)*/DECLARE@def_key varchar(128) = '%foo%', /* <<< definition search key */@name_key varchar(128) = '%bar%', /* <<< name search key */@schema_key varchar(128) = 'dbo'; /* <<< schema search key */
;WITH SearchResults AS (/*STEP 2: DEFINE SEARCH QUERY AS CTE (Common Table Expression)*/SELECT[Object].object_id AS [object_id],[Schema].name AS [schema_name],[Object].name AS [object_name],[Object].type AS [object_type],[Object].type_desc AS [object_type_desc],[Details].definition AS [module_definition]FROM/* sys.sql_modules = where the body of sprocs and views live */sys.sql_modules AS [Details] WITH (NOLOCK)JOIN/* sys.objects = where the metadata for every object in the database lives */sys.objects AS [Object] WITH (NOLOCK) ON [Details].object_id = [Object].object_idJOIN/* sys.schemas = where the schemas in the datatabase live */sys.schemas AS [Schema] WITH (NOLOCK) ON [Object].schema_id = [Schema].schema_idWHERE(@def_key IS NULL OR [Details].definition LIKE @def_key) /* <<< searches definition */AND (@name_key IS NULL OR [Object].name LIKE @name_key) /* <<< searches name */AND (@schema_key IS NULL OR [Schema].name LIKE @schema_key) /* <<< searches schema */)/*STEP 3: SELECT FROM CTE INTO XML*/
/*This outer select wraps the inner queries in to the <sql_object> root element*/SELECT(/*This inner query maps stored procedure rows to <procedure> elements*/SELECT TOP 100 PERCENT[object_id] AS [@object_id],[schema_name] + '.' + [object_name] AS [@full_name],[module_definition] AS [module_definition]FROMSearchResultsWHEREobject_type = 'P'ORDER BY[schema_name], [object_name]FOR XMLPATH ('procedure'), TYPE) AS [procedures], /* <<< as part of the outer query,this alias causes the <procedure> elementsto be wrapped within the <procedures> element */(/*This inner query maps view rows to <view> elements*/SELECT TOP 100 PERCENT[object_id] AS [@object_id],[schema_name] + '.' + [object_name] AS [@full_name],[module_definition] AS [module_definition]FROMSearchResultsWHEREobject_type = 'V'ORDER BY[schema_name], [object_name]FOR XMLPATH ('view'), TYPE) AS [views] /* <<< as part of the outer query,this alias causes the <view> elementsto be wrapped within the <views> element */FOR XMLPATH ('sql_objects')
CREATE PROCEDURE [dbo].[Searchinall](@strFind AS VARCHAR(MAX))ASBEGINSET NOCOUNT ON;--TO FIND STRING IN ALL PROCEDURESBEGINSELECT OBJECT_NAME(OBJECT_ID) SP_Name,OBJECT_DEFINITION(OBJECT_ID) SP_DefinitionFROM sys.proceduresWHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@strFind+'%'END
--TO FIND STRING IN ALL VIEWSBEGINSELECT OBJECT_NAME(OBJECT_ID) View_Name,OBJECT_DEFINITION(OBJECT_ID) View_DefinitionFROM sys.viewsWHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@strFind+'%'END
--TO FIND STRING IN ALL FUNCTIONBEGINSELECT ROUTINE_NAME Function_Name,ROUTINE_DEFINITION Function_definitionFROM INFORMATION_SCHEMA.ROUTINESWHERE ROUTINE_DEFINITION LIKE '%'+@strFind+'%'AND ROUTINE_TYPE = 'FUNCTION'ORDER BYROUTINE_NAMEEND
--TO FIND STRING IN ALL TABLES OF DATABASE.BEGINSELECT t.name AS Table_Name,c.name AS COLUMN_NAMEFROM sys.tables AS tINNER JOIN sys.columns cON t.OBJECT_ID = c.OBJECT_IDWHERE c.name LIKE '%'+@strFind+'%'ORDER BYTable_NameENDEND
if object_id('tempdb..##nothing') is not nulldrop table ##nothing
CREATE TABLE ##nothing(DatabaseName varchar(30),SchemaName varchar(30),ObjectName varchar(100),ObjectType varchar(50))
EXEC master.sys.sp_msforeachdb'USE ?insert into ##nothingSELECTdb_name() AS [Database],[Scehma]=schema_name(o.schema_id),o.Name,o.typeFROM sys.sql_modules mINNER JOIN sys.objects oON o.object_id = m.object_idWHEREm.definition like ''%SOME_TEXT%'''--edit this text
SELECT * FROM ##nothing norder by OBJECTname
SELECT DISTINCT OBJECT_NAME([id]),[text]
FROM syscomments
WHERE [id] IN (SELECT [id] FROM sysobjects WHERE xtype IN
('TF','FN','V','P') AND status >= 0) AND
([text] LIKE '%text to be search%' )
ALTER PROCEDURE [dbo].[usp_find_objects](@search VARCHAR(255),@target VARCHAR(255) = NULL,@db VARCHAR(35) = NULL)AS
SET NOCOUNT ON;
DECLARE @TSQL NVARCHAR(MAX), @USEDB NVARCHAR(50)
IF @db <> '' SET @USEDB = 'USE ' + @dbELSE SET @USEDB = ''
IF @target IS NULL SET @target = ''
SET @TSQL = @USEDB + '
DECLARE @search VARCHAR(128)DECLARE @target VARCHAR(128)
SET @search = ''%' + @search + '%''SET @target = ''' + @target + '''
IF @target LIKE ''%Procedure%'' BEGINSELECT o.name As ''Stored Procedures''FROM SYSOBJECTS oINNER JOIN SYSCOMMENTS c ON o.id = c.idWHERE c.text LIKE @searchAND o.xtype IN (''P'',''FN'')GROUP BY o.nameORDER BY o.nameEND
ELSE IF @target LIKE ''%View%'' BEGINSELECT o.name As ''Views''FROM SYSOBJECTS oINNER JOIN SYSCOMMENTS c ON o.id = c.idWHERE c.text LIKE @searchAND o.xtype = ''V''GROUP BY o.nameORDER BY o.nameEND
/* Table - search table name only, need to add column name */ELSE IF @target LIKE ''%Table%'' BEGINSELECT t.name AS ''TableName''FROM sys.columns cJOIN sys.tables t ON c.object_id = t.object_idWHERE c.name LIKE @searchORDER BY TableNameEND
ELSE IF @target LIKE ''%Job%'' BEGINSELECT j.job_id,s.srvname,j.name,js.step_id,js.command,j.enabledFROM [msdb].dbo.sysjobs jJOIN [msdb].dbo.sysjobsteps jsON js.job_id = j.job_idJOIN master.dbo.sysservers sON s.srvid = j.originating_server_idWHERE js.command LIKE @searchEND
ELSE BEGINSELECT o.name As ''Stored Procedures''FROM SYSOBJECTS oINNER JOIN SYSCOMMENTS c ON o.id = c.idWHERE c.text LIKE @searchAND o.xtype IN (''P'',''FN'')GROUP BY o.nameORDER BY o.name
SELECT o.name As ''Views''FROM SYSOBJECTS oINNER JOIN SYSCOMMENTS c ON o.id = c.idWHERE c.text LIKE @searchAND o.xtype = ''V''GROUP BY o.nameORDER BY o.name
SELECT t.name AS ''Tables''FROM sys.columns cJOIN sys.tables t ON c.object_id = t.object_idWHERE c.name LIKE @searchORDER BY Tables
SELECT j.name AS ''Jobs''FROM [msdb].dbo.sysjobs jJOIN [msdb].dbo.sysjobsteps jsON js.job_id = j.job_idJOIN master.dbo.sysservers sON s.srvid = j.originating_server_idWHERE js.command LIKE @searchEND'
EXECUTE sp_executesql @TSQL
CREATE PROCEDURE [dbo].[Searchinall] (@strFind AS VARCHAR(MAX))ASBEGINSET NOCOUNT ON;--TO FIND STRING IN ALL PROCEDURESBEGINSELECT s.name SP_Schema_Name, OBJECT_NAME(p.OBJECT_ID) SP_Name,OBJECT_DEFINITION(p.OBJECT_ID) SP_DefinitionFROM sys.procedures pINNER JOIN sys.schemas s on p.schema_id = s.schema_idWHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@strFind+'%'END
--TO FIND STRING IN ALL VIEWSBEGINSELECT s.name SP_Schema_Name, OBJECT_NAME(OBJECT_ID) View_Name,OBJECT_DEFINITION(OBJECT_ID) View_DefinitionFROM sys.views vINNER JOIN sys.schemas s on v.schema_id = s.schema_idWHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%'+@strFind+'%'END
--TO FIND STRING IN ALL FUNCTIONBEGINSELECT ROUTINE_SCHEMA, ROUTINE_NAME Function_Name,ROUTINE_DEFINITION Function_definitionFROM INFORMATION_SCHEMA.ROUTINESWHERE ROUTINE_DEFINITION LIKE '%'+@strFind+'%'AND ROUTINE_TYPE = 'FUNCTION'ORDER BYROUTINE_NAMEEND
--TO FIND STRING IN ALL TABLES OF DATABASE.BEGIN
SELECT s.name SP_Schema_Name, t.name AS Table_Name,c.name AS COLUMN_NAMEFROM sys.tables AS tINNER JOIN sys.columns cON t.OBJECT_ID = c.OBJECT_IDINNER JOIN sys.schemas s on t.schema_id = s.schema_idWHERE c.name LIKE '%'+@strFind+'%'ORDER BYTable_NameENDEND