<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
    <channel>
        <title>SQLServer - Kennydust</title>
        <description>Thoughts from a C# Developer</description>
        <link>/site.aspx/Tags/SQLServer/RSS</link>
        <language>en</language>
        <image>
            <url>http://www.kennydust.com/Content/icons/flame.png</url>
            <title>Kennydust</title>
            <link>/site.aspx/Tags/SQLServer/RSS</link>
            <width>64</width>
            <height>64</height>
        </image>
        <item>
            <dc:creator>kennydust</dc:creator>
            <title>Concatenating multiple row data into a single row in SQL Server.</title>
            <description>In one of my recent projects, I needed to combine multiple row data values into a single one-liner. No idea about performance, but I leveraged the FOR XML PATH for my example.. Thanks to this &lt;a href=&quot;http://databases.aspfaq.com/general/how-do-i-concatenate-strings-from-a-column-into-a-single-row.html&quot; target=&quot;_blank&quot;&gt;link&lt;/a&gt; for the tip.

&lt;pre class=&quot;brush: sql;&quot;&gt;
USE AdventureWorks 
GO 
 
SELECT 
    CustomerID, 
    SalesOrderIDs = REPLACE( 
        ( 
            SELECT 
                SalesOrderID AS [data()] 
            FROM 
                Sales.SalesOrderHeader soh 
            WHERE 
                soh.CustomerID = c.CustomerID 
            ORDER BY 
                SalesOrderID 
            FOR XML PATH ('') 
        ), ' ', ',') 
FROM 
    Sales.Customer c 
ORDER BY 
    CustomerID
&lt;/pre&gt;</description>
            <link>http://www.kennydust.com/site.aspx/Blog/Concatenating-multiple-row-data-into-a-single-row-in-SQL-Server</link>
            <guid isPermaLink="true">http://www.kennydust.com/site.aspx/Blog/Concatenating-multiple-row-data-into-a-single-row-in-SQL-Server</guid>
            <pubDate>Wed, 19 Jan 2011 20:01:00 GMT</pubDate>
            <category>SQLServer</category>
        </item>
        <item>
            <dc:creator>kennydust</dc:creator>
            <title>Identifying and diagnosing SQL Server bottlenecks</title>
            <description>&lt;p&gt;In a job where you’re responsible for an application and how it performs, the worst thing that can happen is waking up to an email chain from management, detailing how the application is sluggish, unresponsive and in short, broken. You are to be blamed; finger pointing is running at an all-time high and you’ve just caught up to your 20th email.&lt;/p&gt;
&lt;p&gt;Has this ever happened to you?  It’s pretty much an engineer’s worst nightmare. Things happen unfortunately, and you need to track it down.&lt;/p&gt;
&lt;p&gt;A situation like that arose recently, and it was narrowed down to it being database related. However, in an environment where there's thousands of stored procedures, countless amount of indexes and an application where there's millions of lines of code; identifying the underlining issue is somewhat of a daunting task. &lt;/p&gt;
&lt;p&gt;I’ve done these level of diagnosis a handful of times, and normally I would load up SQL Server Profiler (on production) to try to capture the events that are happening real time.  Well over a half-hour or an hours’ worth of data is then dumped into a table and quickly gleam over it, sorting it by duration. However, being that I only have a basic understanding of this, I needed more insight. So I came across a few posts which takes it further and really breaks down how to best go about that tool and really identify the meat of the data you've just traced:&lt;/p&gt;

&lt;p&gt;
&lt;A href=&quot;http://www.simple-talk.com/sql/performance/finding-the-causes-of-poor-performance-in-sql-server,-part-1/&quot; target=&quot;_blank&quot;&gt;Finding the Causes of Poor Performance in SQL Server, Part 1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;
&lt;A href=&quot;http://www.simple-talk.com/sql/performance/finding-the-causes-of-poor-performance-in-sql-server,-part-2/&quot; target=&quot;_blank&quot;&gt;Finding the Causes of Poor Performance in SQL Server, Part 2&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Referencing Part 1, I had to add the “procedurename” column into that trace table since the trace program does not create it for you out of the gate.&lt;/p&gt;
&lt;p&gt;
The textdata column is an ntext type,  so it needs to be converted to a varchar(max) column type in order for the update procedure to work.&lt;/p&gt;

&lt;pre class=&quot;brush: sql;&quot;&gt;
UPDATE prod11192010_b
   SET ProcedureName = 
   LEFT(
      RIGHT(TextData, LEN(TextData) - CHARINDEX(' ',TextData, CHARINDEX('Exec',TextData))),
      CHARINDEX(' ', RIGHT(TextData, LEN(TextData) - CHARINDEX(' ',TextData, CHARINDEX('Exec',TextData))) + ' ')
   )
