-
Notifications
You must be signed in to change notification settings - Fork 23
/
dbm_Find.sql
177 lines (145 loc) · 11 KB
/
dbm_Find.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
USE [dbamaint]
GO
/****** Object: StoredProcedure [dbo].[dbm_Find] Script Date: 02/02/2012 15:02:28 ******/
IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[dbm_Find]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[dbm_Find]
GO
USE [dbamaint]
GO
/****** Object: StoredProcedure [dbo].[dbm_Find] Script Date: 02/02/2012 15:02:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[dbm_Find]
@SearchText VARCHAR(8000)
, @DBName SYSNAME = Null
, @PreviewTextSize INT = 100
, @SearchDBsFlag CHAR(1) = 'Y'
, @SearchJobsFlag CHAR(1) = 'Y'
, @SearchSSISFlag CHAR(1) = 'Y'
AS
/*
* Created: 12/19/06, Michael F. Berry (SQL Server Magazine contributor)
*
* Modified: 01/25/07, Michael F. Berry, Make it output to one main recordset for clarity
* Modified: 09/04/08, Bill Lescher and Chase Jones, Updated for SQL2005 and added Jobs & SSIS Packages
* Modified: 07/22/09, Bill L, Returning the PreviewText
*
* Description: Find any string within the T-SQL code on this SQL Server instance, specifically
* Database objects and/or SQL Agent Jobs and/or SSIS Packages
*
* Test: sp_Find 'track'
* sp_Find 'AS400'
* sp_Find 'track', 'Common', 50
* sp_Find 'track', 'Common', 50, 'Y', 'N', 'N' --DB Only
* sp_Find 'track', 'Common', 50, 'N', 'N', 'Y' --SSIS Only
*/
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SET NOCOUNT ON;
IF OBJECT_ID(N'tempdb..#FoundObject', 'U') IS NOT NULL
DROP TABLE #FoundObject;
CREATE TABLE #FoundObject (
DatabaseName SYSNAME
, ObjectName SYSNAME
, ObjectTypeDesc NVARCHAR(60)
, PreviewText VARCHAR(MAX))--To show a little bit of the code
DECLARE @SQL as nvarchar(max);
SELECT 'Searching For: ''' + @SearchText + '''' As CurrentSearch;
/**************************
* Database Search
***************************/
If @SearchDBsFlag = 'Y'
BEGIN
IF @DBName Is Null --Loop through all normal user databases
BEGIN
DECLARE ObjCursor CURSOR LOCAL FAST_FORWARD FOR
SELECT [Name]
FROM Master.sys.Databases
WHERE [Name] NOT IN ('AdventureWorks', 'AdventureWorksDW', 'Distribution', 'Master', 'MSDB', 'Model', 'TempDB');
OPEN ObjCursor;
FETCH NEXT FROM ObjCursor INTO @DBName;
WHILE @@Fetch_Status = 0
BEGIN
SELECT @SQL = '
Use [' + @DBName + ']
INSERT INTO #FoundObject (
DatabaseName
, ObjectName
, ObjectTypeDesc
, PreviewText)
SELECT DISTINCT
''' + @DBName + '''
, sch.[Name] + ''.'' + obj.[Name] AS ObjectName
, obj.Type_Desc
, REPLACE(REPLACE(SUBSTRING(mod.Definition, CHARINDEX(''' + @SearchText + ''', mod.Definition) - ' + CAST(@PreviewTextSize / 2 AS VARCHAR) + ', ' +
CAST(@PreviewTextSize AS VARCHAR) + '), char(13) + char(10), ''''), ''' + @SearchText + ''', ''***' + @SearchText + '***'')
FROM sys.objects obj
INNER JOIN master.sys.SQL_Modules mod ON obj.Object_Id = mod.Object_Id
INNER JOIN master.sys.Schemas sch ON obj.Schema_Id = sch.Schema_Id
WHERE mod.Definition Like ''%' + @SearchText + '%''
ORDER BY ObjectName';
EXEC dbo.sp_executesql @SQL;
FETCH NEXT FROM ObjCursor INTO @DBName;
END;
CLOSE ObjCursor;
DEALLOCATE ObjCursor;
END
ELSE --Only look through given database
BEGIN
SELECT @SQL = '
USE [' + @DBName + ']
INSERT INTO #FoundObject (
DatabaseName
, ObjectName
, ObjectTypeDesc
, PreviewText)
SELECT DISTINCT
''' + @DBName + '''
, sch.[Name] + ''.'' + obj.[Name] AS ObjectName
, obj.Type_Desc
, REPLACE(REPLACE(SUBSTRING(mod.Definition, CHARINDEX(''' + @SearchText + ''', mod.Definition) - ' + CAST(@PreviewTextSize / 2 AS VARCHAR) + ', ' +
CAST(@PreviewTextSize AS VARCHAR) + '), CHAR(13) + CHAR(10), ''''), ''' + @SearchText + ''', ''***' + @SearchText + '***'')
FROM sys.objects obj
INNER JOIN sys.SQL_Modules mod On obj.Object_Id = mod.Object_Id
INNER JOIN sys.Schemas sch On obj.Schema_Id = sch.Schema_Id
WHERE mod.Definition Like ''%' + @SearchText + '%''
ORDER BY ObjectName';
EXEC dbo.sp_ExecuteSQL @SQL;
END;
SELECT 'Database Objects' AS SearchType;
SELECT
DatabaseName
, ObjectName
, ObjectTypeDesc AS ObjectType
, PreviewText
FROM #FoundObject
ORDER BY DatabaseName, ObjectName;
END
/**************************
* Job Search
***************************/
IF @SearchJobsFlag = 'Y'
BEGIN
SELECT 'Job Steps' AS SearchType;
SELECT j.[Name] AS [Job Name]
, s.Step_Id AS [Step #]
, REPLACE(REPLACE(SUBSTRING(s.Command, CHARINDEX(@SearchText, s.Command) - @PreviewTextSize / 2, @PreviewTextSize), CHAR(13) + CHAR(10), ''), @SearchText, '***' + @SearchText + '***') AS Command
FROM MSDB.dbo.sysJobs j
INNER JOIN MSDB.dbo.sysJobSteps s On j.Job_Id = s.Job_Id
WHERE s.Command LIKE '%' + @SearchText + '%';
END
/**************************
* SSIS Search
***************************/
IF @SearchSSISFlag = 'Y'
BEGIN
SELECT 'SSIS Packages' AS SearchType;
SELECT [Name] AS [SSIS Name]
, REPLACE(REPLACE(SUBSTRING(CAST(CAST(PackageData AS VARBINARY(Max)) AS VARCHAR(Max)), CHARINDEX(@SearchText, CAST(CAST(PackageData AS VARBINARY(MAX)) AS VARCHAR(MAX))) -
@PreviewTextSize / 2, @PreviewTextSize), char(13) + char(10), ''), @SearchText, '***' + @SearchText + '***') AS [SSIS XML]
FROM MSDB.dbo.sysSSISPackages
WHERE CAST(CAST(PackageData AS VARBINARY(MAX)) AS VARCHAR(MAX)) LIKE '%' + @SearchText + '%';
END
DROP TABLE #FoundObject
GO