forked from ktaranov/sqlserver-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dbo.usp_SearchOnAllDB.sql
189 lines (169 loc) · 5.9 KB
/
dbo.usp_SearchOnAllDB.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
178
179
180
181
182
183
184
185
186
187
188
189
IF Object_id('dbo.usp_SearchOnAllDB', 'P') IS NULL
EXECUTE ('CREATE PROCEDURE dbo.usp_SearchOnAllDB AS SELECT 1');
GO
ALTER PROCEDURE dbo.usp_SearchOnAllDB(@phrase VARCHAR(8000),
@OutFullRecords BIT = 0)
AS
/*
To apply so:
exec sp_SearchOnAllDB 'Arme%'
exec sp_SearchOnAllDB '%soft%'
exec sp_SearchOnAllDB '_5234_57%', 1
exec sp_SearchOnAllDB M_cro_oft
*/
DECLARE @sql NVARCHAR(max);
DECLARE @schema SYSNAME;
DECLARE @tbl SYSNAME;
DECLARE @col SYSNAME;
DECLARE @id_present BIT
DECLARE @is_char_phrase BIT
DECLARE @min_len INT
DECLARE @loop_idx INT
DECLARE @loop_chr CHAR(1)
SET nocount ON
IF Isnull(@phrase, '') = ''
BEGIN
RAISERROR('Phrase is absent',16,-1)
RETURN
END
-- Handle Quotes passed in the search string
SET @phrase = Replace(@phrase, '''', '''''')
SELECT @loop_idx = 1,
@is_char_phrase = 0,
@min_len = 0
WHILE @loop_idx <= Len(@phrase)
BEGIN
SET @loop_chr = Substring(@phrase, @loop_idx, 1)
IF @loop_chr NOT IN ( '%', '_' )
SET @min_len = @min_len + 1
IF @is_char_phrase = 0
AND @loop_chr NOT IN ( '%', '_', '0', '1',
'2', '3', '4', '5',
'6', '7', '8', '9', '.' )
SET @is_char_phrase = 1
SET @loop_idx = @loop_idx + 1
END
CREATE TABLE #tbl_res
(
SchemaName SYSNAME NOT NULL,
TableName SYSNAME NOT NULL,
ColumnName SYSNAME NOT NULL,
Id INT NULL,
ColumnValue NVARCHAR(max) NOT NULL
);
CREATE TABLE #tbl_res2
(
SchemaName SYSNAME NOT NULL,
TableName SYSNAME NOT NULL,
ColumnName SYSNAME NOT NULL,
Id INT NULL,
ColumnValue NVARCHAR(max) NOT NULL
);
DECLARE crr CURSOR local fast_forward FOR
SELECT s.NAME AS SchemaName,
t.NAME AS TableName,
c.NAME AS ColumnName,
1 AS id_present
FROM sys.objects t
INNER JOIN sys.syscolumns c
ON c.id = t.object_id
INNER JOIN sys.schemas AS s
ON t.schema_id = s.schema_id
WHERE t.type = 'U'
AND c.status&0x80 = 0 -- Not IDENTITY
AND EXISTS (SELECT *
FROM syscolumns c2
WHERE t.object_id = c2.id
AND c2.status&0x80 = 0x80
AND c2.xtype IN ( 48, 52, 56 ))
AND ( ( @is_char_phrase = 1
AND c.xtype IN ( 175, 239, 99, 231,
35, 167 )
AND c.length >= @min_len ) -- char only
OR ( @is_char_phrase = 0
AND c.xtype NOT IN ( 34, 165, 173, 189,
61, 58, 36 ) ) )
-- char and numeric
UNION
SELECT s.name,
t.name,
c.name,
0
FROM sys.objects t
INNER JOIN sys.syscolumns c
ON c.id = t.object_id
INNER JOIN sys.schemas AS s
ON t.schema_id = s.schema_id
WHERE t.type = 'U'
AND NOT EXISTS (SELECT *
FROM syscolumns c2
WHERE t.object_id = c2.id
AND c2.status&0x80 = 0x80
AND c2.xtype IN ( 48, 52, 56 ))
AND ( ( @is_char_phrase = 1
AND c.xtype IN ( 175, 239, 99, 231,
35, 167 )
AND c.length >= @min_len ) -- char only
OR ( @is_char_phrase = 0
AND c.xtype NOT IN ( 34, 165, 173, 189,
61, 58, 36 ) ) )
-- char and numeric
ORDER BY 1,
2;
OPEN crr
FETCH crr INTO @schema, @tbl, @col, @id_present
WHILE @@FETCH_STATUS = 0
BEGIN
IF @OutFullRecords = 0
BEGIN
SET @sql =
'insert into #tbl_res (SchemaName, TableName, ColumnName, Id, ColumnValue) '
+ 'select ''[' + @schema + ']'', ''[' + @tbl
+ ']'', ''[' + @col + ']'', '
IF @id_present = 1
SET @sql = @sql + 'IDENTITYCOL, '
ELSE
SET @sql = @sql + 'NULL, '
SET @sql = @sql + 'convert(NVARCHAR(MAX), [' + @col + ']) '
+ 'from [' + @schema + '].[' + @tbl + '] (nolock) '
+ 'where convert(varchar(8000), [' + @col
+ ']) like ''' + @phrase + ''' '
END
IF @OutFullRecords = 1
BEGIN
SET @sql = 'if exists (select * from [' + @tbl
+ '] (nolock) '
+ 'where convert(varchar(8000), [' + @col
+ ']) like ''' + @phrase + ''') ' + 'select ''['
+ @tbl + ']'' TableName, ''[' + @col
+ ']'' ColumnName, * ' + 'from [' + @tbl
+ '] (nolock) where convert(varchar(8000), ['
+ @col + ']) like ''' + @phrase + ''';'
END
EXEC(@sql);
FETCH crr INTO @schema, @tbl, @col, @id_present;
END
CLOSE crr;
DEALLOCATE crr;
IF @OutFullRecords = 0
BEGIN
INSERT #tbl_res2
SELECT SchemaName,
TableName,
ColumnName,
Id,
CONVERT(VARCHAR(255), ColumnValue) ColumnValue
FROM #tbl_res;
END
SELECT SchemaName,
TableName,
ColumnName,
ColumnValue
FROM #tbl_res2
GROUP BY SchemaName,
TableName,
ColumnName,
ColumnValue
ORDER BY SchemaName,
TableName;
GO