where TextData like '%exec%'
&lt;/pre&gt;

&lt;p&gt;and the best part… good data.&lt;/p&gt;
&lt;pre class=&quot;brush: sql;&quot;&gt;
select top(10) procedurename, sum(duration) as timeimpact, sum(reads) as IOImpact, sum(cpu) as cpuimpact,
count(*) as executioncount
from prod11192010_b
group by procedurename
order by timeimpact desc
&lt;/pre&gt;
&lt;p&gt;
to be continued.
&lt;/p&gt;</description>
            <link>http://www.kennydust.com/site.aspx/Blog/Identifying-and-diagnosing-SQL-Server-bottlenecks</link>
            <guid isPermaLink="true">http://www.kennydust.com/site.aspx/Blog/Identifying-and-diagnosing-SQL-Server-bottlenecks</guid>
            <pubDate>Fri, 19 Nov 2010 16:01:00 GMT</pubDate>
            <category>performance</category>
            <category>SQLServer</category>
            <category>bottlenecks</category>
        </item>
        <item>
            <dc:creator>kennydust</dc:creator>
            <title>Dancing around with XQuery and SQL Server - Part 2</title>
            <description>&lt;p&gt;This is a follow up on a &lt;a target=&quot;_self&quot; href=&quot;http://kennydust.com/tech/sql/dancing-around-with-xquery-and-sql-server-part-1/&quot;&gt;prior post&lt;/a&gt; I've made a few months back on XQuery, with the purpose of this post to just show a few more examples on how powerful XML and XQuery can be -- with no additional overhead costs.&amp;nbsp; If implemented correctly, you can even get better performance gains; but I'll possibly discuss that in a future offering. For this segment, I wanted to&amp;nbsp;document the common usages I've come across lately, and refer to it later on as a refresher.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Example 1&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;So, you've got xml data, and the structure looks as such:&lt;/p&gt;
&lt;pre class=&quot;brush: xml;&quot;&gt;
&lt;metadata&gt;
    &lt;item&gt;
        &lt;key&gt;key1&lt;/key&gt;
        &lt;value&gt;value1&lt;/value&gt;
    &lt;/item&gt;
    &lt;item&gt;
        &lt;key&gt;key2&lt;/key&gt;
        &lt;value&gt;value2&lt;/value&gt;
    &lt;/item&gt;
&lt;/metadata&gt;
&lt;/pre&gt;
&lt;p&gt;To query this data, we do something like this:&lt;/p&gt;

&lt;pre class=&quot;brush: sql;&quot;&gt;
declare @xml xml
set @xml = N'
&lt;metadata&gt;
&lt;item&gt;&lt;key&gt;key1&lt;/key&gt;&lt;value&gt;value1&lt;/value&gt;&lt;/item&gt;
&lt;item&gt;&lt;key&gt;key2&lt;/key&gt;&lt;value&gt;value2&lt;/value&gt;&lt;/item&gt;
&lt;item&gt;&lt;key&gt;key3&lt;/key&gt;&lt;value&gt;value3&lt;/value&gt;&lt;/item&gt;&lt;/metadata&gt;
'
select 
metadata.[item].value('key[1]', 'nvarchar(25)') as [key],
metadata.[item].value('value[1]', 'nvarchar(max)') as [value],
metadata.[item].value('value[1]', 'nvarchar(25)') as [valueShort]
from @xml.nodes('/metadata/item') metadata ([item])

&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Results&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;brush: plain;&quot;&gt;
key                       value                     valueShort
------------------------- ------------------------- -------------------------
key1                      value1                    value1
key2                      value2                    value2
key3                      value3                    value3

(3 row(s) affected)


&lt;/pre&gt;


&lt;p&gt;&lt;strong&gt;Example 2&lt;/strong&gt;:&lt;/p&gt;
&lt;p&gt;If you're an attribute guy (or gal), here's one that'll work too:&lt;/p&gt;

&lt;pre class=&quot;brush: sql;&quot;&gt;
declare @xml xml
set @xml = N'
&lt;metadata&gt;
&lt;item key=&quot;key1&quot; value=&quot;value1&quot; /&gt;
&lt;item key=&quot;key2&quot; value=&quot;value2&quot; /&gt;
&lt;item key=&quot;key3&quot; value=&quot;value3&quot; /&gt;
&lt;/metadata&gt;
'

