<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Wayne Sheffield, Author at SQL Solutions Group</title>
	<atom:link href="https://sqlsolutionsgroup.com/author/wsheffield/feed/" rel="self" type="application/rss+xml" />
	<link>https://sqlsolutionsgroup.com/author/wsheffield/</link>
	<description></description>
	<lastBuildDate>Tue, 02 Aug 2022 20:56:58 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://sqlsolutionsgroup.com/wp-content/uploads/2021/01/cropped-SSG_FAVICON0002-32x32.png</url>
	<title>Wayne Sheffield, Author at SQL Solutions Group</title>
	<link>https://sqlsolutionsgroup.com/author/wsheffield/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Working with the registry in SQL Server</title>
		<link>https://sqlsolutionsgroup.com/working-registry-sql-server/</link>
					<comments>https://sqlsolutionsgroup.com/working-registry-sql-server/#comments</comments>
		
		<dc:creator><![CDATA[Wayne Sheffield]]></dc:creator>
		<pubDate>Thu, 02 Apr 2020 15:16:00 +0000</pubDate>
				<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[Registry]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<category><![CDATA[T-SQL]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2956</guid>

					<description><![CDATA[<p>There is a lot of information within the Windows registry. Sometimes, it would sure be nice to work with the registry in SQL Server through your T-SQL scripts. As it turns out, there are several undocumented extended stored procedures in SQL Server that do just this. A listing of these procedures are: Regular Instance-Aware sys.xp_regread [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/working-registry-sql-server/">Working with the registry in SQL Server</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>There is a lot of information within the Windows registry. Sometimes, it would sure be nice to work with the registry in SQL Server through your T-SQL scripts. As it turns out, there are several undocumented extended stored procedures in SQL Server that do just this. A listing of these procedures are:</p>
<table style="border: 1px solid; border-collapse: collapse; border-spacing: 10px; width: 40%; margin-left: 0%; margin-right: 60%;">
<tbody>
<tr>
<th style="border: 1px solid; text-align: center; font-weight: bold;">Regular</th>
<th style="border: 1px solid; text-align: center; font-weight: bold;">Instance-Aware</th>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_regread</td>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_instance_regread</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_regenumvalues</td>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_instance_regenumvalues</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_regenumkeys</td>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_instance_regenumkeys</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_regwrite</td>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_instance_regwrite</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_regdeletevalue</td>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_instance_regdeletevalue</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_regdeletekey</td>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_instance_regdeletekey</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_regaddmultistring</td>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_instance_regaddmultistring</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_regremovemultistring</td>
<td style="border: 1px solid; border-spacing: 10px; text-align: center;">sys.xp_instance_regremovemultistring</td>
</tr>
</tbody>
</table>
<p>As you can see, there are two categories of extended stored procedures you can use, with complimentary procedures between them. The &#8220;Instance-Aware&#8221; category procedures utilize key word substitution in order to navigate to the appropriate registry keys for the SQL Server instance that is running. I&#8217;ll show you an example shortly.</p>
<hr />
<p class="has-text-align-center" style="font-size: 20px;">A lot of companies spin their wheels trying to diagnose a SQL problem.</p>
<p class="has-text-align-center" style="font-size: 20px;">With SQL Server, we’ve seen it all and have done it all. Save time. Call us first.</p>
<p class="has-text-align-center" style="font-size: 20px;"><a href="https://sqlsolutionsgroup.com/services/"><span style="text-decoration: underline;">Learn More</span></a></p>
<hr />
<h3>Registry in SQL Server Basics</h3>
<p>Before we go too much further, let&#8217;s explain a few basic key phrases when dealing with the registry:</p>
<p><strong>Registry</strong>: The <em>registry</em> is a hierarchical database used by Windows. It is organized in a tree view manner.</p>
<p><strong>Registry Hive</strong>: A <em>hive</em> is a logical group of keys, subkeys, and values in the registry that has a set of supporting files containing backups of its data.</p>
<p>Examples of Registry Hives are:</p>
<table style="border: 1px solid; border-collapse: collapse; border-spacing: 10px; width: 20%; margin-left: 00%; margin-right: 80%;">
<tbody>
<tr>
<th style="border: 1px solid; border-spacing: 10px; text-align: center; font-weight: bold;">Registry Hive</th>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: left;">HKEY_CURRENT_CONFIG</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: left;">HKEY_CURRENT_USER</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: left;">HKEY_LOCAL_MACHINE\SAM</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: left;">HKEY_LOCAL_MACHINE\Security</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: left;">HKEY_LOCAL_MACHINE\Software</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: left;">HKEY_LOCAL_MACHINE\System</td>
</tr>
<tr>
<td style="border: 1px solid; border-spacing: 10px; text-align: left;">HKEY_USERS\.DEFAULT</td>
</tr>
</tbody>
</table>
<p>Of all of these hives, you will probably use HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER the most.</p>
<p><strong>Registry Key</strong>: A node within a registry hive is called a <em>key</em>. A key can contain both sub-keys and values. Sometimes an application will only be looking for the presence of a key; at other times it will need to look at the value.</p>
<p><strong>Registry Value</strong>: A registry <em>value</em> is simply a piece of data, of any data type.</p>
<p>The following image shows the tree view layout of the registry, and identifies the various items within it:</p>
<p><a href="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/07/Registry.png"><img fetchpriority="high" decoding="async" class="alignnone wp-image-3209 size-full" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/07/Registry.png" alt="Registry in SQL Server" width="573" height="395" /></a></p>
<h3>Querying the registry from SQL Server</h3>
<h4>Using xp_regread / xp_instance_regread</h4>
<p>Let’s start off by querying some data within SQL Server. I have an instance of SQL Server named “SQL2014” (would you care to take a guess as to what version of SQL Server this is?). One of the items stored in the registry is the location of the SQL Agent working directory. These procedures can query the registry and return the specified values. For example:</p>
<div id="wpshdo_1" class="wp-synhighlighter-outer"><div id="wpshdt_1" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_1"></a><a id="wpshat_1" class="wp-synhighlighter-title" href="#codesyntax_1"  onClick="javascript:wpsh_toggleBlock(1)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_1" onClick="javascript:wpsh_code(1)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_1" onClick="javascript:wpsh_print(1)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_1" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_regread</span> 
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>, 
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>icrosoft SQL Server<span class="es0">\M</span>SSQL12.SQL2014<span class="es0">\S</span>QLServerAgent'</span>, 
    <span class="st0">'WorkingDirectory'</span>;</pre></div></div>
<p>When I execute this statement, SQL returns the following result:</p>
<div id="wpshdo_2" class="wp-synhighlighter-outer"><div id="wpshdt_2" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_2"></a><a id="wpshat_2" class="wp-synhighlighter-title" href="#codesyntax_2"  onClick="javascript:wpsh_toggleBlock(2)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_2" onClick="javascript:wpsh_code(2)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_2" onClick="javascript:wpsh_print(2)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_2" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">VALUE</span>            <span class="kw1">DATA</span>
<span class="co1">---------------- -----------------------------------</span>
WorkingDirectory D:\MSSQL\MSSQL12.<span class="me1">SQL2014</span>\MSSQL\JOBS</pre></div></div>
<p>In order for you to run this, you may need to change the key as appropriate for the version and instance on your server. The result includes both the value, and the data for the specified path.</p>
<h3>xp_instance_regread</h3>
<p>In this example, I used xp_regread to read the direct registry path. If you remember from earlier, there are SQL Server instance-aware versions of each registry procedure. A comparable statement using the instance-aware procedure would be:</p>
<div id="wpshdo_3" class="wp-synhighlighter-outer"><div id="wpshdt_3" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_3"></a><a id="wpshat_3" class="wp-synhighlighter-title" href="#codesyntax_3"  onClick="javascript:wpsh_toggleBlock(3)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_3" onClick="javascript:wpsh_code(3)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_3" onClick="javascript:wpsh_print(3)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_3" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regread</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent'</span>,
    <span class="st0">'WorkingDirectory'</span>;</pre></div></div>
<p>This statement returns the exact same information. Let’s look at the difference between these – in the first query, the registry path is the exact registry path needed. It includes “\Microsoft SQL Server\MSSQL12.SQL2014\”. In the latter query, this string is replaced with “\MSSQLSERVER\”. Since the latter function is instance aware, it replaces the “MSSQLSERVER” with the exact registry path necessary for this instance of SQL Server. Pretty neat, isn’t it? This allows you to have a script that will run properly regardless of instance. The rest of the examples in this post utilize the instance-aware procedures to make it easier for you to follow along and run these yourself.</p>
<h3>Syntax</h3>
<p>The syntax for these procedures is:</p>
<div id="wpshdo_4" class="wp-synhighlighter-outer"><div id="wpshdt_4" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_4"></a><a id="wpshat_4" class="wp-synhighlighter-title" href="#codesyntax_4"  onClick="javascript:wpsh_toggleBlock(4)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_4" onClick="javascript:wpsh_code(4)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_4" onClick="javascript:wpsh_print(4)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_4" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> xp_regread 
    <span class="br0">[</span>@rootkey<span class="sy0">=</span><span class="br0">]</span>’rootkey’,
    <span class="br0">[</span>@<span class="kw1">KEY</span><span class="sy0">=</span><span class="br0">]</span>’<span class="kw1">KEY</span>’
    <span class="br0">[</span>, <span class="br0">[</span>@value_name<span class="sy0">=</span><span class="br0">]</span>’value_name’<span class="br0">]</span>
    <span class="br0">[</span>, <span class="br0">[</span>@<span class="kw1">VALUE</span><span class="sy0">=</span><span class="br0">]</span>@<span class="kw1">VALUE</span> <span class="kw1">OUTPUT</span><span class="br0">]</span></pre></div></div>
<p>The first parameter is the registry hive that you want to query, the second parameter is the key path, and the third is the value name. The third parameter is optional – if provided, then the procedure will return the data from the specified value item; if not provided, then the procedure only returns whether the specified key exists. There is also an optional fourth parameter, which is an output parameter, and the data output goes into that. The parameters are positional, and while you can specify a name for the parameter, any name will work. Since the parameters are positional, my recommendation is to not use the parameter names. The following example utilizes the optional fourth parameter, and it will return just the specified path into the variable:</p>
<div id="wpshdo_5" class="wp-synhighlighter-outer"><div id="wpshdt_5" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_5"></a><a id="wpshat_5" class="wp-synhighlighter-title" href="#codesyntax_5"  onClick="javascript:wpsh_toggleBlock(5)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_5" onClick="javascript:wpsh_code(5)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_5" onClick="javascript:wpsh_print(5)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_5" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">DECLARE</span> @SQLAgentDirectory <span class="kw1">VARCHAR</span><span class="br0">&#40;</span>255<span class="br0">&#41;</span>;
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regread</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent'</span>,
    <span class="st0">'WorkingDirectory'</span>,
    @SQLAgentDirectory <span class="kw1">OUTPUT</span>;
<span class="kw1">SELECT</span> @SQLAgentDirectory;</pre></div></div>
<p>You would think that if you specify either NULL or an empty string for the third parameter, that you could send whether the key exists to an output variable. However, I have not found a way to accomplish this. Specifying either of these values results in an error when running this statement. If you know how to do this, please leave a remark so that this post can be updated with that information.</p>
<h3>Using xp_regenumvalues / xp_instance_regenumvalues</h3>
<p>These procedures enumerate through all of the values of the specified key, returning a separate result set for each value. For instance, the following statement will return all of the values in the above SQLServerAgent key:</p>
<div id="wpshdo_6" class="wp-synhighlighter-outer"><div id="wpshdt_6" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_6"></a><a id="wpshat_6" class="wp-synhighlighter-title" href="#codesyntax_6"  onClick="javascript:wpsh_toggleBlock(6)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_6" onClick="javascript:wpsh_code(6)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_6" onClick="javascript:wpsh_print(6)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_6" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regenumvalues</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent'</span>;</pre></div></div>
<p>Having all of these result sets makes it difficult to work with this procedure. Thankfully, you can put these all into one result set. Just create a temporary table (or table variable) to hold the output, and then use INSERT / EXECUTE to fill it, like the following example does:</p>
<div id="wpshdo_7" class="wp-synhighlighter-outer"><div id="wpshdt_7" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_7"></a><a id="wpshat_7" class="wp-synhighlighter-title" href="#codesyntax_7"  onClick="javascript:wpsh_toggleBlock(7)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_7" onClick="javascript:wpsh_code(7)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_7" onClick="javascript:wpsh_print(7)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_7" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">DECLARE</span> @Registry <span class="kw1">TABLE</span> <span class="br0">&#40;</span><span class="kw1">VALUE</span> <span class="kw1">VARCHAR</span><span class="br0">&#40;</span>255<span class="br0">&#41;</span>, <span class="kw1">DATA</span> <span class="kw1">VARCHAR</span><span class="br0">&#40;</span>255<span class="br0">&#41;</span><span class="br0">&#41;</span>;
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> @Registry
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regenumvalues</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent'</span>;
<span class="kw1">SELECT</span> <span class="sy0">*</span> <span class="kw1">FROM</span> @Registry;</pre></div></div>
<p>Now that all of the results are in one result set, you can work with it a bit easier.</p>
<h3>Syntax</h3>
<p>The syntax for these procedures is:</p>
<div id="wpshdo_8" class="wp-synhighlighter-outer"><div id="wpshdt_8" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_8"></a><a id="wpshat_8" class="wp-synhighlighter-title" href="#codesyntax_8"  onClick="javascript:wpsh_toggleBlock(8)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_8" onClick="javascript:wpsh_code(8)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_8" onClick="javascript:wpsh_print(8)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_8" class="wp-synhighlighter-inner" style="display: block;"><pre class="php" style="font-family:monospace;">EXECUTE xp_regenumvalues 
    <span class="br0">[</span><span class="sy0">@</span>rootkey<span class="sy0">=</span><span class="br0">]</span>’rootkey’<span class="sy0">,</span>
    <span class="br0">[</span><span class="sy0">@</span><a href="http://www.php.net/key"><span class="kw3">key</span></a><span class="sy0">=</span><span class="br0">]</span>’<a href="http://www.php.net/key"><span class="kw3">key</span></a>’</pre></div></div>
<h3>Using xp_regenumkeys / xp_instance_regenumkeys</h3>
<p>These procedures enumerate through all of the keys in a specified path, and return all of the keys in that path. Unlike xp_instance_regenumvalues, all of the keys are returned in one result set, though you will probably want to use INSERT / EXECUTE to put this into temporary storage so that you can work with it. An example of using these procedures is:</p>
<div id="wpshdo_9" class="wp-synhighlighter-outer"><div id="wpshdt_9" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_9"></a><a id="wpshat_9" class="wp-synhighlighter-title" href="#codesyntax_9"  onClick="javascript:wpsh_toggleBlock(9)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_9" onClick="javascript:wpsh_code(9)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_9" onClick="javascript:wpsh_print(9)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_9" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regenumkeys</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER'</span>;</pre></div></div>
<p>The syntax for these procedures is:</p>
<div id="wpshdo_10" class="wp-synhighlighter-outer"><div id="wpshdt_10" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_10"></a><a id="wpshat_10" class="wp-synhighlighter-title" href="#codesyntax_10"  onClick="javascript:wpsh_toggleBlock(10)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_10" onClick="javascript:wpsh_code(10)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_10" onClick="javascript:wpsh_print(10)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_10" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> xp_regenumkeys 
    <span class="br0">[</span>@rootkey<span class="sy0">=</span><span class="br0">]</span>’rootkey’,
    <span class="br0">[</span>@<span class="kw1">KEY</span><span class="sy0">=</span><span class="br0">]</span>’<span class="kw1">KEY</span>’</pre></div></div>
<h3>Modifying the registry from within SQL Server</h3>
<p>Up to this point, we have focused on retrieving data from the registry. What if you want to modify the registry? Read on… <span style="color: #ff0000;">be forewarned that the following procedures are modifying the registry, which means that they can also damage the registry, possibly rendering the server unusable. <strong>Use at your own risk!</strong></span></p>
<h3>xp_regwrite / xp_instance_regwrite</h3>
<p>Use these procedures to create keys and write data into the registry. You can create up to 32 sub-keys at a time. In the following example, a new key “MyNewKey” will be added to the SQLServerAgent key, and the value “MyNewValue” will be added to this new key with the data “Now you see me!”. It will then read this value from the registry.</p>
<div id="crayon-5d1fdb612f205983226844" class="crayon-syntax crayon-theme-ssms2012 crayon-font-monaco crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouseover">
<div class="crayon-toolbar" data-settings=" show">
<div id="wpshdo_11" class="wp-synhighlighter-outer"><div id="wpshdt_11" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_11"></a><a id="wpshat_11" class="wp-synhighlighter-title" href="#codesyntax_11"  onClick="javascript:wpsh_toggleBlock(11)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_11" onClick="javascript:wpsh_code(11)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_11" onClick="javascript:wpsh_print(11)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_11" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regwrite</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>,
    <span class="st0">'MyNewValue'</span>,
    <span class="st0">'REG_SZ'</span>,
    <span class="st0">'Now you see me!'</span>;
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regread</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>,
    <span class="st0">'MyNewValue'</span>;</pre></div></div>
</div>
</div>
<h3 data-settings=" show"><span style="font-size: 1.2em; font-weight: 600;">Syntax</span></h3>
<div data-settings=" show">
<p>The syntax for these procedures is:</p>
</div>
<div id="wpshdo_12" class="wp-synhighlighter-outer"><div id="wpshdt_12" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_12"></a><a id="wpshat_12" class="wp-synhighlighter-title" href="#codesyntax_12"  onClick="javascript:wpsh_toggleBlock(12)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_12" onClick="javascript:wpsh_code(12)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_12" onClick="javascript:wpsh_print(12)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_12" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> xp_regwrite 
    <span class="br0">[</span>@rootkey<span class="sy0">=</span><span class="br0">]</span>’rootkey’,
    <span class="br0">[</span>@<span class="kw1">KEY</span><span class="sy0">=</span><span class="br0">]</span>’<span class="kw1">KEY</span>’,
    <span class="br0">[</span>@value_name<span class="sy0">=</span><span class="br0">]</span>’value_name’,
    <span class="br0">[</span>@type<span class="sy0">=</span><span class="br0">]</span>’type’,
    <span class="br0">[</span>@<span class="kw1">VALUE</span><span class="sy0">=</span><span class="br0">]</span>’<span class="kw1">VALUE</span>’</pre></div></div>
<h3>xp_regdeletevalue / xp_instance_regdeletevalue</h3>
<p>These procedures are used to delete a specified value from the registry. In this example, the “MyNewValue” value will be deleted. The example first enumerates through all of the values in this key (just the one), deletes the value, enumerates through them again (since there are no values, there will be no result set), and then finally shows that the key is still present.</p>
<div id="wpshdo_13" class="wp-synhighlighter-outer"><div id="wpshdt_13" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_13"></a><a id="wpshat_13" class="wp-synhighlighter-title" href="#codesyntax_13"  onClick="javascript:wpsh_toggleBlock(13)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_13" onClick="javascript:wpsh_code(13)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_13" onClick="javascript:wpsh_print(13)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_13" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regenumvalues</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>;
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regdeletevalue</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>,
    <span class="st0">'MyNewValue'</span>;
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regenumvalues</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>;
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regread</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>;</pre></div></div>
<h3>Syntax</h3>
<p>The syntax for these procedures is:</p>
<div id="wpshdo_14" class="wp-synhighlighter-outer"><div id="wpshdt_14" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_14"></a><a id="wpshat_14" class="wp-synhighlighter-title" href="#codesyntax_14"  onClick="javascript:wpsh_toggleBlock(14)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_14" onClick="javascript:wpsh_code(14)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_14" onClick="javascript:wpsh_print(14)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_14" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> xp_regdeletevalue 
    <span class="br0">[</span>@rootkey<span class="sy0">=</span><span class="br0">]</span>’rootkey’,
    <span class="br0">[</span>@<span class="kw1">KEY</span><span class="sy0">=</span><span class="br0">]</span>’<span class="kw1">KEY</span>’,
    <span class="br0">[</span>@value_name<span class="sy0">=</span><span class="br0">]</span>’value_name’</pre></div></div>
<h3>xp_regdeletekey / xp_instance_regdeletekey</h3>
<p>Use these procedures to delete an entire key from the registry. In this example, the script first adds another new key under “MyNewKey”, and a value in that new key. The script then deletes both the “AnotherNewKey” (which deletes the value just added also) and “MyNewKey” keys and finally shows that both keys have been deleted. Note that in order to delete a key, it cannot have any sub-keys, which is why the script deletes “AnotherNewKey” first (try running the script first by commenting out the first xp_instance_regdeletekey to see that it must be empty).</p>
<div id="wpshdo_15" class="wp-synhighlighter-outer"><div id="wpshdt_15" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_15"></a><a id="wpshat_15" class="wp-synhighlighter-title" href="#codesyntax_15"  onClick="javascript:wpsh_toggleBlock(15)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_15" onClick="javascript:wpsh_code(15)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_15" onClick="javascript:wpsh_print(15)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_15" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regwrite</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey<span class="es0">\A</span>notherNewKey'</span>,
    <span class="st0">'MyNewValue'</span>,
    <span class="st0">'REG_SZ'</span>,
    <span class="st0">'Another new value!'</span>;
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regread</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey<span class="es0">\A</span>notherNewKey'</span>;
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regdeletekey</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey<span class="es0">\A</span>notherNewKey'</span>;
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regdeletekey</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>;
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regread</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey<span class="es0">\A</span>notherNewKey'</span>;
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regread</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>;</pre></div></div>
<h3>Syntax</h3>
<p>The syntax for these procedures is:</p>
<div id="wpshdo_16" class="wp-synhighlighter-outer"><div id="wpshdt_16" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_16"></a><a id="wpshat_16" class="wp-synhighlighter-title" href="#codesyntax_16"  onClick="javascript:wpsh_toggleBlock(16)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_16" onClick="javascript:wpsh_code(16)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_16" onClick="javascript:wpsh_print(16)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_16" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> xp_regdeletekey 
    <span class="br0">[</span>@rootkey<span class="sy0">=</span><span class="br0">]</span>’rootkey’,
    <span class="br0">[</span>@<span class="kw1">KEY</span><span class="sy0">=</span><span class="br0">]</span>’<span class="kw1">KEY</span>’</pre></div></div>
<h3>xp_regaddmultistring / xp_instance_regaddmultistring</h3>
<p>Use these procedures to add a string to a multi-string entry in the registry, or to create a multi-string registry entry. In this example, I’ll call the procedure twice. The first time will create the entry with one string in it, and the second time will add a second string to it. Then the example will show the results of this value.</p>
<div id="wpshdo_17" class="wp-synhighlighter-outer"><div id="wpshdt_17" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_17"></a><a id="wpshat_17" class="wp-synhighlighter-title" href="#codesyntax_17"  onClick="javascript:wpsh_toggleBlock(17)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_17" onClick="javascript:wpsh_code(17)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_17" onClick="javascript:wpsh_print(17)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_17" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">EXECUTE master.sys.xp_instance_regaddmultistring
    'HKEY_LOCAL_MACHINE',
    'Software\Microsoft\MSSQLSERVER\SQLServerAgent\MyNewKey',
    'MyNewValue',
    'A multi-string value!';
EXECUTE master.sys.xp_instance_regaddmultistring
    'HKEY_LOCAL_MACHINE',
    'Software\Microsoft\MSSQLSERVER\SQLServerAgent\MyNewKey',
    'MyNewValue',
    'Yet Another new string added to this multi-string value!';
EXECUTE master.sys.xp_instance_regread
    'HKEY_LOCAL_MACHINE',
    'Software\Microsoft\MSSQLSERVER\SQLServerAgent\MyNewKey',
    'MyNewValue'</pre></div></div>
<p>Did you notice that the regread procedure got a little confused here? It has two value columns (which is the name of the value), but the second one has the data for the first one. Then the data column is null. If a third string is added, it still returns just these three columns. To see that we actually added these strings, we’ll have to use regedit.exe:</p>
<div id="wpshdo_18" class="wp-synhighlighter-outer"><div id="wpshdt_18" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_18"></a><a id="wpshat_18" class="wp-synhighlighter-title" href="#codesyntax_18"  onClick="javascript:wpsh_toggleBlock(18)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_18" onClick="javascript:wpsh_code(18)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_18" onClick="javascript:wpsh_print(18)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_18" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="co1">-- add a third string</span>
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regaddmultistring</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>,
    <span class="st0">'MyNewValue'</span>,
    <span class="st0">'How about a third string?'</span>;
<span class="co1">-- only shows the first string</span>
<span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regread</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>,
    <span class="st0">'MyNewValue'</span></pre></div></div>
<p><a href="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/08/Registry2.png"><img decoding="async" class="alignnone size-full wp-image-3230" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/08/Registry2.png" sizes="(max-width: 612px) 100vw, 612px" srcset="//blog.waynesheffield.com/wayne/wp-content/uploads/2016/08/Registry2.png 612w, //blog.waynesheffield.com/wayne/wp-content/uploads/2016/08/Registry2-300x226.png 300w" alt="Registry2" width="612" height="461" /></a></p>
<h3>Syntax</h3>
<p>The syntax for these procedures is:</p>
<div id="wpshdo_19" class="wp-synhighlighter-outer"><div id="wpshdt_19" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_19"></a><a id="wpshat_19" class="wp-synhighlighter-title" href="#codesyntax_19"  onClick="javascript:wpsh_toggleBlock(19)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_19" onClick="javascript:wpsh_code(19)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_19" onClick="javascript:wpsh_print(19)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_19" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> xp_regaddmultistring 
    <span class="br0">[</span>@rootkey<span class="sy0">=</span><span class="br0">]</span>’rootkey’,
    <span class="br0">[</span>@<span class="kw1">KEY</span><span class="sy0">=</span><span class="br0">]</span>’<span class="kw1">KEY</span>’,
    <span class="br0">[</span>@value_name<span class="sy0">=</span><span class="br0">]</span>’value_name’,
    <span class="br0">[</span>@<span class="kw1">VALUE</span><span class="sy0">=</span><span class="br0">]</span>’<span class="kw1">VALUE</span>’</pre></div></div>
<h3>xp_regremovemultistring / xp_instance_regremovemultistring</h3>
<p>These procedures are used to remove a string from a multi-string entry. This example will remove the middle string.</p>
<div id="wpshdo_20" class="wp-synhighlighter-outer"><div id="wpshdt_20" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_20"></a><a id="wpshat_20" class="wp-synhighlighter-title" href="#codesyntax_20"  onClick="javascript:wpsh_toggleBlock(20)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_20" onClick="javascript:wpsh_code(20)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_20" onClick="javascript:wpsh_print(20)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_20" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regremovemultistring</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>,
    <span class="st0">'MyNewValue'</span>,
    <span class="st0">'Yet Another new string added to this multi-string value!'</span>;</pre></div></div>
<p>Examining the string now in regedit.exe, we can see it removed all of the strings starting with the specified string… in other words, it removed the second and third strings. If we add the strings back in, and then remove the third string, we can see that it also removes the second and third string. In testing with a fourth string, it appears that if you are deleting a string that is not the first string, then all of the remainder of the strings after the first string are removed. Deleting the first string deletes all of the strings. Well, these are undocumented procedures, so it’s not likely that this bug will ever be fixed.</p>
<h3>Syntax</h3>
<p>The syntax for these procedures is:</p>
<div id="wpshdo_21" class="wp-synhighlighter-outer"><div id="wpshdt_21" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_21"></a><a id="wpshat_21" class="wp-synhighlighter-title" href="#codesyntax_21"  onClick="javascript:wpsh_toggleBlock(21)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_21" onClick="javascript:wpsh_code(21)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_21" onClick="javascript:wpsh_print(21)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_21" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> xp_regremovemultistring 
    <span class="br0">[</span>@rootkey<span class="sy0">=</span><span class="br0">]</span>’rootkey’,
    <span class="br0">[</span>@<span class="kw1">KEY</span><span class="sy0">=</span><span class="br0">]</span>’<span class="kw1">KEY</span>’,
    <span class="br0">[</span>@value_name<span class="sy0">=</span><span class="br0">]</span>’value_name’,
    <span class="br0">[</span>@<span class="kw1">VALUE</span><span class="sy0">=</span><span class="br0">]</span>’<span class="kw1">VALUE</span>’</pre></div></div>
<p>And finally, let’s ensure that things are cleaned up:</p>
<div id="wpshdo_22" class="wp-synhighlighter-outer"><div id="wpshdt_22" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_22"></a><a id="wpshat_22" class="wp-synhighlighter-title" href="#codesyntax_22"  onClick="javascript:wpsh_toggleBlock(22)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_22" onClick="javascript:wpsh_code(22)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_22" onClick="javascript:wpsh_print(22)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_22" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">EXECUTE</span> master.<span class="me1">sys</span>.<span class="me1">xp_instance_regdeletekey</span>
    <span class="st0">'HKEY_LOCAL_MACHINE'</span>,
    <span class="st0">'Software<span class="es0">\M</span>icrosoft<span class="es0">\M</span>SSQLSERVER<span class="es0">\S</span>QLServerAgent<span class="es0">\M</span>yNewKey'</span>;</pre></div></div>
<h3>Conclusion</h3>
<p>With the exception of removing and reading the multi-string values, these extended stored procedures all work pretty well. We’ve been able to read registry values, and to enumerate through a list of keys and values. We can create and delete keys and values.</p>
<p>If you try to use the procedures in sections outside of SQL Server keys, you may run into registry security problems where the SQL Server service account doesn’t have permissions to accomplish the task. On the internet, you will find articles telling you to add the service account to the local administrators group (and to restart the service). This is a <span style="color: #ff0000;"><strong>VERY BAD IDEA</strong></span> – by doing this, you can completely compromise the server. You could do anything that you desire on the server. Instead, use regedt32.exe, which will allow you to modify the permissions on the key that where you are having access issues.</p>
<p>I close by repeating my warning from above – be very careful when modifying the registry. It is possible to corrupt the registry to the point where the server will no longer function. To recover, an operating system reinstall will be necessary.</p>
<p>As a footnote on working with the instance-aware installation. I recall seeing on the internet other keywords that could be substituted instead of MSSQLSERVER. One of them is SQLServerAgent. However, I can’t find any others right now. If you happen to know of other keywords that work, please reply to this post so that it can be added to this post.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/working-registry-sql-server/">Working with the registry in SQL Server</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlsolutionsgroup.com/working-registry-sql-server/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Server &#8217;16 helps you meet Best Practice</title>
		<link>https://sqlsolutionsgroup.com/sql-server-best-practices/</link>
					<comments>https://sqlsolutionsgroup.com/sql-server-best-practices/#respond</comments>
		
		<dc:creator><![CDATA[Wayne Sheffield]]></dc:creator>
		<pubDate>Tue, 15 Mar 2016 07:37:58 +0000</pubDate>
				<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[Best practices]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Server 2016]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2895</guid>

					<description><![CDATA[<p>In order for your SQL Server instance to run optimally, there are many SQL Server best practices that you need to follow. The SQL Health Check that you can have us perform on your  instances looks at many of these. SQL Server 2016, which at the time of this writing is in a Release Candidate [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/sql-server-best-practices/">SQL Server &#8217;16 helps you meet Best Practice</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In order for your SQL Server instance to run optimally, there are many SQL Server best practices that you need to follow. The SQL Health Check that you can have us perform on your  instances looks at many of these. SQL Server 2016, which at the time of this writing is in a Release Candidate 0 state of readiness, has been changed to handle several of these best practices. This post will look at several of these changes.</p>
<h2>Some of the SQL Server Best Practices</h2>
<h3>Instant File Initialization</h3>
<p>The first change appears during the installation of SQL Server 2016 itself. One of the best practice items that we look for in our Health Check is that the SQL Server Database Engine service has the “Perform Volume Maintenance Task” security privilege. SQL Server 2016 now allows the setting of this privilege right in the install screen.</p>
<p><a href="https://sqlsolutionsgroup.com/wp-content/uploads/2016/03/SSMS2016-PVMT.png" rel="attachment wp-att-2896"><img decoding="async" class="size-full wp-image-2896 alignnone" src="https://sqlsolutionsgroup.com/wp-content/uploads/2016/03/SSMS2016-PVMT.png" alt="SSMS2016-PVMT" width="820" height="615" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2016/03/SSMS2016-PVMT.png 820w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/03/SSMS2016-PVMT-300x225.png 300w, https://sqlsolutionsgroup.com/wp-content/uploads/2016/03/SSMS2016-PVMT-768x576.png 768w" sizes="(max-width: 820px) 100vw, 820px" /></a></p>
<p>The benefit of having this privilege set is that it allows SQL Server to utilize “Instant File Initialization” (IFI). When IFI is not set, then any growth of a database’s data file will be zero-initialized… that is, the entire growth portion of the file is overwritten with zeros (note that database log files are always zero-initialized… this is needed for the crash recovery process). During the time that the database data file is being zero-initialized, no other activity can occur in the database. This setting not only affects growths of data files, but also affects database restores and adding new files to a filegroup. <span style="text-decoration: underline;"><a href="https://www.sqlskills.com/blogs/kimberly/instant-initialization-what-why-and-how/." target="_blank" rel="noopener noreferrer">Kimberly Tripp has an excellent post</a></span> that explains this in more detail, and the kind of performance impact that can be obtained by having IFI enabled.</p>
<p>However, configuring the instance to utilize IFI has a potential negative security side-effect. If you have just dropped a database, and a growth occurs on another database, it could use the part of the drive that the other database was using. And although it is extremely unlikely, the pages could possibly line up. With advanced techniques, one could then read the data that was on those pages. Obviously, zero-initializing the pages would eliminate this problem. It is also obvious that there are a lot of circumstances that have to line up to allow this potential security issue…but the chance of it occurring will never be zero with IFI enabled.</p>
<p>Because of the performance impact, and extremely low security risk, most SQL Server professionals recommend having this option set.</p>
<h3>tempdb</h3>
<p>Ahh, tempdb. In SQL Server, that database does so much… it’s where temporary tables and table variables are stored. It’s where row version activity is stored. It’s where running queries will spill out temporary workspace needed for the query. And on, and on, and on. On busy systems, having this database running optimally is of vital importance. And there are several best practices that need to be observed here.</p>
<p>First up is the number of data files needed for tempdb. On a busy system that is creating a lot of temporary objects (specifically temporary tables and table variables), the creation of these objects creates a hot-spot in one of the database file’s internal page types that stores information about the allocation of these objects. However, each database file has these internal pages. Frequently, this contention can be greatly reduced by simply adding additional database files to this database. The specific advice for how many files to have has changed over time, and there is disagreement over the exact number of files to have. What isn’t disagreed over is that additional files are needed. And once again, during the SQL Server 2016 installation, this can be set, as seen in the following screen shot:</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2017/01/SSMS2016-TempDB.png" alt="SSMS2016-TempDB" /></p>
<p>This screen shot shows several of the changes that are now available in the installation process. First off is the number of data files. Note that the installation limits the number of data files to the lower of 8 or the number of CPUs presented to SQL Server (one of the methods of determining how many files to have).</p>
<p>The next settings that this screen shot shows is the new default size and autogrowth values. Technically, these changes are made to the model database. However, they apply to all new databases, including tempdb when it is created. The initial size of the data and log files is now 8 MB. The default auto-growth of data and log files is now 64MB.</p>
<p>The next setting available in this screen is the directory to place the data and log files in. Specifically, you can have tempdb data files in multiple directories. During the installation, these files are created in the directories in a round-robin fashion.</p>
<h3>Trace Flags</h3>
<p>There are three specific trace flags that are commonly set in SQL Server. If trace flag (TF) 1117 is enabled, then whenever a data file grows, all of the files in that database will also grow. In SQL Server 2016, this trace flag has been replaced with the AUTOGROW_ALL_FLAGS or AUTOGROW_SINGLE_FILE option of the ALTER DATABASE command, and setting TF 1117 no longer has any effect. Since it is set as part of the ALTER DATABASE command, it can be controlled at the database level, where TF 1117 was a setting across the entire instance. Furthermore, because this setting is so valuable to have set in tempdb, by default tempdb is set with this feature enabled.</p>
<p>TF 1118 controls whether an extent can have pages belonging to multiple objects (known as a mixed extent), or to a single object (known as a dedicated extent). Using mixed extents allows having small tables use the absolute minimum amount of storage space…which isn’t really that much of an issue as it was in SQL 7.0 when it was added. Using dedicated extents can also help in the contention when creating lots of objects really fast. In SQL Server 2016, this trace flag has been replaced with the SET MIXED_PAGE_ALLOCATION option of the ALTER DATABASE command (and like TF 1117, TF1118 no longer has any effect). Furthermore, in the tempdb database, this setting is also enabled by default.</p>
<p>The next trace flag to talk about is TF 4199. This trace flag controls whether changes to how the query optimizer functions are used or not. In prior versions of SQL Server, the changes were not used unless this TF had been enabled. Starting in SQL Server 2016, <em>most</em> of these behaviors are enabled if the database is in the SQL Server 2016 compatibility level.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/sql-server-best-practices/">SQL Server &#8217;16 helps you meet Best Practice</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlsolutionsgroup.com/sql-server-best-practices/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Solutions Group&#8217;s 5th Anniversary FreeCon</title>
		<link>https://sqlsolutionsgroup.com/sql-solutions-freecon/</link>
					<comments>https://sqlsolutionsgroup.com/sql-solutions-freecon/#respond</comments>
		
		<dc:creator><![CDATA[Wayne Sheffield]]></dc:creator>
		<pubDate>Tue, 10 Nov 2015 09:21:22 +0000</pubDate>
				<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2713</guid>

					<description><![CDATA[<p>In honor of SQL Solutions Group turning five years old, the four MCMs at SSG recently conducted a day of free training for the SQL community. Due to the many meetings going on Tuesday in conjunction with the PASS Summit, attendees were coming and going all day long. The day started off with Ben Miller [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/sql-solutions-freecon/">SQL Solutions Group&#8217;s 5th Anniversary FreeCon</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In honor of <a href="https://sqlsolutionsgroup.com/sql-freecon/">SQL Solutions Group turning five years old</a>, the four <a href="https://sqlsolutionsgroup.com/microsoft-certified-master/">MCM</a>s at SSG recently conducted a day of free training for the SQL community. Due to the many meetings going on Tuesday in conjunction with the PASS Summit, attendees were coming and going all day long.</p>
<figure style="width: 640px" class="wp-caption alignnone"><img loading="lazy" decoding="async" class="" src="https://1.bp.blogspot.com/-BxoRvz1-jkk/VjAdgMLAIOI/AAAAAAAAcGA/VW5gfRogmV4/s640/IMG_1684.JPG" alt="" width="640" height="427" /><figcaption class="wp-caption-text">Freecon attendees</figcaption></figure>
<p>The day started off with <span style="color: #f25e00;"><strong>Ben Miller</strong> </span>talking about PowerShell.</p>
<figure style="width: 524px" class="wp-caption alignnone"><img loading="lazy" decoding="async" src="https://3.bp.blogspot.com/-WdBs5U3p3gQ/VjAdgYL0cLI/AAAAAAAAcGE/bEGsp-IQpaA/s1600/IMG_1685.JPG" alt="Jason Brimhall of SQL Solutions Group" width="524" height="786" /><figcaption class="wp-caption-text">Ben Miller discusses PowerShell</figcaption></figure>
<p>Next up, SSG founder <span style="color: #f25e00;"><strong>Randy Knight</strong></span> addressed Isolation Levels, Locking, and Deadlocking.</p>
<p>Following Randy, my subject was SQL Injection, Referential Integrity (including optimizations that SQL Server can make when referential integrity is in place), and demonstrating the performance aspects of the various types of functions.</p>
<p><strong><span style="color: #f25e00;">Jason Brimhall</span></strong> concluded the day with the topic of Extended Events.</p>
<p>As one attendee shared, &#8220;Great presentations! I learned a lot. Especially that NOLOCK is bad!&#8221;</p>
<p><img loading="lazy" decoding="async" class="alignnone" src="https://3.bp.blogspot.com/--teY754Hk3E/VjAdirg6JVI/AAAAAAAAcG4/KTCblIBT_Ow/s640/IMG_1795.JPG" alt="Jason Brimhall of SQL Solutions Group" width="640" height="427" /></p>
<p>At the end of the day, SSG gave away some prizes. Kevin Barnett and Delfin Esposo each walked away with a copy of the book <em><a href="https://www.amazon.com/Server-T-SQL-Recipes-Jason-Brimhall/dp/1484200624/ref=sr_1_1?ie=UTF8&amp;qid=1446700161&amp;sr=8-1&amp;keywords=SQL+Server+T-SQL+Recipes" target="_blank" rel="noopener noreferrer">SQL Server T-SQL Recipes</a></em>, co-authored by SSG MCMs Jason Brimhall and me. James Rzepka won the coveted year of Amazon Prime/Amazon Fire tablet combo.</p>
<p>All of us at SQL Solutions Group had a wonderful time doing this FreeCon, and we hope that we can make this an annual event.</p>
<p>Pictures are courtesy of <span class="highlight-container"><span class="highlighted">Slava</span> Murygin</span> (@SlavaSQL), who spent the entire week running around taking pictures of everything SQL related during this week of the PASS Summit. He described our FreeCon as a &#8220;world class SQL learning event.&#8221; Thanks, Slava!</p>
<p>The post <a href="https://sqlsolutionsgroup.com/sql-solutions-freecon/">SQL Solutions Group&#8217;s 5th Anniversary FreeCon</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlsolutionsgroup.com/sql-solutions-freecon/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>FreeCon, October 27, 2015 Seattle</title>
		<link>https://sqlsolutionsgroup.com/freecon-october-27-2015-seattle-2/</link>
		
		<dc:creator><![CDATA[Wayne Sheffield]]></dc:creator>
		<pubDate>Wed, 23 Sep 2015 20:16:05 +0000</pubDate>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[Events]]></category>
		<category><![CDATA[Extended Events]]></category>
		<category><![CDATA[powershell]]></category>
		<category><![CDATA[Presenting]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sql injection]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<category><![CDATA[Training]]></category>
		<guid isPermaLink="false">https://blog.waynesheffield.com/wayne/?p=2474</guid>

					<description><![CDATA[<p>The company that I work with, SQL Solutions Group, is conducting a free community event. The week of the PASS Summit, on Tuesday, we will be hosting a day of free training. This is an event for all data professionals who happen to be in the Seattle area the days leading up to Summit but [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://blog.waynesheffield.com/wayne/archive/2015/09/freecon-october-27-2015-seattle/">FreeCon, October 27, 2015 Seattle</a> appeared first on <a rel="nofollow" href="https://blog.waynesheffield.com/wayne">Wayne Sheffield</a>.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/freecon-october-27-2015-seattle-2/">FreeCon, October 27, 2015 Seattle</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>The company that I work with, <a href="https://blog.waynesheffield.com/wayne/?feed-stats-url=aHR0cDovL3d3dy5zcWxzb2x1dGlvbnNncm91cC5jb20%3D&amp;feed-stats-url-post-id=2474">SQL Solutions Group</a>, is conducting a free community event. The week of the PASS Summit, on Tuesday, we will be hosting a day of free training. This is an event for all data professionals who happen to be in the Seattle area the days leading up to Summit but may not be able to attend one of the <a href="https://blog.waynesheffield.com/wayne/?feed-stats-url=aHR0cDovL3d3dy5zcWxwYXNzLm9yZy9zdW1taXQvMjAxNS9TZXNzaW9ucy9QcmVDb25mZXJlbmNlLmFzcHg%3D&amp;feed-stats-url-post-id=2474">Summit Precons</a>.  This day of free training will be conducted by four of our <a href="https://blog.waynesheffield.com/wayne/?feed-stats-url=aHR0cDovL3d3dy5zcWxzb2x1dGlvbnNncm91cC5jb20vbWljcm9zb2Z0LWNlcnRpZmllZC1tYXN0ZXIv&amp;feed-stats-url-post-id=2474">MCM</a>s. If you’re not going to be at one of the PASS Summit Precons, then we’d love to have you come out to see us!</p>
<p>The workshops that we will be presenting are:</p>
<p><strong>Code Smells for the Consultant </strong>(which I’ll be presenting)<strong><br />
</strong></p>
<blockquote><p>Throughout my career, I’ve seen developers do some pretty crazy things to databases ( I know because I come from a developer background). Come to this session to learn both what I (and SSG) look for and why it’s bad for the database (or your career), and alternatives that can be used. Some of the topics that I will discuss include; how coding mistakes open up the database for SQL Injection attacks, how coding choices can slow down the server, and how design choices keep SQL Server dumb ( if SQL Server was allowed to be smart, it would be faster!). Trust me, your DBA will love you for identifying and fixing these code smells.</p></blockquote>
<p><strong>A Masters Passport to Extended Events </strong>(presented by Jason Brimhall)<strong><br />
</strong></p>
<blockquote><p>As is commonly the case, all good things come to an end.  And now is as good a time as any for the use of SQL Trace and Profiler to come to an end.  Let’s face it, Trace was a good tool and had some wonderful uses.  Profiler for that matter was a good tool and was useful at times.</p>
<p>It is time to let those old tools retire gracefully and move into the world of XE.  This workshop will provide you the means to let Profiler and Trace be retired from your toolset as you discover all that XE has to offer.</p>
<p>This focused session on Extended Events will help prepare you to put this tool to immediate use as you walk back to your daily duties.  This workshop will teach you about Extended Events starting with the basics and moving through to some specific XE sessions that I would use to troubleshoot in a client environment – while doing so with minimal impact.</p>
<p>You will be exposed to advanced troubleshooting techniques as we work through complex issues that are made easier through the use of XE.  Take advantage of this opportunity to dive into the world of Extended Events and learn how you can make best use of this tool in your SQL 2008+ environment.</p></blockquote>
<p>You can read the rest of this article over <a href="https://bit.ly/1iMzl7T">here</a>.</p>
<p>&nbsp;</p>
<p><img loading="lazy" decoding="async" style="display: none;" src="https://blog.waynesheffield.com/wayne/?feed-stats-post-id=2474" width="1" height="1" /></p>
<p>The post <a href="https://blog.waynesheffield.com/wayne/archive/2015/09/freecon-october-27-2015-seattle/" rel="nofollow">FreeCon, October 27, 2015 Seattle</a> appeared first on <a href="https://blog.waynesheffield.com/wayne" rel="nofollow">Wayne Sheffield</a>.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/freecon-october-27-2015-seattle-2/">FreeCon, October 27, 2015 Seattle</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>XE Output: Weaning yourself off of SQL Profiler, Part 2</title>
		<link>https://sqlsolutionsgroup.com/weaning-yourself-off-of-sql-profiler-part-2/</link>
					<comments>https://sqlsolutionsgroup.com/weaning-yourself-off-of-sql-profiler-part-2/#respond</comments>
		
		<dc:creator><![CDATA[Wayne Sheffield]]></dc:creator>
		<pubDate>Wed, 08 Jul 2015 10:01:40 +0000</pubDate>
				<category><![CDATA[Extended Events]]></category>
		<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[sql profiler]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2405</guid>

					<description><![CDATA[<p>In my last post, I shared a script that will take a running trace and show you the XE events that it relates to, and what columns are available within those XE events. Specifically, this was for converting a deadlock trace into an XE session; however the process is the same for converting any trace [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/weaning-yourself-off-of-sql-profiler-part-2/">XE Output: Weaning yourself off of SQL Profiler, Part 2</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://sqlsolutionsgroup.com/wp-content/uploads/2015/06/NoProfiler.png"><img loading="lazy" decoding="async" class="alignright wp-image-2348 size-thumbnail" src="https://sqlsolutionsgroup.com/wp-content/uploads/2015/06/NoProfiler-150x150.png" alt="NoProfiler" width="150" height="150" /></a>In my <span style="color: #0000ff;"><a style="color: #0000ff;" href="https://sqlsolutionsgroup.com/wean-off-sql-profiler-part-1/">last post</a></span>, I shared a script that will take a running trace and show you the XE events that it relates to, and what columns are available within those XE events. Specifically, this was for converting a deadlock trace into an XE session; however the process is the same for converting any trace into an XE session. In today’s post, we’ll compare the deadlock trace and the new XE output by running both, creating a deadlock, and comparing the captured data. We’ll look at the data captured in the XE from both script and GUI, and look at a few other differences between running a trace and an XE session.</p>
<p>The first step is to grab the trace and XE scripts from the prior post at <span style="color: #0000ff;"><a style="color: #0000ff;" href="https://sqlsolutionsgroup.com/wean-off-sql-profiler-part-1/">https://sqlsolutionsgroup.com/wean-off-sql-profiler-part-1/</a></span>. Modify both scripts to put the output files in an appropriate place on your system. Run both of the scripts to start the trace and to create the XE session. Next, start the XE session with the following script:</p>
<div id="wpshdo_23" class="wp-synhighlighter-outer"><div id="wpshdt_23" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_23"></a><a id="wpshat_23" class="wp-synhighlighter-title" href="#codesyntax_23"  onClick="javascript:wpsh_toggleBlock(23)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_23" onClick="javascript:wpsh_code(23)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_23" onClick="javascript:wpsh_print(23)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_23" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">ALTER</span> EVENT <span class="kw1">SESSION</span> Deadlocks
<span class="kw1">ON</span> SERVER
<span class="kw1">STATE</span> <span class="sy0">=</span> <span class="kw1">START</span>;</pre></div></div>
<p>The next step is to create a deadlock. Open up a new query window, and run the following. Leave this query window open.</p>
<div id="wpshdo_24" class="wp-synhighlighter-outer"><div id="wpshdt_24" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_24"></a><a id="wpshat_24" class="wp-synhighlighter-title" href="#codesyntax_24"  onClick="javascript:wpsh_toggleBlock(24)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_24" onClick="javascript:wpsh_code(24)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_24" onClick="javascript:wpsh_print(24)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_24" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">USE</span> tempdb;
GO
<span class="kw1">IF</span> <span class="kw2">OBJECT_ID</span><span class="br0">&#40;</span><span class="st0">'dbo.Test1'</span><span class="br0">&#41;</span> <span class="kw1">IS</span> <span class="sy0">NOT</span> <span class="sy0">NULL</span> <span class="kw1">DROP</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test1</span>;
<span class="kw1">IF</span> <span class="kw2">OBJECT_ID</span><span class="br0">&#40;</span><span class="st0">'dbo.Test2'</span><span class="br0">&#41;</span> <span class="kw1">IS</span> <span class="sy0">NOT</span> <span class="sy0">NULL</span> <span class="kw1">DROP</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test2</span>;
<span class="kw1">CREATE</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test1</span> <span class="br0">&#40;</span>col1 <span class="kw1">INT</span><span class="br0">&#41;</span>;
<span class="kw1">CREATE</span> <span class="kw1">TABLE</span> dbo.<span class="me1">Test2</span> <span class="br0">&#40;</span>col2 <span class="kw1">INT</span><span class="br0">&#41;</span>;
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> dbo.<span class="me1">Test1</span> <span class="kw1">VALUES</span> <span class="br0">&#40;</span>1<span class="br0">&#41;</span>,<span class="br0">&#40;</span>2<span class="br0">&#41;</span>,<span class="br0">&#40;</span>3<span class="br0">&#41;</span>,<span class="br0">&#40;</span>4<span class="br0">&#41;</span>,<span class="br0">&#40;</span>5<span class="br0">&#41;</span>;
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> dbo.<span class="me1">Test2</span> <span class="kw1">VALUES</span> <span class="br0">&#40;</span>1<span class="br0">&#41;</span>,<span class="br0">&#40;</span>2<span class="br0">&#41;</span>,<span class="br0">&#40;</span>3<span class="br0">&#41;</span>,<span class="br0">&#40;</span>4<span class="br0">&#41;</span>,<span class="br0">&#40;</span>5<span class="br0">&#41;</span>;
GO
<span class="kw1">BEGIN</span> <span class="kw1">TRANSACTION</span>
<span class="kw1">UPDATE</span> dbo.<span class="me1">Test1</span> <span class="kw1">SET</span> col1 <span class="sy0">=</span> col1<span class="sy0">*</span>10 <span class="kw1">WHERE</span> col1<span class="sy0">=</span><span class="nu0">3</span>;</pre></div></div>
<p>Next, open up a second query window, and run the following code in that window:<div id="wpshdo_25" class="wp-synhighlighter-outer"><div id="wpshdt_25" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_25"></a><a id="wpshat_25" class="wp-synhighlighter-title" href="#codesyntax_25"  onClick="javascript:wpsh_toggleBlock(25)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_25" onClick="javascript:wpsh_code(25)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_25" onClick="javascript:wpsh_print(25)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_25" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">USE</span> tempdb;
<span class="kw1">BEGIN</span> <span class="kw1">TRANSACTION</span>;
<span class="kw1">UPDATE</span> dbo.<span class="me1">Test2</span> <span class="kw1">SET</span> col2 <span class="sy0">=</span> col2<span class="sy0">*</span>20 <span class="kw1">WHERE</span> col2 <span class="sy0">=</span> <span class="nu0">4</span>;
<span class="kw1">UPDATE</span> dbo.<span class="me1">Test1</span> <span class="kw1">SET</span> col1 <span class="sy0">=</span> col1<span class="sy0">*</span>20 <span class="kw1">WHERE</span> col1 <span class="sy0">=</span> <span class="nu0">3</span>;
<span class="kw1">COMMIT</span> <span class="kw1">TRANSACTION</span>;</pre></div></div></p>
<p>Finally, return to the first query window and run the following code, at which point one of the statements in one of the query windows will be deadlocked:<div id="wpshdo_26" class="wp-synhighlighter-outer"><div id="wpshdt_26" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_26"></a><a id="wpshat_26" class="wp-synhighlighter-title" href="#codesyntax_26"  onClick="javascript:wpsh_toggleBlock(26)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_26" onClick="javascript:wpsh_code(26)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_26" onClick="javascript:wpsh_print(26)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_26" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">UPDATE</span> dbo.<span class="me1">Test2</span> <span class="kw1">SET</span> col2 <span class="sy0">=</span> col2<span class="sy0">*</span>10 <span class="kw1">WHERE</span> col2 <span class="sy0">=</span> <span class="nu0">4</span>;
<span class="kw1">COMMIT</span> <span class="kw1">TRANSACTION</span>;</pre></div></div>

<span style="color: #ff0000;">Msg 1205, Level 13, State 45, Line 4</span></pre>
<pre><span style="color: #ff0000;">Transaction (Process ID 57) was deadlocked on lock resources with another process and has been chosen as the deadlock victim. Rerun the transaction.</span></pre>
<p>Now that we&#8217;ve created a deadlock, let’s compare the trace output data to the XE output data. First, let’s grab the data from the trace file with this script, which selects all of the non-null columns from the table valued function (remember to change the filename/path as appropriate):<div id="wpshdo_27" class="wp-synhighlighter-outer"><div id="wpshdt_27" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_27"></a><a id="wpshat_27" class="wp-synhighlighter-title" href="#codesyntax_27"  onClick="javascript:wpsh_toggleBlock(27)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_27" onClick="javascript:wpsh_code(27)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_27" onClick="javascript:wpsh_print(27)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_27" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">SELECT</span> t2.<span class="me1">TextData</span>, t2.<span class="me1">BinaryData</span>, t2.<span class="me1">NTUserName</span>, t2.<span class="me1">ClientProcessID</span>, t2.<span class="me1">ApplicationName</span>,
       t2.<span class="me1">LoginName</span>, t2.<span class="me1">SPID</span>, t2.<span class="me1">Duration</span>, t2.<span class="me1">StartTime</span>, t2.<span class="me1">EndTime</span>, t2.<span class="me1">ObjectID</span>,
       t2.<span class="me1">ServerName</span>, t2.<span class="me1">EventClass</span>, t2.<span class="me1">Mode</span>, t2.<span class="me1">DatabaseName</span>, t2.<span class="me1">Type</span>
<span class="kw1">FROM</span>   sys.<span class="me1">traces</span> t1
<span class="sy0">CROSS</span> APPLY sys.<span class="kw4">fn_trace_gettable</span> <span class="br0">&#40;</span><span class="kw1">path</span>, <span class="sy0">NULL</span><span class="br0">&#41;</span> t2
<span class="kw1">WHERE</span> t1.<span class="kw1">path</span> <span class="sy0">LIKE</span> <span class="st0">'C:<span class="es0">\S</span>QL<span class="es0">\T</span>races<span class="es0">\%</span>'</span></pre></div></div><a href="https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE09-XE-Collected-Trace-Data.png"><img loading="lazy" decoding="async" class="alignnone wp-image-2407 size-full" src="https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE09-XE-Collected-Trace-Data.png" alt="SSMS2014XE09-XE-Collected Trace Data" width="1164" height="188" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE09-XE-Collected-Trace-Data.png 1164w, https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE09-XE-Collected-Trace-Data-300x48.png 300w, https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE09-XE-Collected-Trace-Data-1024x165.png 1024w, https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE09-XE-Collected-Trace-Data-768x124.png 768w" sizes="(max-width: 1164px) 100vw, 1164px" /></a></p>
<p><a href="https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE10-XE-Collected-Trace-Data.png"><img loading="lazy" decoding="async" class="alignnone wp-image-2408 size-full" src="https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE10-XE-Collected-Trace-Data.png" alt="" width="1131" height="184" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE10-XE-Collected-Trace-Data.png 1131w, https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE10-XE-Collected-Trace-Data-300x49.png 300w, https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE10-XE-Collected-Trace-Data-1024x167.png 1024w, https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE10-XE-Collected-Trace-Data-768x125.png 768w" sizes="(max-width: 1131px) 100vw, 1131px" /></a></p>
<p><a href="https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE11-XE-Collected-Trace-Data.png"><img loading="lazy" decoding="async" class="alignnone wp-image-2406 size-full" src="https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE11-XE-Collected-Trace-Data.png" alt="" width="940" height="186" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE11-XE-Collected-Trace-Data.png 940w, https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE11-XE-Collected-Trace-Data-300x59.png 300w, https://sqlsolutionsgroup.com/wp-content/uploads/2015/07/SSMS2014XE11-XE-Collected-Trace-Data-768x152.png 768w" sizes="(max-width: 940px) 100vw, 940px" /></a></p>
<p>We now want to compare this to the data that was collected by the Extended Event session. In SSMS, expand the Management Tree, then the Extended Events tree, Session tree, and the Deadlocks tree:</p>
<p><img loading="lazy" decoding="async" class="alignnone" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/12/SSMS2014XE07-XE-Session-target.png" alt="" width="290" height="299" /></p>
<p>The event file target can now be seen. Double-click the file target to view the data, and you can see the events that fired for the deadlock. Note that you can right-click on the grid header, and choose additional columns to put into the grid – for this XE session, I like to add the database_name, resource_description, resource_owner, resource_type and xml_report columns.</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/12/SSMS2014XE08-XE-Collected-Data.png" alt="SSMS2014XE08-XE-Collected-Data" /></p>
<p>By selecting one of the rows, all of the data will be available in the lower grid in a Name/Value format.</p>
<p>Let’s compare the XE output to the trace output, and ensure that the data captured by the trace is present in the XE output. The first trace column is TextData. In that, we see multiple bits of data: SPID, lock resource, and the deadlock graph in XML format. In the XE, the resource_description column has the lock resource, the xml_report column has the deadlock graph in XML format, and the session_id column has the SPID. Skipping the BinaryData column, the next column is the NTUserName. Here we can see that this is collected in the XE nt_username column. A minor difference is that the XE includes the domain name, and the trace doesn’t.</p>
<p>The ClientProcessID column maps to the client_pid column. Likewise, the ApplicationName maps to client_app_name, LoginName maps to server_principal_name, SPID maps to session_id, Duration maps to duration, StartTime maps to timestamp, ObjectID maps to object_id. The EventClass, Mode and Type columns maps via friendly names to the name, mode and resource_type columns. The only columns that aren’t explicitly in the XE results are the BinaryData, EndDate and ServerName columns. The EndDate can be calculated from the timestamp and duration columns, and we could have selected the server_instance_name in “Global Fields (Actions)” tab when configuring the XE session. So here, we can see that everything needed from the trace is available in the XE session.</p>
<p>For the next step, one might want to load the data into a table, and viewing it in the viewer doesn’t allow for this. Or maybe you are working on a server prior to SQL Server 2012, and the GUI isn’t available for you to use. The following script can be used to query the data from the file target (again, remember to change the file name/path as necessary):</p>
<div id="wpshdo_28" class="wp-synhighlighter-outer"><div id="wpshdt_28" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_28"></a><a id="wpshat_28" class="wp-synhighlighter-title" href="#codesyntax_28"  onClick="javascript:wpsh_toggleBlock(28)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_28" onClick="javascript:wpsh_code(28)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_28" onClick="javascript:wpsh_print(28)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_28" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">SELECT</span> <span class="kw2">OBJECT_NAME</span>,
       <span class="kw1">CONVERT</span><span class="br0">&#40;</span>XML, event_data<span class="br0">&#41;</span> <span class="kw1">AS</span> event_data
<span class="kw1">FROM</span>   sys.<span class="me1">fn_xe_file_target_read_file</span><span class="br0">&#40;</span><span class="st0">'C:<span class="es0">\S</span>QL<span class="es0">\X</span>E_Out<span class="es0">\D</span>eadlocks*.xel'</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span><span class="br0">&#41;</span>;</pre></div></div>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/12/SSMS2014XE12-XE-target-file-query-results.png" alt="SSMS2014XE12-XE-target-file-query-results" /></p>
<p>As can be seen from the output, the data that we most want to see is stored as XML (when working with Extended Events, all of the data is stored as XML) in the event_data column, so I have converted this column to XML. At this point, you can click the XML output, and it will be opened up in text, allowing you to see all of the data as it is stored in the XML output.</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/12/SSMS2014XE13-XE-target-file-event_data-results.png" alt="SSMS2014XE13-XE-target-file-event_data-results" /></p>
<p>From this point, it’s just a matter of modifying the query to return the columns that you are interested in.</p>
<div id="wpshdo_29" class="wp-synhighlighter-outer"><div id="wpshdt_29" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_29"></a><a id="wpshat_29" class="wp-synhighlighter-title" href="#codesyntax_29"  onClick="javascript:wpsh_toggleBlock(29)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_29" onClick="javascript:wpsh_code(29)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_29" onClick="javascript:wpsh_print(29)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_29" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">WITH</span> cte <span class="kw1">AS</span>
<span class="br0">&#40;</span>
<span class="kw1">SELECT</span>  t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/@name)[1]'</span>,<span class="st0">'varchar(50)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> event_name,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/@timestamp)[1]'</span>, <span class="st0">'datetime2'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> StartTime,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;duration&quot;]/value)[1]'</span>, <span class="st0">'bigint'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> duration,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;database_name&quot;]/value)[1]'</span>, <span class="st0">'sysname'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> DBName,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;nt_username&quot;]/value)[1]'</span>, <span class="st0">'varchar(500)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> nt_username,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;mode&quot;]/value)[1]'</span>, <span class="st0">'varchar(15)'</span><span class="br0">&#41;</span>  <span class="sy0">+</span> <span class="st0">' ('</span> <span class="sy0">+</span> 
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;mode&quot;]/text)[1]'</span>, <span class="st0">'varchar(50)'</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">')'</span> <span class="kw1">AS</span> mode,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;object_id&quot;]/value)[1]'</span>, <span class="st0">'integer'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> <span class="kw2">OBJECT_ID</span>,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;resource_description&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> resource_description,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;resource_owner_type&quot;]/text)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span>  <span class="kw1">AS</span> resource_owner_type,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;resource_type&quot;]/text)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">' ('</span> <span class="sy0">+</span>
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/data[@name=&quot;resource_type&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">')'</span> <span class="kw1">AS</span> resource_type,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;server_principal_name&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> server_principal_name,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;session_id&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> session_id,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;client_pid&quot;]/value)[1]'</span>, <span class="st0">'integer'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> client_pid,
        t2.<span class="me1">event_data</span>.<span class="kw1">VALUE</span><span class="br0">&#40;</span><span class="st0">'(event/action[@name=&quot;client_app_name&quot;]/value)[1]'</span>, <span class="st0">'varchar(max)'</span><span class="br0">&#41;</span> <span class="kw1">AS</span> client_app_name,
        t2.<span class="me1">event_data</span>
<span class="kw1">FROM</span>    sys.<span class="me1">fn_xe_file_target_read_file</span><span class="br0">&#40;</span><span class="st0">'C:<span class="es0">\S</span>QL<span class="es0">\X</span>E_Out<span class="es0">\D</span>eadlocks*.xel'</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span><span class="br0">&#41;</span> t1
<span class="sy0">CROSS</span> APPLY <span class="br0">&#40;</span><span class="kw1">SELECT</span> <span class="kw1">CONVERT</span><span class="br0">&#40;</span>XML, t1.<span class="me1">event_data</span><span class="br0">&#41;</span><span class="br0">&#41;</span> t2<span class="br0">&#40;</span>event_data<span class="br0">&#41;</span>
<span class="br0">&#41;</span>
<span class="kw1">SELECT</span>  cte.<span class="me1">event_name</span>,
        cte.<span class="me1">StartTime</span>,
        <span class="kw2">DATEADD</span><span class="br0">&#40;</span>MICROSECOND, duration, <span class="kw1">CONVERT</span><span class="br0">&#40;</span>DATETIME2, <span class="br0">[</span>cte<span class="br0">]</span>.<span class="me1">StartTime</span><span class="br0">&#41;</span><span class="br0">&#41;</span> <span class="kw1">AS</span> EndDate,
        cte.<span class="me1">duration</span>,
        cte.<span class="me1">DBName</span>,
        cte.<span class="me1">nt_username</span>,
        cte.<span class="me1">server_principal_name</span>,
        cte.<span class="me1">mode</span>,
        cte.<span class="kw2">OBJECT_ID</span>,
        cte.<span class="me1">resource_description</span>,
        cte.<span class="me1">resource_owner_type</span>,
        cte.<span class="me1">resource_type</span>,
        cte.<span class="me1">session_id</span>,
        cte.<span class="me1">client_pid</span>,
        cte.<span class="me1">client_app_name</span>,
        cte.<span class="me1">event_data</span> 
<span class="kw1">FROM</span>    cte;</pre></div></div>
<p>This query extracts the data from XE file target. It also calculates the end date, and displays both the internal and user-friendly names for the resource_type and mode columns – the internal values are what the trace was returning.</p>
<p>For a quick recap: <a href="https://sqlsolutionsgroup.com/wean-off-sql-profiler-part-1/">in Part 1</a>, you learned how to convert an existing trace into an XE session, identifying the columns and stepping through the GUI for creating the XE session. In part 2 you learned how to query both the trace and XE file target outputs, and then compared the two outputs and learned that all of the data in the trace output is available in the XE output.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/weaning-yourself-off-of-sql-profiler-part-2/">XE Output: Weaning yourself off of SQL Profiler, Part 2</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlsolutionsgroup.com/weaning-yourself-off-of-sql-profiler-part-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Weaning yourself off of SQL Profiler (Part 1)</title>
		<link>https://sqlsolutionsgroup.com/wean-off-sql-profiler-part-1/</link>
					<comments>https://sqlsolutionsgroup.com/wean-off-sql-profiler-part-1/#comments</comments>
		
		<dc:creator><![CDATA[Wayne Sheffield]]></dc:creator>
		<pubDate>Tue, 09 Jun 2015 06:26:24 +0000</pubDate>
				<category><![CDATA[Extended Events]]></category>
		<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[scripts]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQL Script]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[XE]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=2336</guid>

					<description><![CDATA[<p>In this brave, new world of Extended Events (XE, XEvents), I find myself with a mixture of scripts for troubleshooting issues – some use XE, and some use traces. We’ve all been told that XE is a much better system (it is much more lightweight, causing less of an issue with the server). In fact, [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/wean-off-sql-profiler-part-1/">Weaning yourself off of SQL Profiler (Part 1)</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://sqlsolutionsgroup.com/wp-content/uploads/2015/06/NoProfiler.png"><img loading="lazy" decoding="async" class="alignright wp-image-2348 size-medium" src="https://sqlsolutionsgroup.com/wp-content/uploads/2015/06/NoProfiler-300x243.png" alt="NoProfiler" width="300" height="243" hspace="15" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2015/06/NoProfiler-300x243.png 300w, https://sqlsolutionsgroup.com/wp-content/uploads/2015/06/NoProfiler.png 560w" sizes="(max-width: 300px) 100vw, 300px" /></a>In this brave, new world of Extended Events (XE, XEvents), I find myself with a mixture of scripts for troubleshooting issues – some use XE, and some use traces. We’ve all been told that XE is a much better system (it is much more lightweight, causing less of an issue with the server). In fact, it is so much better that Microsoft has deprecated SQL Trace and SQL Profiler, and in the future, one will not be able to run any traces at all. Believe it or not, this is a good thing!</p>
<p><a href="https://blogs.lessthandot.com/index.php/uncategorized/youre-invited-to-t-sql-tuesday-67-extended-events/"><img loading="lazy" decoding="async" class="alignleft wp-image-1860 size-thumbnail" src="https://sqlsolutionsgroup.com/wp-content/uploads/2014/12/TSQL2sDay150x150-150x150.jpg" alt="TSQL2sDay150x150" width="150" height="150" srcset="https://sqlsolutionsgroup.com/wp-content/uploads/2014/12/TSQL2sDay150x150-150x150.jpg 150w, https://sqlsolutionsgroup.com/wp-content/uploads/2014/12/TSQL2sDay150x150.jpg 154w" sizes="(max-width: 150px) 100vw, 150px" /></a>It just so happens that today is the 67<sup>th</sup> installment of the monthly T-SQL Tuesday blogging event. T-SQL Tuesday, that wonderful monthly blogging party started by Adam Machanic where a selected host challenges the SQL Server universe to have a blog post about a specific topic. This wild frenzy of SQL Server blog posting occurs on the second Tuesday of each month. This month, it is being hosted by my friend (and the person with the highest amount of energy known to mankind) Jes Borland (<span style="color: #3366ff;"><a style="color: #3366ff;" href="https://blogs.lessthandot.com/index.php?disp=authdir&amp;author=420">b</a></span> / <span style="color: #3366ff;"><a style="color: #3366ff;" href="https://twitter.com/grrl_geek">t</a></span>), and the topic that she has chosen is Extended Events. Her specific challenge to the SQL Server universe is:</p>
<blockquote><p>I want to know (and others do, too) how you’ve solved problems with Extended Events. What sessions have you created? What unique way have you used predicates or targets? What challenges have you overcome?</p></blockquote>
<p>The biggest challenge that I have is not a technical challenge&#8230; it&#8217;s a personal challenge: actually getting started with XE. I have so many scripts for doing traces, that I just immediately use them instead of the better XE system. I really need to wean myself off of using Profiler / traces. Therefore, I’ve decided to start converting my trace scripts into XE scripts, and I&#8217;ll share with you how I go about doing it. Today, I’m going to look at my favorite trace script – a trace to capture deadlock information.</p>
<p>First off, let’s start with the trace script:</p>
<div id="wpshdo_30" class="wp-synhighlighter-outer"><div id="wpshdt_30" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_30"></a><a id="wpshat_30" class="wp-synhighlighter-title" href="#codesyntax_30"  onClick="javascript:wpsh_toggleBlock(30)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_30" onClick="javascript:wpsh_code(30)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_30" onClick="javascript:wpsh_print(30)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_30" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="co1">-- Create a trace to capture deadlocks</span>
<span class="kw1">DECLARE</span> @rc <span class="kw1">INTEGER</span>,
        @TraceID <span class="kw1">INTEGER</span>,
        @maxfilesize <span class="kw1">BIGINT</span>,
        @OutputFileName <span class="kw1">NVARCHAR</span><span class="br0">&#40;</span>256<span class="br0">&#41;</span>,
        @TraceStopTime <span class="kw1">DATETIME</span>,
        @<span class="kw1">ON</span> <span class="kw1">BIT</span>,
        @intfilter <span class="kw1">INT</span>,
        @bigintfilter <span class="kw1">BIGINT</span>;
&nbsp;
<span class="kw1">SET</span> @<span class="kw1">ON</span> <span class="sy0">=</span> <span class="nu0">1</span>;
<span class="kw1">SET</span> @maxfilesize <span class="sy0">=</span> <span class="nu0">10</span>; <span class="co1">--mb </span>
<span class="kw1">DECLARE</span> @FileName <span class="kw1">NVARCHAR</span><span class="br0">&#40;</span>256<span class="br0">&#41;</span>;
<span class="co1">-- added InstanceName for when server is running multiple instances</span>
<span class="kw1">SET</span> @FileName <span class="sy0">=</span> N<span class="st0">'C:<span class="es0">\S</span>QL<span class="es0">\T</span>races<span class="es0">\'</span> + CONVERT(sysname, SERVERPROPERTY('</span>InstanceName<span class="st0">')) + '</span>\<span class="st0">' +
               CONVERT(CHAR(8), GETDATE(), 112) + '</span>_<span class="st0">' +
               REPLACE(CONVERT(CHAR(8), GETDATE(), 114), '</span>:<span class="st0">', '</span><span class="st0">');
EXECUTE xp_create_subdir @FileName;               
SET @FileName = @FileName + N'</span>\<span class="st0">' + REPLACE(@@SERVERNAME, N'</span>\<span class="st0">', N'</span>_<span class="st0">') + '</span>_DeadlockTrace_<span class="st0">' + CONVERT(CHAR(8), GETDATE(), 112) + N'</span>_<span class="st0">' + REPLACE(CONVERT(CHAR(8), GETDATE(), 114), N'</span>:<span class="st0">', N'</span><span class="st0">') + N'</span>Z<span class="st0">';
PRINT @FileName;
SET @TraceStopTime = DATEADD(HOUR, 6, GETDATE());
EXEC @rc = sp_trace_create @TraceID output, 2, @FileName, @maxfilesize, @TraceStopTime;
&nbsp;
IF (@rc != 0) GOTO error
&nbsp;
-- Set the events
-- Event 148: Deadlock Graph
EXEC sp_trace_setevent @TraceID, 148, 1, @on;   -- TextData
EXEC sp_trace_setevent @TraceID, 148, 11, @on;  -- LoginName
EXEC sp_trace_setevent @TraceID, 148, 12, @on;  -- SPID
EXEC sp_trace_setevent @TraceID, 148, 14, @on;  -- StartTime
-- Event 25: Lock:Deadlock
EXEC sp_trace_setevent @TraceID, 25, 1, @on;    -- TextData
EXEC sp_trace_setevent @TraceID, 25, 2, @on;    -- BinaryData
EXEC sp_trace_setevent @TraceID, 25, 6, @on;    -- NTUserName
EXEC sp_trace_setevent @TraceID, 25, 9, @on;    -- ClientProcessID
EXEC sp_trace_setevent @TraceID, 25, 10, @on;   -- ApplicationName
EXEC sp_trace_setevent @TraceID, 25, 11, @on;   -- LoginName
EXEC sp_trace_setevent @TraceID, 25, 12, @on;   -- SPID
EXEC sp_trace_setevent @TraceID, 25, 13, @on;   -- Duration
EXEC sp_trace_setevent @TraceID, 25, 14, @on;   -- StartTime
EXEC sp_trace_setevent @TraceID, 25, 15, @on;   -- EndTime
EXEC sp_trace_setevent @TraceID, 25, 22, @on;   -- ObjectID
EXEC sp_trace_setevent @TraceID, 25, 32, @on;   -- Mode
EXEC sp_trace_setevent @TraceID, 25, 35, @on;   -- DatabaseName
EXEC sp_trace_setevent @TraceID, 25, 57, @on;   -- Type
-- Event 59: Lock:Deadlock Chain
EXEC sp_trace_setevent @TraceID, 59, 1, @on;    -- TextData
EXEC sp_trace_setevent @TraceID, 59, 2, @on;    -- BinaryData
EXEC sp_trace_setevent @TraceID, 59, 12, @on;   -- SPID
EXEC sp_trace_setevent @TraceID, 59, 14, @on;   -- StartTime
EXEC sp_trace_setevent @TraceID, 59, 22, @on;   -- ObjectID
EXEC sp_trace_setevent @TraceID, 59, 32, @on;   -- Mode
EXEC sp_trace_setevent @TraceID, 59, 35, @on;   -- DatabaseName
EXEC sp_trace_setevent @TraceID, 59, 57, @on;   -- Type
&nbsp;
&nbsp;
-- Set the Filters
-- Set the trace status to start
EXEC sp_trace_setstatus @TraceID, 1;
&nbsp;
-- display trace id for future references
SELECT TraceID=@TraceID;
GOTO finish;
&nbsp;
error: 
SELECT ErrorCode=@rc;
&nbsp;
finish: 
GO</span></pre></div></div>
<p>From this script, you can see that the trace is collecting data from three events (Deadlock Graph (148), Lock:Deadlock (25) and Lock:Deadlock Chain (59)) and several columns for each event. The next step is to convert all of this information into XE events / actions. For this, I’ll modify the script in BOL (at <a href="https://msdn.microsoft.com/en-us/library/ff878264.aspx" rel="noopener noreferrer">https://msdn.microsoft.com/en-us/library/ff878264.aspx</a>) to the following to return the XE events / actions for a specified running trace. Since the trace needs to be running, execute the above trace script to start the trace, then run the following code:</p>
<div id="wpshdo_31" class="wp-synhighlighter-outer"><div id="wpshdt_31" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_31"></a><a id="wpshat_31" class="wp-synhighlighter-title" href="#codesyntax_31"  onClick="javascript:wpsh_toggleBlock(31)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_31" onClick="javascript:wpsh_code(31)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_31" onClick="javascript:wpsh_print(31)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_31" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="co1">-- change the following value to the trace_id for the running trace to be converted to XE</span>
<span class="kw1">DECLARE</span> @trace_id <span class="kw1">INTEGER</span> <span class="sy0">=</span> <span class="nu0">2</span>;
&nbsp;
<span class="kw1">SELECT</span> <span class="kw1">DISTINCT</span>
        tb.<span class="me1">trace_event_id</span>,
        te.<span class="me1">name</span> <span class="kw1">AS</span> <span class="st0">'Event Class'</span>,
        em.<span class="me1">package_name</span> <span class="kw1">AS</span> <span class="st0">'Package'</span>,
        em.<span class="me1">xe_event_name</span> <span class="kw1">AS</span> <span class="st0">'XEvent Name'</span>,
        tb.<span class="me1">trace_column_id</span>,
        tc.<span class="me1">name</span> <span class="kw1">AS</span> <span class="st0">'SQL Trace Column'</span>,
        am.<span class="me1">xe_action_name</span> <span class="kw1">AS</span> <span class="st0">'Extended Events action'</span>
<span class="kw1">FROM</span>    sys.<span class="kw4">FN_TRACE_GETEVENTINFO</span><span class="br0">&#40;</span>@trace_id<span class="br0">&#41;</span> ei
        <span class="sy0">JOIN</span> sys.<span class="me1">trace_events</span> te
            <span class="kw1">ON</span> te.<span class="me1">trace_event_id</span> <span class="sy0">=</span> ei.<span class="me1">eventid</span>
        <span class="sy0">JOIN</span> sys.<span class="me1">trace_columns</span> tc  <span class="co1">-- all available trace columns</span>
            <span class="kw1">ON</span> tc.<span class="me1">trace_column_id</span> <span class="sy0">=</span> ei.<span class="me1">columnid</span>
        <span class="kw1">LEFT</span> <span class="sy0">OUTER</span> <span class="sy0">JOIN</span> sys.<span class="me1">trace_xe_event_map</span> em
            <span class="kw1">ON</span> em.<span class="me1">trace_event_id</span> <span class="sy0">=</span> te.<span class="me1">trace_event_id</span>
        <span class="kw1">LEFT</span> <span class="sy0">OUTER</span> <span class="sy0">JOIN</span> sys.<span class="me1">trace_event_bindings</span> tb
            <span class="kw1">ON</span> tb.<span class="me1">trace_event_id</span> <span class="sy0">=</span> em.<span class="me1">trace_event_id</span>
            <span class="sy0">AND</span> tc.<span class="me1">trace_column_id</span> <span class="sy0">=</span> tb.<span class="me1">trace_column_id</span>
        <span class="kw1">LEFT</span> <span class="sy0">OUTER</span> <span class="sy0">JOIN</span> sys.<span class="me1">trace_xe_action_map</span> am
            <span class="kw1">ON</span> am.<span class="me1">trace_column_id</span> <span class="sy0">=</span> tc.<span class="me1">trace_column_id</span>
<span class="kw1">ORDER</span> <span class="kw1">BY</span> tb.<span class="me1">trace_event_id</span>, tb.<span class="me1">trace_column_id</span>;</pre></div></div>
<p>These results may contain NULL values in the “Extended Events action” column. A NULL value here means that there is not a corresponding event action for that column. For my deadlock trace, I get the following results:</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/11/SSMS2014XE00-TraceXExref.png" alt="SSMS2014XE00-TraceXExref" /></p>
<p>With this information, I can now jump into SSMS (2014) and use the New Session wizard to create a new XE session (Expand the Management and Extended Events nodes. Right click on Sessions and select New Session). I name the session “Deadlocks”, and click the “Events” page to select the events that I want to use.</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/11/SSMS2014XE01-NewXESession.png" alt="SSMS2014XE01-NewXESession" /></p>
<p>Since all of the events that I’m looking for contain “Deadlock”, I search for this and 5 events are displayed. I double-click each of the three listed above (or select them and click the “&gt;” button) to move them to the “Selected Events” grid.</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/11/SSMS2014XE02-SelectEvents.png" alt="SSMS2014XE02-SelectEvents" /></p>
<p>The next step is to configure the columns, so click the Configure button.</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/11/SSMS2014XE03-ConfigureEvents.png" alt="SSMS2014XE03-ConfigureEvents" /></p>
<p>Select the lock_deadlock event, and on the “Global Fields (Actions)” tab select the client_app_name, client_pid, database_name, nt_username, server_principal_name and session_id columns.</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/11/SSMS2014XE04-ConfigureEventColumns.png" alt="SSMS2014XE04-ConfigureEventColumns" /></p>
<p>Click on the “Event Fields” tab, and check the checkboxes for database_name and resource_description. Fields that show up in this tab with checkboxes have customizable actions, and don’t actually collect the information unless checked. Frequently this will be because these fields require an additional level of resource usage to gather this information.</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/11/SSMS2014XE05-ConfigureEventColumns.png" alt="SSMS2014XE05-ConfigureEventColumns" /></p>
<p>Select the lock_deadlock_chain event, and select the session_id and database_name columns on the “Global Fields (Actions)” tab. Click on the “Event Fields” tab and check the checkbox for database_name and resource_description. Finally, select the xml_deadlock_report event, and select the server_principal_name and session_id columns on the “Global Fields (Actions)” tab.</p>
<p>Finally, click on the Data Storage page, and add an event_file target type. The file name defaults to the name that you gave the session earlier. I set the maximum size to 250mb and 5 rollover files. If you select to script to a new query window (HIGHLY recommended), you will have a script with all of the DDL necessary to create this XE.</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/11/SSMS2014XE06-ConfigureDataStorage.png" alt="SSMS2014XE06-ConfigureDataStorage" /></p>
<p>At this point, there are two changes that I like to make. First, I like to make my scripts so that they won’t have an error, and if the XE session already exists then one will be generated. To make it bullet-proof, I add an IF EXISTS check at the top and drop the XE session if it already exists. Secondly, I like to call xp_create_subdir to create the directory that the script points to, just in case the directory doesn’t exist. Note that xp_create_subdir will be successful if the directory already exists, so I don’t check to ensure that the directory doesn’t exist before executing this procedure.</p>
<p>My final XE Deadlock script looks like:</p>
<div id="wpshdo_32" class="wp-synhighlighter-outer"><div id="wpshdt_32" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_32"></a><a id="wpshat_32" class="wp-synhighlighter-title" href="#codesyntax_32"  onClick="javascript:wpsh_toggleBlock(32)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_32" onClick="javascript:wpsh_code(32)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_32" onClick="javascript:wpsh_print(32)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_32" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">IF</span> <span class="sy0">EXISTS</span> <span class="br0">&#40;</span><span class="kw1">SELECT</span> 1 <span class="kw1">FROM</span> sys.<span class="me1">server_event_sessions</span> <span class="kw1">WHERE</span> name <span class="sy0">=</span> <span class="st0">'Deadlocks'</span><span class="br0">&#41;</span>
	<span class="kw1">DROP</span> EVENT <span class="kw1">SESSION</span> <span class="br0">[</span>Deadlocks<span class="br0">]</span> <span class="kw1">ON</span> SERVER;
GO
<span class="kw1">EXECUTE</span> xp_create_subdir <span class="st0">'C:<span class="es0">\S</span>QL<span class="es0">\X</span>E_Out'</span>;
GO
<span class="kw1">CREATE</span> EVENT <span class="kw1">SESSION</span> <span class="br0">[</span>Deadlocks<span class="br0">]</span>
<span class="kw1">ON</span> SERVER
<span class="kw1">ADD</span> EVENT sqlserver.<span class="me1">lock_deadlock</span><span class="br0">&#40;</span>
    <span class="kw1">SET</span> collect_database_name<span class="sy0">=</span><span class="br0">&#40;</span>1<span class="br0">&#41;</span>,collect_resource_description<span class="sy0">=</span><span class="br0">&#40;</span>1<span class="br0">&#41;</span>
	<span class="kw1">ACTION</span> 
	<span class="br0">&#40;</span>
			  sqlserver.<span class="me1">client_app_name</span>	<span class="co1">-- ApplicationName from SQLTrace</span>
			, sqlserver.<span class="me1">client_pid</span>	<span class="co1">-- ClientProcessID from SQLTrace</span>
			, sqlserver.<span class="me1">nt_username</span>	<span class="co1">-- NTUserName from SQLTrace</span>
			, sqlserver.<span class="me1">server_principal_name</span>	<span class="co1">-- LoginName from SQLTrace</span>
			, sqlserver.<span class="me1">session_id</span>	<span class="co1">-- SPID from SQLTrace</span>
	<span class="br0">&#41;</span>
<span class="br0">&#41;</span>,
<span class="kw1">ADD</span> EVENT sqlserver.<span class="me1">lock_deadlock_chain</span><span class="br0">&#40;</span>
    <span class="kw1">SET</span> collect_database_name<span class="sy0">=</span><span class="br0">&#40;</span>1<span class="br0">&#41;</span>,collect_resource_description<span class="sy0">=</span><span class="br0">&#40;</span>1<span class="br0">&#41;</span>
	<span class="kw1">ACTION</span> 
	<span class="br0">&#40;</span>
			  sqlserver.<span class="me1">session_id</span>	<span class="co1">-- SPID from SQLTrace</span>
	<span class="br0">&#41;</span>
<span class="br0">&#41;</span>,
<span class="kw1">ADD</span> EVENT sqlserver.<span class="me1">xml_deadlock_report</span><span class="br0">&#40;</span>
	<span class="kw1">ACTION</span> 
	<span class="br0">&#40;</span>
			  sqlserver.<span class="me1">server_principal_name</span>	<span class="co1">-- LoginName from SQLTrace</span>
			, sqlserver.<span class="me1">session_id</span>	<span class="co1">-- SPID from SQLTrace</span>
	<span class="br0">&#41;</span>
<span class="br0">&#41;</span>
<span class="kw1">ADD</span> TARGET package0.<span class="me1">event_file</span>
<span class="br0">&#40;</span>
	<span class="kw1">SET</span> filename <span class="sy0">=</span> <span class="st0">'C:<span class="es0">\S</span>QL<span class="es0">\X</span>E_Out<span class="es0">\D</span>eadlocks.xel'</span>,
		max_file_size <span class="sy0">=</span> 250,
		max_rollover_files <span class="sy0">=</span> 5
<span class="br0">&#41;</span>
<span class="kw1">WITH</span> <span class="br0">&#40;</span>STARTUP_STATE<span class="sy0">=</span><span class="kw1">OFF</span><span class="br0">&#41;</span>
;</pre></div></div>
<p>Now that you have this XE session scripted out, it can be easily installed on multiple servers. If you encounter a deadlock problem, you can easily start the XE session and let it run to trap your deadlocks. They will be persisted to a file dedicated for the deadlocks. You can use my Deadlock Shredder script at <a href="https://bit.ly/ShredDL">https://bit.ly/ShredDL</a> to read the deadlocks from the file and shred the deadlock XML into a tabular output.</p>
<p>Note that the default system_health XE session also captures deadlocks. I like to have a dedicated session for just deadlocks. As lightweight as XE is, sometimes it may benefit a server to turn off the system_health session. Additionally, Jonathan Kehayias has a script that will take a running trace and completely script out an XE session for it. This script can be found at <a href="https://www.sqlskills.com/blogs/jonathan/converting-sql-trace-to-extended-events-in-sql-server-2012/">https://www.sqlskills.com/blogs/jonathan/converting-sql-trace-to-extended-events-in-sql-server-2012/</a>. Even though this script is available, I like to figure things out for myself so that I can learn what is actually going on.</p>
<p><strong>Did you catch the note above where I mentioned that the default system_health XE captures deadlocks</strong><strong>?</strong> This means that if you are still enabling trace flags 1204 and/or 1222 to capture deadlock information in your error logs, you don’t need to do that anymore. Furthermore, by using this Deadlock XE, you can have the deadlocks persisted to a file where, in combination with my Deadlock Shredder script, it will be even easier to analyze the deadlocks than trying to figure it out from the captured information in the error logs.</p>
<p>I hope that this post will help someone with the conversion from using SQL Profiler to using XE instead. The process for converting other traces would be the same, so it can be easily adapted. I also hope that these deadlock scripts will be helpful to you!</p>
<p>In my next post, I will be comparing the results of the deadlock trace and the deadlock XE session.</p>
<p>And Jes – thanks for hosting this month! I’m really looking forward to seeing the roundup for this month.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/wean-off-sql-profiler-part-1/">Weaning yourself off of SQL Profiler (Part 1)</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlsolutionsgroup.com/wean-off-sql-profiler-part-1/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>Automating DBCC PAGE, part 2: Determining object names on pages</title>
		<link>https://sqlsolutionsgroup.com/automating-dbcc-page-part-2/</link>
					<comments>https://sqlsolutionsgroup.com/automating-dbcc-page-part-2/#respond</comments>
		
		<dc:creator><![CDATA[Wayne Sheffield]]></dc:creator>
		<pubDate>Tue, 03 Feb 2015 09:01:00 +0000</pubDate>
				<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[DBCC]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=1926</guid>

					<description><![CDATA[<p>In my last post, I demonstrated how using DBCC PAGE can be automated by using the &#8220;WITH TABLERESULTS&#8221; option. In this post, we will continue with another look at how this can be done. On a nice wintry day, your city ended up being covered in several feet of snow. During the course of the [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/automating-dbcc-page-part-2/">Automating DBCC PAGE, part 2: Determining object names on pages</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In <a href="https://sqlsolutionsgroup.com/automating-dbcc-page/">my last post</a>, I demonstrated how using DBCC PAGE can be automated by using the &#8220;WITH TABLERESULTS&#8221; option. In this post, we will continue with another look at how this can be done.</p>
<p>On a nice wintry day, your city ended up being covered in several feet of snow. During the course of the night, your own house had several power outages. Being concerned about your databases, you shoveled your way into your office, so that you could check on things. (Okay&#8230; actually you would just VPN in, but this is my story after all&#8230;)</p>
<p>Once you get into your server, you check the jobs and find that your job that runs DBCC CHECKDB has failed. Let’s assume that a power glitch has caused corruption in your database. In order to find out what all is affected, you run DBCC CHECKDB WITH NO_INFOMSGS, ALL_ERRORMSGS. But, this happens to be on your 2TB database, and it will take a while for CHECKDB to finish so that you can find the scope of corruption.</p>
<p>You&#8217;d really like to know what tables are affected without having to wait. Luckily(?), this corruption was recorded in msdb.dbo.suspect_pages, and having just recently read Paul Randal&#8217;s post at <a title="Read this post" href="https://www.sqlskills.com/blogs/paul/finding-table-name-page-id/" target="_blank" rel="noopener noreferrer">here</a>, we know we can use DBCC PAGE to determine this information. And, after having read my last blog post, you know that we can automate DBCC PAGE, so we can use our new friend “WITH TABLERESULTS” to find out what objects have been corrupted.</p>
<p>The suspect_pages table, documented <a title="Visit site" href="https://technet.microsoft.com/en-us/library/ms174425%28v=sql.110%29.aspx">here</a>, has three particular columns of interest: database_id, file_id and page_id. These correspond nicely to the first three parameters needed for DBCC PAGE. To automate this, we need to know what information we need to return off of the page – and from Paul’s post, we know that this is the field “METADATA: ObjectId”. For this code example, let’s assume that this corruption is on page 11 of the master database (just change “master” to the name of your 2TB database).</p>
<div id="wpshdo_33" class="wp-synhighlighter-outer"><div id="wpshdt_33" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_33"></a><a id="wpshat_33" class="wp-synhighlighter-title" href="#codesyntax_33"  onClick="javascript:wpsh_toggleBlock(33)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_33" onClick="javascript:wpsh_code(33)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_33" onClick="javascript:wpsh_print(33)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_33" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">IF</span> <span class="kw2">OBJECT_ID</span><span class="br0">&#40;</span><span class="st0">'tempdb.dbo.#DBCCPAGE'</span><span class="br0">&#41;</span> <span class="kw1">IS</span> <span class="sy0">NOT</span> <span class="sy0">NULL</span> <span class="kw1">DROP</span> <span class="kw1">TABLE</span> #DBCCPAGE;
<span class="kw1">CREATE</span> <span class="kw1">TABLE</span> #DBCCPAGE <span class="br0">&#40;</span>
       ParentObject <span class="kw1">VARCHAR</span><span class="br0">&#40;</span>255<span class="br0">&#41;</span>,
       <span class="br0">[</span><span class="kw1">OBJECT</span><span class="br0">]</span>     <span class="kw1">VARCHAR</span><span class="br0">&#40;</span>255<span class="br0">&#41;</span>,
       Field       <span class="kw1">VARCHAR</span><span class="br0">&#40;</span>255<span class="br0">&#41;</span>,
       <span class="br0">[</span><span class="kw1">VALUE</span><span class="br0">]</span>     <span class="kw1">VARCHAR</span><span class="br0">&#40;</span>255<span class="br0">&#41;</span><span class="br0">&#41;</span>;
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> #DBCCPAGE
<span class="kw1">EXECUTE</span> <span class="br0">&#40;</span><span class="st0">'DBCC PAGE ('</span><span class="st0">'master'</span><span class="st0">', 1, 11, 3) WITH TABLERESULTS;'</span><span class="br0">&#41;</span>;
&nbsp;
<span class="kw1">SELECT</span> schema_name <span class="sy0">=</span> OBJECT_SCHEMA_NAME<span class="br0">&#40;</span>ca.<span class="kw2">OBJECT_ID</span><span class="br0">&#41;</span>,
       <span class="kw2">OBJECT_NAME</span> <span class="sy0">=</span> <span class="kw2">OBJECT_NAME</span><span class="br0">&#40;</span>ca.<span class="kw2">OBJECT_ID</span><span class="br0">&#41;</span>
<span class="kw1">FROM</span>   #DBCCPAGE
       <span class="sy0">CROSS</span> APPLY <span class="br0">&#40;</span><span class="kw1">SELECT</span> <span class="kw1">CONVERT</span><span class="br0">&#40;</span><span class="kw1">INTEGER</span>, <span class="kw1">VALUE</span><span class="br0">&#41;</span><span class="br0">&#41;</span> ca<span class="br0">&#40;</span><span class="kw2">OBJECT_ID</span><span class="br0">&#41;</span>
<span class="kw1">WHERE</span>   Field <span class="sy0">=</span> <span class="st0">'Metadata: ObjectId'</span>;</pre></div></div>
<p>And there you go… you now know which object it is that has the corruption. In the same fashion, another interesting field that is returned is the IndexId – the Field value is “Metadata: IndexId”. It would be a similar exercise to grab that from this page also, an exercise that I’ll leave to you.</p>
<p>An automated method for getting the object from all suspect pages would entail encapsulating this logic into a cursor to spin through each row in the suspect_pages table (and I’ll even throw in getting the index_id also):</p>
<div id="wpshdo_34" class="wp-synhighlighter-outer"><div id="wpshdt_34" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_34"></a><a id="wpshat_34" class="wp-synhighlighter-title" href="#codesyntax_34"  onClick="javascript:wpsh_toggleBlock(34)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_34" onClick="javascript:wpsh_code(34)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_34" onClick="javascript:wpsh_print(34)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_34" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">IF</span> <span class="kw2">OBJECT_ID</span><span class="br0">&#40;</span><span class="st0">'tempdb.dbo.#DBCCPAGE'</span><span class="br0">&#41;</span> <span class="kw1">IS</span> <span class="sy0">NOT</span> <span class="sy0">NULL</span> <span class="kw1">DROP</span> <span class="kw1">TABLE</span> #DBCCPAGE;
<span class="kw1">IF</span> <span class="kw2">OBJECT_ID</span><span class="br0">&#40;</span><span class="st0">'tempdb.dbo.#SuspectObjects'</span><span class="br0">&#41;</span> <span class="kw1">IS</span> <span class="sy0">NOT</span> <span class="sy0">NULL</span> <span class="kw1">DROP</span> <span class="kw1">TABLE</span> #SuspectObjects;
<span class="kw1">CREATE</span> <span class="kw1">TABLE</span> #DBCCPAGE <span class="br0">&#40;</span>
        ParentObject    <span class="kw1">VARCHAR</span><span class="br0">&#40;</span>255<span class="br0">&#41;</span>,
        <span class="br0">[</span><span class="kw1">OBJECT</span><span class="br0">]</span>        <span class="kw1">VARCHAR</span><span class="br0">&#40;</span>255<span class="br0">&#41;</span>,
        Field           <span class="kw1">VARCHAR</span><span class="br0">&#40;</span>255<span class="br0">&#41;</span>,
        <span class="br0">[</span><span class="kw1">VALUE</span><span class="br0">]</span>         <span class="kw1">VARCHAR</span><span class="br0">&#40;</span>255<span class="br0">&#41;</span><span class="br0">&#41;</span>;
<span class="kw1">CREATE</span> <span class="kw1">TABLE</span> #SuspectObjects <span class="br0">&#40;</span>
        database_id     <span class="kw1">INTEGER</span>,
        <span class="kw2">FILE_ID</span>         <span class="kw1">INTEGER</span>,
        page_id         <span class="kw1">INTEGER</span>,
        <span class="kw2">OBJECT_ID</span>       <span class="kw1">INTEGER</span>,
        index_id        <span class="kw1">INTEGER</span><span class="br0">&#41;</span>;
<span class="kw1">DECLARE</span> @database_id    <span class="kw1">INTEGER</span>,
        @<span class="kw2">FILE_ID</span>        <span class="kw1">INTEGER</span>,
        @page_id        <span class="kw1">INTEGER</span>,
        @SQLCMD         <span class="kw1">NVARCHAR</span><span class="br0">&#40;</span><span class="kw2">MAX</span><span class="br0">&#41;</span>;
&nbsp;
<span class="kw1">DECLARE</span> cCrackSuspectPages <span class="kw1">CURSOR</span> <span class="kw1">LOCAL</span> FAST_F<span class="sy0">OR</span>WARD <span class="kw1">FOR</span>
<span class="kw1">SELECT</span>  <span class="st0">'EXECUTE ('</span><span class="st0">'DBCC PAGE ('</span> <span class="sy0">+</span>
            <span class="kw1">CONVERT</span><span class="br0">&#40;</span><span class="kw1">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">15</span><span class="br0">&#41;</span>, database_id<span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">', '</span> <span class="sy0">+</span>
            <span class="kw1">CONVERT</span><span class="br0">&#40;</span><span class="kw1">VARCHAR</span><span class="br0">&#40;</span>15<span class="br0">&#41;</span>, <span class="kw2">FILE_ID</span><span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">', '</span> <span class="sy0">+</span>
            <span class="kw1">CONVERT</span><span class="br0">&#40;</span><span class="kw1">VARCHAR</span><span class="br0">&#40;</span><span class="nu0">15</span><span class="br0">&#41;</span>, page_id<span class="br0">&#41;</span> <span class="sy0">+</span> <span class="st0">') WITH TABLERESULTS;'</span><span class="st0">');'</span>,
        database_id, <span class="kw2">FILE_ID</span>, page_id
<span class="kw1">FROM</span>    msdb.<span class="me1">dbo</span>.<span class="me1">suspect_pages</span>;
&nbsp;
<span class="kw1">OPEN</span> cCrackSuspectPages;
<span class="kw1">FETCH</span> <span class="kw1">NEXT</span> <span class="kw1">FROM</span> cCrackSuspectPages <span class="kw1">INTO</span> @SQLCMD, @database_id, @<span class="kw2">FILE_ID</span>, @page_id;
<span class="kw1">WHILE</span> <span class="kw2">@@FETCH_STATUS</span> <span class="sy0">=</span> 0
<span class="kw1">BEGIN</span>
    <span class="kw1">TRUNCATE</span> <span class="kw1">TABLE</span> #DBCCPAGE;
    <span class="kw1">INSERT</span> <span class="kw1">INTO</span> #DBCCPAGE <span class="kw1">EXECUTE</span> <span class="br0">&#40;</span>@SQLCMD<span class="br0">&#41;</span>;
&nbsp;
    <span class="kw1">INSERT</span> <span class="kw1">INTO</span> #SuspectObjects
            <span class="br0">&#40;</span>database_id,
             <span class="kw2">FILE_ID</span>,
             page_id,
             <span class="kw2">OBJECT_ID</span>,
             index_id
            <span class="br0">&#41;</span>
    <span class="kw1">SELECT</span>  @database_id, @<span class="kw2">FILE_ID</span>, @page_id,
            <span class="br0">&#40;</span><span class="kw1">SELECT</span> <span class="kw1">CONVERT</span><span class="br0">&#40;</span><span class="kw1">INTEGER</span>, <span class="kw1">VALUE</span><span class="br0">&#41;</span>
             <span class="kw1">FROM</span>   #DBCCPAGE dp
             <span class="kw1">WHERE</span>  dp.<span class="me1">Field</span> <span class="sy0">=</span> <span class="st0">'Metadata: ObjectId'</span><span class="br0">&#41;</span>,
            <span class="br0">&#40;</span><span class="kw1">SELECT</span> <span class="kw1">CONVERT</span><span class="br0">&#40;</span><span class="kw1">INTEGER</span>, <span class="kw1">VALUE</span><span class="br0">&#41;</span>
             <span class="kw1">FROM</span>   #DBCCPAGE dp
             <span class="kw1">WHERE</span>  dp.<span class="me1">Field</span> <span class="sy0">=</span> <span class="st0">'Metadata: IndexId'</span><span class="br0">&#41;</span>;
&nbsp;
    <span class="kw1">FETCH</span> <span class="kw1">NEXT</span> <span class="kw1">FROM</span> cCrackSuspectPages <span class="kw1">INTO</span> @SQLCMD, @database_id, @<span class="kw2">FILE_ID</span>, @page_id;
<span class="kw1">END</span>
<span class="kw1">CLOSE</span> cCrackSuspectPages;
<span class="kw1">DEALLOCATE</span> cCrackSuspectPages;
&nbsp;
<span class="kw1">SELECT</span>  database_name <span class="sy0">=</span> <span class="kw2">DB_NAME</span><span class="br0">&#40;</span>database_id<span class="br0">&#41;</span>,
        database_id,
        <span class="kw2">FILE_ID</span>,
        page_id,
        schema_name <span class="sy0">=</span> OBJECT_SCHEMA_NAME<span class="br0">&#40;</span><span class="kw2">OBJECT_ID</span>, database_id<span class="br0">&#41;</span>,
        <span class="kw2">OBJECT_NAME</span> <span class="sy0">=</span> <span class="kw2">OBJECT_NAME</span><span class="br0">&#40;</span><span class="kw2">OBJECT_ID</span>, database_id<span class="br0">&#41;</span>,
        index_id
<span class="kw1">FROM</span>    #SuspectObjects;</pre></div></div>
<p>If you happen to be on SQL 2012 or higher, this can be greatly simplified by using the new (undocumented) DMO function sys.dm_db_database_page_allocations (and it also takes away the need to crack the page using DBCC PAGE).</p>
<div id="wpshdo_35" class="wp-synhighlighter-outer"><div id="wpshdt_35" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_35"></a><a id="wpshat_35" class="wp-synhighlighter-title" href="#codesyntax_35"  onClick="javascript:wpsh_toggleBlock(35)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_35" onClick="javascript:wpsh_code(35)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_35" onClick="javascript:wpsh_print(35)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_35" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">SELECT</span>  database_name <span class="sy0">=</span> <span class="kw2">DB_NAME</span><span class="br0">&#40;</span>sp.<span class="me1">database_id</span><span class="br0">&#41;</span>,
        sp.<span class="me1">database_id</span>,
        sp.<span class="kw2">FILE_ID</span>,
        sp.<span class="me1">page_id</span>,
        schema_name <span class="sy0">=</span> OBJECT_SCHEMA_NAME<span class="br0">&#40;</span>dpa.<span class="kw2">OBJECT_ID</span>, sp.<span class="me1">database_id</span><span class="br0">&#41;</span>,
        <span class="kw2">OBJECT_NAME</span> <span class="sy0">=</span> <span class="kw2">OBJECT_NAME</span><span class="br0">&#40;</span>dpa.<span class="kw2">OBJECT_ID</span>, sp.<span class="me1">database_id</span><span class="br0">&#41;</span>,
        dpa.<span class="me1">index_id</span>
<span class="kw1">FROM</span>    msdb.<span class="me1">dbo</span>.<span class="me1">suspect_pages</span> sp
<span class="sy0">CROSS</span> APPLY sys.<span class="me1">dm_db_database_page_allocations</span><span class="br0">&#40;</span>sp.<span class="me1">database_id</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span>, <span class="sy0">NULL</span>, <span class="st0">'LIMITED'</span><span class="br0">&#41;</span> dpa
<span class="kw1">WHERE</span>   sp.<span class="kw2">FILE_ID</span> <span class="sy0">=</span> dpa.<span class="me1">allocated_page_file_id</span>
<span class="sy0">AND</span>     sp.<span class="me1">page_id</span> <span class="sy0">=</span> dpa.<span class="me1">allocated_page_page_id</span>;</pre></div></div>
<p>And there we go – yet another time when you might want to automate using DBCC PAGE. By now you should be able to see other uses for it – as long as you can get the database_id, file_id and page_id, you can automate the usage of it to retrieve the information that you are looking for.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/automating-dbcc-page-part-2/">Automating DBCC PAGE, part 2: Determining object names on pages</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlsolutionsgroup.com/automating-dbcc-page-part-2/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Persisting DBCC output data</title>
		<link>https://sqlsolutionsgroup.com/persisting-dbcc-output-data/</link>
					<comments>https://sqlsolutionsgroup.com/persisting-dbcc-output-data/#respond</comments>
		
		<dc:creator><![CDATA[Wayne Sheffield]]></dc:creator>
		<pubDate>Wed, 26 Nov 2014 13:10:01 +0000</pubDate>
				<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=1759</guid>

					<description><![CDATA[<p>Most of the DBCC commands return their results as textual output, even if you have SSMS configured to return result sets to a grid. This makes examining the output a manual process that is prone to errors. It sure would be nice if there was a way to return the output to a grid. If [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/persisting-dbcc-output-data/">Persisting DBCC output data</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Most of the DBCC commands return their results as textual output, even if you have SSMS configured to return result sets to a grid. This makes examining the output a manual process that is prone to errors. It sure would be nice if there was a way to return the output to a grid.</p>
<p>If we were to examine Books Online (BOL) for DBCC in 2000 (<a href="https://technet.microsoft.com/en-us/library/aa258281%28v=sql.80%29.aspx">https://technet.microsoft.com/en-us/library/aa258281%28v=sql.80%29.aspx</a>) and 2005 (<a href="https://msdn.microsoft.com/en-us/library/ms188796%28v=sql.90%29.aspx">https://msdn.microsoft.com/en-us/library/ms188796%28v=sql.90%29.aspx</a>), we would notice a section titled “Using DBCC Result Set Output”, with the phrase: “Many DBCC commands can produce output in tabular form by using the WITH TABLERESULTS option. This information can be loaded into a table for additional use.” This section has been removed from more recent versions, most likely because the results returned are not documented for the individual DBCC commands and are thus subject to change without notice.</p>
<p>Okay, let’s try this out. At the above BOL links, there are two DBCC commands that are documented to use the TABLERESULTS option: OPENTRAN and SHOWCONTIG. Testing all of the other DBCC commands shows that this option can also be used on the CHECKALLOC, CHECKDB, CHECKFILEGROUP and CHECKTABLE commands. I&#8217;m going to continue this post with using DBCC CHECKDB. If you check <a href="https://msdn.microsoft.com/en-us/library/ms176064.aspx">BOL</a>, it does not mention the TABLERESULTS option. Let’s first see the results we get without the TABLERESULTS option by running:<div id="wpshdo_36" class="wp-synhighlighter-outer"><div id="wpshdt_36" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_36"></a><a id="wpshat_36" class="wp-synhighlighter-title" href="#codesyntax_36"  onClick="javascript:wpsh_toggleBlock(36)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_36" onClick="javascript:wpsh_code(36)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_36" onClick="javascript:wpsh_print(36)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_36" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">DBCC</span> CHECKDB <span class="br0">&#40;</span>master<span class="br0">&#41;</span>;</pre></div></div></p>
<p>For the consistency check that is performed on the master database, I end up with over 300 lines of output. The key lines in the output are:<div id="wpshdo_37" class="wp-synhighlighter-outer"><div id="wpshdt_37" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_37"></a><a id="wpshat_37" class="wp-synhighlighter-title" href="#codesyntax_37"  onClick="javascript:wpsh_toggleBlock(37)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_37" onClick="javascript:wpsh_code(37)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_37" onClick="javascript:wpsh_print(37)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_37" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">…
CHECKDB found 0 allocation errors and 0 consistency errors in database 'master'.
…</pre></div></div>
And the final two lines:<div id="wpshdo_38" class="wp-synhighlighter-outer"><div id="wpshdt_38" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_38"></a><a id="wpshat_38" class="wp-synhighlighter-title" href="#codesyntax_38"  onClick="javascript:wpsh_toggleBlock(38)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_38" onClick="javascript:wpsh_code(38)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_38" onClick="javascript:wpsh_print(38)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_38" class="wp-synhighlighter-inner" style="display: block;"><pre class="text" style="font-family:monospace;">CHECKDB found 0 allocation errors and 0 consistency errors in database 'mssqlsystemresource'. 
DBCC execution completed. If DBCC printed error messages, contact your system administrator.</pre></div></div>(Notice that when we run CHECKDB against the master database, the hidden database mssqlsystemresource is also checked.)</p>
<p>If we were to modify the above statement to use the TABLERESULTS option:</p>
<div id="wpshdo_39" class="wp-synhighlighter-outer"><div id="wpshdt_39" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_39"></a><a id="wpshat_39" class="wp-synhighlighter-title" href="#codesyntax_39"  onClick="javascript:wpsh_toggleBlock(39)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_39" onClick="javascript:wpsh_code(39)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_39" onClick="javascript:wpsh_print(39)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_39" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">DBCC</span> CHECKDB <span class="br0">&#40;</span>master<span class="br0">&#41;</span> <span class="kw1">WITH</span> TABLERESULTS;</pre></div></div>
<p>we would actually get two result sets – one for the master database, and one for the hidden mssqlsystemresource database. Notice that the “DBCC execution completed…” line is not in either of the result sets – it is still displayed on the Messages tab.</p>
<p>Now, most people usually modify this command to suppress informational messages and to show all error messages (it defaults to “only” the first 200 error messages per object), so the command that is normally used would be:</p>
<div id="wpshdo_40" class="wp-synhighlighter-outer"><div id="wpshdt_40" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_40"></a><a id="wpshat_40" class="wp-synhighlighter-title" href="#codesyntax_40"  onClick="javascript:wpsh_toggleBlock(40)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_40" onClick="javascript:wpsh_code(40)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_40" onClick="javascript:wpsh_print(40)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_40" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">DBCC</span> CHECKDB <span class="br0">&#40;</span>master<span class="br0">&#41;</span> <span class="kw1">WITH</span> <span class="sy0">ALL</span>_ERR<span class="sy0">OR</span>MSGS, NO_<span class="sy0">IN</span>FOMSGS;</pre></div></div>
<p>And, hopefully, this only returns the message “Command completed successfully”. Let’s modify this command to use the TABLERESULTS option:</p>
<div id="wpshdo_41" class="wp-synhighlighter-outer"><div id="wpshdt_41" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_41"></a><a id="wpshat_41" class="wp-synhighlighter-title" href="#codesyntax_41"  onClick="javascript:wpsh_toggleBlock(41)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_41" onClick="javascript:wpsh_code(41)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_41" onClick="javascript:wpsh_print(41)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_41" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">DBCC</span> CHECKDB <span class="br0">&#40;</span>master<span class="br0">&#41;</span> <span class="kw1">WITH</span> <span class="sy0">ALL</span>_ERR<span class="sy0">OR</span>MSGS, NO_<span class="sy0">IN</span>FOMSGS, TABLERESULTS;</pre></div></div>
<p>If there is no corruption in the database, it still returns only the message “Command completed successfully”. However, if there is corruption, you will get a result set back. So, I’m now going to run this against a database (Lab) where I have engineered some corruption. First off, let’s run CHECKDB without the TABLERESULTS option to check the initial output:</p>
<div id="wpshdo_42" class="wp-synhighlighter-outer"><div id="wpshdt_42" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_42"></a><a id="wpshat_42" class="wp-synhighlighter-title" href="#codesyntax_42"  onClick="javascript:wpsh_toggleBlock(42)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_42" onClick="javascript:wpsh_code(42)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_42" onClick="javascript:wpsh_print(42)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_42" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">DBCC</span> CHECKDB <span class="br0">&#40;</span>Lab<span class="br0">&#41;</span> <span class="kw1">WITH</span> <span class="sy0">ALL</span>_ERR<span class="sy0">OR</span>MSGS, NO_<span class="sy0">IN</span>FOMSGS;</pre></div></div>
<div id="wpshdo_43" class="wp-synhighlighter-outer"><div id="wpshdt_43" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_43"></a><a id="wpshat_43" class="wp-synhighlighter-title" href="#codesyntax_43"  onClick="javascript:wpsh_toggleBlock(43)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_43" onClick="javascript:wpsh_code(43)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_43" onClick="javascript:wpsh_print(43)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_43" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;">Msg 8939, <span class="kw1">LEVEL</span> 16, <span class="kw1">STATE</span> 98, Line 1
<span class="kw1">TABLE</span> error: <span class="kw1">OBJECT</span> ID 885578193, <span class="kw1">INDEX</span> ID 0, partition ID 72057594039042048, alloc unit ID 72057594043432960 <span class="br0">&#40;</span>type In<span class="sy0">-</span><span class="kw1">ROW</span> <span class="kw1">DATA</span><span class="br0">&#41;</span>, page <span class="br0">&#40;</span>1:328<span class="br0">&#41;</span>. <span class="me1">Test</span> <span class="br0">&#40;</span>IS_OFF <span class="br0">&#40;</span>BUF_IOERR, pBUF<span class="sy0">-&amp;</span>gt;bstat<span class="br0">&#41;</span><span class="br0">&#41;</span> failed. <span class="kw1">VALUES</span> <span class="kw1">ARE</span> 133129 and <span class="sy0">-</span>4.
<span class="me1">Msg</span> 8928, <span class="kw1">LEVEL</span> 16, <span class="kw1">STATE</span> 1, Line 1
<span class="kw1">OBJECT</span> ID 885578193, <span class="kw1">INDEX</span> ID 0, partition ID 72057594039042048, alloc unit ID 72057594043432960 <span class="br0">&#40;</span>type In<span class="sy0">-</span><span class="kw1">ROW</span> <span class="kw1">DATA</span><span class="br0">&#41;</span>: Page <span class="br0">&#40;</span>1:328<span class="br0">&#41;</span> could not be processed. <span class="me1">See</span> other errors <span class="kw1">FOR</span> details.
<span class="me1">CHECKDB</span> <span class="kw1">FOUND</span> 0 allocation errors and 2 consistency errors in <span class="kw1">TABLE</span> <span class="st0">'Tally'</span> <span class="br0">&#40;</span><span class="kw1">OBJECT</span> ID 885578193<span class="br0">&#41;</span>.
<span class="me1">CHECKDB</span> <span class="kw1">FOUND</span> 0 allocation errors and 2 consistency errors in <span class="kw1">DATABASE</span> <span class="st0">'Lab'</span>.
<span class="me1">repair_allow_data_loss</span> <span class="kw1">IS</span> the minimum repair <span class="kw1">LEVEL</span> <span class="kw1">FOR</span> the errors <span class="kw1">FOUND</span> <span class="kw1">BY</span> <span class="kw1">DBCC</span> CHECKDB <span class="br0">&#40;</span>Lab<span class="br0">&#41;</span>.</pre></div></div>
<p>Here we can see that we indeed have corruption. Quickly now… is any of this corruption in a non-clustered index?</p>
<p>Running this statement with the TABLERESULTS option, we get a grid of the results:</p>
<div id="wpshdo_44" class="wp-synhighlighter-outer"><div id="wpshdt_44" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_44"></a><a id="wpshat_44" class="wp-synhighlighter-title" href="#codesyntax_44"  onClick="javascript:wpsh_toggleBlock(44)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_44" onClick="javascript:wpsh_code(44)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_44" onClick="javascript:wpsh_print(44)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_44" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">DBCC</span> CHECKDB <span class="br0">&#40;</span>Lab<span class="br0">&#41;</span> <span class="kw1">WITH</span> <span class="sy0">ALL</span>_ERR<span class="sy0">OR</span>MSGS, NO_<span class="sy0">IN</span>FOMSGS, TABLERESULTS;</pre></div></div>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/07/DBCCTABLE1.png" alt="DBCCTABLE1" /></p>
<p>(This screen shot is only showing some of the columns. Note that the message of the previous output for the minimum repair level is still on the Messages tab.)</p>
<p>As you can see, this already makes examining your errors somewhat easier. For instance, you can easily scroll through this output to see if the corruption is in non-clustered indexes (IndexId &gt; 1), where the corruption could easily be fixed by scripting out the index definition, dropping and then re-creating the non-clustered index. Suppose you had over 100 errors… you can see how much faster this would be.</p>
<p>If you recall, the BOL description says that this data can be loaded into a table for further processing. Furthermore, this post is about persisting DBCC output data, which implies storing it into a table. So, let’s make a table, put these results into the table, and then run a query against it. First off, let’s make a local temporary table to hold the results (you could put this into a permanent table if you so desire):</p>
<div id="wpshdo_45" class="wp-synhighlighter-outer"><div id="wpshdt_45" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_45"></a><a id="wpshat_45" class="wp-synhighlighter-title" href="#codesyntax_45"  onClick="javascript:wpsh_toggleBlock(45)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_45" onClick="javascript:wpsh_code(45)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_45" onClick="javascript:wpsh_print(45)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_45" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">IF</span> <span class="kw2">OBJECT_ID</span><span class="br0">&#40;</span><span class="st0">'tempdb.dbo.#DBCCCHECKDB'</span><span class="br0">&#41;</span> <span class="kw1">IS</span> <span class="sy0">NOT</span> <span class="sy0">NULL</span>
   <span class="kw1">DROP</span> <span class="kw1">TABLE</span> #DBCCCHECKDB;
<span class="kw1">CREATE</span> <span class="kw1">TABLE</span> #DBCCCHECKDB <span class="br0">&#40;</span>
   Error       <span class="kw1">INTEGER</span>,
   <span class="br0">[</span><span class="kw1">LEVEL</span><span class="br0">]</span>     <span class="kw1">INTEGER</span>,
   <span class="br0">[</span><span class="kw1">STATE</span><span class="br0">]</span>     <span class="kw1">INTEGER</span>,
   MessageText <span class="kw1">VARCHAR</span><span class="br0">&#40;</span><span class="kw2">MAX</span><span class="br0">&#41;</span>,
   RepairLevel <span class="kw1">VARCHAR</span><span class="br0">&#40;</span><span class="kw2">MAX</span><span class="br0">&#41;</span>,
   <span class="br0">[</span>Status<span class="br0">]</span>   <span class="kw1">INTEGER</span>,
   <span class="br0">[</span>DbId<span class="br0">]</span>     <span class="kw1">INTEGER</span>,
   DbFragId   <span class="kw1">INTEGER</span>,
   ObjectId   <span class="kw1">INTEGER</span>,
   IndexId     <span class="kw1">INTEGER</span>,
   PartitionId <span class="kw1">BIGINT</span>,
   AllocUnitId <span class="kw1">BIGINT</span>,
   RidDbId     <span class="kw1">INTEGER</span>,
   RidPruId   <span class="kw1">INTEGER</span>,
   <span class="br0">[</span><span class="kw1">FILE</span><span class="br0">]</span>     <span class="kw1">INTEGER</span>,
   <span class="br0">[</span>Page<span class="br0">]</span>     <span class="kw1">INTEGER</span>,
   Slot       <span class="kw1">INTEGER</span>,
   RefDbId     <span class="kw1">INTEGER</span>,
   RefPruId   <span class="kw1">INTEGER</span>,
   RefFile     <span class="kw1">INTEGER</span>,
   RefPage     <span class="kw1">INTEGER</span>,
   RefSlot     <span class="kw1">INTEGER</span>,
   Allocation <span class="kw1">INTEGER</span><span class="br0">&#41;</span>;</pre></div></div>
<p>Since the output from using TABLERESULTS isn’t documented, I’ve had to make some assumptions about the data types for these columns. To actually insert the output, we need to use the INSERT INTO … EXECUTE statement:</p>
<div id="wpshdo_46" class="wp-synhighlighter-outer"><div id="wpshdt_46" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_46"></a><a id="wpshat_46" class="wp-synhighlighter-title" href="#codesyntax_46"  onClick="javascript:wpsh_toggleBlock(46)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_46" onClick="javascript:wpsh_code(46)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_46" onClick="javascript:wpsh_print(46)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_46" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;">Transact<span class="sy0">-</span><span class="kw1">SQL</span>
<span class="kw1">INSERT</span> <span class="kw1">INTO</span> #DBCCCHECKDB
<span class="kw1">EXECUTE</span> <span class="br0">&#40;</span><span class="st0">'DBCC CHECKDB (Lab) WITH ALL_ERRORMSGS, NO_INFOMSGS, TABLERESULTS;'</span><span class="br0">&#41;</span>;</pre></div></div>
<p>Now that we have the data stored in a temporary table, let’s run a query against the table to return some aggregated data.</p>
<p>— Get the objects, # of errors, and repair level</p>
<div id="wpshdo_47" class="wp-synhighlighter-outer"><div id="wpshdt_47" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_47"></a><a id="wpshat_47" class="wp-synhighlighter-title" href="#codesyntax_47"  onClick="javascript:wpsh_toggleBlock(47)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_47" onClick="javascript:wpsh_code(47)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_47" onClick="javascript:wpsh_print(47)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_47" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;"><span class="kw1">SELECT</span> <span class="br0">[</span>DbId<span class="br0">]</span>,
       database_name <span class="sy0">=</span> <span class="kw2">DB_NAME</span><span class="br0">&#40;</span><span class="br0">[</span>DbId<span class="br0">]</span><span class="br0">&#41;</span>,
       ObjectId,
       <span class="br0">[</span><span class="kw1">OBJECT</span><span class="br0">]</span> <span class="sy0">=</span> OBJECT_SCHEMA_NAME<span class="br0">&#40;</span>ObjectId, <span class="br0">[</span>DbId<span class="br0">]</span><span class="br0">&#41;</span>
                   <span class="sy0">+</span> <span class="st0">'.'</span> <span class="sy0">+</span>
                   <span class="kw2">OBJECT_NAME</span><span class="br0">&#40;</span>ObjectId, <span class="br0">[</span>DbId<span class="br0">]</span><span class="br0">&#41;</span>,
       ErrorQty <span class="sy0">=</span> <span class="kw2">COUNT</span><span class="br0">&#40;</span><span class="sy0">*</span><span class="br0">&#41;</span>,
       RepairLvl <span class="sy0">=</span> <span class="kw2">MAX</span><span class="br0">&#40;</span>RepairLevel<span class="br0">&#41;</span>
<span class="kw1">FROM</span>   #DBCCCHECKDB
<span class="kw1">WHERE</span>   Error <span class="sy0">NOT</span> <span class="sy0">IN</span> <span class="br0">&#40;</span>8989,8990<span class="br0">&#41;</span>
<span class="kw1">GROUP</span> <span class="kw1">BY</span> <span class="br0">[</span>DbId<span class="br0">]</span>, ObjectId;</pre></div></div>
<p>With which I get the following results:</p>
<p><img loading="lazy" decoding="async" class="alignnone" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/07/DBCCTABLE2.png" alt="DBCC output data" width="671" height="65" /></p>
<p>So, there we go. <a href="https://en.wiktionary.org/wiki/easy_peasy">Easy-peasy</a>. By utilizing the TABLERESULTS option, the output of the DBCC CHECKDB command has been persisted into a table, and we are now able to run our own queries against that data. In the event that there is corruption in multiple indexes in a table, this query could easily be extended to get the number of errors in each index.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/persisting-dbcc-output-data/">Persisting DBCC output data</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlsolutionsgroup.com/persisting-dbcc-output-data/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>SQL Go Fast button</title>
		<link>https://sqlsolutionsgroup.com/sql-go-fast-button/</link>
					<comments>https://sqlsolutionsgroup.com/sql-go-fast-button/#respond</comments>
		
		<dc:creator><![CDATA[Wayne Sheffield]]></dc:creator>
		<pubDate>Tue, 15 Jul 2014 17:14:24 +0000</pubDate>
				<category><![CDATA[SQL Group]]></category>
		<category><![CDATA[#microsftcertifedmaster]]></category>
		<category><![CDATA[#microsftpartner]]></category>
		<category><![CDATA[#SQLAB]]></category>
		<category><![CDATA[#SQlatino]]></category>
		<category><![CDATA[#SQlatinoamerica]]></category>
		<category><![CDATA[#sqldatabase]]></category>
		<category><![CDATA[#sqldeveloper]]></category>
		<category><![CDATA[#SQLgroupie]]></category>
		<category><![CDATA[#sqlimer]]></category>
		<category><![CDATA[#sqlimerbymay]]></category>
		<category><![CDATA[#sqlinjection]]></category>
		<category><![CDATA[#sqlinternals]]></category>
		<category><![CDATA[#sqlite]]></category>
		<category><![CDATA[#sqlite3]]></category>
		<category><![CDATA[#SQLLearning]]></category>
		<category><![CDATA[#SQLMagazine]]></category>
		<category><![CDATA[#sqlmanagementstudio]]></category>
		<category><![CDATA[#sqlmanager]]></category>
		<category><![CDATA[#Sqlmap]]></category>
		<category><![CDATA[#sqlrun]]></category>
		<category><![CDATA[#sqlsaturday2017]]></category>
		<category><![CDATA[#sqlsatvienna]]></category>
		<category><![CDATA[#sqlserver]]></category>
		<category><![CDATA[#SQLserver2012]]></category>
		<category><![CDATA[#sqlserver2014]]></category>
		<category><![CDATA[#sqlserver2017]]></category>
		<category><![CDATA[#sqlserver2022]]></category>
		<category><![CDATA[#SQLServeronLinux]]></category>
		<category><![CDATA[#SQLsolutionsgroup]]></category>
		<category><![CDATA[#SQLTraining]]></category>
		<category><![CDATA[#SQLYog]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[SQLPASS]]></category>
		<category><![CDATA[SQLSaturday]]></category>
		<category><![CDATA[SSG]]></category>
		<guid isPermaLink="false">http://sqlsolutionsgroup.com/?p=1191</guid>

					<description><![CDATA[<p>When investigating a performance issue, the desired end result is already known… you need to make the queries run faster. It’s been my experience that most performance problems involve optimizing the query that is being run—sometimes the query needs a re-write to be more efficient, sometimes the tables being queried need a new (or modified) [&#8230;]</p>
<p>The post <a href="https://sqlsolutionsgroup.com/sql-go-fast-button/">SQL Go Fast button</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><span style="font-family: arial,helvetica,sans-serif; font-size: 12pt;">When investigating a performance issue, the desired end result is already known… you need to make the queries run faster. It’s been my experience that most performance problems involve optimizing the query that is being run—sometimes the query needs a re-write to be more efficient, sometimes the tables being queried need a new (or modified) index, and sometimes even the underlying database schema might need modifying. Before starting down any of these routes though, the first thing that I do is to check the configuration settings that make a difference. </span><span style="font-size: 12pt;"><span style="font-family: arial,helvetica,sans-serif;">Enter the SQL “Go Fast” button.</span></span></p>
<p><span style="font-family: arial,helvetica,sans-serif; font-size: 12pt;"><a href="https://sqlsolutionsgroup.com/wp-content/uploads/2014/07/turbobutton.png"><img loading="lazy" decoding="async" class="alignright wp-image-1220 size-full" src="https://sqlsolutionsgroup.com/wp-content/uploads/2014/07/turbobutton.png" alt="sql go fast button" width="292" height="217" /></a>In a CPU universe far, far away, there existed a particular series known as the 80486 processor, commonly called just 486. One particular issue that this series of CPUs had was that they were too fast for many games of that era (games that were coded for running off of CPU ticks). If you can get hold of one of these games and try to run it on a modern system, I wish you luck in even being able to press a single key before the game ends! But I digress… in order to counteract this issue, the processors had a feature where they could be slowed down. There were different ways that the CPU could be slowed down, but to interact with the computer user, there was a button on the case, known as the Turbo button, that would cycle the system between the low and high speeds of the CPU. Accidentally leaving this button in the low speed would slow down everything else on that computer; merely putting the system into the high speed would fix your performance problem.</span></p>
<h3><strong>So what does this mean for SQL Server?</strong></h3>
<p>In a CPU universe very, very close to you, today CPUs have a similar feature known as CPU Throttling. This feature, otherwise known as Dynamic Frequency Scaling, dynamically adjusts the CPU frequency on the fly. There are several benefits from doing this: less heat is generated, less power is consumed, and it can even make the systems quieter by also reducing the fan speed necessary to cool the computer. In a large server farm, reducing the heat means that cooling costs are also reduced. By operating the systems in a reduced state, the lower cooling costs coupled with the lower power requirements of the computers themselves can mount up to quite a savings. The dynamic portion of this feature is based upon system monitoring of CPU usage, and when it is low the CPU frequency is reduced, typically by 50% or more.</p>
<p>For most systems, the computer’s BIOS will monitor certain communication from the operating system, so the operating system can also send instructions for setting the CPU Throttling. In the Windows operating system, this is performed through the “Power Plan” setting. Since Windows Server 2008, the default power plan is “Balanced”, which allows Windows to reduce the CPU speed in an effort to balance power and performance.</p>
<h3><strong>How does this affect SQL Server?</strong></h3>
<p>SQL Server is one application that doesn’t typically use CPU resources so much as to make the CPU Throttling disengage and the CPUs to run at full power. This manifests itself in SQL Server as queries just taking longer to run. You might even have a new server that the application runs slower on than the server it is replacing. The solution is to simply disengage the CPU Throttling feature so that the CPUs will run at full speed constantly.</p>
<h3><strong>Detecting if CPU Throttling is engaged</strong></h3>
<p>CPU Throttling is implemented at both the hardware and software level. The Windows operating system can control CPU Throttling, and this can also be controlled from the BIOS. On some systems, the BIOS will ignore signals sent from Windows, so we will cover how to test and set both methods.</p>
<p>To see if your CPUs are running at rated speed, there are two methods. The first is the third-party tool CPU-Z, available at <a href="https://www.cpuid.com/softwares/cpu-z.html">https://www.cpuid.com/softwares/cpu-z.html</a>. In this screen shot (from the above link), you can see that the CPU is rated to run at 3.2GHz, but is actually running at almost 1.2GHz. This system is having its CPU throttled.</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/07/CPU-Z-CPU-Throttling.png" alt="CPU-Z-CPU-Throttling" /></p>
<p>Additionally, you can check out the WMI performance counters from the WIN32_Processor class. This DOS command returns the current and max clock speeds every second for 30 seconds; if the CurrentClockSpeed and the MaxClockSpeed differ significantly, then the CPU is being throttled.</p>
<div id="wpshdo_48" class="wp-synhighlighter-outer"><div id="wpshdt_48" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_48"></a><a id="wpshat_48" class="wp-synhighlighter-title" href="#codesyntax_48"  onClick="javascript:wpsh_toggleBlock(48)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_48" onClick="javascript:wpsh_code(48)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_48" onClick="javascript:wpsh_print(48)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_48" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;">WMIC CPU <span class="kw1">GET</span> CurrentClockSpeed, MaxClockSpeed <span class="sy0">/</span><span class="kw1">VALUE</span> <span class="sy0">/</span><span class="kw1">EVERY</span>:<span class="nu0">1</span> <span class="sy0">/</span>REPEAT:<span class="nu0">30</span></pre></div></div>
<p>In Windows, CPU Throttling is controlled from the Power Settings control panel applet. On a server running SQL Server, the desired setting is to be using the the “High Performance” power plan. You can do this from either the Power Settings control panel applet, or from the command line (starting with Windows Server 2008R2).</p>
<p>To start the Power Settings control panel applet, press the windows key + r key combination. In the run dialog box, enter powercfg.cpl. In the applet, you are looking for:</p>
<p><img decoding="async" src="https://blog.waynesheffield.com/wayne/wp-content/uploads/2016/07/Power-Plan.png" alt="Power-Plan" /></p>
<p>From a command prompt, you can simply run this DOS command:</p>
<div id="wpshdo_49" class="wp-synhighlighter-outer"><div id="wpshdt_49" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_49"></a><a id="wpshat_49" class="wp-synhighlighter-title" href="#codesyntax_49"  onClick="javascript:wpsh_toggleBlock(49)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_49" onClick="javascript:wpsh_code(49)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_49" onClick="javascript:wpsh_print(49)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_49" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;">powercfg <span class="sy0">-</span>GETACTIVESCHEME</pre></div></div>
<h3><strong>Disabling CPU Throttling</strong></h3>
<p>To change the power plan, in the control panel applet simply select the High Performance power plan (as shown above). From the DOS command prompt:</p>
<div id="wpshdo_50" class="wp-synhighlighter-outer"><div id="wpshdt_50" class="wp-synhighlighter-expanded"><table border="0" width="100%"><tr><td align="left" width="80%"><a name="#codesyntax_50"></a><a id="wpshat_50" class="wp-synhighlighter-title" href="#codesyntax_50"  onClick="javascript:wpsh_toggleBlock(50)" title="Click to show/hide code block">Source code</a></td><td align="right"><a href="#codesyntax_50" onClick="javascript:wpsh_code(50)" title="Show code only"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/code.png" /></a>&nbsp;<a href="#codesyntax_50" onClick="javascript:wpsh_print(50)" title="Print code"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/printer.png" /></a>&nbsp;<a href="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/About.html" target="_blank" title="Show plugin information"><img decoding="async" border="0" style="border: 0 none" src="https://sqlsolutionsgroup.com/wp-content/plugins/wp-synhighlight/themes/default/images/info.gif" /></a>&nbsp;</td></tr></table></div><div id="wpshdi_50" class="wp-synhighlighter-inner" style="display: block;"><pre class="tsql" style="font-family:monospace;">powercfg <span class="sy0">-</span>SETACTIVE SCHEME_M<span class="sy0">IN</span></pre></div></div>
<p>Changing the power plan takes effect immediately, so you should immediately see your CPUs running at full speed. This means that you can change the power plan on your SQL Server without requiring an outage. If, however, this does not return your CPUs to full speed, then the BIOS is overriding the Windows setting, and you will need to reboot to go into the BIOS to disable it, and once changed reboot again. Different BIOS manufacturers call CPU Throttling / Dynamic Frequency Scaling by different names, so you will need to investigate your server manufacturer’s web site to determine what it is called. This will obviously cause an outage during this time, so this needs to be a scheduled maintenance action.</p>
<h3><strong>What kind of difference does the SQL &#8220;go fast&#8221; button make?</strong></h3>
<p>This isn’t the fix for all queries, but it should help out pretty dramatically. For a system replacing another, it should return the application to pre-update performance, if not surpassing it.</p>
<p>Recently, I had the opportunity to test out the performance impact of changing the power plan from Balanced to High Performance. Prior to changing the power plan, a set of queries was running in 7 hours, 45 minutes. After changing just the power plan to High Performance, this set of queries is now running in 3 hours, 55 minutes. That’s almost ½ the time, from this simple setting change.</p>
<p>Just remember… SQL Server is not “green”. It needs full power.</p>
<p>And now you know about SQL Server’s hidden “Go Fast” button.</p>
<p>The post <a href="https://sqlsolutionsgroup.com/sql-go-fast-button/">SQL Go Fast button</a> appeared first on <a href="https://sqlsolutionsgroup.com">SQL Solutions Group</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlsolutionsgroup.com/sql-go-fast-button/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
