<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:pingback="http://madskills.com/public/xml/rss/module/pingback/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" version="2.0">
  <channel>
    <title>My torn data pages</title>
    <link>http://resquel.com/ssb/</link>
    <description>ReSQueL -- Blog</description>
    <language>en-us</language>
    <copyright>ReSQueL</copyright>
    <lastBuildDate>Fri, 03 Feb 2012 11:38:29 GMT</lastBuildDate>
    <generator>newtelligence dasBlog 2.3.9074.18820</generator>
    <managingEditor>query@resquel.com</managingEditor>
    <webMaster>query@resquel.com</webMaster>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=cfce3f27-6ffa-4c6b-9e18-390dc8e4534c</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,cfce3f27-6ffa-4c6b-9e18-390dc8e4534c.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,cfce3f27-6ffa-4c6b-9e18-390dc8e4534c.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=cfce3f27-6ffa-4c6b-9e18-390dc8e4534c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When performing maintenance to a cluster, as in adding an SQL Server virtual to a
node or removing it from a node, setup may fail for a number of reasons. And once
something went wrong, things keep getting wrong, even if you verified the <a title="Add / Remove cluster nodes SQL Server 2005" href="http://msdn.microsoft.com/en-us/library/ms191545(v=sql.90).aspx" target="_blank">procedure</a> and
you corrected the cause of the error.
</p>
        <p>
Well, the procedure didn't tell you everything. First of all, you should only be logged
in to the node where you are running the setup. Logoff from all other nodes. The reason
is that SQL Server setup will create a Windows Scheduled task on the other nodes of
the cluster for you, under your account. Having an interactive session (you being
logged in) and logging in as a batch job (the setup task) with the same account, may
lead to unexpected events (and thus setup failing). Obviously Task Scheduler should
be running to ;-). To be honest, Microsoft does tell you about the scheduler and the
logging off in a <a title="Error mesages with setup clustered SQL Server 2005" href="http://support.microsoft.com/kb/910851" target="_blank">KB
article</a> (let's be nice and add some community content).
</p>
        <p>
Where it does get mean in the KB article, is when setup keeps failing (and it most
likely will); <strong>restart all remote nodes</strong>!!! The most likely reason
for the setup failing this time, is a running setup.exe from a previous failed attempt.
This is what may cause the access denied for the scheduled task (or check that your
account is local admin on every node and has the login as a service permission). Here
is how to avoid having to reboot; 
</p>
        <ul>
          <li>
Exit the setup on the local node.</li>
          <li>
Check the scheduled tasks on the remote nodes. Some are running: stop them, then delete
them. Some have failed, delete them</li>
          <li>
Next, check the processes on all nodes, there may be one or two setup.exe processes
from SQL Setup active on the nodes, kill those processes.</li>
          <li>
Don’t forget to log off from the remote nodes.</li>
        </ul>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=cfce3f27-6ffa-4c6b-9e18-390dc8e4534c" />
      </body>
      <title>SQL Server 2005 cluster modifications failing</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,cfce3f27-6ffa-4c6b-9e18-390dc8e4534c.aspx</guid>
      <link>http://resquel.com/ssb/2012/02/03/SQLServer2005ClusterModificationsFailing.aspx</link>
      <pubDate>Fri, 03 Feb 2012 11:38:29 GMT</pubDate>
      <description>&lt;p&gt;
When performing maintenance to a cluster, as in adding an SQL Server virtual to a
node or removing it from a node, setup may fail for a number of reasons. And once
something went wrong, things keep getting wrong, even if you verified the &lt;a title="Add / Remove cluster nodes SQL Server 2005" href="http://msdn.microsoft.com/en-us/library/ms191545(v=sql.90).aspx" target="_blank"&gt;procedure&lt;/a&gt; and
you corrected the cause of the error.
&lt;/p&gt;
&lt;p&gt;
Well, the procedure didn't tell you everything. First of all, you should only be logged
in to the node where you are running the setup. Logoff from all other nodes. The reason
is that SQL Server setup will create a Windows Scheduled task on the other nodes of
the cluster for you, under your account. Having an interactive session (you being
logged in) and logging in as a batch job (the setup task) with the same account, may
lead to unexpected events (and thus setup failing). Obviously Task Scheduler should
be running to ;-). To be honest, Microsoft does tell you about the scheduler and the
logging off in a &lt;a title="Error mesages with setup clustered SQL Server 2005" href="http://support.microsoft.com/kb/910851" target="_blank"&gt;KB
article&lt;/a&gt; (let's be nice and add some community content).
&lt;/p&gt;
&lt;p&gt;
Where it does get mean in the KB article, is when setup keeps failing (and it most
likely will); &lt;strong&gt;restart all remote nodes&lt;/strong&gt;!!! The most likely reason
for the setup failing this time, is a running setup.exe from a previous failed attempt.
This is what may cause the access denied for the scheduled task (or check that your
account is local admin on every node and has the login as a service permission). Here
is how to avoid having to reboot; 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Exit the setup on the local node.&lt;/li&gt;
&lt;li&gt;
Check the scheduled tasks on the remote nodes. Some are running: stop them, then delete
them. Some have failed, delete them&lt;/li&gt;
&lt;li&gt;
Next, check the processes on all nodes, there may be one or two setup.exe processes
from SQL Setup active on the nodes, kill those processes.&lt;/li&gt;
&lt;li&gt;
Don’t forget to log off from the remote nodes.&lt;/li&gt;
&lt;/ul&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=cfce3f27-6ffa-4c6b-9e18-390dc8e4534c" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,cfce3f27-6ffa-4c6b-9e18-390dc8e4534c.aspx</comments>
      <category>Cluster</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=146b6b2c-9edd-4168-9773-ae9da4bc2983</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,146b6b2c-9edd-4168-9773-ae9da4bc2983.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,146b6b2c-9edd-4168-9773-ae9da4bc2983.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=146b6b2c-9edd-4168-9773-ae9da4bc2983</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Character based data types, especially the var types varchar and nvarchar allow to
declare a really big amount and just use a tiny bit and it doesn’t even hurt in SQL
Server. However, it may in the application. Today I was working with a customer for
the implementation of some third party software that would be doing analysis on the
data. It would load the really over-dimensioned (n)varchar types in something like
a (n)char.... you get the picture, huge amounts of wasted space. So from the databases
side, a bit of restructuring was desirable. To see how much we can actually save,
I created query to report on the character usage;
</p>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">CREATE</font>
          </font>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#0000ff">TABLE</font>
            </span>
            <font color="#000000"> ##char_columns</font>
            <span style="color: ">
              <font color="#0000ff">
              </font>
            </span>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">(</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#000000">
            <font style="font-size: 10pt">
              <br />
[SCHEMA] </font>
          </font>
          <font style="font-size: 10pt">
            <span style="color: ">
              <font color="#0000ff">VARCHAR</font>
              <font color="#808080">(</font>
            </span>
            <font color="#000000">128</font>
            <span style="color: ">
              <font color="#808080">)</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL,</font>
          </span>
          <font color="#000000">
            <font style="font-size: 10pt">
              <br />
[TABLE] </font>
          </font>
          <font style="font-size: 10pt">
            <span style="color: ">
              <font color="#0000ff">VARCHAR</font>
              <font color="#808080">(</font>
            </span>
            <font color="#000000">128</font>
            <span style="color: ">
              <font color="#808080">)</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL,</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#000000">
            <font style="font-size: 10pt">
              <br />
[COLUMN] </font>
          </font>
          <font style="font-size: 10pt">
            <span style="color: ">
              <font color="#0000ff">VARCHAR</font>
              <font color="#808080">(</font>
            </span>
            <font color="#000000">128</font>
            <span style="color: ">
              <font color="#808080">)</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL,</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#000000">
            <font style="font-size: 10pt">
              <br />
[data_type] </font>
          </font>
          <font style="font-size: 10pt">
            <span style="color: ">
              <font color="#0000ff">VARCHAR</font>
              <font color="#808080">(</font>
            </span>
            <font color="#000000">128</font>
            <span style="color: ">
              <font color="#808080">)</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL,</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#000000">
            <font style="font-size: 10pt">
              <br />
[max_length] </font>
          </font>
          <font style="font-size: 10pt">
            <span style="color: ">
              <font color="#0000ff">INT</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL,</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#000000">
            <font style="font-size: 10pt">
              <br />
[is_replicated] </font>
          </font>
          <font style="font-size: 10pt">
            <span style="color: ">
              <font color="#0000ff">BIT</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL,</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#000000">
            <font style="font-size: 10pt">
              <br />
[actual_max_length] </font>
          </font>
          <font style="font-size: 10pt">
            <span style="color: ">
              <font color="#0000ff">INT</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL,</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#000000">
            <font style="font-size: 10pt">
              <br />
[row_count] </font>
          </font>
          <font style="font-size: 10pt">
            <span style="color: ">
              <font color="#0000ff">BIGINT</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL,</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#000000">
            <font style="font-size: 10pt">
              <br />
[not_null_count] </font>
          </font>
          <font style="font-size: 10pt">
            <span style="color: ">
              <font color="#0000ff">BIGINT</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL)</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
INSERT</font>
          </font>
          <font style="font-size: 10pt" color="#000000"> ##char_columns</font>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
SELECT</font>
          </font>
          <font style="font-size: 10pt">
            <font color="#000000"> s</font>
            <span style="color: ">
              <font color="#808080">.</font>
            </span>
            <font color="#000000">name </font>
            <span style="color: ">
              <font color="#0000ff">AS</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff0000">'schema'</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <font style="font-size: 10pt">
            <font color="#000000">
              <br />
t</font>
            <span style="color: ">
              <font color="#808080">.</font>
            </span>
            <font color="#000000">name </font>
            <span style="color: ">
              <font color="#0000ff">AS</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff0000">'table'</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <font style="font-size: 10pt">
            <font color="#000000">
              <br />
c</font>
            <span style="color: ">
              <font color="#808080">.</font>
            </span>
            <font color="#000000">name </font>
            <span style="color: ">
              <font color="#0000ff">AS</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff0000">'column'</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <font style="font-size: 10pt">
            <font color="#000000">
              <br />
y</font>
            <span style="color: ">
              <font color="#808080">.</font>
            </span>
            <font color="#000000">name </font>
            <span style="color: ">
              <font color="#0000ff">AS</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff0000">'data_type'</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <font style="font-size: 10pt">
            <font color="#000000">
              <br />
c</font>
            <span style="color: ">
              <font color="#808080">.</font>
            </span>
            <font color="#000000">max_length </font>
          </font>
          <span>
          </span>
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
              <br />
c</font>
            <span style="color: ">
              <font color="#808080">.</font>
            </span>
            <font color="#000000">is_replicated </font>
          </font>
          <span>
          </span>
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt" color="#000000">
            <br />
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt" color="#000000">
            <br />
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt" color="#000000">
            <br />
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
FROM</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#008000">sys</font>
              <font color="#808080">.</font>
              <font color="#008000">tables</font>
            </span>
            <font color="#000000"> t</font>
          </font>
          <span>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">
              <br />
INNER</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">JOIN</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#008000">sys</font>
              <font color="#808080">.</font>
              <font color="#008000">schemas</font>
            </span>
            <font color="#000000"> s </font>
            <span style="color: ">
              <font color="#0000ff">ON</font>
            </span>
            <font color="#000000"> t</font>
            <span style="color: ">
              <font color="#808080">.</font>
              <font color="#ff00ff">schema_id</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">=</font>
            </span>
            <font color="#000000"> s</font>
            <span style="color: ">
              <font color="#808080">.</font>
            </span>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff00ff">schema_id</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">
              <br />
INNER</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">JOIN</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#008000">sys</font>
              <font color="#808080">.</font>
              <font color="#008000">columns</font>
            </span>
            <font color="#000000"> c </font>
            <span style="color: ">
              <font color="#0000ff">ON</font>
            </span>
            <font color="#000000"> t</font>
            <span style="color: ">
              <font color="#808080">.</font>
              <font color="#ff00ff">object_id</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">=</font>
            </span>
            <font color="#000000"> c</font>
            <span style="color: ">
              <font color="#808080">.</font>
            </span>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff00ff">object_id</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">
              <br />
INNER</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">JOIN</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#008000">sys</font>
              <font color="#808080">.</font>
              <font color="#008000">types</font>
            </span>
            <font color="#000000"> y </font>
            <span style="color: ">
              <font color="#0000ff">ON</font>
            </span>
            <font color="#000000"> c</font>
            <span style="color: ">
              <font color="#808080">.</font>
            </span>
            <font color="#000000">system_type_id </font>
            <span style="color: ">
              <font color="#808080">=</font>
            </span>
            <font color="#000000"> y</font>
            <span style="color: ">
              <font color="#808080">.</font>
            </span>
            <font color="#000000">system_type_id</font>
          </font>
          <span>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
