Skip to content

Commit c059099

Browse files
authored
Merge pull request #231 from Imran-imtiaz48/patch-1
Refactor SQL script for clarity.
2 parents 71d70f8 + 6bbed45 commit c059099

File tree

1 file changed

+65
-95
lines changed

1 file changed

+65
-95
lines changed

Scripts/Adding_Trace_Flags_To_Startup_Parameters.sql

+65-95
Original file line numberDiff line numberDiff line change
@@ -2,152 +2,122 @@
22
Source link: https://blog.waynesheffield.com/wayne/archive/2017/09/registry-sql-server-startup-parameters/
33
Author: Wayne Sheffield
44
5-
Globally enable / disable the specified trace flags.
6-
Use DBCC TRACEON/TRACEOFF to enable disable globally trace flags, then adjust
7-
the SQL Server instance startup parameters for these trace flags.
8-
5+
Description: Globally enable or disable the specified trace flags.
6+
Use DBCC TRACEON/TRACEOFF to enable or disable trace flags globally,
7+
then adjust the SQL Server instance startup parameters for these trace flags.
8+
99
SQL Server startup parameters are stored in the registry at:
10-
HKLM\Software\Microsoft\Microsoft SQL Server\MSSQL12.SQL2014\MSSQLServer\Parameters
11-
10+
HKLM\Software\Microsoft\MSSQLSERVER\MSSQLServer\Parameters
11+
1212
To use the xp_instance_reg... XPs, use:
1313
HKLM\Software\Microsoft\MSSQLSERVER\MSSQLServer\Parameters.
14-
15-
To use:
14+
15+
Usage:
1616
1. Add the Trace Flags that you want modified to the @TraceFlags table variable.
1717
2. Set the @DebugLevel variable to 1 to see what will happen on your system first.
18-
3. When satisified what will happen, set @DebugLevel to 0 to actually execute the statements.
18+
3. When satisfied with the output, set @DebugLevel to 0 to actually execute the statements.
1919
********************************************************************************
20-
MODIFICATION LOG
20+
MODIFICATION LOG
2121
********************************************************************************
2222
2016-08-03 WGS Initial Creation.
2323
*******************************************************************************/
2424
SET NOCOUNT ON;
25+
2526
-- Declare and initialize variables.
26-
-- To use with SQL 2005, cannot set the variables in the declare statement.
27-
DECLARE @MaxValue INTEGER,
27+
DECLARE @MaxValue INT,
2828
@SQLCMD VARCHAR(MAX),
2929
@RegHive VARCHAR(50),
3030
@RegKey VARCHAR(100),
3131
@DebugLevel TINYINT;
32-
32+
33+
-- Registry hive and key for SQL Server startup parameters
3334
SET @RegHive = 'HKEY_LOCAL_MACHINE';
3435
SET @RegKey = 'Software\Microsoft\MSSQLSERVER\MSSQLServer\Parameters';
35-
SET @DebugLevel = 0; -- only makes changes if set to zero!
36-
37-
-- Add the trace flags that you want changed here.
38-
-- If enable = 1, DBCC TRACEON will be run; if enable = 0 then DBCC TRACEOFF will be run.
39-
-- If enable_on_startup = 1, then this TF will be added to start up on service restart;
40-
-- If enable_on_startup - 0, then this TF will be removed from starting up service restart
36+
-- Set debug level: 0 to execute changes, 1 to only show what will happen
37+
SET @DebugLevel = 1;
38+
39+
-- Add the trace flags that you want to modify here.
4140
DECLARE @TraceFlags TABLE (
42-
TF INTEGER,
41+
TF INT,
4342
enable BIT,
4443
enable_on_startup BIT,
4544
TF2 AS '-T' + CONVERT(VARCHAR(15), TF)
4645
);
4746
INSERT INTO @TraceFlags (TF, enable, enable_on_startup)
48-
-- To work with SQL 2005, cannot use a table value constructor.
49-
-- So, use SELECT statements with UNION ALL for each TF to modify.
5047
SELECT 1117, 1, 1 UNION ALL
5148
SELECT 1118, 1, 1 UNION ALL
5249
SELECT 1204, 0, 0 UNION ALL
5350
SELECT 1222, 0, 0;
5451

