|
2 | 2 | Source link: https://blog.waynesheffield.com/wayne/archive/2017/09/registry-sql-server-startup-parameters/
|
3 | 3 | Author: Wayne Sheffield
|
4 | 4 |
|
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 | +
|
9 | 9 | 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 | +
|
12 | 12 | To use the xp_instance_reg... XPs, use:
|
13 | 13 | HKLM\Software\Microsoft\MSSQLSERVER\MSSQLServer\Parameters.
|
14 |
| - |
15 |
| -To use: |
| 14 | +
|
| 15 | +Usage: |
16 | 16 | 1. Add the Trace Flags that you want modified to the @TraceFlags table variable.
|
17 | 17 | 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. |
19 | 19 | ********************************************************************************
|
20 |
| - MODIFICATION LOG |
| 20 | +MODIFICATION LOG |
21 | 21 | ********************************************************************************
|
22 | 22 | 2016-08-03 WGS Initial Creation.
|
23 | 23 | *******************************************************************************/
|
24 | 24 | SET NOCOUNT ON;
|
| 25 | + |
25 | 26 | -- 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, |
28 | 28 | @SQLCMD VARCHAR(MAX),
|
29 | 29 | @RegHive VARCHAR(50),
|
30 | 30 | @RegKey VARCHAR(100),
|
31 | 31 | @DebugLevel TINYINT;
|
32 |
| - |
| 32 | + |
| 33 | +-- Registry hive and key for SQL Server startup parameters |
33 | 34 | SET @RegHive = 'HKEY_LOCAL_MACHINE';
|
34 | 35 | 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. |
41 | 40 | DECLARE @TraceFlags TABLE (
|
42 |
| - TF INTEGER, |
| 41 | + TF INT, |
43 | 42 | enable BIT,
|
44 | 43 | enable_on_startup BIT,
|
45 | 44 | TF2 AS '-T' + CONVERT(VARCHAR(15), TF)
|
46 | 45 | );
|
47 | 46 | 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. |
50 | 47 | SELECT 1117, 1, 1 UNION ALL
|
51 | 48 | SELECT 1118, 1, 1 UNION ALL
|
52 | 49 | SELECT 1204, 0, 0 UNION ALL
|
53 | 50 | SELECT 1222, 0, 0;
|
54 | 51 |
|
55 |
| - |
56 |
| --- Get all of the arguments / parameters when starting up the service. |
| 52 | +-- Get all arguments/parameters when starting up the service. |
57 | 53 | DECLARE @SQLArgs TABLE (
|
58 | 54 | Value VARCHAR(50),
|
59 | 55 | Data VARCHAR(500),
|
60 |
| - ArgNum AS CONVERT(INTEGER, REPLACE(Value, 'SQLArg', ''))); |
| 56 | + ArgNum AS CONVERT(INT, REPLACE(Value, 'SQLArg', '')) |
| 57 | +); |
61 | 58 | INSERT INTO @SQLArgs
|
62 |
| - EXECUTE master.sys.xp_instance_regenumvalues @RegHive, @RegKey; |
63 |
| - |
| 59 | +EXEC master.sys.xp_instance_regenumvalues @RegHive, @RegKey; |
64 | 60 |
|
65 | 61 | -- 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); |
78 | 64 |
|
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 + '"'; |
81 | 74 |
|
82 | 75 | -- 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 + '"'; |
93 | 84 |
|
| 85 | +-- Prepare to update the registry with new trace flags |
94 | 86 | 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 |
101 | 91 | 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 |
119 | 98 | )
|
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 |
129 | 102 | 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; |
134 | 105 | OPEN cSQLParams;
|
135 | 106 | FETCH NEXT FROM cSQLParams INTO @Value, @Data, @MaxRN2;
|
136 | 107 | WHILE @@FETCH_STATUS = 0
|
137 | 108 | 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 + ''''; |
140 | 111 | FETCH NEXT FROM cSQLParams INTO @Value, @Data, @MaxRN2;
|
141 | 112 | END;
|
142 | 113 | CLOSE cSQLParams;
|
143 | 114 | DEALLOCATE cSQLParams;
|
144 | 115 |
|
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 |
147 | 117 | WHILE @MaxValue > @MaxRN2
|
148 | 118 | 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; |
153 | 123 | END;
|
0 commit comments