WHERE</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000"> t</font>
            <span style="color: ">
              <font color="#808080">.</font>
              <font color="#0000ff">type</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">=</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff0000">'U'</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">AND</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000"> y</font>
            <span style="color: ">
              <font color="#808080">.</font>
            </span>
            <font color="#000000">name </font>
            <span style="color: ">
              <font color="#808080">IN</font>
              <font color="#0000ff">
              </font>
              <font color="#808080">(</font>
              <font color="#ff0000">'varchar'</font>
              <font color="#808080">,</font>
              <font color="#ff0000">'char'</font>
              <font color="#808080">,</font>
              <font color="#ff0000">'nchar'</font>
              <font color="#808080">,</font>
              <font color="#ff0000">'nvarchar'</font>
            </span>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">)</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
WHILE </font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#808080">(</font>
            <span>
            </span>
            <font color="#0000ff">SELECT</font>
            <span>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#ff00ff">COUNT</font>
              <font color="#808080">(*)</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#0000ff">FROM</font>
            </span>
            <font color="#000000"> ##char_columns </font>
            <span style="color: ">
              <font color="#0000ff">WHERE</font>
            </span>
            <font color="#000000"> actual_max_length </font>
            <span style="color: ">
              <font color="#808080">IS</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">NULL)</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">&gt;</font>
            </span>
            <font color="#000000"> 0</font>
          </font>
          <span>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
BEGIN</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt" color="#000000">
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#0000ff">TRY</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
DECLARE</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000"> @sql </font>
            <span style="color: ">
              <font color="#0000ff">NVARCHAR</font>
              <font color="#808080">(</font>
            </span>
            <font color="#000000">4000</font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">)</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt" color="#000000"> @actual_max_length </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#0000ff">INT</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000"> @column </font>
            <span style="color: ">
              <font color="#0000ff">VARCHAR</font>
              <font color="#808080">(</font>
            </span>
            <font color="#000000">128</font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">)</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000"> @schema </font>
            <span style="color: ">
              <font color="#0000ff">VARCHAR</font>
              <font color="#808080">(</font>
            </span>
            <font color="#000000">128</font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">)</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#808080">
            <font style="font-size: 10pt">,</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000"> @table </font>
            <span style="color: ">
              <font color="#0000ff">VARCHAR</font>
              <font color="#808080">(</font>
            </span>
            <font color="#000000">128</font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">)</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
SELECT</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#0000ff">TOP</font>
            </span>
            <font color="#000000"> 1
@schema </font>
            <span style="color: ">
              <font color="#808080">=</font>
            </span>
            <font color="#000000"> [SCHEMA]</font>
            <span style="color: ">
              <font color="#808080">,</font>
            </span>
            <font color="#000000"> @table </font>
            <span style="color: ">
              <font color="#808080">=</font>
            </span>
            <font color="#000000"> [TABLE]</font>
            <span style="color: ">
              <font color="#808080">,</font>
            </span>
            <font color="#000000"> @column </font>
            <span style="color: ">
              <font color="#808080">=</font>
            </span>
            <font color="#000000"> [COLUMN] 
<br /></font>
          </font>
          <font color="#0000ff">
            <font style="font-size: 10pt">FROM</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt" color="#000000"> ##char_columns</font>
          <span>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
WHERE</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000"> actual_max_length </font>
            <span style="color: ">
              <font color="#808080">IS</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">NULL</font>
          </span>
        </font>
        <font face="Courier New">
          <font style="font-size: 10pt" color="#008000">
            <br />
-- Set the actual_max_length to -1 if a column has only null values or no rows.</font>
        </font>
        <span>
          <br />
        </span>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">SELECT</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000"> @sql </font>
            <span style="color: ">
              <font color="#808080">=</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff0000">'UPDATE
##char_columns 
<br /></font>
          </span>
          <font color="#ff0000">
            <font style="font-size: 10pt">SET [actual_max_length]
= (SELECT ISNULL(MAX(LEN(['</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @column </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#ff0000">'])),-1)
FROM ['</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @schema </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#ff0000">'].['</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @table </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff0000">']) 
<br />
, </font>
          </span>
          <font color="#ff0000">
            <font style="font-size: 10pt">     
[row_count] = (SELECT COUNT_BIG(*) FROM ['</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @schema </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#ff0000">'].['</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @table </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff0000">']) 
<br />
, </font>
          </span>
          <font color="#ff0000">
            <font style="font-size: 10pt">     
[not_null_count] = (SELECT COUNT_BIG(['</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @column </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#ff0000">'])
FROM ['</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @schema </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#ff0000">'].['</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @table </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font color="#ff0000">
              <font style="font-size: 10pt">'])  
<br /></font>
            </font>
          </span>
          <font color="#ff0000">
            <font style="font-size: 10pt">WHERE [schema]
= '''</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @schema </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff0000">''' 
<br />
 </font>
          </span>
          <font color="#ff0000">
            <font style="font-size: 10pt">     
AND [table] = '''</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @table </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#ff0000">''' 
<br />
 </font>
          </span>
          <font color="#ff0000">
            <font style="font-size: 10pt">      <span style="line-height: 12pt; font-family: ; color: ">AND
[column] = '''</span></font>
          </font>
          <span style="line-height: 12pt; font-family: ">
            <font style="font-size: 10pt">
              <font color="#000000">
              </font>
              <span style="color: ">
                <font color="#808080">+</font>
              </span>
              <font color="#000000"> @column </font>
              <span style="color: ">
                <font color="#808080">+</font>
              </span>
              <font color="#000000">
              </font>
            </font>
            <span style="color: ">
              <font style="font-size: 10pt" color="#ff0000">''''</font>
            </span>
          </span>
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
EXEC</font>
          </font>
          <font style="font-size: 10pt">
            <font color="#808080">(</font>
            <span>
            </span>
            <font color="#000000">@sql</font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">)</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
END</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt" color="#000000">
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#0000ff">TRY</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
BEGIN</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt" color="#000000">
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#0000ff">CATCH</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
DECLARE</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000"> @err_num </font>
            <span style="color: ">
              <font color="#0000ff">NVARCHAR</font>
              <font color="#808080">(</font>
            </span>
            <font color="#000000">20</font>
            <span style="color: ">
              <font color="#808080">),</font>
            </span>
            <font color="#000000"> @err_msg </font>
            <span style="color: ">
              <font color="#0000ff">NVARCHAR</font>
              <font color="#808080">(</font>
            </span>
            <font color="#000000">2048</font>
            <span style="color: ">
              <font color="#808080">),</font>
            </span>
            <font color="#000000"> @full_msg </font>
            <span style="color: ">
              <font color="#0000ff">NVARCHAR</font>
              <font color="#808080">(</font>
            </span>
            <font color="#000000">2048</font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">)</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
SELECT</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000"> @err_num </font>
            <span style="color: ">
              <font color="#808080">=</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#ff00ff">ERROR_NUMBER</font>
              <font color="#808080">(),</font>
            </span>
            <font color="#000000"> @err_msg </font>
            <span style="color: ">
              <font color="#808080">=</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#ff00ff">ERROR_MESSAGE</font>
            </span>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">() 
<br /></font>
          </span>
          <font color="#0000ff">
            <font style="font-size: 10pt">SELECT</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000"> @full_msg </font>
            <span style="color: ">
              <font color="#808080">=</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#ff0000">'ERROR:
'</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @err_num </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#ff0000">'
DESCR: '</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @err_msg </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#ff0000">'
QUERY: '</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">+</font>
            </span>
            <font color="#000000"> @sql</font>
          </font>
          <span>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
RAISERROR</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#808080">(</font>
            <span>
            </span>
            <font color="#000000">@full_msg</font>
            <span style="color: ">
              <font color="#808080">,</font>
            </span>
            <font color="#000000">16</font>
            <span style="color: ">
              <font color="#808080">,</font>
            </span>
            <font color="#000000">0</font>
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#808080">)</font>
          </span>
        </font>
        <font face="Courier New">
          <font style="font-size: 10pt" color="#0000ff">
            <br />
BREAK</font>
        </font>
        <span>
        </span>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
END</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt" color="#000000">
          </font>
          <span style="color: ">
            <font style="font-size: 10pt" color="#0000ff">CATCH</font>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
SELECT</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#808080">*</font>
            </span>
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#0000ff">FROM</font>
            </span>
            <font color="#000000"> ##char_columns</font>
          </font>
          <span>
          </span>
        </font>
        <font face="Courier New">
          <font color="#0000ff">
            <font style="font-size: 10pt">
              <br />
DROP</font>
          </font>
          <span>
          </span>
          <font style="font-size: 10pt">
            <font color="#000000">
            </font>
            <span style="color: ">
              <font color="#0000ff">TABLE</font>
            </span>
            <font color="#000000"> ##char_columns</font>
          </font>
          <span>
          </span>
        </font>
        <p>
Be aware that varchar(max) and nvarchar(max) will give a max_length of –1 and that
actual_max_length is –1 if no rows exist in the table or the column only contains
NULLs (refer to the row_count and not_null_count to determine the situation).
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=146b6b2c-9edd-4168-9773-ae9da4bc2983" />
      </body>
      <title>Report how much (var)char you actually use</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,146b6b2c-9edd-4168-9773-ae9da4bc2983.aspx</guid>
      <link>http://resquel.com/ssb/2011/10/25/ReportHowMuchVarcharYouActuallyUse.aspx</link>
      <pubDate>Tue, 25 Oct 2011 20:37:16 GMT</pubDate>
      <description>&lt;p&gt;
Character based data types, especially the var types varchar and nvarchar allow to
declare a really big amount and just use a tiny bit and it doesn’t even hurt in SQL
Server. However, it may in the application. Today I was working with a customer for
the implementation of some third party software that would be doing analysis on the
data. It would load the really over-dimensioned (n)varchar types in something like
a (n)char.... you get the picture, huge amounts of wasted space. So from the databases
side, a bit of restructuring was desirable. To see how much we can actually save,
I created query to report on the character usage;
&lt;/p&gt;
&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;CREATE&lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;TABLE&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; ##char_columns&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#000000"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
[SCHEMA] &lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;VARCHAR&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;128&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL,&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
[TABLE] &lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;VARCHAR&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;128&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL,&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#000000"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
[COLUMN] &lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;VARCHAR&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;128&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL,&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#000000"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
[data_type] &lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;VARCHAR&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;128&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL,&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#000000"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
[max_length] &lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;INT&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL,&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#000000"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
[is_replicated] &lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;BIT&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL,&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#000000"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
[actual_max_length] &lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;INT&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL,&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#000000"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
[row_count] &lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;BIGINT&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL,&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#000000"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
[not_null_count] &lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;BIGINT&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL)&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
INSERT&lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt" color="#000000"&gt; ##char_columns&lt;/font&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
SELECT&lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; s&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;name &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;AS&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff0000"&gt;'schema'&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; 
&lt;br /&gt;
t&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;name &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;AS&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff0000"&gt;'table'&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; 
&lt;br /&gt;
c&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;name &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;AS&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff0000"&gt;'column'&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; 
&lt;br /&gt;
y&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;name &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;AS&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff0000"&gt;'data_type'&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; 
&lt;br /&gt;
c&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;max_length &lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; 
&lt;br /&gt;
c&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;is_replicated &lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt" color="#000000"&gt; 
&lt;br /&gt;
&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt" color="#000000"&gt; 
&lt;br /&gt;
&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt" color="#000000"&gt; 
&lt;br /&gt;
&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
FROM&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#008000"&gt;sys&lt;/font&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;font color="#008000"&gt;tables&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; t&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
INNER&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;JOIN&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#008000"&gt;sys&lt;/font&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;font color="#008000"&gt;schemas&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; s &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;ON&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; t&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;font color="#ff00ff"&gt;schema_id&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;=&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; s&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff00ff"&gt;schema_id&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
INNER&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;JOIN&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#008000"&gt;sys&lt;/font&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;font color="#008000"&gt;columns&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; c &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;ON&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; t&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;font color="#ff00ff"&gt;object_id&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;=&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; c&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff00ff"&gt;object_id&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
INNER&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;JOIN&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#008000"&gt;sys&lt;/font&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;font color="#008000"&gt;types&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; y &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;ON&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; c&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;system_type_id &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;=&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; y&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;system_type_id&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
WHERE&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; t&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;font color="#0000ff"&gt;type&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;=&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff0000"&gt;'U'&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;AND&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; y&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;name &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;IN&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff0000"&gt;'varchar'&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;'char'&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;'nchar'&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;'nvarchar'&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
WHILE &lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font color="#0000ff"&gt;SELECT&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff00ff"&gt;COUNT&lt;/font&gt;&lt;font color="#808080"&gt;(*)&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;FROM&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; ##char_columns &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;WHERE&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; actual_max_length &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;IS&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;NULL)&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;&amp;gt;&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; 0&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
BEGIN&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt" color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;TRY&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
DECLARE&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; @sql &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;NVARCHAR&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;4000&lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt" color="#000000"&gt; @actual_max_length &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;INT&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; @column &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;VARCHAR&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;128&lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; @schema &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;VARCHAR&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;128&lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;&lt;font style="font-size: 10pt"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; @table &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;VARCHAR&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;128&lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
SELECT&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;TOP&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; 1
@schema &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;=&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; [SCHEMA]&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @table &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;=&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; [TABLE]&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @column &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;=&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; [COLUMN] 
&lt;br /&gt;
&lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;FROM&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt" color="#000000"&gt; ##char_columns&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
WHERE&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; actual_max_length &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;IS&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;NULL&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font style="font-size: 10pt" color="#008000"&gt; 
&lt;br /&gt;
-- Set the actual_max_length to -1 if a column has only null values or no rows.&lt;/font&gt;&lt;/font&gt;&lt;span&gt; 
&lt;br /&gt;
&lt;/span&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;SELECT&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; @sql &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;=&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff0000"&gt;'UPDATE
##char_columns 
&lt;br /&gt;
&lt;/font&gt;&lt;/span&gt;&lt;font color="#ff0000"&gt;&lt;font style="font-size: 10pt"&gt;SET [actual_max_length]
= (SELECT ISNULL(MAX(LEN(['&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @column &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff0000"&gt;'])),-1)
FROM ['&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @schema &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff0000"&gt;'].['&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @table &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff0000"&gt;']) 
&lt;br /&gt;
, &lt;/font&gt;&lt;/span&gt;&lt;font color="#ff0000"&gt;&lt;font style="font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
[row_count] = (SELECT COUNT_BIG(*) FROM ['&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @schema &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff0000"&gt;'].['&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @table &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff0000"&gt;']) 
&lt;br /&gt;
, &lt;/font&gt;&lt;/span&gt;&lt;font color="#ff0000"&gt;&lt;font style="font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
[not_null_count] = (SELECT COUNT_BIG(['&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @column &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff0000"&gt;'])
FROM ['&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @schema &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff0000"&gt;'].['&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @table &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff0000"&gt;&lt;font style="font-size: 10pt"&gt;'])&amp;#160; 
&lt;br /&gt;
&lt;/font&gt;&lt;/font&gt;&lt;/span&gt;&lt;font color="#ff0000"&gt;&lt;font style="font-size: 10pt"&gt;WHERE [schema]
= '''&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @schema &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff0000"&gt;''' 
&lt;br /&gt;
&amp;#160;&lt;/font&gt;&lt;/span&gt;&lt;font color="#ff0000"&gt;&lt;font style="font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
AND [table] = '''&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @table &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff0000"&gt;''' 
&lt;br /&gt;
&amp;#160;&lt;/font&gt;&lt;/span&gt;&lt;font color="#ff0000"&gt;&lt;font style="font-size: 10pt"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="line-height: 12pt; font-family: ; color: "&gt;AND
[column] = '''&lt;/span&gt;&lt;/font&gt;&lt;/font&gt;&lt;span style="line-height: 12pt; font-family: "&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @column &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#ff0000"&gt;''''&lt;/font&gt;&lt;/span&gt;&lt;/span&gt; &lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
EXEC&lt;/font&gt;&lt;/font&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font color="#000000"&gt;@sql&lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
END&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt" color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;TRY&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
BEGIN&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt" color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;CATCH&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
DECLARE&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; @err_num &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;NVARCHAR&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;20&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;),&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @err_msg &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;NVARCHAR&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;2048&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;),&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @full_msg &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;NVARCHAR&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;2048&lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
SELECT&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; @err_num &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;=&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff00ff"&gt;ERROR_NUMBER&lt;/font&gt;&lt;font color="#808080"&gt;(),&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @err_msg &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;=&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff00ff"&gt;ERROR_MESSAGE&lt;/font&gt;&lt;/span&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;() 
&lt;br /&gt;
&lt;/font&gt;&lt;/span&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt;SELECT&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; @full_msg &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;=&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff0000"&gt;'ERROR:
'&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @err_num &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff0000"&gt;'
DESCR: '&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @err_msg &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#ff0000"&gt;'
QUERY: '&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;+&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; @sql&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
RAISERROR&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font color="#000000"&gt;@full_msg&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;16&lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt;0&lt;/font&gt;&lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#808080"&gt;)&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt; 
&lt;br /&gt;
BREAK&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
END&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt" color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font style="font-size: 10pt" color="#0000ff"&gt;CATCH&lt;/font&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
SELECT&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#808080"&gt;*&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;FROM&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; ##char_columns&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;/font&gt; &lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;&lt;font style="font-size: 10pt"&gt; 
&lt;br /&gt;
DROP&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;font style="font-size: 10pt"&gt;&lt;font color="#000000"&gt; &lt;/font&gt;&lt;span style="color: "&gt;&lt;font color="#0000ff"&gt;TABLE&lt;/font&gt;&lt;/span&gt;&lt;font color="#000000"&gt; ##char_columns&lt;/font&gt;&lt;/font&gt;&lt;span&gt;&lt;/span&gt;&lt;/font&gt; 
&lt;p&gt;
Be aware that varchar(max) and nvarchar(max) will give a max_length of –1 and that
actual_max_length is –1 if no rows exist in the table or the column only contains
NULLs (refer to the row_count and not_null_count to determine the situation).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=146b6b2c-9edd-4168-9773-ae9da4bc2983" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,146b6b2c-9edd-4168-9773-ae9da4bc2983.aspx</comments>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=bcfff6ef-3ea5-4a14-b69d-ed0a7b92f09b</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,bcfff6ef-3ea5-4a14-b69d-ed0a7b92f09b.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,bcfff6ef-3ea5-4a14-b69d-ed0a7b92f09b.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=bcfff6ef-3ea5-4a14-b69d-ed0a7b92f09b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
In a clustered environment, it may not always be obvious what node SQL Server has
been running on. Most logging is done based on the network name of the resource group,
or the instance name of the virtual server. Still it is logged once in the Windows
Application Event Log and the SQL Server Error Log each, though the “memory” of those
sources is likely limited by configuration; size and overwrite settings of the application
log and number of error logs (maximum 99) for the SQL Server Error log.
</p>
        <p>
In the Windows Application Event Log, the EventID 17664 is of interest (verify the
source is an SQL Server instance). Event Log filtering is relative straight forward
and turns up an event like this.
</p>
        <p>
          <a href="http://resquel.com/ssb/content/binary/Windows-Live-Writer/What-node-was-SQL-Server-running-on_AC09/EventMessageNodeSQLServer_2.png">
            <img style="background-image: none; border-right-width: 0px; margin: 0px 10px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="EventMessageNodeSQLServer" border="0" alt="EventMessageNodeSQLServer" align="left" src="http://resquel.com/ssb/content/binary/Windows-Live-Writer/What-node-was-SQL-Server-running-on_AC09/EventMessageNodeSQLServer_thumb.png" width="220" height="244" />
          </a>
        </p>
        <p>
          <font size="2">
            <em>Figure Event Properties; 1 = Instance name, 2 = Network name of
the resource group, 3 = Computer name of the cluster node, 4 = Binary code containing
network name\instance name</em>
          </font>
        </p>
        <p>
In case of the SQL Server Error Logs, the thing of interest is the message;
</p>
        <p>
The NETBIOS name of the local node that is running the server is '<b><i>computername</i></b>'.
This is an informational message only; no user action is required.
</p>
        <p>
Parsing the files might be the easiest from SQL Server with the xp_readerrorlog. Unfortunately
error handling surrounding eXtended stored Procedures is limited. Using the script
below, the last result set shows the node where a clustered SQL Server was started
on.
</p>
        <p>
          <font color="#0000ff" size="2">
            <br />
            <font face="Courier New">SET</font>
          </font>
          <font face="Courier New">
            <font size="2">
            </font>
            <font color="#0000ff" size="2">NOCOUNT</font>
            <font size="2">
            </font>
          </font>
          <font face="Courier New">
            <font color="#0000ff" size="2">ON 
<br />
DECLARE</font>
            <font size="2"> @iteration </font>
          </font>
          <font face="Courier New">
            <font color="#0000ff" size="2">int 
<br />
DECLARE</font>
            <font size="2"> @log_events </font>
            <font color="#0000ff" size="2">table </font>
            <font color="#808080" size="2">(</font>
            <font size="2">[LogDate] </font>
            <font color="#0000ff" size="2">datetime</font>
            <font color="#808080" size="2">,</font>
            <font size="2"> [ProcessInfo] </font>
            <font color="#0000ff" size="2">nvarchar</font>
            <font color="#808080" size="2">(</font>
            <font size="2">20</font>
            <font color="#808080" size="2">),</font>
            <font size="2"> [Text] </font>
            <font color="#0000ff" size="2">nvarchar</font>
            <font color="#808080" size="2">(</font>
            <font color="#ff00ff" size="2">max</font>
            <font color="#808080" size="2">),</font>
            <font size="2"> [Iteration] </font>
            <font color="#0000ff" size="2">int</font>
          </font>
          <font face="Courier New">
            <font color="#808080" size="2">) 
<br /></font>
            <font color="#0000ff" size="2">SELECT</font>
            <font size="2"> @iteration </font>
            <font color="#808080" size="2">=</font>
            <font size="2"> 0 </font>
          </font>
          <font face="Courier New">
            <font color="#008000" size="2">--
Set to current error log 
<br /></font>
            <font color="#0000ff" size="2">WHILE</font>
            <font size="2"> @iteration </font>
            <font color="#808080" size="2">&lt;</font>
          </font>
          <font size="2" face="Courier New"> 100 
<br /></font>
          <font face="Courier New">
            <font color="#0000ff" size="2">   
BEGIN 
<br />
    BEGIN</font>
            <font size="2">
            </font>
          </font>
          <font color="#0000ff" size="2" face="Courier New">TRY 
<br /></font>
          <font face="Courier New">
            <font color="#008000" size="2">       
-- EXECUTE the XP without INSERT to trip the error. 
<br />
        -- XP error is handled poorly, but it is
even worse 
<br />
        -- when it occurs on the insert from executing
an XP. 
<br /></font>
            <font color="#0000ff" size="2">        EXEC</font>
            <font size="2">
            </font>
            <font color="#800000" size="2">xp_readerrorlog</font>
            <font color="#0000ff" size="2">
            </font>
            <font size="2">@p1 </font>
            <font color="#808080" size="2">=</font>
            <font size="2"> @iteration</font>
            <font color="#808080" size="2">,</font>
            <font size="2"> @p2 </font>
            <font color="#808080" size="2">=</font>
            <font size="2"> 1</font>
            <font color="#808080" size="2">,</font>
            <font size="2"> @p3 </font>
            <font color="#808080" size="2">=</font>
            <font size="2">
            </font>
            <font color="#ff0000" size="2">'NETBIOS
name'</font>
          </font>
          <font face="Courier New">
            <font color="#808080" size="2">; 
<br /></font>
            <font color="#0000ff" size="2">        INSERT</font>
            <font size="2"> @log_events</font>
            <font color="#0000ff" size="2">
            </font>
            <font color="#808080" size="2">(</font>
            <font size="2">[LogDate]</font>
            <font color="#808080" size="2">,</font>
            <font size="2"> [ProcessInfo]</font>
            <font color="#808080" size="2">,</font>
            <font size="2"> [Text]</font>
          </font>
          <font face="Courier New">
            <font color="#808080" size="2">) 
<br /></font>
            <font color="#0000ff" size="2">           
EXEC</font>
            <font size="2">
            </font>
            <font color="#800000" size="2">xp_readerrorlog</font>
            <font color="#0000ff" size="2">
            </font>
            <font size="2">@p1 </font>
            <font color="#808080" size="2">=</font>
            <font size="2"> @iteration</font>
            <font color="#808080" size="2">,</font>
            <font size="2"> @p2 </font>
            <font color="#808080" size="2">=</font>
            <font size="2"> 1</font>
            <font color="#808080" size="2">,</font>
            <font size="2"> @p3 </font>
            <font color="#808080" size="2">=</font>
            <font size="2">
            </font>
            <font color="#ff0000" size="2">'NETBIOS
name'</font>
          </font>
          <font face="Courier New">
            <font color="#808080" size="2">; 
<br /></font>
            <font color="#0000ff" size="2">        UPDATE</font>
          </font>
          <font face="Courier New">
            <font size="2"> @log_events 
<br /></font>
            <font color="#0000ff" size="2">           
SET</font>
            <font size="2"> [Iteration] </font>
            <font color="#808080" size="2">=</font>
          </font>
          <font face="Courier New">
            <font size="2"> @iteration 
<br />
        </font>
            <font color="#0000ff" size="2">   
WHERE</font>
            <font size="2"> [Iteration] </font>
            <font color="#808080" size="2">IS</font>
            <font size="2">
            </font>
          </font>
          <font face="Courier New">
            <font color="#808080" size="2">NULL; 
<br /></font>
            <font color="#0000ff" size="2">        SELECT</font>
            <font size="2"> @iteration </font>
            <font color="#808080" size="2">=</font>
            <font size="2"> @iteration </font>
            <font color="#808080" size="2">+</font>
            <font size="2"> 1</font>
          </font>
          <font face="Courier New">
            <font color="#808080" size="2">; 
<br />
    </font>
            <font color="#0000ff" size="2">END</font>
            <font size="2">
            </font>
          </font>
          <font face="Courier New">
            <font color="#0000ff" size="2">TRY 
<br />
    BEGIN</font>
            <font size="2">
            </font>
          </font>
          <font face="Courier New">
            <font color="#0000ff" size="2">CATCH 
<br />
        SELECT</font>
            <font size="2"> @iteration </font>
            <font color="#808080" size="2">=</font>
            <font size="2"> @iteration </font>
            <font color="#808080" size="2">+</font>
            <font size="2"> 1</font>
          </font>
          <font face="Courier New">
            <font color="#808080" size="2">; 
<br /></font>
            <font color="#0000ff" size="2">    END</font>
            <font size="2">
            </font>
          </font>
          <font face="Courier New">
            <font color="#0000ff" size="2">CATCH 
<br />
    END 
<br />
SELECT</font>
            <font size="2"> [Iteration]</font>
            <font color="#808080" size="2">,</font>
          </font>
          <font face="Courier New">
            <font size="2"> [LogDate] 
<br />
    </font>
            <font color="#808080" size="2">    ,</font>
            <font size="2">
            </font>
            <font color="#ff00ff" size="2">CAST</font>
            <font color="#808080" size="2">(</font>
            <font color="#ff00ff" size="2">SUBSTRING</font>
            <font color="#808080" size="2">(</font>
          </font>
          <font face="Courier New">
            <font size="2">
              <br />
            </font>
            <font color="#ff00ff" size="2">SUBSTRING</font>
            <font color="#808080" size="2">(</font>
            <font size="2">[Text]</font>
          </font>
          <font color="#808080" size="2">
            <br />
            <font face="Courier New">           
,</font>
          </font>
          <font face="Courier New">
            <font color="#ff00ff" size="2">LEN</font>
            <font color="#808080" size="2">(</font>
            <font color="#ff0000" size="2">'The
NETBIOS name of the local node that is running the server is '''</font>
            <font color="#808080" size="2">)</font>
            <font size="2">
            </font>
            <font color="#808080" size="2">+</font>
          </font>
          <font face="Courier New">
            <font size="2"> 1 
<br />
            </font>
            <font color="#808080" size="2">,</font>
            <font size="2">16 </font>
          </font>
          <font face="Courier New">
            <font color="#008000" size="2">--
Max length NETBIOS name is 16 char (15 actually). 
<br />
    </font>
            <font color="#808080" size="2">    ), </font>
            <font size="2">1</font>
            <font color="#808080" size="2">, </font>
            <font color="#ff00ff" size="2">CHARINDEX</font>
            <font color="#808080" size="2">(</font>
            <font color="#ff0000" size="2">''''</font>
            <font color="#808080" size="2">,</font>
            <font size="2">
            </font>
            <font color="#ff00ff" size="2">SUBSTRING</font>
          </font>
          <font color="#808080" size="2" face="Courier New">( 
<br /></font>
          <font face="Courier New">
            <font size="2">           
[Text] 
<br />
            </font>
            <font color="#808080" size="2">,</font>
            <font color="#ff00ff" size="2">LEN</font>
            <font color="#808080" size="2">(</font>
            <font color="#ff0000" size="2">'The
NETBIOS name of the local node that is running the server is '''</font>
            <font color="#808080" size="2">)</font>
            <font size="2">
            </font>
            <font color="#808080" size="2">+</font>
          </font>
          <font face="Courier New">
            <font size="2"> 1 
<br />
            </font>
            <font color="#808080" size="2">,</font>
            <font size="2">16 </font>
          </font>
          <font color="#008000" size="2" face="Courier New">--
Max length NETBIOS name is 16 char (15 actually). 
<br />
    </font>
          <font face="Courier New">
            <font color="#808080" size="2">   
) 
<br />
    )</font>
            <font size="2">
            </font>
            <font color="#808080" size="2">-</font>
            <font size="2"> 1</font>
            <font color="#808080" size="2">)</font>
            <font size="2">
            </font>
            <font color="#0000ff" size="2">AS</font>
            <font size="2">
            </font>
            <font color="#0000ff" size="2">varchar</font>
            <font color="#808080" size="2">(</font>
            <font size="2">16</font>
            <font color="#808080" size="2">))</font>
            <font size="2">
            </font>
            <font color="#0000ff" size="2">AS</font>
            <font size="2">
            </font>
          </font>
          <font face="Courier New">
            <font color="#ff0000" size="2">'NodeName' 
<br /></font>
            <font color="#0000ff" size="2">FROM</font>
          </font>
          <font face="Courier New">
            <font size="2"> @log_events 
<br /></font>
            <font color="#0000ff" size="2">WHERE</font>
            <font size="2"> [Text] </font>
            <font color="#808080" size="2">LIKE</font>
            <font size="2">
            </font>
          </font>
          <font color="#ff0000" size="2" face="Courier New">'The
NETBIOS name of the local node that is running the server is %' 
<br /></font>
        </p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=bcfff6ef-3ea5-4a14-b69d-ed0a7b92f09b" />
      </body>
      <title>What node was SQL Server running on?</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,bcfff6ef-3ea5-4a14-b69d-ed0a7b92f09b.aspx</guid>
      <link>http://resquel.com/ssb/2011/04/15/WhatNodeWasSQLServerRunningOn.aspx</link>
      <pubDate>Fri, 15 Apr 2011 10:56:11 GMT</pubDate>
      <description>&lt;p&gt;
In a clustered environment, it may not always be obvious what node SQL Server has
been running on. Most logging is done based on the network name of the resource group,
or the instance name of the virtual server. Still it is logged once in the Windows
Application Event Log and the SQL Server Error Log each, though the “memory” of those
sources is likely limited by configuration; size and overwrite settings of the application
log and number of error logs (maximum 99) for the SQL Server Error log.
&lt;/p&gt;
&lt;p&gt;
In the Windows Application Event Log, the EventID 17664 is of interest (verify the
source is an SQL Server instance). Event Log filtering is relative straight forward
and turns up an event like this.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://resquel.com/ssb/content/binary/Windows-Live-Writer/What-node-was-SQL-Server-running-on_AC09/EventMessageNodeSQLServer_2.png"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px 10px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="EventMessageNodeSQLServer" border="0" alt="EventMessageNodeSQLServer" align="left" src="http://resquel.com/ssb/content/binary/Windows-Live-Writer/What-node-was-SQL-Server-running-on_AC09/EventMessageNodeSQLServer_thumb.png" width="220" height="244" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;font size="2"&gt;&lt;em&gt;Figure Event Properties; 1 = Instance name, 2 = Network name of
the resource group, 3 = Computer name of the cluster node, 4 = Binary code containing
network name\instance name&lt;/em&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
In case of the SQL Server Error Logs, the thing of interest is the message;
&lt;/p&gt;
&lt;p&gt;
The NETBIOS name of the local node that is running the server is '&lt;b&gt;&lt;i&gt;computername&lt;/i&gt;&lt;/b&gt;'.
This is an informational message only; no user action is required.
&lt;/p&gt;
&lt;p&gt;
Parsing the files might be the easiest from SQL Server with the xp_readerrorlog. Unfortunately
error handling surrounding eXtended stored Procedures is limited. Using the script
below, the last result set shows the node where a clustered SQL Server was started
on.
&lt;/p&gt;
&lt;p&gt;
&lt;font color="#0000ff" size="2"&gt; 
&lt;br /&gt;
&lt;font face="Courier New"&gt;SET&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;NOCOUNT&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff" size="2"&gt;ON 
&lt;br /&gt;
DECLARE&lt;/font&gt;&lt;font size="2"&gt; @iteration &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff" size="2"&gt;int 
&lt;br /&gt;
DECLARE&lt;/font&gt;&lt;font size="2"&gt; @log_events &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;table &lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;font size="2"&gt;[LogDate] &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;datetime&lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;font size="2"&gt; [ProcessInfo] &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;nvarchar&lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;font size="2"&gt;20&lt;/font&gt;&lt;font color="#808080" size="2"&gt;),&lt;/font&gt;&lt;font size="2"&gt; [Text] &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;nvarchar&lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;font color="#ff00ff" size="2"&gt;max&lt;/font&gt;&lt;font color="#808080" size="2"&gt;),&lt;/font&gt;&lt;font size="2"&gt; [Iteration] &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;int&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#808080" size="2"&gt;) 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;SELECT&lt;/font&gt;&lt;font size="2"&gt; @iteration &lt;/font&gt;&lt;font color="#808080" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt; 0 &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#008000" size="2"&gt;--
Set to current error log 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;WHILE&lt;/font&gt;&lt;font size="2"&gt; @iteration &lt;/font&gt;&lt;font color="#808080" size="2"&gt;&amp;lt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2" face="Courier New"&gt; 100 
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;
BEGIN 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; BEGIN&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff" size="2" face="Courier New"&gt;TRY 
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#008000" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
-- EXECUTE the XP without INSERT to trip the error. 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; -- XP error is handled poorly, but it is
even worse 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; -- when it occurs on the insert from executing
an XP. 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; EXEC&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#800000" size="2"&gt;xp_readerrorlog&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font size="2"&gt;@p1 &lt;/font&gt;&lt;font color="#808080" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt; @iteration&lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;font size="2"&gt; @p2 &lt;/font&gt;&lt;font color="#808080" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt; 1&lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;font size="2"&gt; @p3 &lt;/font&gt;&lt;font color="#808080" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;'NETBIOS
name'&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#808080" size="2"&gt;; 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; INSERT&lt;/font&gt;&lt;font size="2"&gt; @log_events&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;font size="2"&gt;[LogDate]&lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;font size="2"&gt; [ProcessInfo]&lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;font size="2"&gt; [Text]&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#808080" size="2"&gt;) 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
EXEC&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#800000" size="2"&gt;xp_readerrorlog&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt; &lt;/font&gt;&lt;font size="2"&gt;@p1 &lt;/font&gt;&lt;font color="#808080" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt; @iteration&lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;font size="2"&gt; @p2 &lt;/font&gt;&lt;font color="#808080" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt; 1&lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;font size="2"&gt; @p3 &lt;/font&gt;&lt;font color="#808080" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;'NETBIOS
name'&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#808080" size="2"&gt;; 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; UPDATE&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font size="2"&gt; @log_events 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
SET&lt;/font&gt;&lt;font size="2"&gt; [Iteration] &lt;/font&gt;&lt;font color="#808080" size="2"&gt;=&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font size="2"&gt; @iteration 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;
WHERE&lt;/font&gt;&lt;font size="2"&gt; [Iteration] &lt;/font&gt;&lt;font color="#808080" size="2"&gt;IS&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#808080" size="2"&gt;NULL; 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SELECT&lt;/font&gt;&lt;font size="2"&gt; @iteration &lt;/font&gt;&lt;font color="#808080" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt; @iteration &lt;/font&gt;&lt;font color="#808080" size="2"&gt;+&lt;/font&gt;&lt;font size="2"&gt; 1&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#808080" size="2"&gt;; 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;END&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff" size="2"&gt;TRY 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; BEGIN&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff" size="2"&gt;CATCH 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SELECT&lt;/font&gt;&lt;font size="2"&gt; @iteration &lt;/font&gt;&lt;font color="#808080" size="2"&gt;=&lt;/font&gt;&lt;font size="2"&gt; @iteration &lt;/font&gt;&lt;font color="#808080" size="2"&gt;+&lt;/font&gt;&lt;font size="2"&gt; 1&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#808080" size="2"&gt;; 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; END&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff" size="2"&gt;CATCH 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; END 
&lt;br /&gt;
SELECT&lt;/font&gt;&lt;font size="2"&gt; [Iteration]&lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font size="2"&gt; [LogDate] 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#808080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; ,&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff00ff" size="2"&gt;CAST&lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;font color="#ff00ff" size="2"&gt;SUBSTRING&lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font size="2"&gt; 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#ff00ff" size="2"&gt;SUBSTRING&lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;font size="2"&gt;[Text]&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt; 
&lt;br /&gt;
&lt;font face="Courier New"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
,&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#ff00ff" size="2"&gt;LEN&lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;'The
NETBIOS name of the local node that is running the server is '''&lt;/font&gt;&lt;font color="#808080" size="2"&gt;)&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#808080" size="2"&gt;+&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font size="2"&gt; 1 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;font size="2"&gt;16 &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#008000" size="2"&gt;--
Max length NETBIOS name is 16 char (15 actually). 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#808080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160; ), &lt;/font&gt;&lt;font size="2"&gt;1&lt;/font&gt;&lt;font color="#808080" size="2"&gt;, &lt;/font&gt;&lt;font color="#ff00ff" size="2"&gt;CHARINDEX&lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;''''&lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#ff00ff" size="2"&gt;SUBSTRING&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2" face="Courier New"&gt;( 
&lt;br /&gt;
&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font size="2"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;
[Text] 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;font color="#ff00ff" size="2"&gt;LEN&lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;font color="#ff0000" size="2"&gt;'The
NETBIOS name of the local node that is running the server is '''&lt;/font&gt;&lt;font color="#808080" size="2"&gt;)&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#808080" size="2"&gt;+&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font size="2"&gt; 1 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font color="#808080" size="2"&gt;,&lt;/font&gt;&lt;font size="2"&gt;16 &lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2" face="Courier New"&gt;--
Max length NETBIOS name is 16 char (15 actually). 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; &lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#808080" size="2"&gt;&amp;#160;&amp;#160;&amp;#160;
) 
&lt;br /&gt;
&amp;#160;&amp;#160;&amp;#160; )&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#808080" size="2"&gt;-&lt;/font&gt;&lt;font size="2"&gt; 1&lt;/font&gt;&lt;font color="#808080" size="2"&gt;)&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;AS&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;varchar&lt;/font&gt;&lt;font color="#808080" size="2"&gt;(&lt;/font&gt;&lt;font size="2"&gt;16&lt;/font&gt;&lt;font color="#808080" size="2"&gt;))&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;AS&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font color="#ff0000" size="2"&gt;'NodeName' 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;FROM&lt;/font&gt;&lt;/font&gt;&lt;font face="Courier New"&gt;&lt;font size="2"&gt; @log_events 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;WHERE&lt;/font&gt;&lt;font size="2"&gt; [Text] &lt;/font&gt;&lt;font color="#808080" size="2"&gt;LIKE&lt;/font&gt;&lt;font size="2"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#ff0000" size="2" face="Courier New"&gt;'The
NETBIOS name of the local node that is running the server is %' 
&lt;br /&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=bcfff6ef-3ea5-4a14-b69d-ed0a7b92f09b" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,bcfff6ef-3ea5-4a14-b69d-ed0a7b92f09b.aspx</comments>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=36cfd967-b7cc-43cd-8af0-15440f81b7c5</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,36cfd967-b7cc-43cd-8af0-15440f81b7c5.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,36cfd967-b7cc-43cd-8af0-15440f81b7c5.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=36cfd967-b7cc-43cd-8af0-15440f81b7c5</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Service Pack 2 for SQL Server 2008 annoyed me on one of my machines (development system,
SQL Server doesn’t autostart). It came via Windows Update started with offering the
service pack without .NET Framework 4 installed… then even after .NET Framework was
installed, it kept failing. So f..k Windows Update and download SQL2k8SP2 and install
it.
</p>
        <p>
A couple of days later I needed SQL Server on that machine, I did the net start mssqlserver
trick and tried to connect using Management Studio directly after that. So management
studio threw me the 
</p>
        <blockquote>
          <p>
Login failed for user 'domain\user'. Reason: Server is in script upgrade mode. Only
administrator can connect at this time. (Microsoft SQL Server, Error: 18401)
</p>
        </blockquote>
        <p>
The very “well” documented error about the fact that SQL Server is running some scripts
and rather has no-one mesh around (unless you’re on a DAC). The strange thing is that
SQL Server seemed to have stopped after; that, something that isn’t supposed to happen.
I restarted the service and tried again… same result, so it’s Google-time; best hit: <a href="http://www.techurbia.com/2009/04/weird-sql-server-error-unable-to-connect-to-server-reason-server-is-in-script-upgrade-mode.html" target="_blank">Weird
SQL Server Error: Unable to Connect to Server. Reason: Server is in script upgrade
mode</a>. With that being the best answer, the behavior I witnessed isn’t supposed
to happen and thus I turned to the SQL Error Logs to find some info;
</p>
        <blockquote>
          <p>
2010-12-03 21:02:04.28 spid7s      Performing replication
job security meta-data upgrades... 
<br />
2010-12-03 21:02:07.34 spid7s      Error: 824, Severity:
24, State: 2. 
<br />
2010-12-03 21:02:07.34 spid7s      <font style="background-color: #ffff00">SQL
Server detected a logical consistency-based I/O error: incorrect pageid (expected
1:2064; actual 0:0). It occurred during a read of page (1:2064) in database ID 21
at offset 0x00000001020000 in file 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\some_database.mdf'. 
Additional messages in the SQL Server error log or system event log may provide more
detail. This is a severe error condition that threatens database integrity and must
be corrected immediately. Complete a full database consistency check (DBCC CHECKDB).
This error can be caused by many factors; for more information, see SQL Server Books
Online. 
<br /></font>2010-12-03 21:02:07.34 spid7s      Error: 912, Severity:
21, State: 2. 
<br />
2010-12-03 21:02:07.34 spid7s      Script level upgrade for
database 'master' failed because upgrade step 'sqlagent100_msdb_upgrade.sql' encountered
error 824, state 2, severity 25. This is a serious error condition which might interfere
with regular operation and the database will be taken offline. If the error happened
during upgrade of the 'master' database, it will prevent the entire SQL Server instance
from starting. Examine the previous errorlog entries for errors, take the appropriate
corrective actions and re-start the database so that the script upgrade steps run
to completion. 
<br />
2010-12-03 21:02:07.35 spid7s      Error: 3417, Severity:
21, State: 3. 
<br />
2010-12-03 21:02:07.35 spid7s      Cannot recover the master
database. SQL Server is unable to run. Restore master from a full backup, repair it,
or rebuild it. For more information about how to rebuild the master database, see
SQL Server Books Online. 
<br />
2010-12-03 21:02:07.86 spid7s      SQL Trace was stopped
due to server shutdown. Trace ID = '1'. This is an informational message only; no
user action is required. 
<br />
2010-12-03 21:02:07.99 Server      The SQL Server Network
Interface library successfully deregistered the Service Principal Name (SPN) [ MSSQLSvc/host.domain.local
] for the SQL Server service. 
</p>
        </blockquote>
        <p>
I had been working on a corrupted database called some_database (name changed for
obvious reasons) for a customer a few weeks ago, but hadn’t removed that database.
After removing the offending database files, SQL Server wasn’t tripped by the exception
while opening the database.
</p>
        <p>
To me, this does smell a like a bug (though I would not recommend running corrupt
databases on a production system), so I filed it on connect; <a href="https://connect.microsoft.com/SQLServer/feedback/details/628098/finishing-service-pack-installation-prohibited-by-corrupt-database" target="_blank">Finishing
service pack installation prohibited by corrupt database</a>.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=36cfd967-b7cc-43cd-8af0-15440f81b7c5" />
      </body>
      <title>Finishing SQL Server 2008 Service Pack 2 installation blocked by corrupt database</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,36cfd967-b7cc-43cd-8af0-15440f81b7c5.aspx</guid>
      <link>http://resquel.com/ssb/2010/12/03/FinishingSQLServer2008ServicePack2InstallationBlockedByCorruptDatabase.aspx</link>
      <pubDate>Fri, 03 Dec 2010 21:04:20 GMT</pubDate>
      <description>&lt;p&gt;
Service Pack 2 for SQL Server 2008 annoyed me on one of my machines (development system,
SQL Server doesn’t autostart). It came via Windows Update started with offering the
service pack without .NET Framework 4 installed… then even after .NET Framework was
installed, it kept failing. So f..k Windows Update and download SQL2k8SP2 and install
it.
&lt;/p&gt;
&lt;p&gt;
A couple of days later I needed SQL Server on that machine, I did the net start mssqlserver
trick and tried to connect using Management Studio directly after that. So management
studio threw me the 
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Login failed for user 'domain\user'. Reason: Server is in script upgrade mode. Only
administrator can connect at this time. (Microsoft SQL Server, Error: 18401)
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The very “well” documented error about the fact that SQL Server is running some scripts
and rather has no-one mesh around (unless you’re on a DAC). The strange thing is that
SQL Server seemed to have stopped after; that, something that isn’t supposed to happen.
I restarted the service and tried again… same result, so it’s Google-time; best hit: &lt;a href="http://www.techurbia.com/2009/04/weird-sql-server-error-unable-to-connect-to-server-reason-server-is-in-script-upgrade-mode.html" target="_blank"&gt;Weird
SQL Server Error: Unable to Connect to Server. Reason: Server is in script upgrade
mode&lt;/a&gt;. With that being the best answer, the behavior I witnessed isn’t supposed
to happen and thus I turned to the SQL Error Logs to find some info;
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
2010-12-03 21:02:04.28 spid7s&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Performing replication
job security meta-data upgrades... 
&lt;br /&gt;
2010-12-03 21:02:07.34 spid7s&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Error: 824, Severity:
24, State: 2. 
&lt;br /&gt;
2010-12-03 21:02:07.34 spid7s&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;font style="background-color: #ffff00"&gt;SQL
Server detected a logical consistency-based I/O error: incorrect pageid (expected
1:2064; actual 0:0). It occurred during a read of page (1:2064) in database ID 21
at offset 0x00000001020000 in file 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\some_database.mdf'.&amp;#160;
Additional messages in the SQL Server error log or system event log may provide more
detail. This is a severe error condition that threatens database integrity and must
be corrected immediately. Complete a full database consistency check (DBCC CHECKDB).
This error can be caused by many factors; for more information, see SQL Server Books
Online. 
&lt;br /&gt;
&lt;/font&gt;2010-12-03 21:02:07.34 spid7s&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Error: 912, Severity:
21, State: 2. 
&lt;br /&gt;
2010-12-03 21:02:07.34 spid7s&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Script level upgrade for
database 'master' failed because upgrade step 'sqlagent100_msdb_upgrade.sql' encountered
error 824, state 2, severity 25. This is a serious error condition which might interfere
with regular operation and the database will be taken offline. If the error happened
during upgrade of the 'master' database, it will prevent the entire SQL Server instance
from starting. Examine the previous errorlog entries for errors, take the appropriate
corrective actions and re-start the database so that the script upgrade steps run
to completion. 
&lt;br /&gt;
2010-12-03 21:02:07.35 spid7s&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Error: 3417, Severity:
21, State: 3. 
&lt;br /&gt;
2010-12-03 21:02:07.35 spid7s&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Cannot recover the master
database. SQL Server is unable to run. Restore master from a full backup, repair it,
or rebuild it. For more information about how to rebuild the master database, see
SQL Server Books Online. 
&lt;br /&gt;
2010-12-03 21:02:07.86 spid7s&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; SQL Trace was stopped
due to server shutdown. Trace ID = '1'. This is an informational message only; no
user action is required. 
&lt;br /&gt;
2010-12-03 21:02:07.99 Server&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; The SQL Server Network
Interface library successfully deregistered the Service Principal Name (SPN) [ MSSQLSvc/host.domain.local
] for the SQL Server service. 
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
I had been working on a corrupted database called some_database (name changed for
obvious reasons) for a customer a few weeks ago, but hadn’t removed that database.
After removing the offending database files, SQL Server wasn’t tripped by the exception
while opening the database.
&lt;/p&gt;
&lt;p&gt;
To me, this does smell a like a bug (though I would not recommend running corrupt
databases on a production system), so I filed it on connect; &lt;a href="https://connect.microsoft.com/SQLServer/feedback/details/628098/finishing-service-pack-installation-prohibited-by-corrupt-database" target="_blank"&gt;Finishing
service pack installation prohibited by corrupt database&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=36cfd967-b7cc-43cd-8af0-15440f81b7c5" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,36cfd967-b7cc-43cd-8af0-15440f81b7c5.aspx</comments>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=d6633014-c1f4-41c6-872e-85842600237a</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,d6633014-c1f4-41c6-872e-85842600237a.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,d6633014-c1f4-41c6-872e-85842600237a.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=d6633014-c1f4-41c6-872e-85842600237a</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
There is nothing fancy about this post… just a little frustration. I wanted to compress
files after they were processed with SSIS 2005. And with compression, I mean NTFS-compression,
not creating a .zip or .gz (because that is what I mostly found through the search
engines). 
</p>
        <p>
My first path of research was to do it in a Script Task, but it turns out, invoking
the compression attribute of files on NTFS requires C# and can’t be done with VB.NET.
So it’s a no go in SSIS 2005.
</p>
        <p>
During the search, somewhere the compact.exe program in Windows was suggested as an
alternative. Very useful from an SSIS perspective, it means the Execute Process Task
is required. To make it all happen, three things are needed;
</p>
        <ul>
          <li>
A Foreach Loop container 
</li>
          <li>
A string variable (either in the scope of the Foreach Loop Container or in the Package-scope),
named filename in this example. 
</li>
          <li>
An Execute Process Task in the Foreach Loop container 
</li>
        </ul>
        <p>
          <a href="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image002_2.jpg">
            <img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image002_thumb.jpg" width="244" height="189" />
          </a>
        </p>
        <p>
Configure (Collection Page) the Foreach Loop Container as Foreach File enumerator,
point to the folder where the files reside and retrieve the file names as Fully qualified.
Depending your requirements, you may also specify an additional mask and include subfolders.
</p>
        <p>
          <a href="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image004_2.jpg">
            <img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image004_thumb.jpg" width="244" height="236" />
          </a>
        </p>
        <p>
Next visit the Variable Mappings tab and point to the previously created variable
listed as User::filename.
</p>
        <p>
Next is the Execute Process Task. But first a little on compact.exe, to compress a
file, compact.exe should be called with the /C flag and the name of the file to be
compressed. Something like <font face="Courier New"><strong>C:\WINOWS\System32\compact.exe
/C "D:\ftp\archive\dump_001208.csv"</strong></font></p>
        <p>
In terms of what the Execute Process Task expects, the Executable should be set to
compact.exe. The arguments should be <strong><font face="Courier New">/C "D:\ftp\archive\dump_001208.csv"</font></strong> and
since the filename is supplied by the variable, an expression should be used. On the
Expressions tab, create an expression for Arguments. With the expression designer,
create the following expression
</p>
        <p>
"/C \"" + @[User::filename] + "\""
</p>
        <p>
The important part to recognize is that expressions are largely built on C-style syntax
(download the PragmaticWorks white paper  <a title="SSIS Accelerator Series: Working with SSIS Expressions" href="http://www.pragmaticworks.com/downloads/ssisexpressionswhitepaper/" target="_blank">SSIS
Accelerator Series: Working with SSIS Expressions</a> to learn more on SSIS expressions).
The arguments expression should return a string, hence the opening and closing double-quotes.
The filename, which is supplied by the @[User::filename] variable (drag and drop the
variable in the expression designer) should be enclosed within double-quotes too.
For SSIS to recognize that these double-quotes are part of the string it should return,
the double-quotes need to be escaped (using the backslash). To confirm the expression
is correct, use the Evaluate Expression button. The evaluated value should return
/C "" (slash + C + space + double-quote + double-quote), once the filename
variable is filled, it will appear between the two double-quotes. 
</p>
        <p>
          <a href="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image006_2.jpg">
            <img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image006_thumb.jpg" width="244" height="219" />
          </a>
        </p>
        <p>
Enclosing the filename in double quotes prevents files with spaces in the name from
causing errors. There could be other errors though, like a file being locked. With
current configuration, the package would stop and throw an error on the first occurrence
of this condition. To prevent the package from failing in that event, set the FailTaskIfReturnCodeIsNotSuccessValue
property of the Execute Process Task to false.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=d6633014-c1f4-41c6-872e-85842600237a" />
      </body>
      <title>Compress files with SSIS</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,d6633014-c1f4-41c6-872e-85842600237a.aspx</guid>
      <link>http://resquel.com/ssb/2010/11/30/CompressFilesWithSSIS.aspx</link>
      <pubDate>Tue, 30 Nov 2010 20:49:56 GMT</pubDate>
      <description>&lt;p&gt;
There is nothing fancy about this post… just a little frustration. I wanted to compress
files after they were processed with SSIS 2005. And with compression, I mean NTFS-compression,
not creating a .zip or .gz (because that is what I mostly found through the search
engines). 
&lt;/p&gt;
&lt;p&gt;
My first path of research was to do it in a Script Task, but it turns out, invoking
the compression attribute of files on NTFS requires C# and can’t be done with VB.NET.
So it’s a no go in SSIS 2005.
&lt;/p&gt;
&lt;p&gt;
During the search, somewhere the compact.exe program in Windows was suggested as an
alternative. Very useful from an SSIS perspective, it means the Execute Process Task
is required. To make it all happen, three things are needed;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
A Foreach Loop container 
&lt;/li&gt;
&lt;li&gt;
A string variable (either in the scope of the Foreach Loop Container or in the Package-scope),
named filename in this example. 
&lt;/li&gt;
&lt;li&gt;
An Execute Process Task in the Foreach Loop container 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
&lt;a href="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image002_2.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="clip_image002" border="0" alt="clip_image002" src="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image002_thumb.jpg" width="244" height="189" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Configure (Collection Page) the Foreach Loop Container as Foreach File enumerator,
point to the folder where the files reside and retrieve the file names as Fully qualified.
Depending your requirements, you may also specify an additional mask and include subfolders.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image004_2.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="clip_image004" border="0" alt="clip_image004" src="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image004_thumb.jpg" width="244" height="236" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Next visit the Variable Mappings tab and point to the previously created variable
listed as User::filename.
&lt;/p&gt;
&lt;p&gt;
Next is the Execute Process Task. But first a little on compact.exe, to compress a
file, compact.exe should be called with the /C flag and the name of the file to be
compressed. Something like &lt;font face="Courier New"&gt;&lt;strong&gt;C:\WINOWS\System32\compact.exe
/C &amp;quot;D:\ftp\archive\dump_001208.csv&amp;quot;&lt;/strong&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
In terms of what the Execute Process Task expects, the Executable should be set to
compact.exe. The arguments should be &lt;strong&gt;&lt;font face="Courier New"&gt;/C &amp;quot;D:\ftp\archive\dump_001208.csv&amp;quot;&lt;/font&gt;&lt;/strong&gt; and
since the filename is supplied by the variable, an expression should be used. On the
Expressions tab, create an expression for Arguments. With the expression designer,
create the following expression
&lt;/p&gt;
&lt;p&gt;
&amp;quot;/C \&amp;quot;&amp;quot; + @[User::filename] + &amp;quot;\&amp;quot;&amp;quot;
&lt;/p&gt;
&lt;p&gt;
The important part to recognize is that expressions are largely built on C-style syntax
(download the PragmaticWorks white paper&amp;#160; &lt;a title="SSIS Accelerator Series: Working with SSIS Expressions" href="http://www.pragmaticworks.com/downloads/ssisexpressionswhitepaper/" target="_blank"&gt;SSIS
Accelerator Series: Working with SSIS Expressions&lt;/a&gt; to learn more on SSIS expressions).
The arguments expression should return a string, hence the opening and closing double-quotes.
The filename, which is supplied by the @[User::filename] variable (drag and drop the
variable in the expression designer) should be enclosed within double-quotes too.
For SSIS to recognize that these double-quotes are part of the string it should return,
the double-quotes need to be escaped (using the backslash). To confirm the expression
is correct, use the Evaluate Expression button. The evaluated value should return
/C &amp;quot;&amp;quot; (slash + C + space + double-quote + double-quote), once the filename
variable is filled, it will appear between the two double-quotes. 
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image006_2.jpg"&gt;&lt;img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="clip_image006" border="0" alt="clip_image006" src="http://resquel.com/ssb/content/binary/Windows-Live-Writer/cd161238505c_1311D/clip_image006_thumb.jpg" width="244" height="219" /&gt;&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
Enclosing the filename in double quotes prevents files with spaces in the name from
causing errors. There could be other errors though, like a file being locked. With
current configuration, the package would stop and throw an error on the first occurrence
of this condition. To prevent the package from failing in that event, set the FailTaskIfReturnCodeIsNotSuccessValue
property of the Execute Process Task to false.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=d6633014-c1f4-41c6-872e-85842600237a" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,d6633014-c1f4-41c6-872e-85842600237a.aspx</comments>
      <category>Intergation Services</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=8c669762-8d69-41cf-b2e2-5da1b47f9f8d</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,8c669762-8d69-41cf-b2e2-5da1b47f9f8d.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,8c669762-8d69-41cf-b2e2-5da1b47f9f8d.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=8c669762-8d69-41cf-b2e2-5da1b47f9f8d</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Some time ago I was working with performance data collected by a customer to look
for I/O-patterns and make recommendations for the I/O-subsystem for a new SQL Server
2008 machine that would replace a couple of SQL Server 2000 servers. Unfortunately
I ran into issues with the values for Avg. Disk sec/Read and Avg. Disk sec/Write,
which were ridiculous with average values around 100-million seconds per read or write.
Those values occurred on two virtualized Windows 2000 Servers. 
</p>
        <p>
          <br />
Today, I was working for another customer. Physical Windows Server 2003 boxes (both
nodes in the same cluster). I imported the .blg files into a database and was looking
at very high values (up to 100 million) for Avg. Disk sec/Read and Avg. Disk sec/Write.
I consulted the staff-DBA, who referred me to one off the Windows administrators.
So the Windows administrator logged in, opened performance monitor and… got reasonable
values for those counters (hovering at about 0.006, I’d even say those values are
good). To prove I wasn’t making a fool of myself, we walked over to my desk and I
showed the results I had… same servers, same counters, very different values. Next
we tried using the account of the staff-DBA, which showed the “normal” values.
</p>
        <p>
          <br />
Simply eliminating, the Win-admin said the only difference is the account, so he went
and checked whether the account I used had full administrator permissions. It had.
Next he turned his attention to the user profile for the account I used (which happened
to be quite big). The user profile belonging to the account I used was deleted and
I logged in again, this time to be shown the correct values. RESOLVED!!!
</p>
        <p>
          <br />
Unfortunately the collected data still was useless. I started the counter log again,
let it run for about 10 minutes and examined it… sky high values!!! In disbelief I
switched to current activity, to be shown normal values!!! Next I created a new counter
log definition, let it run for a couple of minutes and this time I got normal values.
</p>
        <p>
          <br />
Bottom line, to resolve insanely high values for Avg. Disk sec/Read and Avg. Disk
sec/Write (it might apply for other counters of the type PERF_AVERAGE_TIMER too),
use a fresh user profile and a fresh counter log definition.
</p>
        <p>
          <br />
Thanks Ruud.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=8c669762-8d69-41cf-b2e2-5da1b47f9f8d" />
      </body>
      <title>Avg. Disk sec/Read and Write were sky high (again…)</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,8c669762-8d69-41cf-b2e2-5da1b47f9f8d.aspx</guid>
      <link>http://resquel.com/ssb/2010/10/19/AvgDiskSecReadAndWriteWereSkyHighAgain.aspx</link>
      <pubDate>Tue, 19 Oct 2010 19:44:49 GMT</pubDate>
      <description>&lt;p&gt;
Some time ago I was working with performance data collected by a customer to look
for I/O-patterns and make recommendations for the I/O-subsystem for a new SQL Server
2008 machine that would replace a couple of SQL Server 2000 servers. Unfortunately
I ran into issues with the values for Avg. Disk sec/Read and Avg. Disk sec/Write,
which were ridiculous with average values around 100-million seconds per read or write.
Those values occurred on two virtualized Windows 2000 Servers. 
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
Today, I was working for another customer. Physical Windows Server 2003 boxes (both
nodes in the same cluster). I imported the .blg files into a database and was looking
at very high values (up to 100 million) for Avg. Disk sec/Read and Avg. Disk sec/Write.
I consulted the staff-DBA, who referred me to one off the Windows administrators.
So the Windows administrator logged in, opened performance monitor and… got reasonable
values for those counters (hovering at about 0.006, I’d even say those values are
good). To prove I wasn’t making a fool of myself, we walked over to my desk and I
showed the results I had… same servers, same counters, very different values. Next
we tried using the account of the staff-DBA, which showed the “normal” values.
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
Simply eliminating, the Win-admin said the only difference is the account, so he went
and checked whether the account I used had full administrator permissions. It had.
Next he turned his attention to the user profile for the account I used (which happened
to be quite big). The user profile belonging to the account I used was deleted and
I logged in again, this time to be shown the correct values. RESOLVED!!!
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
Unfortunately the collected data still was useless. I started the counter log again,
let it run for about 10 minutes and examined it… sky high values!!! In disbelief I
switched to current activity, to be shown normal values!!! Next I created a new counter
log definition, let it run for a couple of minutes and this time I got normal values.
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
Bottom line, to resolve insanely high values for Avg. Disk sec/Read and Avg. Disk
sec/Write (it might apply for other counters of the type PERF_AVERAGE_TIMER too),
use a fresh user profile and a fresh counter log definition.
&lt;/p&gt;
&lt;p&gt;
&lt;br /&gt;
Thanks Ruud.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=8c669762-8d69-41cf-b2e2-5da1b47f9f8d" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,8c669762-8d69-41cf-b2e2-5da1b47f9f8d.aspx</comments>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=c97ef7e3-a574-4286-8eb8-650c2cba696e</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,c97ef7e3-a574-4286-8eb8-650c2cba696e.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,c97ef7e3-a574-4286-8eb8-650c2cba696e.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=c97ef7e3-a574-4286-8eb8-650c2cba696e</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I am working on an MS Access database that needs to be integrated in a CRM system.
Since the backend of the CRM is SQL Server (why else would I be involved), I migrated
the Access database to SQL Server using the <a title="Download SQL Server Migration Assistant for Access v4.2" href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;FamilyID=5abe098d-c7e1-46c6-994a-09a2856eef0b" rel="nofollow" target="_blank">SQL
Server Migration Assistant for Access v4.2</a> (SSMA for Access). Next I turned my
attention to translating attributes between the systems and unfortunately, a free
text notes field in the Access database is used for a lot of things that need to be
translated to attributes. Some things I know how to translate, but some things have
to be determined by the administrators for the CRM System and Access application.
So I built a query in SQL on the tables formerly stored in the Access database and
used Excel (with MS Query) to create a report for the administrators, so they can
sort out how customer statuses should be translated.
</p>
        <p>
Extremely simplified, the query looked like this; 
</p>
        <blockquote>
          <font size="2">
            <font face="Courier New">
              <font color="#0000ff">SELECT</font> tlbCustomer<font color="#808080">.</font></font>
          </font>
          <font size="2">
            <font face="Courier New">CUST_CODE 
<br /><font color="#808080">  ,</font> tlbCustomer<font color="#808080">.</font></font>
          </font>
          <font size="2">
            <font face="Courier New">PAY_REF 
<br /><font color="#808080">  ,</font><font color="#ff00ff">COALESCE</font><font color="#808080">(</font><font color="#ff00ff">UPPER</font><font color="#808080">(</font><font color="#ff00ff">REPLACE</font><font color="#808080">(</font>tblNotes<font color="#808080">.</font>NOTE_TXT<font color="#808080">,</font><font color="#ff0000">'.'</font><font color="#808080">,</font><font color="#ff0000">''</font><font color="#808080">)),</font><font color="#ff0000">''</font><font color="#808080">)</font><font color="#0000ff">AS</font></font>
          </font>
          <font size="2">
            <font face="Courier New"> NOTE_TXT 
<br /><font color="#0000ff">FROM</font> tlbCustomer <font color="#808080">LEFT</font><font color="#808080">OUTER</font><font color="#808080">JOIN</font></font>
          </font>
          <font size="2">
            <font face="Courier New"> tblNotes 
<br /><font color="#0000ff">      ON</font> tlbCustomer<font color="#808080">.</font>CUST_ID <font color="#808080">=</font> tblNotes<font color="#808080">.</font></font>
          </font>
          <font size="2">
            <font face="Courier New">CUST_ID 
<br /><font color="#0000ff">GROUP</font><font color="#0000ff">BY</font> tlbCustomer<font color="#808080">.</font></font>
          </font>
          <font size="2">
            <font face="Courier New">CUST_CODE 
<br /><font color="#808080">  ,</font> tlbCustomer<font color="#808080">.</font></font>
          </font>
          <font size="2">
            <font face="Courier New">PAY_REF 
<br /><font color="#808080">  ,</font><font color="#ff00ff">UPPER</font><font color="#808080">(</font><font color="#ff00ff">REPLACE</font><font color="#808080">(</font>tblNotes<font color="#808080">.</font>NOTE_TXT<font color="#808080">,</font><font color="#ff0000">'.'</font><font color="#808080">,</font><font color="#ff0000">''</font></font>
          </font>
          <font size="2">
            <font face="Courier New">
              <font color="#808080">)) 
<br /></font>
              <font color="#0000ff">ORDER</font>
              <font color="#0000ff">BY</font>
              <font color="#ff00ff">COUNT</font>
              <font color="#808080">(*)</font>
            </font>
          </font>
          <font color="#0000ff">
            <font size="2" face="Courier New">DESC</font>
            <br />
          </font>
        </blockquote>
        <p>
        </p>
        <p>
The query worked great on SQL, but did not return the text (NOTE_TXT) to Excel. 
</p>
        <p>
To cut the long story short, the original tblNotes.NOTE_TXT field in the Access database
was of type MEMO. SSMA 2008 for Access converted this to nvarchar(max) and somehow
MS Query (in Office 2007) does not return, nor throw an error on, large data types
(varchar(max), nvarchar(max)). A quick test showed that MAX was not really needed;
</p>
        <blockquote>
          <font size="2">
            <font face="Courier New">
              <font color="#0000ff">SELECT</font>
              <font color="#ff00ff">MAX</font>
              <font color="#808080">(</font>
              <font color="#ff00ff">LEN</font>
              <font color="#808080">(</font>tblNotes<font color="#808080">.</font>NOTE_TXT<font color="#808080">))</font><font color="#0000ff">FROM</font> tblNotes</font>
          </font>
          <font size="+0">
            <br />
          </font>
        </blockquote>
        <p>
So modify the query to return NOTE_TXT as nvarchar(256) does the trick;
</p>
        <blockquote>
          <font size="2">
            <font face="Courier New">
              <font color="#0000ff">SELECT</font> tlbCustomer<font color="#808080">.</font></font>
          </font>
          <font size="2">
            <font face="Courier New">CUST_CODE 
<br /><font color="#808080">  ,</font> tlbCustomer<font color="#808080">.</font></font>
          </font>
          <font size="2">
            <font face="Courier New">PAY_REF 
<br /><font color="#808080">  ,</font><font color="#ff00ff">CAST</font></font>
          </font>
          <font size="2">
            <font face="Courier New">
              <font color="#808080">( 
<br /></font>
              <font color="#ff00ff">      COALESCE</font>
              <font color="#808080">(</font>
              <font color="#ff00ff">UPPER</font>
              <font color="#808080">(</font>
              <font color="#ff00ff">REPLACE</font>
              <font color="#808080">(</font>tblNotes<font color="#808080">.</font>NOTE_TXT<font color="#808080">,</font><font color="#ff0000">'.'</font><font color="#808080">,</font><font color="#ff0000">''</font><font color="#808080">)),</font><font color="#ff0000">''</font><font color="#808080">)</font></font>
          </font>
          <font size="2">
            <font face="Courier New">
              <br />
              <font color="#0000ff">    AS</font>
              <font color="#0000ff">nvarchar</font>
              <font color="#808080">(</font>256<font color="#808080">))</font><font color="#0000ff">AS</font></font>
          </font>
          <font size="2">
            <font face="Courier New"> NOTE_TXT 
<br /><font color="#0000ff">FROM</font> tlbCustomer <font color="#808080">LEFT</font><font color="#808080">OUTER</font><font color="#808080">JOIN</font></font>
          </font>
          <font size="2">
            <font face="Courier New"> tblNotes 
<br /><font color="#0000ff">      ON</font> tlbCustomer<font color="#808080">.</font>CUST_ID <font color="#808080">=</font> tblNotes<font color="#808080">.</font></font>
          </font>
          <font size="2">
            <font face="Courier New">CUST_ID 
<br /><font color="#0000ff">GROUP</font><font color="#0000ff">BY</font> tlbCustomer<font color="#808080">.</font></font>
          </font>
          <font size="2">
            <font face="Courier New">CUST_CODE 
<br /><font color="#808080">  ,</font> tlbCustomer<font color="#808080">.</font></font>
          </font>
          <font size="2">
            <font face="Courier New">PAY_REF 
<br /><font color="#808080">  ,</font><font color="#ff00ff">UPPER</font><font color="#808080">(</font><font color="#ff00ff">REPLACE</font><font color="#808080">(</font>tblNotes<font color="#808080">.</font>NOTE_TXT<font color="#808080">,</font><font color="#ff0000">'.'</font><font color="#808080">,</font><font color="#ff0000">''</font></font>
          </font>
          <font size="2">
            <font face="Courier New">
              <font color="#808080">)) 
<br /></font>
              <font color="#0000ff">ORDER</font>
              <font color="#0000ff">BY</font>
              <font color="#ff00ff">COUNT</font>
              <font color="#808080">(*)</font>
            </font>
          </font>
          <font color="#0000ff">
            <font size="2" face="Courier New">DESC</font>
            <br />
          </font>
        </blockquote>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=c97ef7e3-a574-4286-8eb8-650c2cba696e" />
      </body>
      <title>MS Query not returning text</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,c97ef7e3-a574-4286-8eb8-650c2cba696e.aspx</guid>
      <link>http://resquel.com/ssb/2010/08/19/MSQueryNotReturningText.aspx</link>
      <pubDate>Thu, 19 Aug 2010 10:26:16 GMT</pubDate>
      <description>&lt;p&gt;
I am working on an MS Access database that needs to be integrated in a CRM system.
Since the backend of the CRM is SQL Server (why else would I be involved), I migrated
the Access database to SQL Server using the &lt;a title="Download SQL Server Migration Assistant for Access v4.2" href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=5abe098d-c7e1-46c6-994a-09a2856eef0b" rel="nofollow" target="_blank"&gt;SQL
Server Migration Assistant for Access v4.2&lt;/a&gt; (SSMA for Access). Next I turned my
attention to translating attributes between the systems and unfortunately, a free
text notes field in the Access database is used for a lot of things that need to be
translated to attributes. Some things I know how to translate, but some things have
to be determined by the administrators for the CRM System and Access application.
So I built a query in SQL on the tables formerly stored in the Access database and
used Excel (with MS Query) to create a report for the administrators, so they can
sort out how customer statuses should be translated.
&lt;/p&gt;
&lt;p&gt;
Extremely simplified, the query looked like this; 
&lt;/p&gt;
&lt;blockquote&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;SELECT&lt;/font&gt; tlbCustomer&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;CUST_CODE 
&lt;br /&gt;
&lt;font color="#808080"&gt;&amp;#160; ,&lt;/font&gt; tlbCustomer&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;PAY_REF 
&lt;br /&gt;
&lt;font color="#808080"&gt;&amp;#160; ,&lt;/font&gt; &lt;font color="#ff00ff"&gt;COALESCE&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff00ff"&gt;UPPER&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff00ff"&gt;REPLACE&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;tblNotes&lt;font color="#808080"&gt;.&lt;/font&gt;NOTE_TXT&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;'.'&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;''&lt;/font&gt;&lt;font color="#808080"&gt;)),&lt;/font&gt;&lt;font color="#ff0000"&gt;''&lt;/font&gt;&lt;font color="#808080"&gt;)&lt;/font&gt; &lt;font color="#0000ff"&gt;AS&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt; NOTE_TXT 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;FROM&lt;/font&gt; tlbCustomer &lt;font color="#808080"&gt;LEFT&lt;/font&gt; &lt;font color="#808080"&gt;OUTER&lt;/font&gt; &lt;font color="#808080"&gt;JOIN&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt; tblNotes 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ON&lt;/font&gt; tlbCustomer&lt;font color="#808080"&gt;.&lt;/font&gt;CUST_ID &lt;font color="#808080"&gt;=&lt;/font&gt; tblNotes&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;CUST_ID 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;GROUP&lt;/font&gt; &lt;font color="#0000ff"&gt;BY&lt;/font&gt; tlbCustomer&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;CUST_CODE 
&lt;br /&gt;
&lt;font color="#808080"&gt;&amp;#160; ,&lt;/font&gt; tlbCustomer&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;PAY_REF 
&lt;br /&gt;
&lt;font color="#808080"&gt;&amp;#160; ,&lt;/font&gt; &lt;font color="#ff00ff"&gt;UPPER&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff00ff"&gt;REPLACE&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;tblNotes&lt;font color="#808080"&gt;.&lt;/font&gt;NOTE_TXT&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;'.'&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;''&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;)) 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;ORDER&lt;/font&gt; &lt;font color="#0000ff"&gt;BY&lt;/font&gt; &lt;font color="#ff00ff"&gt;COUNT&lt;/font&gt;&lt;font color="#808080"&gt;(*)&lt;/font&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff"&gt;&lt;font size="2" face="Courier New"&gt;DESC&lt;/font&gt; 
&lt;br /&gt;
&lt;/font&gt;&lt;/blockquote&gt; 
&lt;p&gt;
&lt;/p&gt;
&lt;p&gt;
The query worked great on SQL, but did not return the text (NOTE_TXT) to Excel. 
&lt;/p&gt;
&lt;p&gt;
To cut the long story short, the original tblNotes.NOTE_TXT field in the Access database
was of type MEMO. SSMA 2008 for Access converted this to nvarchar(max) and somehow
MS Query (in Office 2007) does not return, nor throw an error on, large data types
(varchar(max), nvarchar(max)). A quick test showed that MAX was not really needed;
&lt;/p&gt;
&lt;blockquote&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;SELECT&lt;/font&gt; &lt;font color="#ff00ff"&gt;MAX&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff00ff"&gt;LEN&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;tblNotes&lt;font color="#808080"&gt;.&lt;/font&gt;NOTE_TXT&lt;font color="#808080"&gt;))&lt;/font&gt; &lt;font color="#0000ff"&gt;FROM&lt;/font&gt; tblNotes&lt;/font&gt;&lt;/font&gt;&lt;font size="+0"&gt; 
&lt;br /&gt;
&lt;/font&gt;&lt;/blockquote&gt; 
&lt;p&gt;
So modify the query to return NOTE_TXT as nvarchar(256) does the trick;
&lt;/p&gt;
&lt;blockquote&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#0000ff"&gt;SELECT&lt;/font&gt; tlbCustomer&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;CUST_CODE 
&lt;br /&gt;
&lt;font color="#808080"&gt;&amp;#160; ,&lt;/font&gt; tlbCustomer&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;PAY_REF 
&lt;br /&gt;
&lt;font color="#808080"&gt;&amp;#160; ,&lt;/font&gt; &lt;font color="#ff00ff"&gt;CAST&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;( 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#ff00ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; COALESCE&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff00ff"&gt;UPPER&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff00ff"&gt;REPLACE&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;tblNotes&lt;font color="#808080"&gt;.&lt;/font&gt;NOTE_TXT&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;'.'&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;''&lt;/font&gt;&lt;font color="#808080"&gt;)),&lt;/font&gt;&lt;font color="#ff0000"&gt;''&lt;/font&gt;&lt;font color="#808080"&gt;)&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt; 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160; AS&lt;/font&gt; &lt;font color="#0000ff"&gt;nvarchar&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;256&lt;font color="#808080"&gt;))&lt;/font&gt; &lt;font color="#0000ff"&gt;AS&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt; NOTE_TXT 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;FROM&lt;/font&gt; tlbCustomer &lt;font color="#808080"&gt;LEFT&lt;/font&gt; &lt;font color="#808080"&gt;OUTER&lt;/font&gt; &lt;font color="#808080"&gt;JOIN&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt; tblNotes 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; ON&lt;/font&gt; tlbCustomer&lt;font color="#808080"&gt;.&lt;/font&gt;CUST_ID &lt;font color="#808080"&gt;=&lt;/font&gt; tblNotes&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;CUST_ID 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;GROUP&lt;/font&gt; &lt;font color="#0000ff"&gt;BY&lt;/font&gt; tlbCustomer&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;CUST_CODE 
&lt;br /&gt;
&lt;font color="#808080"&gt;&amp;#160; ,&lt;/font&gt; tlbCustomer&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;PAY_REF 
&lt;br /&gt;
&lt;font color="#808080"&gt;&amp;#160; ,&lt;/font&gt; &lt;font color="#ff00ff"&gt;UPPER&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff00ff"&gt;REPLACE&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;tblNotes&lt;font color="#808080"&gt;.&lt;/font&gt;NOTE_TXT&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;'.'&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;''&lt;/font&gt;&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font face="Courier New"&gt;&lt;font color="#808080"&gt;)) 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;ORDER&lt;/font&gt; &lt;font color="#0000ff"&gt;BY&lt;/font&gt; &lt;font color="#ff00ff"&gt;COUNT&lt;/font&gt;&lt;font color="#808080"&gt;(*)&lt;/font&gt; &lt;/font&gt;&lt;/font&gt;&lt;font color="#0000ff"&gt;&lt;font size="2" face="Courier New"&gt;DESC&lt;/font&gt; 
&lt;br /&gt;
&lt;/font&gt;&lt;/blockquote&gt;&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=c97ef7e3-a574-4286-8eb8-650c2cba696e" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,c97ef7e3-a574-4286-8eb8-650c2cba696e.aspx</comments>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=af4f0b36-0b2f-47f7-bbe6-61216f6f9be3</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,af4f0b36-0b2f-47f7-bbe6-61216f6f9be3.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,af4f0b36-0b2f-47f7-bbe6-61216f6f9be3.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=af4f0b36-0b2f-47f7-bbe6-61216f6f9be3</wfw:commentRss>
      <slash:comments>2</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Sometimes one is confronted with forgetting an option... It all started with a database
that had a single data file of 1.8 GB and a transaction log more than 6 times the
size of that. Usually that would be a database in full recovery model and no transaction
log backups. This database was in simple recovery model though and according to the
Disk Usage report, 99.99% of the transaction log was free. So a DBCC SHRINKFILE should
solve the problem, however DBCC SHRINKFILE solved nothing, the log remained at about
11 GB.
</p>
        <p>
A query on sys.databases learned that this particular database had ACTIVE_TRANSACTION
for log_reuse_wait_desc. A peek in Activity Monitor then showed that there was an
open transaction for the database; a DELETE task with a huge wait time and the BROKER_RECEIVE_WAITFOR
wait type.
</p>
        <p>
Then it occurred to me this is the database that used to have <a title="Deadlock Alerts Through Event Notification" href="http://resquel.com/ssb/2009/03/02/DeadlockAlertsThroughEventNotification.aspx" target="_blank">a
deadlock problem</a>. After implementing some procedure fixes, there haven’t been
any deadlocks for over a half year and neither has the server (or service) been shutdown
in between.
</p>
        <p>
The stored procedure on the queue does a receive, but no timeout associated to it.
Effectively this means that the receive will wait inside the transaction, until a
messages comes into the queue. That hasn’t happened in the last six months. The resolution
is simple, add the TIMEOUT option to the RECEIVE and the procedure will stop when
there wasn’t any work to be done for the specified amount of time (in milliseconds).
For the code, see the post “<a title="Deadlock Alerts Through Event Notification" href="http://resquel.com/ssb/2009/03/02/DeadlockAlertsThroughEventNotification.aspx" target="_blank">Deadlock
Alerts Through Event Notification</a>”, which has been updated with the TIMEOUT option.
</p>
        <p>
After altering the procedure, kill the spid. Service Broker will restart, but now
with the TIMEOUT option in the procedure and stop after the specified amount of time
has elapsed without messages arriving in the queue.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=af4f0b36-0b2f-47f7-bbe6-61216f6f9be3" />
      </body>
      <title>Service Broker can make your transaction log big</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,af4f0b36-0b2f-47f7-bbe6-61216f6f9be3.aspx</guid>
      <link>http://resquel.com/ssb/2010/07/24/ServiceBrokerCanMakeYourTransactionLogBig.aspx</link>
      <pubDate>Sat, 24 Jul 2010 16:48:00 GMT</pubDate>
      <description>&lt;p&gt;
Sometimes one is confronted with forgetting an option... It all started with a database
that had a single data file of 1.8 GB and a transaction log more than 6 times the
size of that. Usually that would be a database in full recovery model and no transaction
log backups. This database was in simple recovery model though and according to the
Disk Usage report, 99.99% of the transaction log was free. So a DBCC SHRINKFILE should
solve the problem, however DBCC SHRINKFILE solved nothing, the log remained at about
11 GB.
&lt;/p&gt;
&lt;p&gt;
A query on sys.databases learned that this particular database had ACTIVE_TRANSACTION
for log_reuse_wait_desc. A peek in Activity Monitor then showed that there was an
open transaction for the database; a DELETE task with a huge wait time and the BROKER_RECEIVE_WAITFOR
wait type.
&lt;/p&gt;
&lt;p&gt;
Then it occurred to me this is the database that used to have &lt;a title="Deadlock Alerts Through Event Notification" href="http://resquel.com/ssb/2009/03/02/DeadlockAlertsThroughEventNotification.aspx" target="_blank"&gt;a
deadlock problem&lt;/a&gt;. After implementing some procedure fixes, there haven’t been
any deadlocks for over a half year and neither has the server (or service) been shutdown
in between.
&lt;/p&gt;
&lt;p&gt;
The stored procedure on the queue does a receive, but no timeout associated to it.
Effectively this means that the receive will wait inside the transaction, until a
messages comes into the queue. That hasn’t happened in the last six months. The resolution
is simple, add the TIMEOUT option to the RECEIVE and the procedure will stop when
there wasn’t any work to be done for the specified amount of time (in milliseconds).
For the code, see the post “&lt;a title="Deadlock Alerts Through Event Notification" href="http://resquel.com/ssb/2009/03/02/DeadlockAlertsThroughEventNotification.aspx" target="_blank"&gt;Deadlock
Alerts Through Event Notification&lt;/a&gt;”, which has been updated with the TIMEOUT option.
&lt;/p&gt;
&lt;p&gt;
After altering the procedure, kill the spid. Service Broker will restart, but now
with the TIMEOUT option in the procedure and stop after the specified amount of time
has elapsed without messages arriving in the queue.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=af4f0b36-0b2f-47f7-bbe6-61216f6f9be3" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,af4f0b36-0b2f-47f7-bbe6-61216f6f9be3.aspx</comments>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=05ac51dd-6982-4e80-86b6-0d9c4ecb5f1f</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,05ac51dd-6982-4e80-86b6-0d9c4ecb5f1f.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,05ac51dd-6982-4e80-86b6-0d9c4ecb5f1f.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=05ac51dd-6982-4e80-86b6-0d9c4ecb5f1f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
When restoring a backup of a database, SQL Server recreates the files with the same
sizes as listed in the backup, or at least tries to do so. The only option you have,
is specifying the path for each file. Generally not a problem, but every now and then
I encounter a database with a transaction log about 10 to 50 times the size of the
data file(s) . . . yep, full recovery model and just daily full backups. I am the
first to acknowledge transaction logs backups are key to prevent this situation, but
the next best thing still is that recent valid database backup. . . and then you can’t
restore it, because the 200 GB to write that big empty file called transaction log
cannot be allocated.
</p>
        <p>
Today was one of those days; 
</p>
        <blockquote>
          <font color="#ff0000" face="Courier">Msg 3257, Level 16, State 1, Line
1 
<br />
There is insufficient free space on disk volume 'D:\' to create the database. The
database requires 101197938688 additional free bytes, while only 19386400768 bytes
are available. 
<br />
Msg 3119, Level 16, State 4, Line 1 
<br />
Problems were identified while planning for the RESTORE statement. Previous messages
provide details. 
<br />
Msg 3013, Level 16, State 1, Line 1 
<br />
RESTORE DATABASE is terminating abnormally. 
<br /></font>
        </blockquote>
        <p>
Surely I wouldn’t be the only one having problems with this and I found two others
on connect;
</p>
        <ul>
          <li>
            <a href="https://connect.microsoft.com/SQLServer/feedback/details/532739" target="_blank">Option
to resize datafile/transaction log during a backup or restore operation</a>
          </li>
          <li>
            <a href="https://connect.microsoft.com/SQLServer/feedback/details/125287/" target="_blank">Restore
option to restore minimum file sizes</a>
          </li>
        </ul>
        <p>
I voted (as important) for both items, though the second one is already closed. Yet
I was triggered by the comments from the MS-people to ask for a specific solution
towards the transaction log;
</p>
        <h3>
          <a href="https://connect.microsoft.com/SQLServer/feedback/details/536710" target="_blank">RESTORE
DATABASE ... WITH STUB_LOG</a>
        </h3>
        <p>
One of the habits of RESTORE DATABASE is that it wants to allocate the log size (and
files) as found in the backup. Strange, as the size of those files is the result of
past activity (not contained in the backup being restored). The only space needed
in the transaction log, is that of the activity during the backup. Additional space
requirements may exist for subsequent restores of differential backups and log backups
(in norecovery or standby) or future activity (after recovery), but that isn’t an
issue as the log files would grow again during these restores. 
<br /><br />
The only real issue I can think of is having a database with fixed size transaction
log file(s), which means no auto grow. So for a WITH STUB_LOG option to work properly
in these scenarios, it may need to imply that the log file(s) are always set to unrestricted
auto grow (possibly only honoring the original increment). 
<br /><br />
It would at least prevent me from having to allocate 100GB for an empty transaction
log on 20GB available. 
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=05ac51dd-6982-4e80-86b6-0d9c4ecb5f1f" />
      </body>
      <title>Restoring Databases with big transaction logs (wishful thinking)</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,05ac51dd-6982-4e80-86b6-0d9c4ecb5f1f.aspx</guid>
      <link>http://resquel.com/ssb/2010/02/26/RestoringDatabasesWithBigTransactionLogsWishfulThinking.aspx</link>
      <pubDate>Fri, 26 Feb 2010 20:26:02 GMT</pubDate>
      <description>&lt;p&gt;
When restoring a backup of a database, SQL Server recreates the files with the same
sizes as listed in the backup, or at least tries to do so. The only option you have,
is specifying the path for each file. Generally not a problem, but every now and then
I encounter a database with a transaction log about 10 to 50 times the size of the
data file(s) . . . yep, full recovery model and just daily full backups. I am the
first to acknowledge transaction logs backups are key to prevent this situation, but
the next best thing still is that recent valid database backup. . . and then you can’t
restore it, because the 200 GB to write that big empty file called transaction log
cannot be allocated.
&lt;/p&gt;
&lt;p&gt;
Today was one of those days; 
&lt;/p&gt;
&lt;blockquote&gt;&lt;font color="#ff0000" face="Courier"&gt;Msg 3257, Level 16, State 1, Line
1 
&lt;br /&gt;
There is insufficient free space on disk volume 'D:\' to create the database. The
database requires 101197938688 additional free bytes, while only 19386400768 bytes
are available. 
&lt;br /&gt;
Msg 3119, Level 16, State 4, Line 1 
&lt;br /&gt;
Problems were identified while planning for the RESTORE statement. Previous messages
provide details. 
&lt;br /&gt;
Msg 3013, Level 16, State 1, Line 1 
&lt;br /&gt;
RESTORE DATABASE is terminating abnormally. 
&lt;br /&gt;
&lt;/font&gt;&lt;/blockquote&gt; 
&lt;p&gt;
Surely I wouldn’t be the only one having problems with this and I found two others
on connect;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://connect.microsoft.com/SQLServer/feedback/details/532739" target="_blank"&gt;Option
to resize datafile/transaction log during a backup or restore operation&lt;/a&gt; 
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://connect.microsoft.com/SQLServer/feedback/details/125287/" target="_blank"&gt;Restore
option to restore minimum file sizes&lt;/a&gt; 
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
I voted (as important) for both items, though the second one is already closed. Yet
I was triggered by the comments from the MS-people to ask for a specific solution
towards the transaction log;
&lt;/p&gt;
&lt;h3&gt;&lt;a href="https://connect.microsoft.com/SQLServer/feedback/details/536710" target="_blank"&gt;RESTORE
DATABASE ... WITH STUB_LOG&lt;/a&gt;
&lt;/h3&gt;
&lt;p&gt;
One of the habits of RESTORE DATABASE is that it wants to allocate the log size (and
files) as found in the backup. Strange, as the size of those files is the result of
past activity (not contained in the backup being restored). The only space needed
in the transaction log, is that of the activity during the backup. Additional space
requirements may exist for subsequent restores of differential backups and log backups
(in norecovery or standby) or future activity (after recovery), but that isn’t an
issue as the log files would grow again during these restores. 
&lt;br /&gt;
&lt;br /&gt;
The only real issue I can think of is having a database with fixed size transaction
log file(s), which means no auto grow. So for a WITH STUB_LOG option to work properly
in these scenarios, it may need to imply that the log file(s) are always set to unrestricted
auto grow (possibly only honoring the original increment). 
&lt;br /&gt;
&lt;br /&gt;
It would at least prevent me from having to allocate 100GB for an empty transaction
log on 20GB available. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=05ac51dd-6982-4e80-86b6-0d9c4ecb5f1f" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,05ac51dd-6982-4e80-86b6-0d9c4ecb5f1f.aspx</comments>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=feae038f-b53c-4b46-833c-049ec01b5b66</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,feae038f-b53c-4b46-833c-049ec01b5b66.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,feae038f-b53c-4b46-833c-049ec01b5b66.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=feae038f-b53c-4b46-833c-049ec01b5b66</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
If you're interested in expanding your skills in SQL Server, check out these events;
</p>
        <ul>
          <li>
            <a title="Quest Virtual Training for SQL Server" href="http://www.vconferenceonline.com/shows/spring10/quest/" target="_blank">Quest
Virtual Training for SQL Server</a>, an online and worldwide available event on Wednesday
March 3<sup>rd</sup></li>
          <li>
            <a title="SQL Zaterdag" href="http://www.sqlzaterdag.nl" target="_blank">SQL Zaterdag</a>,
hosted at InfoSupport in Veenendaal, Netherlands at Saturday March 6<sup>th</sup></li>
        </ul>
        <p>
Happy learning.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=feae038f-b53c-4b46-833c-049ec01b5b66" />
      </body>
      <title>Free SQL Server learning events (March 2010)</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,feae038f-b53c-4b46-833c-049ec01b5b66.aspx</guid>
      <link>http://resquel.com/ssb/2010/02/09/FreeSQLServerLearningEventsMarch2010.aspx</link>
      <pubDate>Tue, 09 Feb 2010 09:43:22 GMT</pubDate>
      <description>&lt;p&gt;
If you're interested in expanding your skills in SQL Server, check out these events;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;a title="Quest Virtual Training for SQL Server" href="http://www.vconferenceonline.com/shows/spring10/quest/" target="_blank"&gt;Quest
Virtual Training for SQL Server&lt;/a&gt;, an online and worldwide available event on Wednesday
March 3&lt;sup&gt;rd&lt;/sup&gt; 
&lt;li&gt;
&lt;a title="SQL Zaterdag" href="http://www.sqlzaterdag.nl" target="_blank"&gt;SQL Zaterdag&lt;/a&gt;,
hosted at InfoSupport in Veenendaal, Netherlands at Saturday March 6&lt;sup&gt;th&lt;/sup&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Happy learning.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=feae038f-b53c-4b46-833c-049ec01b5b66" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,feae038f-b53c-4b46-833c-049ec01b5b66.aspx</comments>
      <category>SQL Server</category>
      <category>Train_Cert</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=96d24ef8-78f7-44c5-98df-823a466f3ba0</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,96d24ef8-78f7-44c5-98df-823a466f3ba0.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,96d24ef8-78f7-44c5-98df-823a466f3ba0.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=96d24ef8-78f7-44c5-98df-823a466f3ba0</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Microsoft recently launched a Virtual Business Card site for Microsoft Certified Professionals.
So if you ever passed one or more of those Microsoft exams in the NT 4.0 or later
eras, check out the <a href="http://www.mcpvirtualbusinesscard.com">www.mcpvirtualbusinesscard.com</a> site
(Windows Live ID sign in to connect to you records in Microsoft's certification database).
</p>
        <p>
I set my profile page up to show off the credentials..
</p>
        <iframe frameborder="0" scrolling="no" width="397px" height="180px" src="https://www.mcpvirtualbusinesscard.com/VBCServer/StanSegers/card">
        </iframe>
        <p>
Also had a look at including the transcript, but that would be a 13-page pdf, a bit
of overkill if you ask me.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=96d24ef8-78f7-44c5-98df-823a466f3ba0" />
      </body>
      <title>MCP added value</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,96d24ef8-78f7-44c5-98df-823a466f3ba0.aspx</guid>
      <link>http://resquel.com/ssb/2009/12/18/MCPAddedValue.aspx</link>
      <pubDate>Fri, 18 Dec 2009 10:47:33 GMT</pubDate>
      <description>&lt;p&gt;
Microsoft recently launched a Virtual Business Card site for Microsoft Certified Professionals.
So if you ever passed one or more of those Microsoft exams in the NT 4.0 or later
eras, check out the &lt;a href="http://www.mcpvirtualbusinesscard.com"&gt;www.mcpvirtualbusinesscard.com&lt;/a&gt; site
(Windows Live ID sign in to connect to you records in Microsoft's certification database).
&lt;/p&gt;
&lt;p&gt;
I set my profile page up to show off the credentials..
&lt;/p&gt;
&lt;iframe frameborder="0" scrolling="no" width="397px" height="180px" src="https://www.mcpvirtualbusinesscard.com/VBCServer/StanSegers/card"&gt;
&lt;/iframe&gt;
&lt;p&gt;
Also had a look at including the transcript, but that would be a 13-page pdf, a bit
of overkill if you ask me.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=96d24ef8-78f7-44c5-98df-823a466f3ba0" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,96d24ef8-78f7-44c5-98df-823a466f3ba0.aspx</comments>
      <category>Train_Cert</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=98faceda-3d98-4059-9e5c-defe82adde2b</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,98faceda-3d98-4059-9e5c-defe82adde2b.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,98faceda-3d98-4059-9e5c-defe82adde2b.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=98faceda-3d98-4059-9e5c-defe82adde2b</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Yesterday I visited the <a href="http://sqlserverday.be" target="_blank">SQL Server
day 2009</a>, organized by the Belgian SQL Server User Group <a href="http://sqlug.be/" target="_blank">SQLUG.BE</a>,
in Mechelen. Congratulations on the event guys!
</p>
        <p>
After the keynote by Microsoft Belgium (I wish they had talked a little bit more about
SQL Azure), I visited the session by Henk van der Valk on world record ETL... now
Henk has control over some top notch iron, but that doesn't mean his tips don't apply
to a modest setup. Henk also mentioned he recently joined the blogsphere at <a href="http://www.henkvandervalk.com">www.henkvandervalk.com</a>.
</p>
        <p>
Next I sat (sorry I have to say so) horrible sponsored session by <a href="http://www.quest.com" target="_blank">Quest</a>...
and this has nothing to do with the FogLight product. On another occasion (an afternoon
session by Quest Netherlands) I witnessed the possibilities of Foglight (for .NET,
SQL Server and VMware) and I must say it's a good looking product. However we got
30 minutes of boring listing of challenges and day to day problems (as if we weren't
aware of them already) and in the end got some screenshots, which were completely
out of context. I would have be completely lost in the presentation if I hadn't been
to the session earlier by Quest NL.
</p>
        <p>
After that, I meant to sit the session "Reporting Services a DBA's tool? YES!!!",
but since the agenda card was a little confusing (or better said, I didn't pay enough
attention) I walked into the session by <a href="http://www.u2u.be/res/nico.aspx" target="_blank">Nico
Jacobs</a> on "Handling XML in SQL Server". Funny, as there was nothing new for me
in the session but still I really enjoyed it... most important because as a trainer
you rarely get an opportunity to see one of your colleagues at work on a familiar
subject. Thanks Nico, I really enjoyed it.
</p>
        <p>
The other session I attended was on "Policy Based Management", again by Nico Jacobs.
I hoped it would go deeper into the possibilities of implementing it for SQL Server
2005 and SQL Server 2000. Unfortunately that was not the case, so I'll have to dive
into the <a href="http://epmframework.codeplex.com/" target="_blank">Enterprise Policy
Management Framework</a> and PowerShell without a quick start. But again, it was a
joy listening to Nico.
</p>
        <p>
Final session and closing keynote was by <a href="http://cwebbbi.spaces.live.com/" target="_blank">Chris
Webb</a> on PowerPivot (a.k.a. Gemini). It wasn't my first glance at Gemini, but it
definitely is the kind of quick start I was looking for. Sitting a session like that
saves a day of looking for stuff.
</p>
        <p>
All-in-all, a day well spent.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=98faceda-3d98-4059-9e5c-defe82adde2b" />
      </body>
      <title>SQL Server day 2009</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,98faceda-3d98-4059-9e5c-defe82adde2b.aspx</guid>
      <link>http://resquel.com/ssb/2009/12/04/SQLServerDay2009.aspx</link>
      <pubDate>Fri, 04 Dec 2009 13:35:00 GMT</pubDate>
      <description>&lt;p&gt;
Yesterday I visited the &lt;a href="http://sqlserverday.be" target="_blank"&gt;SQL Server
day 2009&lt;/a&gt;, organized by the Belgian SQL Server User Group &lt;a href="http://sqlug.be/" target="_blank"&gt;SQLUG.BE&lt;/a&gt;,
in Mechelen. Congratulations on the event guys!
&lt;/p&gt;
&lt;p&gt;
After the keynote by Microsoft Belgium (I wish they had talked a little bit more about
SQL Azure), I visited the session by Henk van der Valk on world record ETL... now
Henk has control over some top notch iron, but that doesn't mean his tips don't apply
to a modest setup. Henk also mentioned he recently joined the blogsphere at &lt;a href="http://www.henkvandervalk.com"&gt;www.henkvandervalk.com&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Next I sat (sorry I have to say so) horrible sponsored session by &lt;a href="http://www.quest.com" target="_blank"&gt;Quest&lt;/a&gt;...
and this has nothing to do with the FogLight product. On another occasion (an afternoon
session by Quest Netherlands) I witnessed the possibilities of Foglight (for .NET,
SQL Server and VMware) and I must say it's a good looking product. However we got
30 minutes of boring listing of challenges and day to day problems (as if we weren't
aware of them already) and in the end got some screenshots, which were completely
out of context. I would have be completely lost in the presentation if I hadn't been
to the session earlier by Quest NL.
&lt;/p&gt;
&lt;p&gt;
After that, I meant to sit the session "Reporting Services a DBA's tool? YES!!!",
but since the agenda card was a little confusing (or better said, I didn't pay enough
attention) I walked into the session by &lt;a href="http://www.u2u.be/res/nico.aspx" target="_blank"&gt;Nico
Jacobs&lt;/a&gt; on "Handling XML in SQL Server". Funny, as there was nothing new for me
in the session but still I really enjoyed it... most important because as a trainer
you rarely get an opportunity to see one of your colleagues at work on a familiar
subject. Thanks Nico, I really enjoyed it.
&lt;/p&gt;
&lt;p&gt;
The other session I attended was on "Policy Based Management", again by Nico Jacobs.
I hoped it would go deeper into the possibilities of implementing it for SQL Server
2005 and SQL Server 2000. Unfortunately that was not the case, so I'll have to dive
into the &lt;a href="http://epmframework.codeplex.com/" target="_blank"&gt;Enterprise Policy
Management Framework&lt;/a&gt; and PowerShell without a quick start. But again, it was a
joy listening to Nico.
&lt;/p&gt;
&lt;p&gt;
Final session and closing keynote was by &lt;a href="http://cwebbbi.spaces.live.com/" target="_blank"&gt;Chris
Webb&lt;/a&gt; on PowerPivot (a.k.a. Gemini). It wasn't my first glance at Gemini, but it
definitely is the kind of quick start I was looking for. Sitting a session like that
saves a day of looking for stuff.
&lt;/p&gt;
&lt;p&gt;
All-in-all, a day well spent.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=98faceda-3d98-4059-9e5c-defe82adde2b" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,98faceda-3d98-4059-9e5c-defe82adde2b.aspx</comments>
      <category>Intergation Services</category>
      <category>SQL Server</category>
      <category>Train_Cert</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=fafaec29-44bb-4ca2-bf2d-ddfba92f6352</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,fafaec29-44bb-4ca2-bf2d-ddfba92f6352.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,fafaec29-44bb-4ca2-bf2d-ddfba92f6352.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=fafaec29-44bb-4ca2-bf2d-ddfba92f6352</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Creating linked servers between two instances of SQL Server may not always be as straight
forward as hoped. To make it more complicated, interference may be caused by matters
beyond the control of the database administrator; Active Directory. After creating
a linked server, being able to successfully use it may be the real challenge, it may
even work for one user and fail for another.
</p>
        <h3>Service Account
</h3>
        <p>
To communicate successfully in a domain environment, the service account for your
database engine is the first to check/configure. Possible choices for the service
account are;
</p>
        <ul>
          <li>
Domain User (recommended) 
</li>
          <li>
Local User 
</li>
          <li>
LocalSystem 
</li>
          <li>
LocalService 
</li>
          <li>
NetworkService (recommended)</li>
        </ul>
        <p>
From those options, a local user or LocalService for the service account will eventually
stop you. In both cases a SQL Server has the permission set of user on the machine
where SQL Server operates, with no identity on the network. In case the SQL Server
has to connect to any remote resource, it will do so as an anonymous user (null session).
Unless you want to be absolutely sure that SQL Server should never do anything beyond
anonymous on the network, you can go this route... but hey, you're reading about linked
servers, so just drop the thought of using a local user or LocalService.
</p>
        <p>
Valid options to enable SQL Server to operate on the network are LocalSystem, NetworkService
and Domain User. All have the potential to work well, but LocalSystem is not recommended
from a security perspective. LocalSystem has the permission set of an administrator
on the system where SQL Server operates, anyone obtaining control over your SQL Server
has full access to the underlying operating system (think about the damage that could
be done when xp_cmdshell is enabled). From a network perspective, LocalSystem and
NetworkService are identical; when the SQL Server operates over the network, it will
do so under the credentials of the Windows machine. With a domain user as service
account, SQL Server has user permissions locally (as is the case with NetworkService,
LocalService and a local user), on the network SQL Server will present the credentials
of the domain user.
</p>
        <p>
So two good options remain; NetworkService and a domain user. Which one you chose
mainly depends on the advantages of one over the other;
</p>
        <ul>
          <li>
NetworkService: 
<ul><li>
No account or password management in Active Directory 
</li><li>
Service Principal Names are automatically generated</li></ul></li>
          <li>
Domain User 
<ul><li>
Multiple instances on different machines can have the same identity which is required
for clustering. 
</li><li>
Transparent security for Database Mirroring.  
</li><li>
Usually SQL Agent will be the same account, this makes things easier with Log Shipping
and Replication. <a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/ChangeServiceAccount_2.png"><img style="border-right-width: 0px; margin: 5px 0px 5px 5px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="ChangeServiceAccount" align="right" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/ChangeServiceAccount_thumb.png" width="244" height="130" /></a></li><li>
Improved access to Service Master Key (in case you don't have a backup of the key
itself). <a title="BOL -- ALTER SERVICE MASTER KEY" href="http://technet.microsoft.com/en-us/library/ms187788(SQL.100).aspx" target="_blank">--&gt;</a></li></ul></li>
        </ul>
        <p>
Personally, I favor using a domain user.
</p>
        <p>
Should you find that you need to change the Service Account; use the "SQL Server Configuration
Manager" 
</p>
        <h3>Configuring and using a SQL Server linked server for Windows Authentication
</h3>
        <p>
          <a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/NewLinkedServer1_2.png">
            <img style="border-right-width: 0px; margin: 5px 5px 5px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="NewLinkedServer1" align="left" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/NewLinkedServer1_thumb.png" width="215" height="143" />
          </a>There
are two ways to go about the configuration; graphically and via stored procedure.
The graphical approach brings up a dialog, where just a little configuration is needed.
</p>
        <p>
On the General page, select the server type to be SQL Server (choosing SQL Server
Native or OLEDB drivers will work too, but require extra configuration) and type the
name of the target instance using the flat name or FQDN followed by a backslash and
instance name in case of a named instance.
</p>
        <p>
On the Security page, select the option to "Be made using the login's current security
context", which effectively tells SQL Server to impersonate or delegate the login
requesting access though the linked server.
</p>
        <p>
On the Server Options page, verify that Data Access is set to True. To see the stored
procedures involved, either use the Script button on top of the dialog instead of
pressing ok or right-click a configured linked server and "Script Linked Server as
&gt;".
</p>
        <p>
To avoid running into (later explained) problems when creating linked servers, create
the linked server from a shell (interactive or remote desktop) on the Windows machine
where the linked server is configured.
</p>
        <p>
To use the linked server, issue the query 
</p>
        <blockquote>
          <p>
SELECT TOP 50 * FROM [HOST.NET.LOCAL\INSTANCE].AdventureWorks.Sales.SalesOrderDetail 
</p>
        </blockquote>
        <p>
Note that the linked server HOST.NET.LOCAL\INSTANCE is enclosed in braces, which is
required when the server name is fully qualified and/or when it is a named instance.
If you don't want to expose host and/or instance names, consider creating an alias
in "SQL Server Configuration Manager" under client configuration and point the linked
server to the alias.
</p>
        <h3>Impersonation and delegation
</h3>
        <p>
During the configuration (Security page) impersonation and delegation were mentioned.
Also to avoid problems while configuring a linked server, the best way to do so was
on the server where the linked server was to be created. The reason to do so is to
avoid delegation. Below are a summary of impersonation and delegation (and links to
a more complete overview). Windows accounts will be impersonated if necessary, Windows
accounts will not be delegated unless both account and delegating system are configured
to do so. 
</p>
        <p>
Impersonation happens when a process connects to resources pretending to have the
identity of it's caller. Like when a user Bobbie is connected to SQLINST1 and issues
a query for a table via a linked server on SQLINST2. Depending on the configuration
of the linked server, SQLINST1 will pretend to be Bobbie when connecting on Bobbies
behalf to SQLINST2. For a more detailed overview of impersonation, check the <a title="What is impersonation" href="http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook/WhatIsImpersonation.html" target="_blank">Pluralsight-wiki
on impersonation</a>. Note that impersonation is limited to the  system where
process making the call is active.
</p>
        <p>
Delegation is much like impersonation, with the additional feature that delegation
is not limited to the system of the process making the call. Because the process now
has to forward the credentials to another system and prove to operate on behalf of
someone else than the system itself, the <a title="What is Kerberos" href="http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook/WhatIsKerberos.html" target="_blank">Kerberos</a> protocol
is required (that or knowledge of the password of the delegated user). For a more
detailed overview of delegation, check the <a title="What is delegation" href="http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook/WhatIsDelegation.html" target="_blank">Pluralsight-wiki
on delegation</a>.
</p>
        <p>
          <a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/UserDelegationProperty_2.png">
            <img style="border-right-width: 0px; margin: 5px 0px 5px 5px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="UserDelegationProperty" align="right" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/UserDelegationProperty_thumb.png" width="204" height="244" />
          </a>From
the perspective of Windows systems, Bobbie (DOMAIN\Bobbie) is logged in on a typical
client system (Windows XP, Vista or 7) and uses an application that connects to SERVER1\SQLINST1.
In SQLINST1 two linked servers are configured; SERVER1 (being the default instance
on SERVER1) and SERVER2\SQLINST1. The context of the application is DOMAIN\Bobbie
and thus an access token for DOMAIN\Bobbie is handed to SERVER1 (for logging in on
the SQL Server service SERVER1\SQLINST1). When the application issues a query for
an object on the default instance of SERVER1, one local process on SERVER1 has to
impersonate DOMAIN\Bobbie to the other local process on SERVER1. This will succeed
and can work both with NTLM and Kerberos, because SERVER1 trusts itself and the domain;
DOMAIN\Bobbie was authenticated by the domain and is only used by SERVER1 locally. 
</p>
        <p>
Next the application calls for SERVER2 and now the challenge is on! Bobbie get's a 
</p>
        <blockquote>
          <p>
Msg 18456, Level 14, State 1, Line 1<br />
Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
</p>
        </blockquote>
        <p>
          <a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/SystemDelegationProperty_2.png">
            <img style="border-right-width: 0px; margin: 5px 5px 5px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="SystemDelegationProperty" align="left" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/SystemDelegationProperty_thumb.png" width="220" height="244" />
          </a>SERVER2
does not trust SERVER1 and therefor will not accept the claim of SERVER1 that it acts
on Bobbies behalf, SERVER2 wants to see PROOF! One way of proof would be if SERVER1
had the password of Bobbie, indicating that Bobbie fully trusts SERVER1... but Lesson
number 1; never give anyone your password. Lesson number 2; don't write it on a Post-It...
Lesson number 3; don't trust computers that violate lessons 1 or 2. The other option
is to turn to a mutually trusted third party: the domain, but now the configuration
needs to be right for Kerberos.
</p>
        <p>
For Kerberos to do it's trick, the user (Domain\Bobbie) and the machine delegating
(SERVER1) the user have to be trusted for delegation. This is sufficient if the SQL
Server services on SERVER1\SQLINST1 and SERVER2 run as NetworkService.
</p>
        <p>
In case SERVER1\SQLINST1 is running under a domain user account, trust that user account
for delegation, not the server.
</p>
        <h3>Service Principal Name
</h3>
        <p>
Kerberos distinguishes three roles; 
</p>
        <ul>
          <li>
Key Distribution Center (KDC), in Active Directory this role is held by the Domain
Controllers. 
</li>
          <li>
Client, these are the users. 
</li>
          <li>
Server, these are the computers in your network.</li>
        </ul>
        <p>
With this basic division of roles, things work well in a domain, until a service account
like that of the SQL Server database engine is a domain user. If so, that particular
domain user should be made a server too. To mark an account as a service, a Service
Principal Name must be registered. With NetworkService, this was done by the server
(because servers can write SPN's on their own behalf), for any other account, by default
only Domain Administrators can write an SPN. Use the SetSPN tool to mark an account
as Service Principal Name, SetSPN can be <a title="SetSPN" href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;FamilyID=5fd831fd-ab77-46a3-9cfe-ff01d29e5c46" target="_blank">downloaded</a>,
is part of the Windows Server 2003 Support Tools and is installed on Windows Server
2008 with the AD DS role.
</p>
        <p>
To register an SPN for a service account, use the following syntax:
</p>
        <blockquote>
          <p>
SetSPN -a MsSqlSvc/<font color="#0000ff">server1.domain.local</font>:<font color="#0000ff">1433
domain\user</font></p>
          <p>
SetSPN -a MsSqlSvc/<font color="#0000ff">server1</font>:<font color="#0000ff">1433
domain\user</font></p>
        </blockquote>
        <p>
Substitute the blue parts in the syntax for the server names (FQDN and NetBIOS), the
port where SQL Server listens (this could be different from 1433, especially when
registering an SPN for additional instances) and the domain user account used as SQL
Server service account.
</p>
        <p>
When running more SQL Server instances under the same domain account, it may be useful
to check the approach listed in Step 3 of <a title="How to use Kerberos authentication in SQL Server" href="http://support.microsoft.com/kb/319723" target="_blank">How
to use Kerberos authentication in SQL Server</a>, so the AD-people have to be called
upon only once for the service account, not for every instance installation.
</p>
        <p>
Reference Environment
</p>
        <p>
          <a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/ReferenceEnvironment_4.png">
            <img style="border-right-width: 0px; margin: 5px 0px 5px 5px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="ReferenceEnvironment" align="right" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/ReferenceEnvironment_thumb_1.png" width="244" height="215" />
          </a> As
reference environment (may be a layout for a test-network), the following machines,
users and services are used in a single domain configuration;
</p>
        <ul>
          <li>
Windows Server 2003 or 2008: DC1 (domain controller), SERVER1 (member), SERVER2 (member) 
</li>
          <li>
Windows XP, Vista or 7: CLIENT (member) 
</li>
          <li>
SQL Server 2005 or 2008: SERVER1 (default instance) tcp 1433 static, SERVER1\SQLINST1
tcp dynamic (named instance), SERVER2\SQLINST1 (named instance) tcp 1433 static</li>
        </ul>
        <p>
To make it work, user Domain\Bobbie has to be trusted for delegation in all four scenario's.
Depending on the service accounts for SERVER1\SQLINST1 and SERVER2\SQLINST2, use the
following settings
</p>
        <table border="1" cellspacing="0" cellpadding="2" width="100%">
          <tbody>
            <tr>
              <td valign="top">
                <strong>Service account for SERVER1\SQLINST1</strong>
              </td>
              <td valign="top">
                <strong>Service account for SERVER2\SQLINST1</strong>
              </td>
              <td valign="top">
                <strong>Users and or computers to be trusted for delegation</strong>
              </td>
              <td valign="top">
                <strong>SPN's to create</strong>
              </td>
            </tr>
            <tr>
              <td valign="top">
NetworkService</td>
              <td valign="top">
NetworkService</td>
              <td valign="top">
SERVER1<br />
DOMAIN\Bobbie</td>
              <td valign="top">
&lt;none&gt;</td>
            </tr>
            <tr>
              <td valign="top">
NetworkService</td>
              <td valign="top">
DOMAIN\SRV_SQL2</td>
              <td valign="top">
SERVER1<br />
DOMAIN\Bobbie</td>
              <td valign="top">
MSSQLSvc/server2.domain.local:1433 DOMAIN\SRV_SQL2</td>
            </tr>
            <tr>
              <td valign="top">
DOMAIN\SRV_SQL1</td>
              <td valign="top">
NetworkService</td>
              <td valign="top">
DOMAIN\SRV_SQL1<br />
DOMAIN\Bobbie</td>
              <td valign="top">
&lt;none&gt;</td>
            </tr>
            <tr>
              <td valign="top">
DOMAIN\SRV_SQL1</td>
              <td valign="top">
DOMAIN\SRV_SQL2</td>
              <td valign="top">
DOMAIN\SRV_SQL1<br />
DOMAIN\Bobbie</td>
              <td valign="top">
MSSQLSvc/server2.domain.local:1433 DOMAIN\SRV_SQL2</td>
            </tr>
          </tbody>
        </table>
        <h3>Things to consider
</h3>
        <p>
          <a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/AdjustKerberosIntervals_2.png">
            <img style="border-right-width: 0px; margin: 5px 5px 5px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="AdjustKerberosIntervals" align="left" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/AdjustKerberosIntervals_thumb.png" width="244" height="104" />
          </a> When
working with Kerberos, tickets are granted for a longer period of time (typically
10 hours), so configuration changes usually take a longer time to apply. Use the KERBTRAY
and/or KLIST <a title="Windows Server 2003 Resource Kit" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&amp;displaylang=en" target="_blank">resource
kit utilities</a> to view and purge tickets, in order to speed things up (avoids rebooting
systems or logging off/on users). Alternatively, consider shortening the ticket lifetimes
to the minimum; 10 minutes for services and 1 hour for users.
</p>
        <p>
When configuring the linked servers, it was best to do so at the server where the
linked server is configured from. Depending on your role you may be a domain admin,
so should domain admins be delegated in order to make setting up linked servers easier?
Rather not! Imagine someone installed malicious program or script on a system that
is trusted for delegation and somehow a domain administrator executes this program
or script. It could hurt any system or user in your network.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=fafaec29-44bb-4ca2-bf2d-ddfba92f6352" />
      </body>
      <title>SQL Server linked servers with Windows Authentication</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,fafaec29-44bb-4ca2-bf2d-ddfba92f6352.aspx</guid>
      <link>http://resquel.com/ssb/2009/11/21/SQLServerLinkedServersWithWindowsAuthentication.aspx</link>
      <pubDate>Sat, 21 Nov 2009 19:56:50 GMT</pubDate>
      <description>&lt;p&gt;
Creating linked servers between two instances of SQL Server may not always be as straight
forward as hoped. To make it more complicated, interference may be caused by matters
beyond the control of the database administrator; Active Directory. After creating
a linked server, being able to successfully use it may be the real challenge, it may
even work for one user and fail for another.
&lt;/p&gt;
&lt;h3&gt;Service Account
&lt;/h3&gt;
&lt;p&gt;
To communicate successfully in a domain environment, the service account for your
database engine is the first to check/configure. Possible choices for the service
account are;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Domain User (recommended) 
&lt;li&gt;
Local User 
&lt;li&gt;
LocalSystem 
&lt;li&gt;
LocalService 
&lt;li&gt;
NetworkService (recommended)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
From those options, a local user or LocalService for the service account will eventually
stop you. In both cases a SQL Server has the permission set of user on the machine
where SQL Server operates, with no identity on the network. In case the SQL Server
has to connect to any remote resource, it will do so as an anonymous user (null session).
Unless you want to be absolutely sure that SQL Server should never do anything beyond
anonymous on the network, you can go this route... but hey, you're reading about linked
servers, so just drop the thought of using a local user or LocalService.
&lt;/p&gt;
&lt;p&gt;
Valid options to enable SQL Server to operate on the network are LocalSystem, NetworkService
and Domain User. All have the potential to work well, but LocalSystem is not recommended
from a security perspective. LocalSystem has the permission set of an administrator
on the system where SQL Server operates, anyone obtaining control over your SQL Server
has full access to the underlying operating system (think about the damage that could
be done when xp_cmdshell is enabled). From a network perspective, LocalSystem and
NetworkService are identical; when the SQL Server operates over the network, it will
do so under the credentials of the Windows machine. With a domain user as service
account, SQL Server has user permissions locally (as is the case with NetworkService,
LocalService and a local user), on the network SQL Server will present the credentials
of the domain user.
&lt;/p&gt;
&lt;p&gt;
So two good options remain; NetworkService and a domain user. Which one you chose
mainly depends on the advantages of one over the other;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
NetworkService: 
&lt;ul&gt;
&lt;li&gt;
No account or password management in Active Directory 
&lt;li&gt;
Service Principal Names are automatically generated&lt;/li&gt;
&lt;/ul&gt;
&lt;li&gt;
Domain User 
&lt;ul&gt;
&lt;li&gt;
Multiple instances on different machines can have the same identity which is required
for clustering. 
&lt;li&gt;
Transparent security for Database Mirroring.&amp;nbsp; 
&lt;li&gt;
Usually SQL Agent will be the same account, this makes things easier with Log Shipping
and Replication. &lt;a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/ChangeServiceAccount_2.png"&gt;&lt;img style="border-right-width: 0px; margin: 5px 0px 5px 5px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="ChangeServiceAccount" align="right" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/ChangeServiceAccount_thumb.png" width="244" height="130"&gt;&lt;/a&gt; 
&lt;li&gt;
Improved access to Service Master Key (in case you don't have a backup of the key
itself). &lt;a title="BOL -- ALTER SERVICE MASTER KEY" href="http://technet.microsoft.com/en-us/library/ms187788(SQL.100).aspx" target="_blank"&gt;--&amp;gt;&lt;/a&gt;&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Personally, I favor using a domain user.
&lt;/p&gt;
&lt;p&gt;
Should you find that you need to change the Service Account; use the "SQL Server Configuration
Manager" 
&lt;/p&gt;
&lt;h3&gt;Configuring and using a SQL Server linked server for Windows Authentication
&lt;/h3&gt;
&lt;p&gt;
&lt;a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/NewLinkedServer1_2.png"&gt;&lt;img style="border-right-width: 0px; margin: 5px 5px 5px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="NewLinkedServer1" align="left" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/NewLinkedServer1_thumb.png" width="215" height="143"&gt;&lt;/a&gt;There
are two ways to go about the configuration; graphically and via stored procedure.
The graphical approach brings up a dialog, where just a little configuration is needed.
&lt;/p&gt;
&lt;p&gt;
On the General page, select the server type to be SQL Server (choosing SQL Server
Native or OLEDB drivers will work too, but require extra configuration) and type the
name of the target instance using the flat name or FQDN followed by a backslash and
instance name in case of a named instance.
&lt;/p&gt;
&lt;p&gt;
On the Security page, select the option to "Be made using the login's current security
context", which effectively tells SQL Server to impersonate or delegate the login
requesting access though the linked server.
&lt;/p&gt;
&lt;p&gt;
On the Server Options page, verify that Data Access is set to True. To see the stored
procedures involved, either use the Script button on top of the dialog instead of
pressing ok or right-click a configured linked server and "Script Linked Server as
&amp;gt;".
&lt;/p&gt;
&lt;p&gt;
To avoid running into (later explained) problems when creating linked servers, create
the linked server from a shell (interactive or remote desktop) on the Windows machine
where the linked server is configured.
&lt;/p&gt;
&lt;p&gt;
To use the linked server, issue the query 
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
SELECT TOP 50 * FROM [HOST.NET.LOCAL\INSTANCE].AdventureWorks.Sales.SalesOrderDetail 
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Note that the linked server HOST.NET.LOCAL\INSTANCE is enclosed in braces, which is
required when the server name is fully qualified and/or when it is a named instance.
If you don't want to expose host and/or instance names, consider creating an alias
in "SQL Server Configuration Manager" under client configuration and point the linked
server to the alias.
&lt;/p&gt;
&lt;h3&gt;Impersonation and delegation
&lt;/h3&gt;
&lt;p&gt;
During the configuration (Security page) impersonation and delegation were mentioned.
Also to avoid problems while configuring a linked server, the best way to do so was
on the server where the linked server was to be created. The reason to do so is to
avoid delegation. Below are a summary of impersonation and delegation (and links to
a more complete overview). Windows accounts will be impersonated if necessary, Windows
accounts will not be delegated unless both account and delegating system are configured
to do so. 
&lt;/p&gt;
&lt;p&gt;
Impersonation happens when a process connects to resources pretending to have the
identity of it's caller. Like when a user Bobbie is connected to SQLINST1 and issues
a query for a table via a linked server on SQLINST2. Depending on the configuration
of the linked server, SQLINST1 will pretend to be Bobbie when connecting on Bobbies
behalf to SQLINST2. For a more detailed overview of impersonation, check the &lt;a title="What is impersonation" href="http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook/WhatIsImpersonation.html" target="_blank"&gt;Pluralsight-wiki
on impersonation&lt;/a&gt;. Note that impersonation is limited to the&amp;nbsp; system where
process making the call is active.
&lt;/p&gt;
&lt;p&gt;
Delegation is much like impersonation, with the additional feature that delegation
is not limited to the system of the process making the call. Because the process now
has to forward the credentials to another system and prove to operate on behalf of
someone else than the system itself, the &lt;a title="What is Kerberos" href="http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook/WhatIsKerberos.html" target="_blank"&gt;Kerberos&lt;/a&gt; protocol
is required (that or knowledge of the password of the delegated user). For a more
detailed overview of delegation, check the &lt;a title="What is delegation" href="http://alt.pluralsight.com/wiki/default.aspx/Keith.GuideBook/WhatIsDelegation.html" target="_blank"&gt;Pluralsight-wiki
on delegation&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/UserDelegationProperty_2.png"&gt;&lt;img style="border-right-width: 0px; margin: 5px 0px 5px 5px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="UserDelegationProperty" align="right" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/UserDelegationProperty_thumb.png" width="204" height="244"&gt;&lt;/a&gt;From
the perspective of Windows systems, Bobbie (DOMAIN\Bobbie) is logged in on a typical
client system (Windows XP, Vista or 7) and uses an application that connects to SERVER1\SQLINST1.
In SQLINST1 two linked servers are configured; SERVER1 (being the default instance
on SERVER1) and SERVER2\SQLINST1. The context of the application is DOMAIN\Bobbie
and thus an access token for DOMAIN\Bobbie is handed to SERVER1 (for logging in on
the SQL Server service SERVER1\SQLINST1). When the application issues a query for
an object on the default instance of SERVER1, one local process on SERVER1 has to
impersonate DOMAIN\Bobbie to the other local process on SERVER1. This will succeed
and can work both with NTLM and Kerberos, because SERVER1 trusts itself and the domain;
DOMAIN\Bobbie was authenticated by the domain and is only used by SERVER1 locally. 
&lt;/p&gt;
&lt;p&gt;
Next the application calls for SERVER2 and now the challenge is on! Bobbie get's a 
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Msg 18456, Level 14, State 1, Line 1&lt;br&gt;
Login failed for user 'NT AUTHORITY\ANONYMOUS LOGON'.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
&lt;a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/SystemDelegationProperty_2.png"&gt;&lt;img style="border-right-width: 0px; margin: 5px 5px 5px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="SystemDelegationProperty" align="left" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/SystemDelegationProperty_thumb.png" width="220" height="244"&gt;&lt;/a&gt;SERVER2
does not trust SERVER1 and therefor will not accept the claim of SERVER1 that it acts
on Bobbies behalf, SERVER2 wants to see PROOF! One way of proof would be if SERVER1
had the password of Bobbie, indicating that Bobbie fully trusts SERVER1... but Lesson
number 1; never give anyone your password. Lesson number 2; don't write it on a Post-It...
Lesson number 3; don't trust computers that violate lessons 1 or 2. The other option
is to turn to a mutually trusted third party: the domain, but now the configuration
needs to be right for Kerberos.
&lt;/p&gt;
&lt;p&gt;
For Kerberos to do it's trick, the user (Domain\Bobbie) and the machine delegating
(SERVER1) the user have to be trusted for delegation. This is sufficient if the SQL
Server services on SERVER1\SQLINST1 and SERVER2 run as NetworkService.
&lt;/p&gt;
&lt;p&gt;
In case SERVER1\SQLINST1 is running under a domain user account, trust that user account
for delegation, not the server.
&lt;/p&gt;
&lt;h3&gt;Service Principal Name
&lt;/h3&gt;
&lt;p&gt;
Kerberos distinguishes three roles; 
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Key Distribution Center (KDC), in Active Directory this role is held by the Domain
Controllers. 
&lt;li&gt;
Client, these are the users. 
&lt;li&gt;
Server, these are the computers in your network.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
With this basic division of roles, things work well in a domain, until a service account
like that of the SQL Server database engine is a domain user. If so, that particular
domain user should be made a server too. To mark an account as a service, a Service
Principal Name must be registered. With NetworkService, this was done by the server
(because servers can write SPN's on their own behalf), for any other account, by default
only Domain Administrators can write an SPN. Use the SetSPN tool to mark an account
as Service Principal Name, SetSPN can be &lt;a title="SetSPN" href="http://www.microsoft.com/downloads/details.aspx?displaylang=en&amp;amp;FamilyID=5fd831fd-ab77-46a3-9cfe-ff01d29e5c46" target="_blank"&gt;downloaded&lt;/a&gt;,
is part of the Windows Server 2003 Support Tools and is installed on Windows Server
2008 with the AD DS role.
&lt;/p&gt;
&lt;p&gt;
To register an SPN for a service account, use the following syntax:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
SetSPN -a MsSqlSvc/&lt;font color="#0000ff"&gt;server1.domain.local&lt;/font&gt;:&lt;font color="#0000ff"&gt;1433
domain\user&lt;/font&gt;
&lt;/p&gt;
&lt;p&gt;
SetSPN -a MsSqlSvc/&lt;font color="#0000ff"&gt;server1&lt;/font&gt;:&lt;font color="#0000ff"&gt;1433
domain\user&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Substitute the blue parts in the syntax for the server names (FQDN and NetBIOS), the
port where SQL Server listens (this could be different from 1433, especially when
registering an SPN for additional instances) and the domain user account used as SQL
Server service account.
&lt;/p&gt;
&lt;p&gt;
When running more SQL Server instances under the same domain account, it may be useful
to check the approach listed in Step 3 of &lt;a title="How to use Kerberos authentication in SQL Server" href="http://support.microsoft.com/kb/319723" target="_blank"&gt;How
to use Kerberos authentication in SQL Server&lt;/a&gt;, so the AD-people have to be called
upon only once for the service account, not for every instance installation.
&lt;/p&gt;
&lt;p&gt;
Reference Environment
&lt;/p&gt;
&lt;p&gt;
&lt;a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/ReferenceEnvironment_4.png"&gt;&lt;img style="border-right-width: 0px; margin: 5px 0px 5px 5px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="ReferenceEnvironment" align="right" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/ReferenceEnvironment_thumb_1.png" width="244" height="215"&gt;&lt;/a&gt; As
reference environment (may be a layout for a test-network), the following machines,
users and services are used in a single domain configuration;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Windows Server 2003 or 2008: DC1 (domain controller), SERVER1 (member), SERVER2 (member) 
&lt;li&gt;
Windows XP, Vista or 7: CLIENT (member) 
&lt;li&gt;
SQL Server 2005 or 2008: SERVER1 (default instance) tcp 1433 static, SERVER1\SQLINST1
tcp dynamic (named instance), SERVER2\SQLINST1 (named instance) tcp 1433 static&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
To make it work, user Domain\Bobbie has to be trusted for delegation in all four scenario's.
Depending on the service accounts for SERVER1\SQLINST1 and SERVER2\SQLINST2, use the
following settings
&lt;/p&gt;
&lt;table border="1" cellspacing="0" cellpadding="2" width="100%"&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td valign="top"&gt;
&lt;strong&gt;Service account for SERVER1\SQLINST1&lt;/strong&gt;&lt;/td&gt;
&lt;td valign="top"&gt;
&lt;strong&gt;Service account for SERVER2\SQLINST1&lt;/strong&gt;&lt;/td&gt;
&lt;td valign="top"&gt;
&lt;strong&gt;Users and or computers to be trusted for delegation&lt;/strong&gt;&lt;/td&gt;
&lt;td valign="top"&gt;
&lt;strong&gt;SPN's to create&lt;/strong&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top"&gt;
NetworkService&lt;/td&gt;
&lt;td valign="top"&gt;
NetworkService&lt;/td&gt;
&lt;td valign="top"&gt;
SERVER1&lt;br&gt;
DOMAIN\Bobbie&lt;/td&gt;
&lt;td valign="top"&gt;
&amp;lt;none&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top"&gt;
NetworkService&lt;/td&gt;
&lt;td valign="top"&gt;
DOMAIN\SRV_SQL2&lt;/td&gt;
&lt;td valign="top"&gt;
SERVER1&lt;br&gt;
DOMAIN\Bobbie&lt;/td&gt;
&lt;td valign="top"&gt;
MSSQLSvc/server2.domain.local:1433 DOMAIN\SRV_SQL2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top"&gt;
DOMAIN\SRV_SQL1&lt;/td&gt;
&lt;td valign="top"&gt;
NetworkService&lt;/td&gt;
&lt;td valign="top"&gt;
DOMAIN\SRV_SQL1&lt;br&gt;
DOMAIN\Bobbie&lt;/td&gt;
&lt;td valign="top"&gt;
&amp;lt;none&amp;gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td valign="top"&gt;
DOMAIN\SRV_SQL1&lt;/td&gt;
&lt;td valign="top"&gt;
DOMAIN\SRV_SQL2&lt;/td&gt;
&lt;td valign="top"&gt;
DOMAIN\SRV_SQL1&lt;br&gt;
DOMAIN\Bobbie&lt;/td&gt;
&lt;td valign="top"&gt;
MSSQLSvc/server2.domain.local:1433 DOMAIN\SRV_SQL2&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;h3&gt;Things to consider
&lt;/h3&gt;
&lt;p&gt;
&lt;a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/AdjustKerberosIntervals_2.png"&gt;&lt;img style="border-right-width: 0px; margin: 5px 5px 5px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="AdjustKerberosIntervals" align="left" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/ImpersonationanddelegationwithSQLServer_14C28/AdjustKerberosIntervals_thumb.png" width="244" height="104"&gt;&lt;/a&gt; When
working with Kerberos, tickets are granted for a longer period of time (typically
10 hours), so configuration changes usually take a longer time to apply. Use the KERBTRAY
and/or KLIST &lt;a title="Windows Server 2003 Resource Kit" href="http://www.microsoft.com/downloads/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&amp;amp;displaylang=en" target="_blank"&gt;resource
kit utilities&lt;/a&gt; to view and purge tickets, in order to speed things up (avoids rebooting
systems or logging off/on users). Alternatively, consider shortening the ticket lifetimes
to the minimum; 10 minutes for services and 1 hour for users.
&lt;/p&gt;
&lt;p&gt;
When configuring the linked servers, it was best to do so at the server where the
linked server is configured from. Depending on your role you may be a domain admin,
so should domain admins be delegated in order to make setting up linked servers easier?
Rather not! Imagine someone installed malicious program or script on a system that
is trusted for delegation and somehow a domain administrator executes this program
or script. It could hurt any system or user in your network.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=fafaec29-44bb-4ca2-bf2d-ddfba92f6352" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,fafaec29-44bb-4ca2-bf2d-ddfba92f6352.aspx</comments>
      <category>Linked Server</category>
      <category>Security</category>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=2ad0edcd-3c42-4915-bb29-2216e440b95f</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,2ad0edcd-3c42-4915-bb29-2216e440b95f.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,2ad0edcd-3c42-4915-bb29-2216e440b95f.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=2ad0edcd-3c42-4915-bb29-2216e440b95f</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/Bram_CE1F/Bram_Stan.jpg">
            <img style="border-bottom: 0px; border-left: 0px; margin: 0px 0px 0px 10px; border-top: 0px; border-right: 0px" border="0" alt="Bram_Stan" align="right" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/Bram_CE1F/Bram_Stan_thumb.jpg" width="180" height="260" />
          </a>I
knew this was coming and still I was taken by surprise; my son Bram was born last
week (July 27th). An event like that turns your life upside down, forever. I'm not
in the habit of putting personal stuff on the net, but this is worth the exception.
So here they are, a proud father and his one day old son. 
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=2ad0edcd-3c42-4915-bb29-2216e440b95f" />
      </body>
      <title>Bram</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,2ad0edcd-3c42-4915-bb29-2216e440b95f.aspx</guid>
      <link>http://resquel.com/ssb/2009/08/05/Bram.aspx</link>
      <pubDate>Wed, 05 Aug 2009 12:39:29 GMT</pubDate>
      <description>&lt;p&gt;
&lt;a href="http://resquel.com/ssb/content/binary/WindowsLiveWriter/Bram_CE1F/Bram_Stan.jpg"&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 0px 0px 10px; border-top: 0px; border-right: 0px" border="0" alt="Bram_Stan" align="right" src="http://resquel.com/ssb/content/binary/WindowsLiveWriter/Bram_CE1F/Bram_Stan_thumb.jpg" width="180" height="260"&gt;&lt;/a&gt;I
knew this was coming and still I was taken by surprise; my son Bram was born last
week (July 27th). An event like that turns your life upside down, forever. I'm not
in the habit of putting personal stuff on the net, but this is worth the exception.
So here they are, a proud father and his one day old son. 
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=2ad0edcd-3c42-4915-bb29-2216e440b95f" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,2ad0edcd-3c42-4915-bb29-2216e440b95f.aspx</comments>
      <category>ReSQueL</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=19cb3089-1ca4-4c50-b85b-2375d8de1f1c</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,19cb3089-1ca4-4c50-b85b-2375d8de1f1c.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,19cb3089-1ca4-4c50-b85b-2375d8de1f1c.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=19cb3089-1ca4-4c50-b85b-2375d8de1f1c</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Sometimes SQL Server gets it wrong, UNIQUE constraints and NULL is one of the parts
where it is wrong. People call it a bug and request for it to be fixed, Microsoft
calls requests to fix it common and offers workarounds and overall current SQL Server
behavior does not adhere to the standard (ISO-9075).
</p>
        <p>
To see the bug reports (and vote); <a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=126533" target="_blank">126533</a>, <a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=299229" target="_blank">299229</a>, <a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=311223" target="_blank">311223</a> (closed), <a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=387273" target="_blank">387273</a>.
Basically they are each others duplicates and from those, the request 299229 has most
attention and the best discussion.
</p>
        <p>
Available workarounds;
</p>
        <ul>
          <li>
Filtered index (starting with SQL Server 2008) 
</li>
          <li>
Indexed view (starting with SQL Server 2000) 
</li>
          <li>
Triggers (not recommended as the check is after the modification is completed and
if invalid has to perform a rollback)</li>
        </ul>
        <p>
... and don't say Microsoft didn't get it right because it's Microsoft... Access implements
UNIQUE constraints correctly.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=19cb3089-1ca4-4c50-b85b-2375d8de1f1c" />
      </body>
      <title>NULL in unique index (where SQL Server gets it wrong)</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,19cb3089-1ca4-4c50-b85b-2375d8de1f1c.aspx</guid>
      <link>http://resquel.com/ssb/2009/07/22/NULLInUniqueIndexWhereSQLServerGetsItWrong.aspx</link>
      <pubDate>Wed, 22 Jul 2009 13:00:57 GMT</pubDate>
      <description>&lt;p&gt;
Sometimes SQL Server gets it wrong, UNIQUE constraints and NULL is one of the parts
where it is wrong. People call it a bug and request for it to be fixed, Microsoft
calls requests to fix it common and offers workarounds and overall current SQL Server
behavior does not adhere to the standard (ISO-9075).
&lt;/p&gt;
&lt;p&gt;
To see the bug reports (and vote); &lt;a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=126533" target="_blank"&gt;126533&lt;/a&gt;, &lt;a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=299229" target="_blank"&gt;299229&lt;/a&gt;, &lt;a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=311223" target="_blank"&gt;311223&lt;/a&gt; (closed), &lt;a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=387273" target="_blank"&gt;387273&lt;/a&gt;.
Basically they are each others duplicates and from those, the request 299229 has most
attention and the best discussion.
&lt;/p&gt;
&lt;p&gt;
Available workarounds;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Filtered index (starting with SQL Server 2008) 
&lt;li&gt;
Indexed view (starting with SQL Server 2000) 
&lt;li&gt;
Triggers (not recommended as the check is after the modification is completed and
if invalid has to perform a rollback)&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
... and don't say Microsoft didn't get it right because it's Microsoft... Access implements
UNIQUE constraints correctly.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=19cb3089-1ca4-4c50-b85b-2375d8de1f1c" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,19cb3089-1ca4-4c50-b85b-2375d8de1f1c.aspx</comments>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=9f424662-7e1f-4f60-a7d5-ed2755ea6499</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,9f424662-7e1f-4f60-a7d5-ed2755ea6499.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,9f424662-7e1f-4f60-a7d5-ed2755ea6499.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=9f424662-7e1f-4f60-a7d5-ed2755ea6499</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
I've been holding this one off for quite some time, but finally bit the bullet...
migrate the server.Coming from Windows Small Business Server 2003 (32-bit) going to
Windows Server 2008 with Exchange 2007 (both x64), including transferring the web
site. There were a few nasty pieces in the process and I didn't have/take the time/resources
to test it all though before jumping in... so I got a disruption of about one hour
for the blog and inbound smtp.
</p>
        <p>
First issue I encountered was the installation of the server, which I ordered from
Dell without operating system. I am a <a href="https://partner.microsoft.com/40013779" target="_blank">Microsoft
Action Pack</a> subscriber, so I slammed the 64-bit Windows Server from the Action
Pack on the system, entered the Product Key and then went on to phone based activation.
After putting in the last confirmation code and pushing the next button... my activation
wasn't accepted!!! ... to cut a long story short, after dozens of voice menu selections
and 4 persons, I found the guy who could help me out: Philip. Though it still toke
Philip and me about 20 minutes to find the actual problem... I wasn't an Action Pack
subscriber anymore!!! Technical story here, but it happened when Action Pack was integrated
more tightly with the Partner Program. Somehow I didn't complete the Action Pack renewal
transaction completely in February, but the partner site didn't show that (it only
showed I'm still good for the Partner Program).
</p>
        <p>
Couple of days later, I could go ahead with the migration process. It turned out to
be easier than I expected after I found a very useful document about <a href="http://www.itexpertmag.com/server/upgrading-small-business-server-2003-to-exchange-2007" target="_blank">Upgrading
Small Business Server 2003 to Exchange 2007</a>. Some pointers on using the document
though;
</p>
        <ul>
          <li>
Transferring the the other FSMO's (Schema Master is described); <a href="http://www.petri.co.il/determining_fsmo_role_holders.htm" target="_blank">Determining
FSMO Role Holders</a></li>
          <li>
With Windows Server 2008 you NEED the Exchange 2007 SP1 DVD, Exchange 2007 without
SP1 will not pass installation checks. 
</li>
          <li>
Section Migrate mail send does not cover migrate mail receive. This caused me a bit
of downtime... The issue was 
<br /><font face="Courier New">SMTP error from remote mail server after MAIL FROM:&lt;abcd@efg.hi&gt;
SIZE=2703: 
<br />
host abcdef.ghi [10.10.10.10]: 530 5.7.1 Client was not authenticated</font><br />
And can be solved in the following manner in Exchange Management Console: 
<ul><li>
Go to the Server Configuration, Hub Transport. Select your server and add Anonymous
to the Default SERVERNAME Receive Connector. While you're there, you can increase
receiving message size here too.</li></ul></li>
        </ul>
        <p>
And there were parts I did different;
</p>
        <ul>
          <li>
I continue to rely on <a href="http://www.vamsoft.com/" target="_blank">Vamsoft's
ORF</a> for the anti-spam. Version 4.3 is fully compatible with Windows Server 2008
and Exchange 2007 and an ease to configure. 
</li>
          <li>
Since I will continue the same DNS name for OWA and Windows mobile Active Sync, I
transferred the Self-signed certificate from the SBS2003 (find it in your IIS, web
site, directory security tab) to the new server.</li>
        </ul>
        <p>
My other bit of downtime was the blog... simply copy from the old server to the new
one didn't cut it. So I downloaded the latest release of <a href="http://dasblog.codeplex.com/Release/ProjectReleases.aspx" target="_blank">dasBlog
from Codeplex</a>, copied it into the virtual director of new server. Next I had to;
</p>
        <ul>
          <li>
Copy my own theme 
</li>
          <li>
Compare (and adjust) the \SiteConfig files 
</li>
          <li>
Transfer \Content files and \Logs files 
</li>
          <li>
Make sure the account running the Application Pool has read on all dasBlog folders
and change on \SiteConfig, \Content and \Logs. 
</li>
          <li>
The Application Pool is running in Classic Managed Pipeline Mode</li>
        </ul>
        <p>
Guess I can say I didn't plan for the blog to be upgraded... that just had to happen.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=9f424662-7e1f-4f60-a7d5-ed2755ea6499" />
      </body>
      <title>Migration</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,9f424662-7e1f-4f60-a7d5-ed2755ea6499.aspx</guid>
      <link>http://resquel.com/ssb/2009/06/17/Migration.aspx</link>
      <pubDate>Wed, 17 Jun 2009 19:31:05 GMT</pubDate>
      <description>&lt;p&gt;
I've been holding this one off for quite some time, but finally bit the bullet...
migrate the server.Coming from Windows Small Business Server 2003 (32-bit) going to
Windows Server 2008 with Exchange 2007 (both x64), including transferring the web
site. There were a few nasty pieces in the process and I didn't have/take the time/resources
to test it all though before jumping in... so I got a disruption of about one hour
for the blog and inbound smtp.
&lt;/p&gt;
&lt;p&gt;
First issue I encountered was the installation of the server, which I ordered from
Dell without operating system. I am a &lt;a href="https://partner.microsoft.com/40013779" target="_blank"&gt;Microsoft
Action Pack&lt;/a&gt; subscriber, so I slammed the 64-bit Windows Server from the Action
Pack on the system, entered the Product Key and then went on to phone based activation.
After putting in the last confirmation code and pushing the next button... my activation
wasn't accepted!!! ... to cut a long story short, after dozens of voice menu selections
and 4 persons, I found the guy who could help me out: Philip. Though it still toke
Philip and me about 20 minutes to find the actual problem... I wasn't an Action Pack
subscriber anymore!!! Technical story here, but it happened when Action Pack was integrated
more tightly with the Partner Program. Somehow I didn't complete the Action Pack renewal
transaction completely in February, but the partner site didn't show that (it only
showed I'm still good for the Partner Program).
&lt;/p&gt;
&lt;p&gt;
Couple of days later, I could go ahead with the migration process. It turned out to
be easier than I expected after I found a very useful document about &lt;a href="http://www.itexpertmag.com/server/upgrading-small-business-server-2003-to-exchange-2007" target="_blank"&gt;Upgrading
Small Business Server 2003 to Exchange 2007&lt;/a&gt;. Some pointers on using the document
though;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Transferring the the other FSMO's (Schema Master is described); &lt;a href="http://www.petri.co.il/determining_fsmo_role_holders.htm" target="_blank"&gt;Determining
FSMO Role Holders&lt;/a&gt; 
&lt;li&gt;
With Windows Server 2008 you NEED the Exchange 2007 SP1 DVD, Exchange 2007 without
SP1 will not pass installation checks. 
&lt;li&gt;
Section Migrate mail send does not cover migrate mail receive. This caused me a bit
of downtime... The issue was 
&lt;br&gt;
&lt;font face="Courier New"&gt;SMTP error from remote mail server after MAIL FROM:&amp;lt;abcd@efg.hi&amp;gt;
SIZE=2703: 
&lt;br&gt;
host abcdef.ghi [10.10.10.10]: 530 5.7.1 Client was not authenticated&lt;/font&gt; 
&lt;br&gt;
And can be solved in the following manner in Exchange Management Console: 
&lt;ul&gt;
&lt;li&gt;
Go to the Server Configuration, Hub Transport. Select your server and add Anonymous
to the Default SERVERNAME Receive Connector. While you're there, you can increase
receiving message size here too.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
And there were parts I did different;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
I continue to rely on &lt;a href="http://www.vamsoft.com/" target="_blank"&gt;Vamsoft's
ORF&lt;/a&gt; for the anti-spam. Version 4.3 is fully compatible with Windows Server 2008
and Exchange 2007 and an ease to configure. 
&lt;li&gt;
Since I will continue the same DNS name for OWA and Windows mobile Active Sync, I
transferred the Self-signed certificate from the SBS2003 (find it in your IIS, web
site, directory security tab) to the new server.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
My other bit of downtime was the blog... simply copy from the old server to the new
one didn't cut it. So I downloaded the latest release of &lt;a href="http://dasblog.codeplex.com/Release/ProjectReleases.aspx" target="_blank"&gt;dasBlog
from Codeplex&lt;/a&gt;, copied it into the virtual director of new server. Next I had to;
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;
Copy my own theme 
&lt;li&gt;
Compare (and adjust) the \SiteConfig files 
&lt;li&gt;
Transfer \Content files and \Logs files 
&lt;li&gt;
Make sure the account running the Application Pool has read on all dasBlog folders
and change on \SiteConfig, \Content and \Logs. 
&lt;li&gt;
The Application Pool is running in Classic Managed Pipeline Mode&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Guess I can say I didn't plan for the blog to be upgraded... that just had to happen.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=9f424662-7e1f-4f60-a7d5-ed2755ea6499" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,9f424662-7e1f-4f60-a7d5-ed2755ea6499.aspx</comments>
      <category>dasBlog</category>
      <category>Hardware</category>
      <category>ReSQueL</category>
      <category>SBS</category>
      <category>Windows Server 2008</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=52a8d242-2863-4875-aac2-51794f3d529f</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,52a8d242-2863-4875-aac2-51794f3d529f.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,52a8d242-2863-4875-aac2-51794f3d529f.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=52a8d242-2863-4875-aac2-51794f3d529f</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Microsoft launched a new preview site for <a href="http://www.microsoft.com/sqlserver/2008/en/us/r2.aspx" target="_blank">SQL
Server 2008 R2</a>. You can sign up for notifications about the releases of CTP's.
</p>
        <p>
From an administrative viewpoint, focus seems at tools to manage, utilize and consolidate
instances and machines.
</p>
        <p>
Towards usability (mainly business intelligence), enhancements to integrate with Office
2010 (mainly Excel and SharePoint) are featured. Also further integration with Virtual
Earth is shown.
</p>
        <p>
Finally there is mention about something called Master Data Services and the Stewardship
Portal. In concept a very powerful way to create and maintain a data warehouse bus.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=52a8d242-2863-4875-aac2-51794f3d529f" />
      </body>
      <title>Release 2 for SQL Server 2008 in the make</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,52a8d242-2863-4875-aac2-51794f3d529f.aspx</guid>
      <link>http://resquel.com/ssb/2009/05/15/Release2ForSQLServer2008InTheMake.aspx</link>
      <pubDate>Fri, 15 May 2009 11:30:57 GMT</pubDate>
      <description>&lt;p&gt;
Microsoft launched a new preview site for &lt;a href="http://www.microsoft.com/sqlserver/2008/en/us/r2.aspx" target="_blank"&gt;SQL
Server 2008 R2&lt;/a&gt;. You can sign up for notifications about the releases of CTP's.
&lt;/p&gt;
&lt;p&gt;
From an administrative viewpoint, focus seems at tools to manage, utilize and consolidate
instances and machines.
&lt;/p&gt;
&lt;p&gt;
Towards usability (mainly business intelligence), enhancements to integrate with Office
2010 (mainly Excel and SharePoint) are featured. Also further integration with Virtual
Earth is shown.
&lt;/p&gt;
&lt;p&gt;
Finally there is mention about something called Master Data Services and the Stewardship
Portal. In concept a very powerful way to create and maintain a data warehouse bus.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=52a8d242-2863-4875-aac2-51794f3d529f" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,52a8d242-2863-4875-aac2-51794f3d529f.aspx</comments>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=f9f3eb3c-f4a6-4d7a-a07d-6bd21d15973f</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,f9f3eb3c-f4a6-4d7a-a07d-6bd21d15973f.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,f9f3eb3c-f4a6-4d7a-a07d-6bd21d15973f.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=f9f3eb3c-f4a6-4d7a-a07d-6bd21d15973f</wfw:commentRss>
      <slash:comments>1</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Noticed <a title="Don't Miss Coaching Session for Exam 70-432" href="http://blogs.technet.com/mslcommunity/archive/2009/03/23/don-t-miss-coaching-session-for-exam-70-432.aspx" target="_blank">this
one on Born to Learn</a>... Microsoft has two sessions today/tomorrow on preparing
for the exam 70-432 
</p>
        <blockquote>
          <p>
Tuesday, March 24  <a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032404570&amp;Culture=en-US">7:30
A.M. Pacific Time</a>   (<a href="http://www.timeanddate.com/worldclock/fixedtime.html?month=3&amp;day=24&amp;year=2009&amp;hour=7&amp;min=30&amp;sec=0&amp;p1=234">What
time is this in my region?</a> 2009-03-24 15:30 CET)
</p>
        </blockquote>
        <blockquote>
          <p>
Tuesday, March 24  <a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032404571&amp;Culture=en-US">5:30
P.M. Pacific Time</a>   (<a href="http://www.timeanddate.com/worldclock/fixedtime.html?month=3&amp;day=24&amp;year=2009&amp;hour=17&amp;min=30&amp;sec=0&amp;p1=234">What
time is this in my region?</a> 2009-03-25 01:30 CET)
</p>
        </blockquote>
        <p>
It slipped past me too, but since I can manage to squeeze the first session in my
schedule, I'll attend. Follow up and <a href="https://www112.livemeeting.com/cc/microsoft/view?id=MCP032409-1&amp;pw=certify" target="_blank">link
to the recording</a> to be posted later.
</p>
        <p>
My impressions of this sessions is that <a href="http://robboek.com" target="_blank">Rob
Boek</a> takes you through the main areas of interest for the exam, at least it aligned
quite well with what I can remember from my beta-test. Good stuff if you need an impression
of the most important topics of the exam to prepare for.
</p>
        <p>
Updated 2009-04-02
</p>
        <div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:1a4cd14a-69b6-4c82-827d-b341154c5be5" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px">Technorati
tags: <a href="http://technorati.com/tags/70-432" rel="tag">70-432</a>, <a href="http://technorati.com/tags/MCTS" rel="tag">MCTS</a></div>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=f9f3eb3c-f4a6-4d7a-a07d-6bd21d15973f" />
      </body>
      <title>Prep coaching session for 70-432 from Microsoft</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,f9f3eb3c-f4a6-4d7a-a07d-6bd21d15973f.aspx</guid>
      <link>http://resquel.com/ssb/2009/03/24/PrepCoachingSessionFor70432FromMicrosoft.aspx</link>
      <pubDate>Tue, 24 Mar 2009 08:46:13 GMT</pubDate>
      <description>&lt;p&gt;
Noticed &lt;a title="Don't Miss Coaching Session for Exam 70-432" href="http://blogs.technet.com/mslcommunity/archive/2009/03/23/don-t-miss-coaching-session-for-exam-70-432.aspx" target="_blank"&gt;this
one on Born to Learn&lt;/a&gt;... Microsoft has two sessions today/tomorrow on preparing
for the exam 70-432 
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Tuesday, March 24&amp;nbsp; &lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032404570&amp;amp;Culture=en-US"&gt;7:30
A.M. Pacific Time&lt;/a&gt;&amp;nbsp;&amp;nbsp; (&lt;a href="http://www.timeanddate.com/worldclock/fixedtime.html?month=3&amp;amp;day=24&amp;amp;year=2009&amp;amp;hour=7&amp;amp;min=30&amp;amp;sec=0&amp;amp;p1=234"&gt;What
time is this in my region?&lt;/a&gt; 2009-03-24 15:30 CET)
&lt;/p&gt;
&lt;/blockquote&gt; &lt;blockquote&gt; 
&lt;p&gt;
Tuesday, March 24&amp;nbsp; &lt;a href="http://msevents.microsoft.com/CUI/EventDetail.aspx?EventID=1032404571&amp;amp;Culture=en-US"&gt;5:30
P.M. Pacific Time&lt;/a&gt;&amp;nbsp;&amp;nbsp; (&lt;a href="http://www.timeanddate.com/worldclock/fixedtime.html?month=3&amp;amp;day=24&amp;amp;year=2009&amp;amp;hour=17&amp;amp;min=30&amp;amp;sec=0&amp;amp;p1=234"&gt;What
time is this in my region?&lt;/a&gt; 2009-03-25 01:30 CET)
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
It slipped past me too, but since I can manage to squeeze the first session in my
schedule, I'll attend. Follow up and &lt;a href="https://www112.livemeeting.com/cc/microsoft/view?id=MCP032409-1&amp;amp;pw=certify" target="_blank"&gt;link
to the recording&lt;/a&gt; to be posted later.
&lt;/p&gt;
&lt;p&gt;
My impressions of this sessions is that &lt;a href="http://robboek.com" target="_blank"&gt;Rob
Boek&lt;/a&gt; takes you through the main areas of interest for the exam, at least it aligned
quite well with what I can remember from my beta-test. Good stuff if you need an impression
of the most important topics of the exam to prepare for.
&lt;/p&gt;
&lt;p&gt;
Updated 2009-04-02
&lt;/p&gt;
&lt;div class="wlWriterSmartContent" id="scid:0767317B-992E-4b12-91E0-4F059A8CECA8:1a4cd14a-69b6-4c82-827d-b341154c5be5" style="padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px"&gt;Technorati
tags: &lt;a href="http://technorati.com/tags/70-432" rel="tag"&gt;70-432&lt;/a&gt;, &lt;a href="http://technorati.com/tags/MCTS" rel="tag"&gt;MCTS&lt;/a&gt;
&lt;/div&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=f9f3eb3c-f4a6-4d7a-a07d-6bd21d15973f" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,f9f3eb3c-f4a6-4d7a-a07d-6bd21d15973f.aspx</comments>
      <category>SQL Server</category>
      <category>Train_Cert</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=2a1df111-2a66-4919-820f-019df215381d</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,2a1df111-2a66-4919-820f-019df215381d.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,2a1df111-2a66-4919-820f-019df215381d.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=2a1df111-2a66-4919-820f-019df215381d</wfw:commentRss>
      <slash:comments>3</slash:comments>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
          <strong>
            <em>
              <u>Updated 2010-07-24; added timeout to the receive command in the stored
procedure. For more info, see </u>
            </em>
          </strong>
          <a title="Service Broker can make your transaction log big" href="http://resquel.com/ssb/2010/07/24/ServiceBrokerCanMakeYourTransactionLogBig.aspx" target="_blank">
            <em>
              <u>Service
Broker can make your transaction log big</u>
            </em>
          </a>
          <strong>
            <em>
              <u>.</u>
            </em>
          </strong>
        </p>
        <p>
Event Notifications are based on Service Broker. This brings an advantage and a disadvantage;
enhanced scalability at the cost of increased complexity (and lack of a User Interface,
so we have to take it to T-SQL code). Lets take it there step by step for SQL Server
2005 and 2008;
</p>
        <ol>
          <li>
Some code to cause deadlocks. 
</li>
          <li>
See how the deadlocks look like using the Trace Flags. 
</li>
          <li>
See how the deadlocks look like in Profiler. 
</li>
          <li>
Use the Event Notification on the deadlocks. 
</li>
          <li>
Sending out the mail. 
</li>
        </ol>
        <p>
The code shown as part of this article is also available at this <a title="DeadLock Event Notification script." href="http://www.resquel.com/ssb/content/upload/ReSQueLBlogPost20090302_DeadLockEventNotification.sql.txt" target="_blank">link</a>.
To code to create the database for the deadlocks is available at this <a title="DeadLockSample database script." href="http://www.resquel.com/ssb/content/upload/ReSQueLBlogPost20090302_DeadLockSample.sql.txt" target="_blank">link</a>.
The code works with SQL Server 2005 SP2 and SQL Server 2008
</p>
        <h3>1. Some code to cause deadlocks.
</h3>
        <p>
To get the code to cause the deadlocks, click this <a title="DeadLockSample database script." href="http://www.resquel.com/ssb/content/upload/ReSQueLBlogPost20090302_DeadLockSample.sql.txt" target="_blank">link</a>.
Run the code from SQL Server Management Studio (SSMS) to create the DeadLockSample
database. Next create two additional connections (New Query) to the DeadLockSample
database, execute the procedure procLockedTran1 on one connection and procLockedTran2
on the other connection. There is a 5 seconds delay in the two procedures between
locking the first and second resource. So if you execute the two procedures within
5 seconds, you get your deadlock. You could also change the delay in the procedures
if you think 5 seconds is to short/long.
</p>
        <p>
Normally a process would be terminated when it is the deadlock victim. For the procedures
created by the script, the error is handled , so the process that is the deadlock
victim just selects it is the victim and the connection will not be terminated.
</p>
        <p>
If you want to create a deadlock in any of the following scenarios, just run procLockedTran1
and procLockedTran2 on two separate connections simultaneously.
</p>
        <h3>2. See how the deadlocks look like using the Trace Flags.
</h3>
        <a href="http://www.resquel.com/ssb/content/binary/WindowsLiveWriter/DeadlockalertsthroughEventNotification_14104/TraceFlagConfiguration_2.png">
          <img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="TraceFlagConfiguration" align="right" src="http://www.resquel.com/ssb/content/binary/WindowsLiveWriter/DeadlockalertsthroughEventNotification_14104/TraceFlagConfiguration_thumb.png" width="244" height="243" />
        </a>
        <p>
To get the deadlocks recorded in the SQL Server Error Log, either set the trace flag
as startup parameter in SQL Server Configuration Manager (see picture) which will
cause the trace flag to be active each time you start your SQL Server (until you remove
the trace flag again), or issue a DBCC TRACEON (1222,-1) command which sets the trace
flag until you stop SQL Server or issue a DBCC TRACEOFF (1222,-1).
</p>
        <p>
With the trace flag active, you can now see the deadlock appear in you SQL Server
Error Log.
</p>
        <h3>3. See how the deadlocks look like in Profiler.
</h3>
        <p>
... or in SSMS for that matter. With the SQL Server tools it is possible to display
deadlock graphs; XML documents (with .xdl extension) describing the deadlock information
of one or more deadlocks. When the deadlock graph event (EventClass 148) with the
TextData column is included in the trace definition, the XML to create the deadlock
graph is captured (both through the Profiler graphical tool and the sp_trace procedures)
and the graph will automatically be displayed when opened in Profiler. Also the graphical
profiler has the option to write deadlock graphs directly to a file. Note that the
graphical presentation of the deadlock is limited compared to the information in the
.xdl document. The information in the .xdl document is the same information as the
information written to the SQL Server Error Log with trace flag 1222. One extra comment
if you filter your trace on database, you won't get the deadlock graph, as <a href="http://sqlblog.com/blogs/kalen_delaney/archive/2006/11/28/376.aspx" target="_blank">noticed
by Kalen Delaney</a>.
</p>
        <h3>4. Use the Event Notification on the deadlocks.
</h3>
        <p>
Event Notification lets you capture events that can occur on server, database or broker
queue, for a list of events, query sys.event_notification_event_types. For this purpose,
the event type 1148, DEADLOCK_GRAPH is of interest. Also note that the DEADLOCK_GRAPH
is a <a href="http://msdn.microsoft.com/en-us/library/ms190655.aspx" target="_blank">trace
event</a>, which can only be captured at server level.
</p>
        <p>
The second thing needed is a broker SERVICE to send the event to. Since we'll be handling
the events at server level, the SERVICE (and QUEUE) will be created in the msdb database.
There is no need to create the MESSAGE TYPEs  or CONTRACTs as they already exist
for Event Notification.
</p>
        <blockquote>
          <p>
            <font size="2">
              <font color="#0000ff">USE</font> msdb 
<br /></font>
            <font color="#0000ff" size="2">GO 
<br /></font>
            <font size="2">
              <font color="#008000">-- Create a queue to receive messages. 
<br /></font>
              <font color="#0000ff">CREATE</font>
              <font color="#0000ff">QUEUE</font> queDeadLock_Graph</font>
            <font color="#808080" size="2">; 
<br /></font>
            <font color="#0000ff" size="2">GO 
<br /></font>
            <font size="2">
              <font color="#008000">-- Create a service on the queue that
references 
<br />
-- the event notifications contract. 
<br /></font>
              <font color="#0000ff">CREATE</font>
              <font color="#0000ff">SERVICE</font> svcDeadLock_Graph 
<br /><font color="#0000ff">ON</font><font color="#0000ff">QUEUE</font> queDeadLock_Graph 
<br /><font color="#808080">(</font>[http://schemas.microsoft.com/SQL/Notifications/PostEventNotification]</font>
            <font color="#808080" size="2">); 
<br /></font>
            <font size="2">
              <font color="#0000ff">GO 
<br /></font>
              <font color="#008000">-- Now query the sys.databases for the service_broker_guid
of the msdb database.</font>
            </font>
            <font size="2">
              <font color="#008000">
                <br />
              </font>
              <font color="#0000ff">SELECT</font> service_broker_guid <font color="#0000ff">FROM</font><font color="#008000">sys</font><font color="#808080">.</font><font color="#008000">databases</font><font color="#0000ff">WHERE</font> name <font color="#808080">=</font><font color="#ff0000">'msdb'</font><font color="#808080">;</font></font>
          </p>
        </blockquote>
        <p>
The result of the query for the Service Broker GUID for the msdb databases is needed
to point to the right database for the service.
</p>
        <blockquote>
          <p>
            <font size="2">
              <font color="#008000">-- Create the event notification. 
<br /></font>
              <font color="#0000ff">CREATE</font>
              <font color="#0000ff">EVENT</font>
              <font color="#0000ff">NOTIFICATION</font> evnDeadLock_Graph 
<br /><font color="#0000ff">ON</font><font color="#0000ff">SERVER</font><br /><font color="#0000ff">FOR</font> DEADLOCK_GRAPH 
<br /><font color="#0000ff">TO</font><font color="#0000ff">SERVICE</font></font>
            <font size="2">
              <font color="#ff0000">'svcDeadLock_Graph' 
<br /></font>
              <font color="#808080">,</font>
              <font color="#ff0000">'2A7C5300-7C64-43F5-AB10-5B079D07678E'</font>
              <font color="#808080">;</font>
              <font color="#008000">--
the GUID for msdb goes here.</font>
            </font>
            <font color="#008000" size="2">
              <br />
            </font>
          </p>
        </blockquote>
        <p>
You can look at the content of the queue through SELECT * FROM msdb.dbo.queDeadLock_Graph,
as a queue in fact is a table. Next step will be processing the messages that get
into the queue and more specifically get the deadlock details out of the message_body.
The message body looks binary, but if you query it like this;
</p>
        <blockquote>
          <p>
            <font size="2">
              <font color="#0000ff">SELECT</font>
              <font color="#ff00ff">CAST</font>
              <font color="#808080">(</font>message_body <font color="#0000ff">AS</font><font color="#0000ff">xml</font><font color="#808080">)</font><font color="#0000ff">FROM</font> msdb<font color="#808080">.</font>dbo<font color="#808080">.</font>queDeadLock_Graph</font>
            <font size="+0">
            </font>
          </p>
        </blockquote>
        <p>
you'll notice that it is actually XML, where the textdata node holds the xdl structure
of the deadlock graph.
</p>
        <h3>5. Sending out the mail
</h3>
        <p>
Okay, we're in Service Broker land by now. Event Notification acts as the Initiator
Service and the svcDeadLock_Graph is the Target Service. In this scenario, the only
thing left to be done is specify how the queue should be processed. The processing
shall be done by a stored procedure that will be tied to the queue as the activation
program. Once something comes in to the queue, this stored procedure will be invoked...
kind of like a trigger for insert on a normal table. The specification for the stored
procedure will be:
</p>
        <ol>
          <li>
Extract the deadlock graph from the message_body and store it in a variable. 
</li>
          <li>
Extract the datetime of the deadlock graph notification and store it in a variable. 
</li>
          <li>
Store the datetime and deadlock graph in a table. 
</li>
          <li>
Send the datetime as part of the message and deadlock graph as attachment in an email.
This does require database mail to be set up, if you haven't, an error will be logged
in the table. 
</li>
        </ol>
        <p>
The code to create the table to hold the deadlock information (and the reason why
no mail was sent).
</p>
        <blockquote>
          <p>
            <font size="2">
              <font color="#0000ff">USE</font> msdb 
<br /></font>
            <font size="2">
              <font color="#0000ff">GO 
<br />
CREATE</font>
              <font color="#0000ff">TABLE</font> dbo<font color="#808080">.</font>tblDeadLock_Log<font color="#0000ff"></font></font>
            <font size="2">
              <font color="#808080">( 
<br /></font>DeadLock_ID <font color="#0000ff">int</font><font color="#0000ff">IDENTITY</font><font color="#808080">(</font>1<font color="#808080">,</font>1<font color="#808080">)</font><font color="#0000ff">CONSTRAINT</font> pk_tblDeadLock_Log <font color="#0000ff">PRIMARY</font></font>
            <font size="2">
              <font color="#0000ff">KEY 
<br /></font>
              <font color="#808080">,</font> DeadLock_Detected </font>
            <font size="2">
              <font color="#0000ff">datetime 
<br /></font>
              <font color="#808080">,</font> DeadLock_Graph </font>
            <font size="2">
              <font color="#0000ff">xml 
<br /></font>
              <font color="#808080">,</font> NoMailReason <font color="#0000ff">nvarchar</font><font color="#808080">(</font>2048</font>
            <font color="#808080" size="2">)) 
<br /></font>
            <font color="#0000ff">
              <font size="2">GO</font>
              <br />
            </font>
          </p>
        </blockquote>
        <p>
The stored procedure to be used as the activation program on the queue, this is where
the real work is done. Alter the @profile_name and @recipients in at sp_send_dbmail
to match the setup of your dbmail setup.
</p>
        <blockquote>
          <p>
            <font size="2">
              <font color="#0000ff">CREATE</font>
              <font color="#0000ff">PROCEDURE</font> dbo<font color="#808080">.</font>procReceiveDeadLock_Graph 
<br /></font>
            <font size="2">
              <font color="#0000ff">AS 
<br />
DECLARE</font> @conversation_handle </font>
            <font size="2">
              <font color="#0000ff">uniqueidentifier 
<br />
DECLARE</font> @message_body </font>
            <font size="2">
              <font color="#0000ff">xml 
<br />
DECLARE</font> @message_type_name <font color="#0000ff">nvarchar</font><font color="#808080">(</font>128</font>
            <font size="2">
              <font color="#808080">) 
<br /></font>
              <font color="#0000ff">DECLARE</font> @deadlock_graph </font>
            <font size="2">
              <font color="#0000ff">xml 
<br />
DECLARE</font> @event_datetime </font>
            <font size="2">
              <font color="#0000ff">datetime 
<br />
DECLARE</font> @deadlock_id </font>
            <font size="2">
              <font color="#0000ff">int 
<br />
BEGIN</font>
            </font>
            <font size="2">
              <font color="#0000ff">TRY 
<br />
BEGIN</font>
            </font>
            <font color="#0000ff" size="2">TRAN 
<br />
WAITFOR</font>
            <font size="2">
              <font color="#808080">( 
<br /></font>
              <font color="#0000ff">RECEIVE</font>
              <font color="#0000ff">TOP</font>
              <font color="#808080">(</font>1<font color="#808080">)</font> @conversation_handle <font color="#808080">=</font></font>
            <font size="2">
              <font color="#0000ff">conversation_handle 
<br /></font>
              <font color="#808080">,</font> @message_body <font color="#808080">=</font><font color="#ff00ff">CAST</font><font color="#808080">(</font>message_body <font color="#0000ff">AS</font><font color="#0000ff">xml</font></font>
            <font size="2">
              <font color="#808080">) 
<br />
,</font> @message_type_name <font color="#808080">=</font> message_type_name 
<br /><font color="#0000ff">FROM</font> dbo<font color="#808080">.</font>queDeadLock_Graph<font color="#808080">)</font></font>
            <font color="#808080">
              <font size="2">
                <br />
, <font color="#0000ff">TIMEOUT </font><font color="#808080">10000 </font></font>
              <font size="2">
                <font color="#008000">--
Line added 2010-07-24; 
<br />
-- http://resquel.com/ssb/2010/07/24/ServiceBrokerCanMakeYourTransactionLogBig.aspx </font>
                <br />
              </font>
            </font>
            <font size="2">
              <font color="#008000">-- Validate message 
<br /></font>
              <font color="#0000ff">IF </font>
              <font color="#808080">(</font>@message_type_name <font color="#808080">=</font><font color="#ff0000">'http://schemas.microsoft.com/SQL/Notifications/EventNotification'</font></font>
            <font size="2">
              <font color="#808080">AND 
<br /></font>@message_body<font color="#808080">.</font>exist<font color="#808080">(</font><font color="#ff0000">'(/EVENT_INSTANCE/TextData/deadlock-list)'</font><font color="#808080">)</font><font color="#808080">=</font> 1</font>
            <font color="#808080" size="2">) 
<br /></font>
            <font color="#0000ff" size="2">BEGIN 
<br /></font>
            <font size="2">
              <font color="#008000">-- Extract the info from the message 
<br /></font>
              <font color="#0000ff">SELECT</font> @deadlock_graph <font color="#808080">=</font> @message_body<font color="#808080">.</font>query<font color="#808080">(</font><font color="#ff0000">'(/EVENT_INSTANCE/TextData/deadlock-list)'</font></font>
            <font size="2">
              <font color="#808080">) 
<br />
,</font> @event_datetime <font color="#808080">= </font>@message_body<font color="#808080">.</font>value<font color="#808080">(</font><font color="#ff0000">'(/EVENT_INSTANCE/PostTime)[1]'</font><font color="#808080">,</font><font color="#ff0000">'datetime'</font></font>
            <font color="#808080" size="2">) 
<br /></font>
            <font size="2">
              <font color="#008000">-- Put the info in the table 
<br /></font>
              <font color="#0000ff">INSERT</font> dbo<font color="#808080">.</font>tblDeadLock_Log<font color="#0000ff"></font><font color="#808080">(</font>DeadLock_Detected<font color="#808080">,</font> DeadLock_Graph</font>
            <font size="2">
              <font color="#808080">) 
<br /></font>
              <font color="#0000ff">VALUES </font>
              <font color="#808080">(</font>@event_datetime<font color="#808080">,</font> @deadlock_graph</font>
            <font size="2">
              <font color="#808080">) 
<br /></font>
              <font color="#0000ff">SELECT</font> @deadlock_id <font color="#808080">=</font><font color="#ff00ff">SCOPE_IDENTITY</font></font>
            <font color="#808080" size="2">() 
<br /></font>
            <font size="2">
              <font color="#008000">-- Send deadlock alert mail. 
<br />
-- Requires configured database mail, will log an error if not (or anything else goes
wrong). 
<br /></font>
              <font color="#0000ff">BEGIN</font>
            </font>
            <font size="2">
              <font color="#0000ff">TRY 
<br /></font>
              <font color="#0000ff">DECLARE</font> @subj <font color="#0000ff">nvarchar</font><font color="#808080">(</font>255<font color="#808080">),</font> @bdy <font color="#0000ff">nvarchar</font><font color="#808080">(</font><font color="#ff00ff">max</font><font color="#808080">),</font> @qry <font color="#0000ff">nvarchar</font><font color="#808080">(</font><font color="#ff00ff">max</font><font color="#808080">),</font> @attfn <font color="#0000ff">nvarchar</font><font color="#808080">(</font>255</font>
            <font size="2">
              <font color="#808080">) 
<br /></font>
              <font color="#0000ff">SELECT</font> @subj <font color="#808080">=</font><font color="#ff0000">'A
deadlock occurred on '</font><font color="#808080">+</font></font>
            <font size="2">
              <font color="#ff00ff">@@SERVERNAME 
<br /></font>
              <font color="#808080">,</font> @bdy <font color="#808080">=</font><font color="#ff0000">'A
deadlock occurred at '</font><font color="#808080">+</font><font color="#ff00ff">CONVERT</font><font color="#808080">(</font><font color="#0000ff">varchar</font><font color="#808080">(</font>50<font color="#808080">),</font>@event_datetime<font color="#808080">,</font> 120<font color="#808080">)</font><font color="#808080">+</font><font color="#ff0000">'
on SQL Server: '</font><font color="#808080">+</font><font color="#ff00ff">@@SERVERNAME</font><font color="#808080">+</font></font>
            <font size="2">
              <font color="#ff0000">'.
See attached xdl-file for deadlock details.' 
<br /></font>
              <font color="#808080">,</font> @qry <font color="#808080">=</font><font color="#ff0000">'SET
NOCOUNT ON; SELECT deadlock_graph FROM msdb.dbo.tblDeadLock_Log WITH (READUNCOMMITTED)
WHERE DeadLock_ID = '</font><font color="#808080">+</font><font color="#ff00ff">CAST</font><font color="#808080">(</font>@deadlock_id <font color="#0000ff">AS</font><font color="#0000ff">varchar</font><font color="#808080">(</font>10</font>
            <font size="2">
              <font color="#808080">)) <font color="#008000">--
Locking hint is to prevent this dynamic query to be blocked by the lock held by the
insert. The dynamic SQL will not come from inside this transaction.</font><br /></font>
              <font color="#808080">,</font> @attfn <font color="#808080">=</font><font color="#ff00ff">@@SERVERNAME</font><font color="#808080">+</font><font color="#ff0000">'_'</font><font color="#808080">+</font><font color="#ff00ff">CAST</font><font color="#808080">(</font>@deadlock_id <font color="#0000ff">AS</font><font color="#0000ff">varchar</font><font color="#808080">(</font>10<font color="#808080">))</font><font color="#808080">+</font></font>
            <font size="2">
              <font color="#ff0000">'.xdl' 
<br /></font>
              <font color="#0000ff">EXEC</font>
              <font color="#800000">sp_send_dbmail</font>
              <font color="#0000ff">
              </font>@profile_name <font color="#808080">=</font></font>
            <font size="2">
              <font color="#ff0000">'default' 
<br /></font>
              <font color="#808080">,</font> @recipients <font color="#808080">=</font></font>
            <font size="2">
              <font color="#ff0000">'administrator@localhost' 
<br /></font>
              <font color="#808080">,</font> @subject <font color="#808080">=</font> @subj 
<br /><font color="#808080">,</font> @body <font color="#808080">=</font> @bdy 
<br /><font color="#808080">,</font> @query <font color="#808080">=</font> @qry 
<br /><font color="#808080">,</font> @attach_query_result_as_file <font color="#808080">=</font> 1 
<br /><font color="#808080">,</font> @query_attachment_filename <font color="#808080">=</font> @attfn </font>
            <font size="2">
              <font color="#008000">--
http://support.microsoft.com/kb/924345 
<br /></font>
              <font color="#808080">,</font> @query_result_header <font color="#808080">=</font> 0 
<br /><font color="#808080">,</font> @query_result_width <font color="#808080">=</font> 32767 
<br /><font color="#808080">,</font> @query_no_truncate <font color="#808080">=</font> 1 
<br /><font color="#0000ff">END</font></font>
            <font size="2">
              <font color="#0000ff">TRY 
<br /></font>
              <font color="#0000ff">BEGIN</font>
            </font>
            <font size="2">
              <font color="#0000ff">CATCH 
<br /></font>
              <font color="#0000ff">UPDATE</font> dbo<font color="#808080">.</font>tblDeadLock_Log 
<br /><font color="#0000ff">SET</font> NoMailReason <font color="#808080">=</font><font color="#ff00ff">ERROR_MESSAGE</font></font>
            <font size="2">
              <font color="#808080">() 
<br /></font>
              <font color="#0000ff">WHERE</font> DeadLock_ID <font color="#808080">=</font> @deadlock_id 
<br /><font color="#0000ff">END</font></font>
            <font size="2">
              <font color="#0000ff">CATCH 
<br />
END 
<br />
ELSE <font color="#008000">-- Not an event notification with deadlock-list</font><br />
END</font>
              <font color="#0000ff">CONVERSATION</font> @conversation_handle 
<br /><font color="#0000ff">COMMIT</font></font>
            <font size="2">
              <font color="#0000ff">TRAN 
<br />
END</font>
            </font>
            <font size="2">
              <font color="#0000ff">TRY 
<br />
BEGIN</font>
            </font>
            <font size="2">
              <font color="#0000ff">CATCH 
<br />
ROLLBACK</font>
            </font>
            <font size="2">
              <font color="#0000ff">TRAN 
<br />
END</font>
            </font>
            <font color="#0000ff" size="2">CATCH 
<br />
GO 
<br /></font>
          </p>
        </blockquote>
        <p>
Finally to tie the stored procedure to the queue so incoming notifications will be
processed.
</p>
        <blockquote>
          <p>
            <font size="2">
              <font color="#0000ff">ALTER</font>
              <font color="#0000ff">QUEUE</font> dbo<font color="#808080">.</font>queDeadLock_Graph 
<br /></font>
            <font size="2">
              <font color="#0000ff">WITH 
<br />
STATUS</font>
              <font color="#808080">=</font>
              <font color="#0000ff">ON</font>
            </font>
            <font size="2">
              <font color="#808080">, 
<br /></font>
              <font color="#0000ff">ACTIVATION </font>
            </font>
            <font size="2">
              <font color="#808080">( 
<br /></font>
              <font color="#0000ff">PROCEDURE_NAME</font>
              <font color="#808080">=</font> msdb<font color="#808080">.</font>dbo<font color="#808080">.</font>procReceiveDeadLock_Graph</font>
            <font size="2">
              <font color="#808080">, 
<br /></font>
              <font color="#0000ff">STATUS</font>
              <font color="#808080">=</font>
              <font color="#0000ff">ON</font>
            </font>
            <font size="2">
              <font color="#808080">, 
<br /></font>
              <font color="#0000ff">MAX_QUEUE_READERS</font>
              <font color="#808080">=</font> 1</font>
            <font size="2">
              <font color="#808080">, 
<br /></font>
              <font color="#0000ff">EXECUTE</font>
              <font color="#0000ff">AS</font>
              <font color="#0000ff">OWNER</font>
            </font>
            <font color="#808080" size="2">) 
<br /></font>
            <font color="#0000ff">
              <font size="2">GO</font>
            </font>
          </p>
        </blockquote>
        <p>
Now try the procedures procLockedTran1 and procLockedTran2 and see the results in
the msdb.dbo.tblDeadLock_Log and in your mail (if configured).
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=2a1df111-2a66-4919-820f-019df215381d" />
      </body>
      <title>Deadlock alerts through Event Notification</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,2a1df111-2a66-4919-820f-019df215381d.aspx</guid>
      <link>http://resquel.com/ssb/2009/03/02/DeadlockAlertsThroughEventNotification.aspx</link>
      <pubDate>Mon, 02 Mar 2009 11:30:01 GMT</pubDate>
      <description>&lt;p&gt;
&lt;strong&gt;&lt;em&gt;&lt;u&gt;Updated 2010-07-24; added timeout to the receive command in the stored
procedure. For more info, see &lt;/u&gt;&lt;/em&gt;&lt;/strong&gt;&lt;a title="Service Broker can make your transaction log big" href="http://resquel.com/ssb/2010/07/24/ServiceBrokerCanMakeYourTransactionLogBig.aspx" target="_blank"&gt;&lt;em&gt;&lt;u&gt;Service
Broker can make your transaction log big&lt;/u&gt;&lt;/em&gt;&lt;/a&gt;&lt;strong&gt;&lt;em&gt;&lt;u&gt;.&lt;/u&gt;&lt;/em&gt;&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Event Notifications are based on Service Broker. This brings an advantage and a disadvantage;
enhanced scalability at the cost of increased complexity (and lack of a User Interface,
so we have to take it to T-SQL code). Lets take it there step by step for SQL Server
2005 and 2008;
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Some code to cause deadlocks. 
&lt;/li&gt;
&lt;li&gt;
See how the deadlocks look like using the Trace Flags. 
&lt;/li&gt;
&lt;li&gt;
See how the deadlocks look like in Profiler. 
&lt;/li&gt;
&lt;li&gt;
Use the Event Notification on the deadlocks. 
&lt;/li&gt;
&lt;li&gt;
Sending out the mail. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The code shown as part of this article is also available at this &lt;a title="DeadLock Event Notification script." href="http://www.resquel.com/ssb/content/upload/ReSQueLBlogPost20090302_DeadLockEventNotification.sql.txt" target="_blank"&gt;link&lt;/a&gt;.
To code to create the database for the deadlocks is available at this &lt;a title="DeadLockSample database script." href="http://www.resquel.com/ssb/content/upload/ReSQueLBlogPost20090302_DeadLockSample.sql.txt" target="_blank"&gt;link&lt;/a&gt;.
The code works with SQL Server 2005 SP2 and SQL Server 2008
&lt;/p&gt;
&lt;h3&gt;1. Some code to cause deadlocks.
&lt;/h3&gt;
&lt;p&gt;
To get the code to cause the deadlocks, click this &lt;a title="DeadLockSample database script." href="http://www.resquel.com/ssb/content/upload/ReSQueLBlogPost20090302_DeadLockSample.sql.txt" target="_blank"&gt;link&lt;/a&gt;.
Run the code from SQL Server Management Studio (SSMS) to create the DeadLockSample
database. Next create two additional connections (New Query) to the DeadLockSample
database, execute the procedure procLockedTran1 on one connection and procLockedTran2
on the other connection. There is a 5 seconds delay in the two procedures between
locking the first and second resource. So if you execute the two procedures within
5 seconds, you get your deadlock. You could also change the delay in the procedures
if you think 5 seconds is to short/long.
&lt;/p&gt;
&lt;p&gt;
Normally a process would be terminated when it is the deadlock victim. For the procedures
created by the script, the error is handled , so the process that is the deadlock
victim just selects it is the victim and the connection will not be terminated.
&lt;/p&gt;
&lt;p&gt;
If you want to create a deadlock in any of the following scenarios, just run procLockedTran1
and procLockedTran2 on two separate connections simultaneously.
&lt;/p&gt;
&lt;h3&gt;2. See how the deadlocks look like using the Trace Flags.
&lt;/h3&gt;
&lt;a href="http://www.resquel.com/ssb/content/binary/WindowsLiveWriter/DeadlockalertsthroughEventNotification_14104/TraceFlagConfiguration_2.png"&gt;&lt;img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="TraceFlagConfiguration" align="right" src="http://www.resquel.com/ssb/content/binary/WindowsLiveWriter/DeadlockalertsthroughEventNotification_14104/TraceFlagConfiguration_thumb.png" width="244" height="243" /&gt;&lt;/a&gt; 
&lt;p&gt;
To get the deadlocks recorded in the SQL Server Error Log, either set the trace flag
as startup parameter in SQL Server Configuration Manager (see picture) which will
cause the trace flag to be active each time you start your SQL Server (until you remove
the trace flag again), or issue a DBCC TRACEON (1222,-1) command which sets the trace
flag until you stop SQL Server or issue a DBCC TRACEOFF (1222,-1).
&lt;/p&gt;
&lt;p&gt;
With the trace flag active, you can now see the deadlock appear in you SQL Server
Error Log.
&lt;/p&gt;
&lt;h3&gt;3. See how the deadlocks look like in Profiler.
&lt;/h3&gt;
&lt;p&gt;
... or in SSMS for that matter. With the SQL Server tools it is possible to display
deadlock graphs; XML documents (with .xdl extension) describing the deadlock information
of one or more deadlocks. When the deadlock graph event (EventClass 148) with the
TextData column is included in the trace definition, the XML to create the deadlock
graph is captured (both through the Profiler graphical tool and the sp_trace procedures)
and the graph will automatically be displayed when opened in Profiler. Also the graphical
profiler has the option to write deadlock graphs directly to a file. Note that the
graphical presentation of the deadlock is limited compared to the information in the
.xdl document. The information in the .xdl document is the same information as the
information written to the SQL Server Error Log with trace flag 1222. One extra comment
if you filter your trace on database, you won't get the deadlock graph, as &lt;a href="http://sqlblog.com/blogs/kalen_delaney/archive/2006/11/28/376.aspx" target="_blank"&gt;noticed
by Kalen Delaney&lt;/a&gt;.
&lt;/p&gt;
&lt;h3&gt;4. Use the Event Notification on the deadlocks.
&lt;/h3&gt;
&lt;p&gt;
Event Notification lets you capture events that can occur on server, database or broker
queue, for a list of events, query sys.event_notification_event_types. For this purpose,
the event type 1148, DEADLOCK_GRAPH is of interest. Also note that the DEADLOCK_GRAPH
is a &lt;a href="http://msdn.microsoft.com/en-us/library/ms190655.aspx" target="_blank"&gt;trace
event&lt;/a&gt;, which can only be captured at server level.
&lt;/p&gt;
&lt;p&gt;
The second thing needed is a broker SERVICE to send the event to. Since we'll be handling
the events at server level, the SERVICE (and QUEUE) will be created in the msdb database.
There is no need to create the MESSAGE TYPEs&amp;#160; or CONTRACTs as they already exist
for Event Notification.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;USE&lt;/font&gt; msdb 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;GO 
&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#008000"&gt;-- Create a queue to receive messages. 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;CREATE&lt;/font&gt; &lt;font color="#0000ff"&gt;QUEUE&lt;/font&gt; queDeadLock_Graph&lt;/font&gt;&lt;font color="#808080" size="2"&gt;; 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;GO 
&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#008000"&gt;-- Create a service on the queue that
references 
&lt;br /&gt;
-- the event notifications contract. 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;CREATE&lt;/font&gt; &lt;font color="#0000ff"&gt;SERVICE&lt;/font&gt; svcDeadLock_Graph 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;ON&lt;/font&gt; &lt;font color="#0000ff"&gt;QUEUE&lt;/font&gt; queDeadLock_Graph 
&lt;br /&gt;
&lt;font color="#808080"&gt;(&lt;/font&gt;[http://schemas.microsoft.com/SQL/Notifications/PostEventNotification]&lt;/font&gt;&lt;font color="#808080" size="2"&gt;); 
&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;GO 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#008000"&gt;-- Now query the sys.databases for the service_broker_guid
of the msdb database.&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#008000"&gt; 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;SELECT&lt;/font&gt; service_broker_guid &lt;font color="#0000ff"&gt;FROM&lt;/font&gt; &lt;font color="#008000"&gt;sys&lt;/font&gt;&lt;font color="#808080"&gt;.&lt;/font&gt;&lt;font color="#008000"&gt;databases&lt;/font&gt; &lt;font color="#0000ff"&gt;WHERE&lt;/font&gt; name &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;font color="#ff0000"&gt;'msdb'&lt;/font&gt;&lt;font color="#808080"&gt;;&lt;/font&gt;&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The result of the query for the Service Broker GUID for the msdb databases is needed
to point to the right database for the service.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font size="2"&gt;&lt;font color="#008000"&gt;-- Create the event notification. 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;CREATE&lt;/font&gt; &lt;font color="#0000ff"&gt;EVENT&lt;/font&gt; &lt;font color="#0000ff"&gt;NOTIFICATION&lt;/font&gt; evnDeadLock_Graph 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;ON&lt;/font&gt; &lt;font color="#0000ff"&gt;SERVER&lt;/font&gt; 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;FOR&lt;/font&gt; DEADLOCK_GRAPH 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;TO&lt;/font&gt; &lt;font color="#0000ff"&gt;SERVICE&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#ff0000"&gt;'svcDeadLock_Graph' 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt; &lt;font color="#ff0000"&gt;'2A7C5300-7C64-43F5-AB10-5B079D07678E'&lt;/font&gt;&lt;font color="#808080"&gt;;&lt;/font&gt; &lt;font color="#008000"&gt;--
the GUID for msdb goes here.&lt;/font&gt;&lt;/font&gt;&lt;font color="#008000" size="2"&gt; 
&lt;br /&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
You can look at the content of the queue through SELECT * FROM msdb.dbo.queDeadLock_Graph,
as a queue in fact is a table. Next step will be processing the messages that get
into the queue and more specifically get the deadlock details out of the message_body.
The message body looks binary, but if you query it like this;
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;SELECT&lt;/font&gt; &lt;font color="#ff00ff"&gt;CAST&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;message_body &lt;font color="#0000ff"&gt;AS&lt;/font&gt; &lt;font color="#0000ff"&gt;xml&lt;/font&gt;&lt;font color="#808080"&gt;)&lt;/font&gt; &lt;font color="#0000ff"&gt;FROM&lt;/font&gt; msdb&lt;font color="#808080"&gt;.&lt;/font&gt;dbo&lt;font color="#808080"&gt;.&lt;/font&gt;queDeadLock_Graph&lt;/font&gt;&lt;font size="+0"&gt; &lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
you'll notice that it is actually XML, where the textdata node holds the xdl structure
of the deadlock graph.
&lt;/p&gt;
&lt;h3&gt;5. Sending out the mail
&lt;/h3&gt;
&lt;p&gt;
Okay, we're in Service Broker land by now. Event Notification acts as the Initiator
Service and the svcDeadLock_Graph is the Target Service. In this scenario, the only
thing left to be done is specify how the queue should be processed. The processing
shall be done by a stored procedure that will be tied to the queue as the activation
program. Once something comes in to the queue, this stored procedure will be invoked...
kind of like a trigger for insert on a normal table. The specification for the stored
procedure will be:
&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;
Extract the deadlock graph from the message_body and store it in a variable. 
&lt;/li&gt;
&lt;li&gt;
Extract the datetime of the deadlock graph notification and store it in a variable. 
&lt;/li&gt;
&lt;li&gt;
Store the datetime and deadlock graph in a table. 
&lt;/li&gt;
&lt;li&gt;
Send the datetime as part of the message and deadlock graph as attachment in an email.
This does require database mail to be set up, if you haven't, an error will be logged
in the table. 
&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;
The code to create the table to hold the deadlock information (and the reason why
no mail was sent).
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;USE&lt;/font&gt; msdb 
&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;GO 
&lt;br /&gt;
CREATE&lt;/font&gt; &lt;font color="#0000ff"&gt;TABLE&lt;/font&gt; dbo&lt;font color="#808080"&gt;.&lt;/font&gt;tblDeadLock_Log&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;( 
&lt;br /&gt;
&lt;/font&gt;DeadLock_ID &lt;font color="#0000ff"&gt;int&lt;/font&gt; &lt;font color="#0000ff"&gt;IDENTITY&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;1&lt;font color="#808080"&gt;,&lt;/font&gt;1&lt;font color="#808080"&gt;)&lt;/font&gt; &lt;font color="#0000ff"&gt;CONSTRAINT&lt;/font&gt; pk_tblDeadLock_Log &lt;font color="#0000ff"&gt;PRIMARY&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;KEY 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt; DeadLock_Detected &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;datetime 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt; DeadLock_Graph &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;xml 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt; NoMailReason &lt;font color="#0000ff"&gt;nvarchar&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;2048&lt;/font&gt;&lt;font color="#808080" size="2"&gt;)) 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;&lt;font size="2"&gt;GO&lt;/font&gt; 
&lt;br /&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
The stored procedure to be used as the activation program on the queue, this is where
the real work is done. Alter the @profile_name and @recipients in at sp_send_dbmail
to match the setup of your dbmail setup.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;CREATE&lt;/font&gt; &lt;font color="#0000ff"&gt;PROCEDURE&lt;/font&gt; dbo&lt;font color="#808080"&gt;.&lt;/font&gt;procReceiveDeadLock_Graph 
&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;AS 
&lt;br /&gt;
DECLARE&lt;/font&gt; @conversation_handle &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;uniqueidentifier 
&lt;br /&gt;
DECLARE&lt;/font&gt; @message_body &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;xml 
&lt;br /&gt;
DECLARE&lt;/font&gt; @message_type_name &lt;font color="#0000ff"&gt;nvarchar&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;128&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;) 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;DECLARE&lt;/font&gt; @deadlock_graph &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;xml 
&lt;br /&gt;
DECLARE&lt;/font&gt; @event_datetime &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;datetime 
&lt;br /&gt;
DECLARE&lt;/font&gt; @deadlock_id &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;int 
&lt;br /&gt;
BEGIN&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;TRY 
&lt;br /&gt;
BEGIN&lt;/font&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;TRAN 
&lt;br /&gt;
WAITFOR&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;( 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;RECEIVE&lt;/font&gt; &lt;font color="#0000ff"&gt;TOP&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;1&lt;font color="#808080"&gt;)&lt;/font&gt; @conversation_handle &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;conversation_handle 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt; @message_body &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;font color="#ff00ff"&gt;CAST&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;message_body &lt;font color="#0000ff"&gt;AS&lt;/font&gt; &lt;font color="#0000ff"&gt;xml&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;) 
&lt;br /&gt;
,&lt;/font&gt; @message_type_name &lt;font color="#808080"&gt;=&lt;/font&gt; message_type_name 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;FROM&lt;/font&gt; dbo&lt;font color="#808080"&gt;.&lt;/font&gt;queDeadLock_Graph&lt;font color="#808080"&gt;)&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080"&gt;&lt;font size="2"&gt; 
&lt;br /&gt;
, &lt;font color="#0000ff"&gt;TIMEOUT &lt;/font&gt;&lt;font color="#808080"&gt;10000 &lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#008000"&gt;--
Line added 2010-07-24; 
&lt;br /&gt;
-- http://resquel.com/ssb/2010/07/24/ServiceBrokerCanMakeYourTransactionLogBig.aspx &lt;/font&gt; 
&lt;br /&gt;
&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#008000"&gt;-- Validate message 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;IF &lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;@message_type_name &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;font color="#ff0000"&gt;'http://schemas.microsoft.com/SQL/Notifications/EventNotification'&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;AND 
&lt;br /&gt;
&lt;/font&gt;@message_body&lt;font color="#808080"&gt;.&lt;/font&gt;exist&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff0000"&gt;'(/EVENT_INSTANCE/TextData/deadlock-list)'&lt;/font&gt;&lt;font color="#808080"&gt;)&lt;/font&gt; &lt;font color="#808080"&gt;=&lt;/font&gt; 1&lt;/font&gt;&lt;font color="#808080" size="2"&gt;) 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;BEGIN 
&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#008000"&gt;-- Extract the info from the message 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;SELECT&lt;/font&gt; @deadlock_graph &lt;font color="#808080"&gt;=&lt;/font&gt; @message_body&lt;font color="#808080"&gt;.&lt;/font&gt;query&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff0000"&gt;'(/EVENT_INSTANCE/TextData/deadlock-list)'&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;) 
&lt;br /&gt;
,&lt;/font&gt; @event_datetime &lt;font color="#808080"&gt;= &lt;/font&gt;@message_body&lt;font color="#808080"&gt;.&lt;/font&gt;value&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff0000"&gt;'(/EVENT_INSTANCE/PostTime)[1]'&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt;&lt;font color="#ff0000"&gt;'datetime'&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;) 
&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#008000"&gt;-- Put the info in the table 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;INSERT&lt;/font&gt; dbo&lt;font color="#808080"&gt;.&lt;/font&gt;tblDeadLock_Log&lt;font color="#0000ff"&gt; &lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;DeadLock_Detected&lt;font color="#808080"&gt;,&lt;/font&gt; DeadLock_Graph&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;) 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;VALUES &lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;@event_datetime&lt;font color="#808080"&gt;,&lt;/font&gt; @deadlock_graph&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;) 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;SELECT&lt;/font&gt; @deadlock_id &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;font color="#ff00ff"&gt;SCOPE_IDENTITY&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;() 
&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#008000"&gt;-- Send deadlock alert mail. 
&lt;br /&gt;
-- Requires configured database mail, will log an error if not (or anything else goes
wrong). 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;BEGIN&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;TRY 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;DECLARE&lt;/font&gt; @subj &lt;font color="#0000ff"&gt;nvarchar&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;255&lt;font color="#808080"&gt;),&lt;/font&gt; @bdy &lt;font color="#0000ff"&gt;nvarchar&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff00ff"&gt;max&lt;/font&gt;&lt;font color="#808080"&gt;),&lt;/font&gt; @qry &lt;font color="#0000ff"&gt;nvarchar&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#ff00ff"&gt;max&lt;/font&gt;&lt;font color="#808080"&gt;),&lt;/font&gt; @attfn &lt;font color="#0000ff"&gt;nvarchar&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;255&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;) 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;SELECT&lt;/font&gt; @subj &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;font color="#ff0000"&gt;'A
deadlock occurred on '&lt;/font&gt; &lt;font color="#808080"&gt;+&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#ff00ff"&gt;@@SERVERNAME 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt; @bdy &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;font color="#ff0000"&gt;'A
deadlock occurred at '&lt;/font&gt; &lt;font color="#808080"&gt;+&lt;/font&gt; &lt;font color="#ff00ff"&gt;CONVERT&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;&lt;font color="#0000ff"&gt;varchar&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;50&lt;font color="#808080"&gt;),&lt;/font&gt;@event_datetime&lt;font color="#808080"&gt;,&lt;/font&gt; 120&lt;font color="#808080"&gt;)&lt;/font&gt; &lt;font color="#808080"&gt;+&lt;/font&gt; &lt;font color="#ff0000"&gt;'
on SQL Server: '&lt;/font&gt; &lt;font color="#808080"&gt;+&lt;/font&gt; &lt;font color="#ff00ff"&gt;@@SERVERNAME&lt;/font&gt; &lt;font color="#808080"&gt;+&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#ff0000"&gt;'.
See attached xdl-file for deadlock details.' 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt; @qry &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;font color="#ff0000"&gt;'SET
NOCOUNT ON; SELECT deadlock_graph FROM msdb.dbo.tblDeadLock_Log WITH (READUNCOMMITTED)
WHERE DeadLock_ID = '&lt;/font&gt; &lt;font color="#808080"&gt;+&lt;/font&gt; &lt;font color="#ff00ff"&gt;CAST&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;@deadlock_id &lt;font color="#0000ff"&gt;AS&lt;/font&gt; &lt;font color="#0000ff"&gt;varchar&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;10&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;)) &lt;font color="#008000"&gt;--
Locking hint is to prevent this dynamic query to be blocked by the lock held by the
insert. The dynamic SQL will not come from inside this transaction.&lt;/font&gt; 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt; @attfn &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;font color="#ff00ff"&gt;@@SERVERNAME&lt;/font&gt; &lt;font color="#808080"&gt;+&lt;/font&gt; &lt;font color="#ff0000"&gt;'_'&lt;/font&gt; &lt;font color="#808080"&gt;+&lt;/font&gt; &lt;font color="#ff00ff"&gt;CAST&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;@deadlock_id &lt;font color="#0000ff"&gt;AS&lt;/font&gt; &lt;font color="#0000ff"&gt;varchar&lt;/font&gt;&lt;font color="#808080"&gt;(&lt;/font&gt;10&lt;font color="#808080"&gt;))&lt;/font&gt; &lt;font color="#808080"&gt;+&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#ff0000"&gt;'.xdl' 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;EXEC&lt;/font&gt; &lt;font color="#800000"&gt;sp_send_dbmail&lt;/font&gt;&lt;font color="#0000ff"&gt; &lt;/font&gt;@profile_name &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#ff0000"&gt;'default' 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt; @recipients &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#ff0000"&gt;'administrator@localhost' 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt; @subject &lt;font color="#808080"&gt;=&lt;/font&gt; @subj 
&lt;br /&gt;
&lt;font color="#808080"&gt;,&lt;/font&gt; @body &lt;font color="#808080"&gt;=&lt;/font&gt; @bdy 
&lt;br /&gt;
&lt;font color="#808080"&gt;,&lt;/font&gt; @query &lt;font color="#808080"&gt;=&lt;/font&gt; @qry 
&lt;br /&gt;
&lt;font color="#808080"&gt;,&lt;/font&gt; @attach_query_result_as_file &lt;font color="#808080"&gt;=&lt;/font&gt; 1 
&lt;br /&gt;
&lt;font color="#808080"&gt;,&lt;/font&gt; @query_attachment_filename &lt;font color="#808080"&gt;=&lt;/font&gt; @attfn &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#008000"&gt;--
http://support.microsoft.com/kb/924345 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#808080"&gt;,&lt;/font&gt; @query_result_header &lt;font color="#808080"&gt;=&lt;/font&gt; 0 
&lt;br /&gt;
&lt;font color="#808080"&gt;,&lt;/font&gt; @query_result_width &lt;font color="#808080"&gt;=&lt;/font&gt; 32767 
&lt;br /&gt;
&lt;font color="#808080"&gt;,&lt;/font&gt; @query_no_truncate &lt;font color="#808080"&gt;=&lt;/font&gt; 1 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;END&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;TRY 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;BEGIN&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;CATCH 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;UPDATE&lt;/font&gt; dbo&lt;font color="#808080"&gt;.&lt;/font&gt;tblDeadLock_Log 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;SET&lt;/font&gt; NoMailReason &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;font color="#ff00ff"&gt;ERROR_MESSAGE&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;() 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;WHERE&lt;/font&gt; DeadLock_ID &lt;font color="#808080"&gt;=&lt;/font&gt; @deadlock_id 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;END&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;CATCH 
&lt;br /&gt;
END 
&lt;br /&gt;
ELSE &lt;font color="#008000"&gt;-- Not an event notification with deadlock-list&lt;/font&gt; 
&lt;br /&gt;
END&lt;/font&gt; &lt;font color="#0000ff"&gt;CONVERSATION&lt;/font&gt; @conversation_handle 
&lt;br /&gt;
&lt;font color="#0000ff"&gt;COMMIT&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;TRAN 
&lt;br /&gt;
END&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;TRY 
&lt;br /&gt;
BEGIN&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;CATCH 
&lt;br /&gt;
ROLLBACK&lt;/font&gt; &lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;TRAN 
&lt;br /&gt;
END&lt;/font&gt; &lt;/font&gt;&lt;font color="#0000ff" size="2"&gt;CATCH 
&lt;br /&gt;
GO 
&lt;br /&gt;
&lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Finally to tie the stored procedure to the queue so incoming notifications will be
processed.
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;ALTER&lt;/font&gt; &lt;font color="#0000ff"&gt;QUEUE&lt;/font&gt; dbo&lt;font color="#808080"&gt;.&lt;/font&gt;queDeadLock_Graph 
&lt;br /&gt;
&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#0000ff"&gt;WITH 
&lt;br /&gt;
STATUS&lt;/font&gt; &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;font color="#0000ff"&gt;ON&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;, 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;ACTIVATION &lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;( 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;PROCEDURE_NAME&lt;/font&gt; &lt;font color="#808080"&gt;=&lt;/font&gt; msdb&lt;font color="#808080"&gt;.&lt;/font&gt;dbo&lt;font color="#808080"&gt;.&lt;/font&gt;procReceiveDeadLock_Graph&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;, 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;STATUS&lt;/font&gt; &lt;font color="#808080"&gt;=&lt;/font&gt; &lt;font color="#0000ff"&gt;ON&lt;/font&gt;&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;, 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;MAX_QUEUE_READERS&lt;/font&gt; &lt;font color="#808080"&gt;=&lt;/font&gt; 1&lt;/font&gt;&lt;font size="2"&gt;&lt;font color="#808080"&gt;, 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;EXECUTE&lt;/font&gt; &lt;font color="#0000ff"&gt;AS&lt;/font&gt; &lt;font color="#0000ff"&gt;OWNER&lt;/font&gt;&lt;/font&gt;&lt;font color="#808080" size="2"&gt;) 
&lt;br /&gt;
&lt;/font&gt;&lt;font color="#0000ff"&gt;&lt;font size="2"&gt;GO&lt;/font&gt; &lt;/font&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
Now try the procedures procLockedTran1 and procLockedTran2 and see the results in
the msdb.dbo.tblDeadLock_Log and in your mail (if configured).
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=2a1df111-2a66-4919-820f-019df215381d" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,2a1df111-2a66-4919-820f-019df215381d.aspx</comments>
      <category>SQL Server</category>
    </item>
    <item>
      <trackback:ping>http://resquel.com/ssb/Trackback.aspx?guid=c63b8f8e-06e5-42cc-bfdb-f9ea8b76adbc</trackback:ping>
      <pingback:server>http://resquel.com/ssb/pingback.aspx</pingback:server>
      <pingback:target>http://resquel.com/ssb/PermaLink,guid,c63b8f8e-06e5-42cc-bfdb-f9ea8b76adbc.aspx</pingback:target>
      <dc:creator>Stan Segers</dc:creator>
      <wfw:comment>http://resquel.com/ssb/CommentView,guid,c63b8f8e-06e5-42cc-bfdb-f9ea8b76adbc.aspx</wfw:comment>
      <wfw:commentRss>http://resquel.com/ssb/SyndicationService.asmx/GetEntryCommentsRss?guid=c63b8f8e-06e5-42cc-bfdb-f9ea8b76adbc</wfw:commentRss>
      <body xmlns="http://www.w3.org/1999/xhtml">
        <p>
Okay, here's the deal; deadlocks sometimes occur and you want to be notified as they
happen.
</p>
        <p>
So define an SQL Agent Alert on error 1205 to send you an e-mail notification... partially,
but it won't work as error 1205 isn't logged.
</p>
        <p>
How to get the error logged; either set the severity level to 19 or above when the
message is created (does not apply to 1205) or flag the message to be logged to event
log, which can be done using sp_altermessage... at least this was the way to do it
with SQL Server 2000 but one of the changes for SQL Server 2005 says:
</p>
        <blockquote>
          <p>
            <b>sp_altermessage</b> cannot be used to change the logging behavior of system messages
(messages with Message ID &lt; 50000). To audit system messages, use SQL Trace and
the <a href="http://msdn.microsoft.com/en-us/library/ms190953(SQL.90).aspx" target="_blank">User
Error Message Event Class</a>. For more information, see <a href="http://msdn.microsoft.com/en-us/library/ms191006(SQL.90).aspx" target="_blank">Introducing
SQL Trace</a>.
</p>
        </blockquote>
        <p>
For SQL Server 2005 it used to be a dead end, but with as of Service Pack 3 it is
again possible to alter sys.messages &lt; 50000 so they are written to the Windows
NT Application Event Log. Specifically for deadlocks you'd run:
</p>
        <blockquote>
          <p>
EXEC sp_altermessage 1205, 'WITH_LOG', 'true'
</p>
        </blockquote>
        <p>
but it would apply to other system events you want logged too.
</p>
        <p>
Update 2009-04-08; issue fixed in Service Pack 1 for SQL Server 2008. <strike>For
SQL Server 2008 (up until Cumulative Update 3) it is still impossible to alter sys.messages
&lt; 50000. Though it was mentioned (2008-07-28) at connect in </strike><a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=294122" target="_blank"><strike>FeedBackID=294122</strike></a><strike> that
this issue would be fixed for SQL Server 2008 too, so far (2009-02-27) it isn't.</strike></p>
        <p>
          <strike>Besides the fact that the alert option is still impossible with SQL Server
2008,</strike> it can only tell you a deadlock occurred. Should you need more information
on the deadlock however, your next option would be <a href="http://msdn.microsoft.com/en-us/library/ms188396(SQL.90).aspx" target="_blank">TRACE
flags</a> 1204 or 1222, the difference between the two of them being the amount of
detail. Trace flag 1222 will be use during the post as it provides the most detail.
Setting the trace flags writes the deadlock information to the SQL Server Error Log,
but not to the Windows NT Application Event Log... so there's <strike>one</strike> two
(and a half) option remaining:
</p>
        <blockquote>
          <p>
Process the Error Log with the undocumented sp_readerrorlog as part of the alert response...
but that won't work for SQL Server 2008 (yet) and you have a better option for SQL
Server 2005. For more info see <a href="http://www.mssqltips.com/tip.asp?tip=1476" target="_blank">MSSQLTips</a>.
</p>
          <p>
SQL Server 2005 &amp; 2008: <a title="Deadlock alerts through Event Notification" href="http://www.resquel.com/ssb/2009/03/02/DeadlockAlertsThroughEventNotification.aspx" target="_blank">Event
Notification</a></p>
          <p>
SQL Server 2005 &amp; 2008: WMI Alert, see <a href="http://msdn.microsoft.com/en-us/library/ms186385(SQL.90).aspx" target="_blank">MSDN</a></p>
        </blockquote>
        <p>
So next post will be on getting your <a title="Deadlock alerts through Event Notification" href="http://www.resquel.com/ssb/2009/03/02/DeadlockAlertsThroughEventNotification.aspx" target="_blank">deadlock
info through Event Notification</a>.
</p>
        <img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=c63b8f8e-06e5-42cc-bfdb-f9ea8b76adbc" />
      </body>
      <title>Alert me (on deadlocks)</title>
      <guid isPermaLink="false">http://resquel.com/ssb/PermaLink,guid,c63b8f8e-06e5-42cc-bfdb-f9ea8b76adbc.aspx</guid>
      <link>http://resquel.com/ssb/2009/02/27/AlertMeOnDeadlocks.aspx</link>
      <pubDate>Fri, 27 Feb 2009 21:47:32 GMT</pubDate>
      <description>&lt;p&gt;
Okay, here's the deal; deadlocks sometimes occur and you want to be notified as they
happen.
&lt;/p&gt;
&lt;p&gt;
So define an SQL Agent Alert on error 1205 to send you an e-mail notification... partially,
but it won't work as error 1205 isn't logged.
&lt;/p&gt;
&lt;p&gt;
How to get the error logged; either set the severity level to 19 or above when the
message is created (does not apply to 1205) or flag the message to be logged to event
log, which can be done using sp_altermessage... at least this was the way to do it
with SQL Server 2000 but one of the changes for SQL Server 2005 says:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
&lt;b&gt;sp_altermessage&lt;/b&gt; cannot be used to change the logging behavior of system messages
(messages with Message ID &amp;lt; 50000). To audit system messages, use SQL Trace and
the &lt;a href="http://msdn.microsoft.com/en-us/library/ms190953(SQL.90).aspx" target="_blank"&gt;User
Error Message Event Class&lt;/a&gt;. For more information, see &lt;a href="http://msdn.microsoft.com/en-us/library/ms191006(SQL.90).aspx" target="_blank"&gt;Introducing
SQL Trace&lt;/a&gt;.
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
For SQL Server 2005 it used to be a dead end, but with as of Service Pack 3 it is
again possible to alter sys.messages &amp;lt; 50000 so they are written to the Windows
NT Application Event Log. Specifically for deadlocks you'd run:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
EXEC sp_altermessage 1205, 'WITH_LOG', 'true'
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
but it would apply to other system events you want logged too.
&lt;/p&gt;
&lt;p&gt;
Update 2009-04-08; issue fixed in Service Pack 1 for SQL Server 2008. &lt;strike&gt;For
SQL Server 2008 (up until Cumulative Update 3) it is still impossible to alter sys.messages
&amp;lt; 50000. Though it was mentioned (2008-07-28) at connect in &lt;/strike&gt;&lt;a href="https://connect.microsoft.com/SQLServer/feedback/ViewFeedback.aspx?FeedbackID=294122" target="_blank"&gt;&lt;strike&gt;FeedBackID=294122&lt;/strike&gt;&lt;/a&gt;&lt;strike&gt; that
this issue would be fixed for SQL Server 2008 too, so far (2009-02-27) it isn't.&lt;/strike&gt;
&lt;/p&gt;
&lt;p&gt;
&lt;strike&gt;Besides the fact that the alert option is still impossible with SQL Server
2008,&lt;/strike&gt; it can only tell you a deadlock occurred. Should you need more information
on the deadlock however, your next option would be &lt;a href="http://msdn.microsoft.com/en-us/library/ms188396(SQL.90).aspx" target="_blank"&gt;TRACE
flags&lt;/a&gt; 1204 or 1222, the difference between the two of them being the amount of
detail. Trace flag 1222 will be use during the post as it provides the most detail.
Setting the trace flags writes the deadlock information to the SQL Server Error Log,
but not to the Windows NT Application Event Log... so there's &lt;strike&gt;one&lt;/strike&gt; two
(and a half) option remaining:
&lt;/p&gt;
&lt;blockquote&gt; 
&lt;p&gt;
Process the Error Log with the undocumented sp_readerrorlog as part of the alert response...
but that won't work for SQL Server 2008 (yet) and you have a better option for SQL
Server 2005. For more info see &lt;a href="http://www.mssqltips.com/tip.asp?tip=1476" target="_blank"&gt;MSSQLTips&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
SQL Server 2005 &amp;amp; 2008: &lt;a title="Deadlock alerts through Event Notification" href="http://www.resquel.com/ssb/2009/03/02/DeadlockAlertsThroughEventNotification.aspx" target="_blank"&gt;Event
Notification&lt;/a&gt;
&lt;/p&gt;
&lt;p&gt;
SQL Server 2005 &amp;amp; 2008: WMI Alert, see &lt;a href="http://msdn.microsoft.com/en-us/library/ms186385(SQL.90).aspx" target="_blank"&gt;MSDN&lt;/a&gt;
&lt;/p&gt;
&lt;/blockquote&gt; 
&lt;p&gt;
So next post will be on getting your &lt;a title="Deadlock alerts through Event Notification" href="http://www.resquel.com/ssb/2009/03/02/DeadlockAlertsThroughEventNotification.aspx" target="_blank"&gt;deadlock
info through Event Notification&lt;/a&gt;.
&lt;/p&gt;
&lt;img width="0" height="0" src="http://resquel.com/ssb/aggbug.ashx?id=c63b8f8e-06e5-42cc-bfdb-f9ea8b76adbc" /&gt;</description>
      <comments>http://resquel.com/ssb/CommentView,guid,c63b8f8e-06e5-42cc-bfdb-f9ea8b76adbc.aspx</comments>
      <category>SQL Server</category>
    </item>
  </channel>
</rss>
