forked from BrentOzarULTD/SQL-Server-First-Responder-Kit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sp_foreachdb.sql
189 lines (174 loc) · 7.3 KB
/
sp_foreachdb.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
USE [master];
GO
IF OBJECT_ID('dbo.sp_foreachdb') IS NULL
EXEC ('CREATE PROCEDURE dbo.sp_foreachdb AS RETURN 0');
GO
ALTER PROCEDURE dbo.sp_foreachdb
@command NVARCHAR(MAX) ,
@replace_character NCHAR(1) = N'?' ,
@print_dbname BIT = 0 ,
@print_command_only BIT = 0 ,
@suppress_quotename BIT = 0 ,
@system_only BIT = NULL ,
@user_only BIT = NULL ,
@name_pattern NVARCHAR(300) = N'%' ,
@database_list NVARCHAR(MAX) = NULL ,
@exclude_list NVARCHAR(MAX) = NULL ,
@recovery_model_desc NVARCHAR(120) = NULL ,
@compatibility_level TINYINT = NULL ,
@state_desc NVARCHAR(120) = N'ONLINE' ,
@is_read_only BIT = 0 ,
@is_auto_close_on BIT = NULL ,
@is_auto_shrink_on BIT = NULL ,
@is_broker_enabled BIT = NULL
AS
BEGIN
SET NOCOUNT ON;
DECLARE @sql NVARCHAR(MAX) ,
@dblist NVARCHAR(MAX) ,
@exlist NVARCHAR(MAX) ,
@db NVARCHAR(300) ,
@i INT;
IF @database_list > N''
BEGIN
;
WITH n ( n )
AS ( SELECT ROW_NUMBER() OVER ( ORDER BY s1.name )
- 1
FROM sys.objects AS s1
CROSS JOIN sys.objects AS s2
)
SELECT @dblist = REPLACE(REPLACE(REPLACE(x, '</x><x>',
','), '</x>', ''),
'<x>', '')
FROM ( SELECT DISTINCT
x = 'N'''
+ LTRIM(RTRIM(SUBSTRING(@database_list,
n,
CHARINDEX(',',
@database_list
+ ',', n) - n)))
+ ''''
FROM n
WHERE n <= LEN(@database_list)
AND SUBSTRING(',' + @database_list, n,
1) = ','
FOR
XML PATH('')
) AS y ( x );
END
-- Added for @exclude_list
IF @exclude_list > N''
BEGIN
;
WITH n ( n )
AS ( SELECT ROW_NUMBER() OVER ( ORDER BY s1.name )
- 1
FROM sys.objects AS s1
CROSS JOIN sys.objects AS s2
)
SELECT @exlist = REPLACE(REPLACE(REPLACE(x, '</x><x>',
','), '</x>', ''),
'<x>', '')
FROM ( SELECT DISTINCT
x = 'N'''
+ LTRIM(RTRIM(SUBSTRING(@exclude_list,
n,
CHARINDEX(',',
@exclude_list
+ ',', n) - n)))
+ ''''
FROM n
WHERE n <= LEN(@exclude_list)
AND SUBSTRING(',' + @exclude_list, n,
1) = ','
FOR
XML PATH('')
) AS y ( x );
END
CREATE TABLE #x ( db NVARCHAR(300) );
SET @sql = N'SELECT name FROM sys.databases WHERE 1=1'
+ CASE WHEN @system_only = 1 THEN ' AND database_id IN (1,2,3,4)'
ELSE ''
END
+ CASE WHEN @user_only = 1
THEN ' AND database_id NOT IN (1,2,3,4)'
ELSE ''
END
-- To exclude databases from changes
+ CASE WHEN @exlist IS NOT NULL
THEN ' AND name NOT IN (' + @exlist + ')'
ELSE ''
END + CASE WHEN @name_pattern <> N'%'
THEN ' AND name LIKE N''%' + REPLACE(@name_pattern,
'''', '''''')
+ '%'''
ELSE ''
END + CASE WHEN @dblist IS NOT NULL
THEN ' AND name IN (' + @dblist + ')'
ELSE ''
END
+ CASE WHEN @recovery_model_desc IS NOT NULL
THEN ' AND recovery_model_desc = N'''
+ @recovery_model_desc + ''''
ELSE ''
END
+ CASE WHEN @compatibility_level IS NOT NULL
THEN ' AND compatibility_level = '
+ RTRIM(@compatibility_level)
ELSE ''
END
+ CASE WHEN @state_desc IS NOT NULL
THEN ' AND state_desc = N''' + @state_desc + ''''
ELSE ''
END
+ CASE WHEN @is_read_only IS NOT NULL
THEN ' AND is_read_only = ' + RTRIM(@is_read_only)
ELSE ''
END
+ CASE WHEN @is_auto_close_on IS NOT NULL
THEN ' AND is_auto_close_on = ' + RTRIM(@is_auto_close_on)
ELSE ''
END
+ CASE WHEN @is_auto_shrink_on IS NOT NULL
THEN ' AND is_auto_shrink_on = ' + RTRIM(@is_auto_shrink_on)
ELSE ''
END
+ CASE WHEN @is_broker_enabled IS NOT NULL
THEN ' AND is_broker_enabled = ' + RTRIM(@is_broker_enabled)
ELSE ''
END;
INSERT #x
EXEC sp_executesql @sql;
DECLARE c CURSOR LOCAL FORWARD_ONLY STATIC READ_ONLY
FOR
SELECT CASE WHEN @suppress_quotename = 1 THEN db
ELSE QUOTENAME(db)
END
FROM #x
ORDER BY db;
OPEN c;
FETCH NEXT FROM c INTO @db;
WHILE @@FETCH_STATUS = 0
BEGIN
SET @sql = REPLACE(@command, @replace_character, @db);
IF @print_command_only = 1
BEGIN
PRINT '/* For ' + @db + ': */' + CHAR(13) + CHAR(10)
+ CHAR(13) + CHAR(10) + @sql + CHAR(13) + CHAR(10)
+ CHAR(13) + CHAR(10);
END
ELSE
BEGIN
IF @print_dbname = 1
BEGIN
PRINT '/* ' + @db + ' */';
END
EXEC sp_executesql @sql;
END
FETCH NEXT FROM c INTO @db;
END
CLOSE c;
DEALLOCATE c;
END
GO