55-
56-
-- Get all of the arguments / parameters when starting up the service.
52+
-- Get all arguments/parameters when starting up the service.
5753
DECLARE @SQLArgs TABLE (
5854
Value VARCHAR(50),
5955
Data VARCHAR(500),
60-
ArgNum AS CONVERT(INTEGER, REPLACE(Value, 'SQLArg', '')));
56+
ArgNum AS CONVERT(INT, REPLACE(Value, 'SQLArg', ''))
57+
);
6158
INSERT INTO @SQLArgs
62-
EXECUTE master.sys.xp_instance_regenumvalues @RegHive, @RegKey;
63-
59+
EXEC master.sys.xp_instance_regenumvalues @RegHive, @RegKey;
6460

6561
-- Get the highest argument number that is currently set
66-
SELECT @MaxValue = MAX(ArgNum)
67-
FROM @SQLArgs;
68-
RAISERROR('MaxValue: %i', 10, 1, @MaxValue) WITH NOWAIT;
69-
70-
-- Disable specified trace flags
71-
SELECT @SQLCMD = 'DBCC TRACEOFF(' +
72-
STUFF((SELECT ',' + CONVERT(VARCHAR(15), TF)
73-
FROM @TraceFlags
74-
WHERE enable = 0
75-
ORDER BY TF
76-
FOR XML PATH(''), TYPE).value('.','varchar(max)')
77-
,1,1,'') + ', -1);'
62+
SELECT @MaxValue = MAX(ArgNum) FROM @SQLArgs;
63+
PRINT 'MaxValue: ' + CAST(@MaxValue AS VARCHAR);
7864

79-
IF @DebugLevel = 0 EXECUTE (@SQLCMD);
80-
RAISERROR('Disable TFs Command: "%s"', 10, 1, @SQLCMD) WITH NOWAIT;
65+
-- Disable specified trace flags
66+
SELECT @SQLCMD = 'DBCC TRACEOFF(' +
67+
STUFF((SELECT ',' + CONVERT(VARCHAR(15), TF)
68+
FROM @TraceFlags
69+
WHERE enable = 0
70+
ORDER BY TF
71+
FOR XML PATH(''), TYPE).value('.','varchar(max)'), 1, 1, '') + ', -1);'
72+
IF @DebugLevel = 0 EXEC (@SQLCMD);
73+
PRINT 'Disable TFs Command: "' + @SQLCMD + '"';
8174

8275
-- Enable specified trace flags
83-
SELECT @SQLCMD = 'DBCC TRACEON(' +
84-
STUFF((SELECT ',' + CONVERT(VARCHAR(15), TF)
85-
FROM @TraceFlags
86-
WHERE enable = 1
87-
ORDER BY TF
88-
FOR XML PATH(''), TYPE).value('.','varchar(max)')
89-
,1,1,'') + ', -1);'
90-
91-
IF @DebugLevel = 0 EXECUTE (@SQLCMD);
92-
RAISERROR('Enable TFs Command: "%s"', 10, 1, @SQLCMD) WITH NOWAIT;
76+
SELECT @SQLCMD = 'DBCC TRACEON(' +
77+
STUFF((SELECT ',' + CONVERT(VARCHAR(15), TF)
78+
FROM @TraceFlags
79+
WHERE enable = 1
80+
ORDER BY TF
81+
FOR XML PATH(''), TYPE).value('.','varchar(max)'), 1, 1, '') + ', -1);'
82+
IF @DebugLevel = 0 EXEC (@SQLCMD);
83+
PRINT 'Enable TFs Command: "' + @SQLCMD + '"';
9384

