My colleague has kindly provided me with this one recently. It performs some similar searches, as others have noted, but with a bit more added.
DECLARE @chvStringToFind varchar(256), @chrObjectType char(2),--=null, @intNbCharToExtract int --=50 --exec DBA_FindStringInDB @chvStringToFind='sp_helpdia', @chrObjectType=null, @intNbCharToExtract=50 SET @chvStringToFind = 'EnterSearchTextHere' -- Change this to search SET @chrObjectType = NULL SET @intNbCharToExtract = 50 SELECT t.Name, t.TypeDescription, t.CreationDate, t.ModificationDate, '...' + SUBSTRING ( t.ObjectDefinition, CHARINDEX(@chvStringToFind, t.ObjectDefinition) - @intNbCharToExtract, LEN(@chvStringToFind) + (@intNbCharToExtract*2) ) + '...' AS Extract FROM ( SELECT o.name AS Name, o.type_desc AS TypeDescription, o.create_date AS CreationDate, o.modify_date AS ModificationDate, OBJECT_DEFINITION(object_id) AS ObjectDefinition FROM sys.objects o WHERE ((o.type IN ('AF', 'FN', 'IF', 'P', 'TF', 'TT', 'U', 'V', 'X') AND @chrObjectType IS NULL) OR o.type = @chrObjectType) AND OBJECT_DEFINITION(o.object_id) LIKE '%' + @chvStringToFind + '%' ) AS t ORDER BY TypeDescription, Name
Vinnie Oct 21 2018-10-21 19:38
source share