Skip to content

Commit 5300d22

Browse files
BrentOzarHarsha Vasa
authored and
Harsha Vasa
committed
2019-08 Release (BrentOzarULTD#2094)
* BrentOzarULTD#2005 sp_Blitz version numbers (BrentOzarULTD#2006) Working on BrentOzarULTD#2005. * BrentOzarULTD#2005 easier merges for versions Putting Version, VersionDate variables on a single line. Closes BrentOzarULTD#2005. * BrentOzarULTD#2010 sp_Blitz paused online index operations (BrentOzarULTD#2011) Add warning for rows in sys.index_resumable_operations. Closes BrentOzarULTD#2010. * Fix collation error. (BrentOzarULTD#2014) * BrentOzarULTD#2015 sp_BlitzCache sorting in Azure SQL DB (BrentOzarULTD#2016) Instead of checking version numbers, look at sys.all_columns for the grant and spill columns. Closes BrentOzarULTD#2015. * BrentOzarULTD#2024 sp_BlitzQueryStore s.is_cursor error (BrentOzarULTD#2025) Changed alias on a temp table to match other nearby aliases. Closes BrentOzarULTD#2024. * First responder consistency check (BrentOzarULTD#1997) * sp_Blitz update compile and runs on MSSQL 2014 * Update sp_Blitz.sql Added @debug handling - bug found for missing mandatory component * Update sp_Blitz.sql * Update sp_Blitz.sql CheckId familly: 226 - FRK consistency Messages: 2260 - First Responder kit consistency check has been unexpectedly modified (check names) 2261 - First Responder kit consistency check has been unexpectedly modified (dynamic query failure) 2262 - First Responder kit mandatory component called %s is missing 2263 - First Responder kit consistency check has been unexpectedly modified (checks ordering) 2264 - Component %s is not at the minimum version required to run this procedure 2265 - First Responder kit consistency check (Failed dynamic SP call to %s) 2266 - First Responder kit consistency: outdated component (%s) removed "everything ok message" * Resolves almost all Brent's comments except CHAR(92) change removed 2264 - Component %s is not at the minimum version required to run this procedure changed detailled finding message for previous 2264 message Checked compilation on SQL Server 2014 * BrentOzarULTD#1994 sp_Blitz version checking (BrentOzarULTD#2028) Changed wording from "consistency check" to "version check," updated check IDs, removed mandatory scripts. Closes BrentOzarULTD#1994. * BrentOzarULTD#1944 sp_BlitzIndex ignore databases (BrentOzarULTD#2029) Adds new IgnoreDatabases parameter, and skips databases with >100 partitions. Closes BrentOzarULTD#1944. * BrentOzarULTD#2009 sp_BlitzFirst call BC differently (BrentOzarULTD#2030) Use SkipAnalysis = 1, SortOrder = all when calling sp_BlitzCache. Closes BrentOzarULTD#2009. * BrentOzarULTD#2026 sp_BlitzCache show multiple plan count (BrentOzarULTD#2031) Closes BrentOzarULTD#2026. * BrentOzarULTD#2017 sp_Blitz Evaluation Edition expiration check (BrentOzarULTD#2032) Closes BrentOzarULTD#2017. * BrentOzarULTD#2018 sp_BlitzCache arith overflow (BrentOzarULTD#2033) Changing MONEY on totals to BIGINT. Closes BrentOzarULTD#2018. * BrentOzarULTD#2019 sp_BlitzCache air_quote_actual plans (BrentOzarULTD#2034) Closes BrentOzarULTD#2019. * Add END in the right spot for IF @Help = 1 (BrentOzarULTD#2035) The END for IF @Help = 1 was in the wrong place, making the procedure a no-op *unless* you also specify @Help = 1. * BrentOzarULTD#2037 sp_BlitzLock support for AWS RDS (BrentOzarULTD#2038) Skips update stats with options that RDS doesn't support. Closes BrentOzarULTD#2037. * 2019_04_release_prep Bumping version numbers, building installation scripts. * Moved SQL header build outside of If block (BrentOzarULTD#2040) * BrentOzarULTD#2042 implicit transaction troubleshooting (BrentOzarULTD#2043) Clarify warnings in sp_Blitz, sp_BlitzFirst. Closes BrentOzarULTD#2042. * edit hyperlink (BrentOzarULTD#2056) * add @OutputType = 'XML' (BrentOzarULTD#2048) * Update sp_blitzcache to avoid arithmetic overflow errors (BrentOzarULTD#2046) Updated sp_blitzcache to avoid arithmetic overflow errors - tabel definitions had changed to BIGINT but the actual logic in this section still casted values as money which was breaking the ceiling for this on our setup. I've pretty much blanket chantged everything except averages to be bigint and have been running for a few days with no failrues since so looks ok and worth someone who knows what they're doing casting an eye over it! * Update sp_ineachdb.sql (BrentOzarULTD#2061) Added 2 Variables @SQLVersion: This was implemented so that large organizations with SQL Server 2008 and R2 can use this sproc. Currently without this change SQL Server versions 2008 and 2008R2 will error with the following. Msg 208, Level 16, State 1, Procedure sp_ineachdb, Line 176 Invalid object name 'sys.dm_hadr_database_replica_states'. @ServerName: This was added to account for VMware SnapShots at times @@ServerName will either come up NULL or with old server name. CONVERT(sysname, SERVERPROPERTY('ServerName')) is a better option and reduces minor nuisances. Feel free to blend in the code if you feel this brings value to the project. * ignore readable secondaries in sp_blitzcache (BrentOzarULTD#2049) * ignore readable secondaries in sp_blitzcache this is a fix for issue BrentOzarULTD#2027 * Checking for is_primary_replica column In sys.dm_hadr_database_replica_states. * 2019_07 Release Bumping version numbers and updating install scripts. * Set DBOwner after restoring database (BrentOzarULTD#2082) * Default @RestoreDiff = NULL If @RestoreDiff IS NULL and @BackupPathDiff != null: treat RestoreDiff=1 * Set database owner after restore * Revert restorediff change from dev branch * Exclude Readable secondary DBs (BrentOzarULTD#2073) Temp table added (#ReadableDBs) this table is populated with all read intent database id's which is used to filter out these database when querying against dm_exec_requests or sessions with a cross apply against a plan cache function. * BrentOzarULTD#2067 Updating directory "/" fixing to work with URLs (BrentOzarULTD#2071) * Updating directory "/" fixing to work with URLs Also correcting incorrect logging message for fixing @MoveLogDrive slashes * Fixing variable typo Data should be log * BrentOzarULTD#2076 sp_BlitzIndex columnstore to table (BrentOzarULTD#2085) When saving columnstore definitions to table, trim them to 4000 characters to avoid an overflow. Closes BrentOzarULTD#2076. * @2060 sp_BlitzFirst 2TB RAM (BrentOzarULTD#2086) Casting RAM as a BIGINT instead of INT. Closes BrentOzarULTD#2060. * BrentOzarULTD#2053 sp_BlitzIndex ignoring databases (BrentOzarULTD#2087) Strip out CR/LF from @IgnoreDatabases, trim spaces afterwards. Closes BrentOzarULTD#2053. * BrentOzarULTD#2022 sp_BlitzCache disabling air_quote_actuals (BrentOzarULTD#2088) This SQL 2019 & Azure SQL DB feature isn't ready yet, so disabling til Microsoft is ready. Closes BrentOzarULTD#2022. * BrentOzarULTD#2044 sp_Blitz new power mode (BrentOzarULTD#2089) Adds Windows 10's new Ultimate Performance Power Mode, whatever that is. Closes BrentOzarULTD#2044. * BrentOzarULTD#2062 sp_BlitzFirst false alarm on index reorgs (BrentOzarULTD#2091) Filtering for sql_text not like %alter index%'. Closes BrentOzarULTD#2062 with duct tape. * BrentOzarULTD#2052 sp_BlitzCache prioritization (BrentOzarULTD#2092) Fixes bug in AirQuoteActual plans, plus if plan cache is 75% new as of today, raise priority 1 warning. Closes BrentOzarULTD#2052. * BrentOzarULTD#2070 sp_BlitzCache duplicated index count (BrentOzarULTD#2093) Filters index recommendations by spid. Closes BrentOzarULTD#2070. * sp_DatabaseRestore case bug Inconsistent case sensitivity. * 2019-09 release prep Bumping version numbers and dates.
1 parent 05fc177 commit 5300d22

14 files changed

+302
-0
lines changed

sp_AllNightLog.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ SELECT @Version = '3.9', @VersionDate = '20191024';
3434
SELECT @Version = '3.7', @VersionDate = '20190826';
3535
SELECT @Version = '3.8', @VersionDate = '20190922';
3636
SELECT @Version = '3.91', @VersionDate = '20191202';
37+
SELECT @Version = '3.7', @VersionDate = '20190826';
3738

3839
IF(@VersionCheckMode = 1)
3940
BEGIN

sp_AllNightLog_Setup.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ SELECT @Version = '3.9', @VersionDate = '20191024';
4040
SELECT @Version = '3.7', @VersionDate = '20190826';
4141
SELECT @Version = '3.8', @VersionDate = '20190922';
4242
SELECT @Version = '3.91', @VersionDate = '20191202';
43+
SELECT @Version = '3.7', @VersionDate = '20190826';
4344

4445
IF(@VersionCheckMode = 1)
4546
BEGIN

sp_Blitz.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ AS
4141
SELECT @Version = '7.7', @VersionDate = '20190826';
4242
SELECT @Version = '7.8', @VersionDate = '20190922';
4343
SELECT @Version = '7.91', @VersionDate = '20191202';
44+
SELECT @Version = '7.7', @VersionDate = '20190826';
4445
SET @OutputType = UPPER(@OutputType);
4546

4647
IF(@VersionCheckMode = 1)

sp_BlitzBackups.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ AS
2727
SELECT @Version = '3.7', @VersionDate = '20190826';
2828
SELECT @Version = '3.8', @VersionDate = '20190922';
2929
SELECT @Version = '3.91', @VersionDate = '20191202';
30+
SELECT @Version = '3.7', @VersionDate = '20190826';
3031

3132
IF(@VersionCheckMode = 1)
3233
BEGIN

sp_BlitzCache.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ SELECT @Version = '7.9', @VersionDate = '20191024';
276276
SELECT @Version = '7.7', @VersionDate = '20190826';
277277
SELECT @Version = '7.8', @VersionDate = '20190922';
278278
SELECT @Version = '7.91', @VersionDate = '20191202';
279+
SELECT @Version = '7.7', @VersionDate = '20190826';
279280

280281

281282
IF(@VersionCheckMode = 1)
@@ -1130,9 +1131,12 @@ IF EXISTS(SELECT * FROM sys.all_columns WHERE OBJECT_ID = OBJECT_ID('sys.dm_exec
11301131
ELSE
11311132
SET @VersionShowsSpills = 0;
11321133

1134+
/* This new 2019 & Azure SQL DB feature isn't working consistently, so turning it back off til Microsoft gets it ready.
1135+
See this Github issue for more details: https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/issues/2022
11331136
IF EXISTS(SELECT * FROM sys.all_columns WHERE OBJECT_ID = OBJECT_ID('sys.dm_exec_query_plan_stats') AND name = 'query_plan')
11341137
SET @VersionShowsAirQuoteActualPlans = 1;
11351138
ELSE
1139+
*/
11361140
SET @VersionShowsAirQuoteActualPlans = 0;
11371141

11381142
IF @Reanalyze = 1 AND OBJECT_ID('tempdb..##BlitzCacheResults') IS NULL

sp_BlitzFirst.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ SELECT @Version = '7.9', @VersionDate = '20191024';
4848
SELECT @Version = '7.7', @VersionDate = '20190826';
4949
SELECT @Version = '7.8', @VersionDate = '20190922';
5050
SELECT @Version = '7.91', @VersionDate = '20191202';
51+
SELECT @Version = '7.7', @VersionDate = '20190826';
5152

5253
IF(@VersionCheckMode = 1)
5354
BEGIN

sp_BlitzInMemoryOLTP.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ SELECT @ScriptVersion = '1.8', @VersionDate = '20191024';
8686
SELECT @ScriptVersion = '1.9', @VersionDate = '20190826';
8787
SELECT @ScriptVersion = '1.8', @VersionDate = '20190922';
8888
SELECT @ScriptVersion = '1.8', @VersionDate = '20191202';
89+
SELECT @ScriptVersion = '1.9', @VersionDate = '20190826';
8990

9091
IF(@VersionCheckMode = 1)
9192
BEGIN

sp_BlitzIndex.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ SELECT @Version = '7.9', @VersionDate = '20191024';
4545
SELECT @Version = '7.7', @VersionDate = '20190826';
4646
SELECT @Version = '7.8', @VersionDate = '20190922';
4747
SELECT @Version = '7.91', @VersionDate = '20191202';
48+
SELECT @Version = '7.7', @VersionDate = '20190826';
4849
SET @OutputType = UPPER(@OutputType);
4950

5051
IF(@VersionCheckMode = 1)

sp_BlitzLock.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ SELECT @Version = '2.9', @VersionDate = '20191024';
3131
SELECT @Version = '2.7', @VersionDate = '20190826';
3232
SELECT @Version = '2.8', @VersionDate = '20190922';
3333
SELECT @Version = '2.91', @VersionDate = '20191202';
34+
SELECT @Version = '2.7', @VersionDate = '20190826';
3435

3536

3637
IF(@VersionCheckMode = 1)

sp_BlitzQueryStore.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ SELECT @Version = '3.9', @VersionDate = '20191024';
6060
SELECT @Version = '3.7', @VersionDate = '20190826';
6161
SELECT @Version = '3.8', @VersionDate = '20190922';
6262
SELECT @Version = '3.91', @VersionDate = '20191202';
63+
SELECT @Version = '3.7', @VersionDate = '20190826';
6364
IF(@VersionCheckMode = 1)
6465
BEGIN
6566
RETURN;

sp_BlitzWho.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ BEGIN
3232
SELECT @Version = '7.7', @VersionDate = '20190826';
3333
SELECT @Version = '7.8', @VersionDate = '20190922';
3434
SELECT @Version = '7.91', @VersionDate = '20191202';
35+
SELECT @Version = '7.7', @VersionDate = '20190826';
3536

3637
IF(@VersionCheckMode = 1)
3738
BEGIN

sp_DatabaseRestore.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ SELECT @Version = '7.9', @VersionDate = '20191024';
4343
SELECT @Version = '7.7', @VersionDate = '20190826';
4444
SELECT @Version = '7.8', @VersionDate = '20190922';
4545
SELECT @Version = '7.91', @VersionDate = '20191202';
46+
SELECT @Version = '7.7', @VersionDate = '20190826';
4647

4748
IF(@VersionCheckMode = 1)
4849
BEGIN

sp_foreachdb.sql

Lines changed: 286 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,286 @@
1+
IF OBJECT_ID('dbo.sp_foreachdb') IS NULL
2+
EXEC ('CREATE PROCEDURE dbo.sp_foreachdb AS RETURN 0');
3+
GO
4+
5+
ALTER PROCEDURE dbo.sp_foreachdb
6+
-- Original fields from sp_MSforeachdb...
7+
@command1 NVARCHAR(MAX) = NULL,
8+
@replacechar NCHAR(1) = N'?' ,
9+
@command2 NVARCHAR(MAX) = NULL ,
10+
@command3 NVARCHAR(MAX) = NULL ,
11+
@precommand NVARCHAR(MAX) = NULL ,
12+
@postcommand NVARCHAR(MAX) = NULL ,
13+
-- Additional fields for our sp_foreachdb!
14+
@command NVARCHAR(MAX) = NULL, --For backwards compatibility
15+
@print_dbname BIT = 0 ,
16+
@print_command_only BIT = 0 ,
17+
@suppress_quotename BIT = 0 ,
18+
@system_only BIT = NULL ,
19+
@user_only BIT = NULL ,
20+
@name_pattern NVARCHAR(300) = N'%' ,
21+
@database_list NVARCHAR(MAX) = NULL ,
22+
@exclude_list NVARCHAR(MAX) = NULL ,
23+
@recovery_model_desc NVARCHAR(120) = NULL ,
24+
@compatibility_level TINYINT = NULL ,
25+
@state_desc NVARCHAR(120) = N'ONLINE' ,
26+
@is_read_only BIT = 0 ,
27+
@is_auto_close_on BIT = NULL ,
28+
@is_auto_shrink_on BIT = NULL ,
29+
@is_broker_enabled BIT = NULL ,
30+
@Help BIT = 0,
31+
@Version VARCHAR(30) = NULL OUTPUT,
32+
@VersionDate DATETIME = NULL OUTPUT,
33+
@VersionCheckMode BIT = 0
34+
AS
35+
BEGIN
36+
SET NOCOUNT ON;
37+
SELECT @Version = '3.7', @VersionDate = '20190826';
38+
39+
IF(@VersionCheckMode = 1)
40+
BEGIN
41+
RETURN;
42+
END;
43+
44+
45+
IF @Help = 1
46+
47+
BEGIN
48+
49+
PRINT '
50+
/*
51+
sp_foreachdb from http://FirstResponderKit.org
52+
53+
This script will execute a given command against all, or user-specified,
54+
online, readable databases on an instance.
55+
56+
To learn more, visit http://FirstResponderKit.org where you can download new
57+
versions for free, watch training videos on how it works, get more info on
58+
the findings, contribute your own code, and more.
59+
60+
Known limitations of this version:
61+
- Only Microsoft-supported versions of SQL Server. Sorry, 2005 and 2000.
62+
- Tastes awful with marmite.
63+
64+
Unknown limitations of this version:
65+
- None. (If we knew them, they would be known. Duh.)
66+
67+
Changes - for the full list of improvements and fixes in this version, see:
68+
https://github.com/BrentOzarULTD/SQL-Server-First-Responder-Kit/
69+
70+
MIT License
71+
72+
Copyright (c) 2019 Brent Ozar Unlimited
73+
74+
Permission is hereby granted, free of charge, to any person obtaining a copy
75+
of this software and associated documentation files (the "Software"), to deal
76+
in the Software without restriction, including without limitation the rights
77+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
78+
copies of the Software, and to permit persons to whom the Software is
79+
furnished to do so, subject to the following conditions:
80+
81+
The above copyright notice and this permission notice shall be included in all
82+
copies or substantial portions of the Software.
83+
84+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
85+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
86+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
87+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
88+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
89+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
90+
SOFTWARE.
91+
92+
Example for basic execution of the stored procedure:
93+
94+
exec dbo.sp_foreachdb
95+
@command = ''select [name] sys.tables''
96+
,@database_list = ''Database1,Database2''
97+
,@exclude_list = ''Database5,OldDatabase'';
98+
99+
*/
100+
';
101+
RETURN -1;
102+
END
103+
104+
IF ( (@command1 IS NOT NULL AND @command IS NOT NULL)
105+
OR (@command1 IS NULL AND @command IS NULL) )
106+
BEGIN
107+
RAISERROR('You must supply either @command1 or @command, but not both.',16,1);
108+
RETURN -1;
109+
END;
110+
111+
SET @command1 = COALESCE(@command1,@command);
112+
113+
DECLARE @sql NVARCHAR(MAX) ,
114+
@dblist NVARCHAR(MAX) ,
115+
@exlist NVARCHAR(MAX) ,
116+
@db NVARCHAR(300) ,
117+
@i INT;
118+
119+
IF @database_list > N''
120+
BEGIN
121+
;
122+
WITH n ( n )
123+
AS ( SELECT ROW_NUMBER() OVER ( ORDER BY s1.name )
124+
- 1
125+
FROM sys.objects AS s1
126+
CROSS JOIN sys.objects AS s2
127+
)
128+
SELECT @dblist = REPLACE(REPLACE(REPLACE(x, '</x><x>',
129+
','), '</x>', ''),
130+
'<x>', '')
131+
FROM ( SELECT DISTINCT
132+
x = 'N'''
133+
+ LTRIM(RTRIM(SUBSTRING(@database_list,
134+
n,
135+
CHARINDEX(',',
136+
@database_list
137+
+ ',', n) - n)))
138+
+ ''''
139+
FROM n
140+
WHERE n <= LEN(@database_list)
141+
AND SUBSTRING(',' + @database_list, n,
142+
1) = ','
143+
FOR
144+
XML PATH('')
145+
) AS y ( x );
146+
END
147+
-- Added for @exclude_list
148+
IF @exclude_list > N''
149+
BEGIN
150+
;
151+
WITH n ( n )
152+
AS ( SELECT ROW_NUMBER() OVER ( ORDER BY s1.name )
153+
- 1
154+
FROM sys.objects AS s1
155+
CROSS JOIN sys.objects AS s2
156+
)
157+
SELECT @exlist = REPLACE(REPLACE(REPLACE(x, '</x><x>',
158+
','), '</x>', ''),
159+
'<x>', '')
160+
FROM ( SELECT DISTINCT
161+
x = 'N'''
162+
+ LTRIM(RTRIM(SUBSTRING(@exclude_list,
163+
n,
164+
CHARINDEX(',',
165+
@exclude_list
166+
+ ',', n) - n)))
167+
+ ''''
168+
FROM n
169+
WHERE n <= LEN(@exclude_list)
170+
AND SUBSTRING(',' + @exclude_list, n,
171+
1) = ','
172+
FOR
173+
XML PATH('')
174+
) AS y ( x );
175+
END
176+
177+
CREATE TABLE #x ( db NVARCHAR(300) );
178+
179+
SET @sql = N'SELECT name FROM sys.databases d WHERE 1=1'
180+
+ CASE WHEN @system_only = 1 THEN ' AND d.database_id IN (1,2,3,4)'
181+
ELSE ''
182+
END
183+
+ CASE WHEN @user_only = 1
184+
THEN ' AND d.database_id NOT IN (1,2,3,4)'
185+
ELSE ''
186+
END
187+
-- To exclude databases from changes
188+
+ CASE WHEN @exlist IS NOT NULL
189+
THEN ' AND d.name NOT IN (' + @exlist + ')'
190+
ELSE ''
191+
END + CASE WHEN @name_pattern <> N'%'
192+
THEN ' AND d.name LIKE N''' + REPLACE(@name_pattern,
193+
'''', '''''')
194+
+ ''''
195+
ELSE ''
196+
END + CASE WHEN @dblist IS NOT NULL
197+
THEN ' AND d.name IN (' + @dblist + ')'
198+
ELSE ''
199+
END
200+
+ CASE WHEN @recovery_model_desc IS NOT NULL
201+
THEN ' AND d.recovery_model_desc = N'''
202+
+ @recovery_model_desc + ''''
203+
ELSE ''
204+
END
205+
+ CASE WHEN @compatibility_level IS NOT NULL
206+
THEN ' AND d.compatibility_level = '
207+
+ RTRIM(@compatibility_level)
208+
ELSE ''
209+
END
210+
+ CASE WHEN @state_desc IS NOT NULL
211+
THEN ' AND d.state_desc = N''' + @state_desc + ''''
212+
ELSE ''
213+
END
214+
+ CASE WHEN @state_desc = 'ONLINE' AND SERVERPROPERTY('IsHadrEnabled') = 1
215+
THEN ' AND NOT EXISTS (SELECT 1
216+
FROM sys.dm_hadr_database_replica_states drs
217+
JOIN sys.availability_replicas ar
218+
ON ar.replica_id = drs.replica_id
219+
JOIN sys.dm_hadr_availability_group_states ags
220+
ON ags.group_id = ar.group_id
221+
WHERE drs.database_id = d.database_id
222+
AND ar.secondary_role_allow_connections = 0
223+
AND ags.primary_replica <> @@SERVERNAME)'
224+
ELSE ''
225+
END
226+
+ CASE WHEN @is_read_only IS NOT NULL
227+
THEN ' AND d.is_read_only = ' + RTRIM(@is_read_only)
228+
ELSE ''
229+
END
230+
+ CASE WHEN @is_auto_close_on IS NOT NULL
231+
THEN ' AND d.is_auto_close_on = ' + RTRIM(@is_auto_close_on)
232+
ELSE ''
233+
END
234+
+ CASE WHEN @is_auto_shrink_on IS NOT NULL
235+
THEN ' AND d.is_auto_shrink_on = ' + RTRIM(@is_auto_shrink_on)
236+
ELSE ''
237+
END
238+
+ CASE WHEN @is_broker_enabled IS NOT NULL
239+
THEN ' AND d.is_broker_enabled = ' + RTRIM(@is_broker_enabled)
240+
ELSE ''
241+
END;
242+
243+
INSERT #x
244+
EXEC sp_executesql @sql;
245+
246+
DECLARE c CURSOR LOCAL FORWARD_ONLY STATIC READ_ONLY
247+
FOR
248+
SELECT CASE WHEN @suppress_quotename = 1 THEN db
249+
ELSE QUOTENAME(db)
250+
END
251+
FROM #x
252+
ORDER BY db;
253+
254+
OPEN c;
255+
256+
FETCH NEXT FROM c INTO @db;
257+
258+
WHILE @@FETCH_STATUS = 0
259+
BEGIN
260+
SET @sql = REPLACE(@command1, @replacechar, @db);
261+
262+
IF @suppress_quotename = 0 SET @sql = REPLACE(REPLACE(@sql,'[[','['),']]',']');
263+
264+
IF @print_command_only = 1
265+
BEGIN
266+
PRINT '/* For ' + @db + ': */' + CHAR(13) + CHAR(10)
267+
+ CHAR(13) + CHAR(10) + @sql + CHAR(13) + CHAR(10)
268+
+ CHAR(13) + CHAR(10);
269+
END
270+
ELSE
271+
BEGIN
272+
IF @print_dbname = 1
273+
BEGIN
274+
PRINT '/* ' + @db + ' */';
275+
END
276+
277+
EXEC sp_executesql @sql;
278+
END
279+
280+
FETCH NEXT FROM c INTO @db;
281+
END
282+
283+
CLOSE c;
284+
DEALLOCATE c;
285+
END
286+
GO

0 commit comments

Comments
 (0)