select metadata.[item].query('data(@key)').value('.', 'nvarchar(25)') as [key]
,metadata.[item].query('data(@value)').value('.', 'nvarchar(max)') as [value]
,metadata.[item].query('data(@value)').value('.', 'nvarchar(25)') as [valueshort]
from @xml.nodes('/metadata/item') metadata ([item])
&lt;/pre&gt;

&lt;p&gt;&lt;strong&gt;Results&lt;/strong&gt;:&lt;/p&gt;
&lt;pre class=&quot;brush: plain;&quot;&gt;
key                       value                     valueshort
------------------------- ------------------------- -------------------------
key1                      value1                    value1
key2                      value2                    value2
key3                      value3                    value3

(3 row(s) affected)

&lt;/pre&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;/p&gt;
&lt;p&gt;-Kennydust&lt;/p&gt;</description>
            <link>http://www.kennydust.com/site.aspx/Blog/Dancing-around-with-XQuery-and-SQL-Server-Part-2</link>
            <guid isPermaLink="true">http://www.kennydust.com/site.aspx/Blog/Dancing-around-with-XQuery-and-SQL-Server-Part-2</guid>
            <pubDate>Thu, 04 Jun 2009 14:56:00 GMT</pubDate>
            <category>SQLServer</category>
        </item>
        <item>
            <dc:creator>kennydust</dc:creator>
            <title>Dancing around with XQuery and SQL Server - Part 1</title>
            <description>&lt;p&gt;What's XQuery? It's a language backed by the W3C to standardize querying XML.&lt;/p&gt;
&lt;p&gt;Why XQuery? Microsoft started offering support for it in SQL Server 2000 and in SQL Server 2005, extended it with more support with the introduction of the XML data type column. It's fast, it's compact, and you don't need to add 10 million trivial columns. You just add one XML column and stick raw XML in there. It's that easy, a caveman can do it. Here's more information about &lt;a target=&quot;_blank&quot; href=&quot;http://msdn.microsoft.com/en-us/library/ms345122(SQL.90).aspx&quot;&gt;Microsoft and XQuery&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;So what's the bad news? It's yet another language to learn and the syntax is pretty cryptic for simple operations. For those developers that aren't DBA's, it's likely you won't be touching this very often. So since this came up at work, I'm writing part one of XQuery so I can refer back to it at some point in the future when I know I'll be using it once again.&lt;/p&gt;
&lt;p&gt;So I've got a table with an XML data type in one of the columns.&lt;/p&gt;

&lt;pre class=&quot;brush: sql;&quot;&gt;

CREATE TABLE [dbo].[User](
    [UserID] [uniqueidentifier] NOT NULL,
    [WebAddressName] varchar(100) NOT NULL,
    [Properties] [xml] NULL
) ON [PRIMARY]



&lt;/pre&gt;

&lt;p&gt;In the Properties XML data type column, I've got a few rows... and the XML looks like this:&lt;/p&gt;
&lt;pre class=&quot;brush: xml;&quot;&gt;

&lt;properties&gt;
  &lt;property key=&quot;SessionID&quot;&gt;2.iiDhbDmiWSt4UA5CJWRDzg__.3600.1234461600-625164376&lt;/property&gt;
  &lt;property key=&quot;DisplayName&quot;&gt;Kenny Lai&lt;/property&gt;
  &lt;property key=&quot;SquarePictureUrl&quot;&gt;http://kennydust.com/v227/1279/94/q625164376_5201.jpg&lt;/property&gt;
  &lt;property key=&quot;LastProfilePush&quot;&gt;2/12/2009 4:38:47 PM&lt;/property&gt;
  &lt;property key=&quot;Created&quot;&gt;12/30/2008 8:27:54 PM&lt;/property&gt;
&lt;/properties&gt;


&lt;/pre&gt;

&lt;p&gt;So imagine we've got a couple of thousand records. I just need one record with the DisplayName = &amp;quot;Kenny Lai&amp;quot;. How do I grab that row?&lt;/p&gt;

&lt;pre class=&quot;brush: sql;&quot;&gt;

declare @Key varchar(50)
set @Key ='Kenny Lai'

SELECT * FROM User
WHERE [properties].exist('/properties/property/text()[contains(.,sql:variable(&quot;@Key&quot;))]') = 1



&lt;/pre&gt;