85+
-- Prepare to update the registry with new trace flags
9486
DECLARE cSQLParams CURSOR LOCAL FAST_FORWARD FOR
95-
WITH cte AS
96-
(
97-
-- Current arguments, with new TFs added at the end. Get a row number to sort by.
98-
SELECT *,
99-
ROW_NUMBER() OVER (ORDER BY ISNULL(ArgNum, 999999999), TF) - 1 AS RN
100-
FROM @SQLArgs arg
87+
WITH cte AS (
88+
SELECT *,
89+
ROW_NUMBER() OVER (ORDER BY ISNULL(ArgNum, 999999999), TF) - 1 AS RN
90+
FROM @SQLArgs arg
10191
FULL OUTER JOIN @TraceFlags tf ON arg.Data = tf.TF2
102-
), cte2 AS
103-
(
104-
-- Use the row number to calc the SQLArg# for new TFs.
105-
-- Use the original Value (SQLArg#) and Data for all rows if possible,
106-
-- Otherwise use the calculated SQLArg# and the calculated TF2 column.
107-
-- Only get the original non-TF-matched parameters, and the TFs set to be enabled
108-
-- (existing startup TFs not in @TraceFlags are left alone).
109-
SELECT ca.Value,
110-
ca.Data
111-
-- in case any TFs are removed, calculate new row numbers in order
112-
-- to renumber the SQLArg values
113-
, ROW_NUMBER() OVER (ORDER BY RN) - 1 AS RN2
114-
FROM cte
115-
-- Again, for SQL 2005, use SELECT statement instead of VALUES.
116-
CROSS APPLY (SELECT ISNULL(Value, 'SQLArg' + CONVERT(VARCHAR(15), RN)),
117-
ISNULL(Data, TF2) ) ca(Value, Data)
118-
WHERE ISNULL(enable_on_startup, 1) = 1 -- ISNULL handles non-TF parameters
92+
), cte2 AS (
93+
SELECT ca.Value, ca.Data,
94+
ROW_NUMBER() OVER (ORDER BY RN) - 1 AS RN2
95+
FROM cte
96+
CROSS APPLY (SELECT ISNULL(Value, 'SQLArg' + CONVERT(VARCHAR(15), RN)), ISNULL(Data, TF2)) ca(Value, Data)
97+
WHERE ISNULL(enable_on_startup, 1) = 1
11998
)
120-
-- The first three parameters are the location of the errorlog directory,
121-
-- and the master database file locations. Ignore these.
122-
-- This returns the remaining parameters that should be set.
123-
-- Also return the highest number of parameters, so can determine if any need to be deleted.
124-
SELECT 'SQLArg' + CONVERT(VARCHAR(15), RN2) AS Value,
125-
Data,
126-
MAX(RN2) OVER () AS MaxRN2
127-
FROM cte2
128-
WHERE RN2 > 2
99+
SELECT 'SQLArg' + CONVERT(VARCHAR(15), RN2) AS Value, Data, MAX(RN2) OVER () AS MaxRN2
100+
FROM cte2
101+
WHERE RN2 > 2
129102
ORDER BY RN2;
130-
131-
DECLARE @Value VARCHAR(50),
132-
@Data VARCHAR(500),
133-
@MaxRN2 INTEGER;
103+
104+
DECLARE @Value VARCHAR(50), @Data VARCHAR(500), @MaxRN2 INT;
134105
OPEN cSQLParams;
135106
FETCH NEXT FROM cSQLParams INTO @Value, @Data, @MaxRN2;
136107
WHILE @@FETCH_STATUS = 0
137108
BEGIN
138-
IF @DebugLevel = 0 EXECUTE master.sys.xp_instance_regwrite @RegHive, @RegKey, @Value, 'REG_SZ', @Data;
139-
RAISERROR('EXECUTE master.sys.xp_instance_regwrite ''%s'', ''%s'', ''%s'', ''REG_SZ'', ''%s''', 10, 1, @RegHive, @RegKey, @Value, @Data) WITH NOWAIT;
109+
IF @DebugLevel = 0 EXEC master.sys.xp_instance_regwrite @RegHive, @RegKey, @Value, 'REG_SZ', @Data;
110+
PRINT 'EXEC master.sys.xp_instance_regwrite ''' + @RegHive + ''', ''' + @RegKey + ''', ''' + @Value + ''', ''REG_SZ'', ''' + @Data + '''';
140111
FETCH NEXT FROM cSQLParams INTO @Value, @Data, @MaxRN2;
141112
END;
142113
CLOSE cSQLParams;
143114
DEALLOCATE cSQLParams;
144115

145-
-- In case deleting more TFs than added, there may be extra SQLArg values left behind.
146-
-- Need to delete the extras now.
116+
-- Delete extra SQLArg values if more trace flags were removed than added
147117
WHILE @MaxValue > @MaxRN2
148118
BEGIN
149-
SET @Value = 'SQLArg' + CONVERT(VARCHAR(15), @MaxValue);
150-
IF @DebugLevel = 0 EXECUTE master.sys.xp_instance_regdeletevalue @RegHive, @RegKey, @Value;
151-
RAISERROR('EXECUTE master.sys.xp_instance_regdeletevalue ''%s'', ''%s'', ''%s''', 10, 1, @RegHive, @RegKey, @Value) WITH NOWAIT;
152-
SET @MaxValue = @MaxValue - 1;
119+
SET @Value = 'SQLArg' + CONVERT(VARCHAR(15), @MaxValue);
120+
IF @DebugLevel = 0 EXEC master.sys.xp_instance_regdeletevalue @RegHive, @RegKey, @Value;
121+
PRINT 'EXEC master.sys.xp_instance_regdeletevalue ''' + @RegHive + ''', ''' + @RegKey + ''', ''' + @Value + '''';
122+
SET @MaxValue = @MaxValue - 1;
153123
END;

0 commit comments

Comments
 (0)