&lt;p&gt;So there you have it, there are other techniques and ways to get more granular with your data, including targeting attributes and whatnot -- but this is the simplest way for the goal that I needed to achieve.&lt;/p&gt;
&lt;p&gt;Happy coding.&lt;/p&gt;</description>
            <link>http://www.kennydust.com/site.aspx/Blog/Dancing-around-with-XQuery-and-SQL-Server-Part-1</link>
            <guid isPermaLink="true">http://www.kennydust.com/site.aspx/Blog/Dancing-around-with-XQuery-and-SQL-Server-Part-1</guid>
            <pubDate>Thu, 12 Feb 2009 16:32:00 GMT</pubDate>
            <category>SQLServer</category>
        </item>
        <item>
            <dc:creator>kennydust</dc:creator>
            <title>Initialization failure: WMI provider and SQL Server 2005</title>
            <description>&lt;p&gt;Ever open up the SQL Server Configuration Manager and receive this message?&lt;/p&gt;
&lt;p&gt;&lt;a target=&quot;_blank&quot; href=&quot;http://kennydust.com/files/media/image/WindowsLiveWriter/InitializationfailureWMIproviderandSQLSe_14FD7/Picture%202_2.png&quot;&gt;&lt;img style=&quot;border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px&quot; height=&quot;80&quot; alt=&quot;Picture 2&quot; width=&quot;514&quot; border=&quot;0&quot; src=&quot;http://kennydust.com/files/media/image/WindowsLiveWriter/InitializationfailureWMIproviderandSQLSe_14FD7/Picture%202_thumb.png&quot; /&gt;&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I have; and an initial google search bring up a bevy of solutions. Unfortunately, the first several offerings did not solve my problem -- which prompted me to dig a little deeper.&lt;/p&gt;
&lt;p&gt;Apparently, I had corrupted my WMI repository somewhere along the line; and after some reading, the WMI can be corrupted after multiple restores of a database... which I'm definitely guilty of. So the solution? rebuild the WMI. Piece of cake, right? Luckily, I stumbled across this &lt;a target=&quot;_blank&quot; href=&quot;http://msmvps.com/blogs/lduncan/articles/20217.aspx&quot;&gt;blog link&lt;/a&gt; which detail these instructions (which can be batched into a .bat file):&lt;/p&gt;

&lt;pre class=&quot;brush: plain;&quot;&gt;
net stop winmgmt 
c: 
cd %systemroot%\system32\wbem 
rd /S /Q repository 

regsvr32 /s %systemroot%\system32\scecli.dll 
regsvr32 /s %systemroot%\system32\userenv.dll 

mofcomp cimwin32.mof 
mofcomp cimwin32.mfl 
mofcomp rsop.mof 
mofcomp rsop.mfl 
for /f %%s in ('dir /b /s *.dll') do regsvr32 /s %%s 
for /f %%s in ('dir /b *.mof') do mofcomp %%s 
for /f %%s in ('dir /b *.mfl') do mofcomp %%s 
echo DONE reboot 
pause
&lt;/pre&gt;

&lt;p&gt;Ottimo! Everything should work. But not so fast, this message appears the next time I try to start up the SQL Server Configuration Manager:&lt;/p&gt;

&lt;pre class=&quot;brush: plain;&quot;&gt;

&quot;Cannot connect to WMI provider. You do not have permission or the server is unreachable. Note that you can only manage SQL Server 2005 servers with SQL Server Configuration Manager. 
Invalid class [0x80041010]&quot;

&lt;/pre&gt;

&lt;p&gt;Motherfunbag. I guess that didn't do it. Opening the command prompt and typing &lt;a target=&quot;_blank&quot; href=&quot;http://blogs.msdn.com/echarran/archive/2006/01/03/509061.aspx&quot;&gt;the below&lt;/a&gt; addresses this and ultimately fixes my issue:&lt;/p&gt;

&lt;pre class=&quot;brush: plain;&quot;&gt;

C:\Program Files\Microsoft SQL Server\90\Shared&gt;mofcomp &quot;C:\Program Files\Microsoft SQL Server\90\Shared\sqlmgmproviderxpsp2up.mof&quot;

Microsoft (R) 32-bit MOF Compiler Version 5.1.2600.2180 
Copyright (c) Microsoft Corp. 1997-2001. All rights reserved. 
Parsing MOF file: C:\Program Files\Microsoft SQL Server\90\Shared\sqlmgmprovider 
xpsp2up.mof 
MOF file has been successfully parsed 
Storing data in the repository... 
Done!


&lt;/pre&gt;</description>
            <link>http://www.kennydust.com/site.aspx/Blog/Initialization-failure-WMI-provider-and-SQL-Server-2005</link>
            <guid isPermaLink="true">http://www.kennydust.com/site.aspx/Blog/Initialization-failure-WMI-provider-and-SQL-Server-2005</guid>
            <pubDate>Sun, 09 Nov 2008 23:54:00 GMT</pubDate>
            <category>SQLServer</category>
        </item>
    </channel>
</rss>

