<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>sql_keys</title>
	<atom:link href="http://sqlkeys.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://sqlkeys.wordpress.com</link>
	<description></description>
	<lastBuildDate>Fri, 26 Apr 2013 19:47:03 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='sqlkeys.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/6d75a162a8cd15a1cdee1b8257f48208?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>sql_keys</title>
		<link>http://sqlkeys.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://sqlkeys.wordpress.com/osd.xml" title="sql_keys" />
	<atom:link rel='hub' href='http://sqlkeys.wordpress.com/?pushpress=hub'/>
		<item>
		<title>A Patriotic SQL Script</title>
		<link>http://sqlkeys.wordpress.com/2012/07/18/a-patriotic-sql-script/</link>
		<comments>http://sqlkeys.wordpress.com/2012/07/18/a-patriotic-sql-script/#comments</comments>
		<pubDate>Wed, 18 Jul 2012 16:22:46 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://sqlkeys.wordpress.com/2012/07/18/a-patriotic-sql-script/</guid>
		<description><![CDATA[<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=740&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><a href="http://sqlkeys.files.wordpress.com/2012/07/flag1.png"><img class="alignnone size-full wp-image-741" title="flag" src="http://sqlkeys.files.wordpress.com/2012/07/flag1.png?w=604" alt=""   /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlkeys.wordpress.com/740/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlkeys.wordpress.com/740/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=740&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlkeys.wordpress.com/2012/07/18/a-patriotic-sql-script/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b9f68ea70e072376ab12b8fc92d4fd1c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">SD</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/07/flag1.png" medium="image">
			<media:title type="html">flag</media:title>
		</media:content>
	</item>
		<item>
		<title>XQuery Lab 2</title>
		<link>http://sqlkeys.wordpress.com/2012/07/13/xquery-lab-2/</link>
		<comments>http://sqlkeys.wordpress.com/2012/07/13/xquery-lab-2/#comments</comments>
		<pubDate>Fri, 13 Jul 2012 20:44:19 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://sqlkeys.wordpress.com/?p=719</guid>
		<description><![CDATA[In our first XQuery lab, we used the query() method to filter XML tags for specific criteria matches.  This time, let&#8217;s work on performing DML operations using an XML extension method &#8211; modify(). We again need some test data, so let&#8217;s create a table and insert a record of XML data: CREATE TABLE EMPLOYEES (ID [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=719&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In our <a href="http://sqlkeys.wordpress.com/2012/05/02/xquery-lab-1/">first XQuery lab</a>, we used the <a href="http://msdn.microsoft.com/en-us/library/ms191474.aspx">query()</a> method to filter XML tags for specific criteria matches.  This time, let&#8217;s work on performing DML operations using an XML extension method &#8211; <a href="http://msdn.microsoft.com/en-us/library/ms187093.aspx">modify()</a>.</p>
<p>We again need some test data, so let&#8217;s create a table and insert a record of XML data:</p>
<pre>CREATE TABLE EMPLOYEES (ID INT IDENTITY, Details XML)
GO
INSERT EMPLOYEES VALUES(
 '&lt;Employees&gt;
 &lt;Employee ID="1" salary="49000"&gt;
 &lt;Department&gt;Sales&lt;/Department&gt;
 &lt;/Employee&gt;
 &lt;Employee ID="2" salary="56000" /&gt;
 &lt;Employee ID="3" salary="79000" /&gt;
 &lt;/Employees&gt;
 '
)
GO</pre>
<p>We now have one record of Employee details, in the form of an XML BLOB, in the Employees table:</p>
<p><a href="http://sqlkeys.files.wordpress.com/2012/07/1.png"><img class="size-full wp-image-721 alignnone" title="1" src="http://sqlkeys.files.wordpress.com/2012/07/1.png?w=604" alt=""   /></a></p>
<p>Notice that not all employees have a Department node.  Let&#8217;s use the <strong>insert&#8230;into</strong> keywords to insert a node for employee #2:</p>
<pre>UPDATE EMPLOYEES
SET Details.modify('insert &lt;Department&gt;Development&lt;/Department&gt; into 
(/Employees/Employee[@ID=("2")])[1]')
GO</pre>
<p>We have created a new Department node for employee #2:</p>
<p><a href="http://sqlkeys.files.wordpress.com/2012/07/12.png"><img class="alignnone size-full wp-image-725" title="1" src="http://sqlkeys.files.wordpress.com/2012/07/12.png?w=604" alt=""   /></a></p>
<p>We can also use the modify() method to replace values using the <strong>replace value of</strong> keywords.  Employee #1 should not be a member of the Sales department.  Let&#8217;s replace the Department node value with &#8220;Quality Assurance&#8221;:</p>
<pre>UPDATE EMPLOYEES
SET Details.modify('replace value of 
(/Employees/Employee[@ID=("1")]/Department/text())[1] with ("Quality Assurance")')
GO</pre>
<p>The Department node&#8217;s value has been replaced:</p>
<p><a href="http://sqlkeys.files.wordpress.com/2012/07/13.png"><img class="alignnone size-full wp-image-727" title="1" src="http://sqlkeys.files.wordpress.com/2012/07/13.png?w=604" alt=""   /></a></p>
<p>The modify() method also accommodates deleting nodes.  Let&#8217;s use it to remove the Employee node for employee #3:</p>
<pre>UPDATE EMPLOYEES
SET Details.modify('delete (/Employees/Employee[@ID=("3")])[1]')
GO</pre>
<p>The Employee node has been removed:</p>
<p><a href="http://sqlkeys.files.wordpress.com/2012/07/14.png"><img class="alignnone size-full wp-image-731" title="1" src="http://sqlkeys.files.wordpress.com/2012/07/14.png?w=604" alt=""   /></a></p>
<p>You may have noticed the singleton designation (&#8216;[1]&#8216;) we&#8217;ve used at the end of every node location specification.  This is required for the <strong>insert</strong> and<strong> replace value of</strong> keywords, to indicate which node instance will receive the change, even if there is only one instance of the node.  We did not need to use the singleton in our delete operation, as there was only one Employee node having an ID of 3.  <strong>Delete</strong> will remove the first instance that matches the criteria; the singleton is not required.</p>
<p>&nbsp;</p>
<p><a href="http://sqlkeys.wordpress.com/2012/05/02/xquery-lab-1/"><strong>[XQuery Lab 1]</strong></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlkeys.wordpress.com/719/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlkeys.wordpress.com/719/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=719&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlkeys.wordpress.com/2012/07/13/xquery-lab-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b9f68ea70e072376ab12b8fc92d4fd1c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">SD</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/07/1.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/07/12.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/07/13.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/07/14.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>
	</item>
		<item>
		<title>Find all XML columns in table</title>
		<link>http://sqlkeys.wordpress.com/2012/06/11/find-all-xml-columns-in-table/</link>
		<comments>http://sqlkeys.wordpress.com/2012/06/11/find-all-xml-columns-in-table/#comments</comments>
		<pubDate>Mon, 11 Jun 2012 21:57:13 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[find xml columns]]></category>
		<category><![CDATA[get all xml columns]]></category>
		<category><![CDATA[list xml columns]]></category>

		<guid isPermaLink="false">http://sqlkeys.wordpress.com/?p=691</guid>
		<description><![CDATA[Two methods: #1: &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; &#160; #2:  <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=691&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Two methods:</p>
<p>#1:</p>
<pre><a href="http://sqlkeys.files.wordpress.com/2012/06/1.png"><img class="alignleft size-full wp-image-705" title="1" src="http://sqlkeys.files.wordpress.com/2012/06/1.png?w=604" alt=""   /></a>

<em> </em>
<em> </em>
<em> </em>

<em><a href="http://sqlkeys.files.wordpress.com/2012/06/12.png"><img class="alignleft size-full wp-image-714" title="1" src="http://sqlkeys.files.wordpress.com/2012/06/12.png?w=604" alt=""   /></a></em></pre>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>#2:</p>
<pre><a href="http://sqlkeys.files.wordpress.com/2012/06/11.png"><img class="alignleft size-full wp-image-706" title="1" src="http://sqlkeys.files.wordpress.com/2012/06/11.png?w=604" alt=""   /></a>

<em> </em>
<em> </em>
<em> </em>
<em> </em>

<em> </em>

<a href="http://sqlkeys.files.wordpress.com/2012/06/13.png"><img class="alignleft size-full wp-image-716" title="1" src="http://sqlkeys.files.wordpress.com/2012/06/13.png?w=604" alt=""   /></a></pre>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlkeys.wordpress.com/691/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlkeys.wordpress.com/691/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=691&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlkeys.wordpress.com/2012/06/11/find-all-xml-columns-in-table/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b9f68ea70e072376ab12b8fc92d4fd1c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">SD</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/06/1.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/06/12.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/06/11.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/06/13.png" medium="image">
			<media:title type="html">1</media:title>
		</media:content>
	</item>
		<item>
		<title>Get List of SQL Server Agent Job Names, Category Names</title>
		<link>http://sqlkeys.wordpress.com/2012/05/04/get-list-of-sql-server-agent-job-names-category-names/</link>
		<comments>http://sqlkeys.wordpress.com/2012/05/04/get-list-of-sql-server-agent-job-names-category-names/#comments</comments>
		<pubDate>Fri, 04 May 2012 19:22:37 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://sqlkeys.wordpress.com/?p=671</guid>
		<description><![CDATA[Need to get a quick list of all SQL Agent jobs, with their respective categories?  Join the sysjobs table to the syscategories table: SELECT j.[job_id], j.[name] [job_name], j.[category_id], c.name [category_name] FROM [msdb].[dbo].[sysjobs] j JOIN [msdb].[dbo].[syscategories] c ON j.category_id = c.category_id ORDER BY 2<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=671&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Need to get a quick list of all SQL Agent jobs, with their respective categories?  Join the <strong>sysjobs</strong> table to the <strong>syscategories</strong> table:</p>
<pre><strong>SELECT j.[job_id], j.[name] [job_name], j.[category_id], c.name [category_name]</strong>
<strong>FROM [msdb].[dbo].[sysjobs] j</strong>
<strong>JOIN [msdb].[dbo].[syscategories] c</strong>
<strong>ON j.category_id = c.category_id</strong>
<strong>ORDER BY 2</strong></pre>
<p><a href="http://sqlkeys.files.wordpress.com/2012/05/sysjobs.png"><img class="aligncenter size-full wp-image-672" title="sysjobs" src="http://sqlkeys.files.wordpress.com/2012/05/sysjobs.png?w=604&#038;h=106" alt="" width="604" height="106" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlkeys.wordpress.com/671/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlkeys.wordpress.com/671/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=671&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlkeys.wordpress.com/2012/05/04/get-list-of-sql-server-agent-job-names-category-names/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b9f68ea70e072376ab12b8fc92d4fd1c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">SD</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/05/sysjobs.png" medium="image">
			<media:title type="html">sysjobs</media:title>
		</media:content>
	</item>
		<item>
		<title>XQuery Lab 1</title>
		<link>http://sqlkeys.wordpress.com/2012/05/02/xquery-lab-1/</link>
		<comments>http://sqlkeys.wordpress.com/2012/05/02/xquery-lab-1/#comments</comments>
		<pubDate>Wed, 02 May 2012 21:03:21 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://sqlkeys.wordpress.com/?p=655</guid>
		<description><![CDATA[XQuery is a database programming language developed for filtering and manipulating XML BLOB data.  It can be especially useful for ad-hoc reporting.  In this post, I want to show a few examples of some very basic XQuery expressions against untyped XML data. For our examples, we&#8217;ll need sample data.  Let&#8217;s create a simple table to [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=655&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>XQuery is a database programming language developed for filtering and manipulating XML BLOB data.  It can be especially useful for ad-hoc reporting.  In this post, I want to show a few examples of some very basic XQuery expressions against untyped XML data.</p>
<p>For our examples, we&#8217;ll need sample data.  Let&#8217;s create a simple table to hold XML data:</p>
<pre><strong>CREATE TABLE Avengers (ID INT IDENTITY, AdditionalInfo XML) </strong>
<strong>GO</strong></pre>
<p>Now let&#8217;s populate the table with a few records:</p>
<pre><strong>INSERT INTO Avengers VALUES('</strong>
<strong> &lt;Person&gt;</strong>
<strong> &lt;Names&gt;</strong>
<strong> &lt;Name&gt;</strong>
<strong> &lt;First&gt;Tony&lt;/First&gt;</strong>
<strong> &lt;Last&gt;Stark&lt;/Last&gt;</strong>
<strong> &lt;/Name&gt;</strong>
<strong> &lt;/Names&gt;</strong>
<strong> &lt;Aliases&gt;</strong>
<strong> &lt;Alias type="Primary"&gt;Iron Man&lt;/Alias&gt;</strong>
<strong> &lt;Alias type="Other"&gt;Shellhead&lt;/Alias&gt;</strong>
<strong> &lt;Alias type="Other"&gt;Golden Avenger&lt;/Alias&gt;</strong>
<strong> &lt;/Aliases&gt;</strong>
<strong> &lt;/Person&gt;'),</strong>
<strong> ('</strong>
<strong> &lt;Person&gt;</strong>
<strong> &lt;Names&gt;</strong>
<strong> &lt;Name&gt;</strong>
<strong> &lt;First&gt;Bruce&lt;/First&gt;</strong>
<strong> &lt;Last&gt;Banner&lt;/Last&gt;</strong>
<strong> &lt;/Name&gt;</strong>
<strong> &lt;/Names&gt;</strong>
<strong> &lt;Aliases&gt;</strong>
<strong> &lt;Alias type="Primary"&gt;Hulk&lt;/Alias&gt;</strong>
<strong> &lt;Alias type="Other"&gt;Joe Fixit&lt;/Alias&gt;</strong>
<strong> &lt;Alias type="Other"&gt;Green King&lt;/Alias&gt;</strong>
<strong> &lt;Alias type="Other"&gt;Green Goliath&lt;/Alias&gt;</strong>
<strong> &lt;Alias type="Other"&gt;Holku&lt;/Alias&gt;</strong>
<strong> &lt;/Aliases&gt;</strong>
<strong> &lt;/Person&gt;'),</strong>
<strong> ('</strong>
<strong> &lt;Person&gt; </strong>
<strong> &lt;Names&gt; </strong>
<strong> &lt;Name&gt; </strong>
<strong> &lt;First&gt;Thor&lt;/First&gt;</strong>
<strong> &lt;Last&gt;Odinson&lt;/Last&gt; </strong>
<strong> &lt;/Name&gt;</strong>
<strong> &lt;/Names&gt; </strong>
<strong> &lt;Aliases&gt;</strong>
<strong> &lt;Alias type="Primary"&gt;Thor&lt;/Alias&gt;</strong>
<strong> &lt;Alias type="Other"&gt;Donald Blake&lt;/Alias&gt;</strong>
<strong> &lt;Alias type="Other"&gt;Sigurd Jarlson&lt;/Alias&gt;</strong>
<strong> &lt;Alias type="Other"&gt;Jake Olsen&lt;/Alias&gt;</strong>
<strong> &lt;/Aliases&gt;</strong>
<strong> &lt;/Person&gt; </strong>
<strong> ')</strong>
<strong>GO</strong></pre>
<p>Upon running a <strong>SELECT * FROM Avengers</strong>, we see the contents of the new table:</p>
<p style="text-align:center;"><a href="http://sqlkeys.files.wordpress.com/2012/05/aveng11.png"><img class="aligncenter size-full wp-image-658" title="aveng1" src="http://sqlkeys.files.wordpress.com/2012/05/aveng11.png?w=604" alt="SELECT * FROM Avengers"   /></a></p>
<p>You can see that we have 3 records of XML BLOBs.  To perform a simple query, let&#8217;s pull the primary Alias for each record:</p>
<pre><strong>SELECT AdditionalInfo.query('/Person/Aliases/Alias[@type="Primary"]') </strong>
<strong>AS PrimaryAlias </strong>
<strong>FROM Avengers</strong>
GO
<a href="http://sqlkeys.files.wordpress.com/2012/05/aveng2.png"><img class="aligncenter size-full wp-image-660" title="aveng2" src="http://sqlkeys.files.wordpress.com/2012/05/aveng2.png?w=604" alt=""   /></a></pre>
<p>We&#8217;ve used the query() XQuery method to do this &#8211; it gives us what we want, but we could make this more meaningful by associating the Person&#8217;s Name with the Alias, and also remove the XML tags:</p>
<pre><strong>SELECT AdditionalInfo.query('/Person/Aliases/Alias[@type="Primary"]/text()')  </strong>
<strong>AS PrimaryAlias, </strong>
<strong>AdditionalInfo.value('(/Person/Names/Name/Last)[1]','VARCHAR(25)') </strong>
<strong>AS LastName </strong>
<strong>FROM Avengers</strong>
<strong>GO</strong></pre>
<p><a href="http://sqlkeys.files.wordpress.com/2012/05/aveng3.png"><img class="aligncenter size-full wp-image-662" title="aveng3" src="http://sqlkeys.files.wordpress.com/2012/05/aveng3.png?w=604" alt=""   /></a></p>
<p>This time we&#8217;ve added the use of the text() method to remove the XML tags from the PrimaryAlias results, and introduced the value() method to obtain the LastName values &#8211; we don&#8217;t require the query method for LastName.  The value() method requires a second paramter &#8211; data type.  We used VARCHAR, and that effectively converted the result&#8217;s XML type to VARCHAR.  We could also convert the PrimaryAlias result values to VARCHAR by wrapping the query in a CONVERT function call.</p>
<p><strong>More XQuery to come!</strong></p>
<p><a href="http://sqlkeys.wordpress.com/2012/07/13/xquery-lab-2/"><strong>[XQuery Lab 2]</strong></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlkeys.wordpress.com/655/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlkeys.wordpress.com/655/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=655&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlkeys.wordpress.com/2012/05/02/xquery-lab-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b9f68ea70e072376ab12b8fc92d4fd1c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">SD</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/05/aveng11.png" medium="image">
			<media:title type="html">aveng1</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/05/aveng2.png" medium="image">
			<media:title type="html">aveng2</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/05/aveng3.png" medium="image">
			<media:title type="html">aveng3</media:title>
		</media:content>
	</item>
		<item>
		<title>Get List of Columns in a Table</title>
		<link>http://sqlkeys.wordpress.com/2012/02/10/get-list-of-columns-in-a-table/</link>
		<comments>http://sqlkeys.wordpress.com/2012/02/10/get-list-of-columns-in-a-table/#comments</comments>
		<pubDate>Fri, 10 Feb 2012 14:45:38 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[list all columns]]></category>
		<category><![CDATA[list all columns except one]]></category>
		<category><![CDATA[list columns]]></category>
		<category><![CDATA[list sql columns]]></category>
		<category><![CDATA[sql columns]]></category>
		<category><![CDATA[sql fieldnames]]></category>
		<category><![CDATA[sql server]]></category>
		<category><![CDATA[sys.columns]]></category>
		<category><![CDATA[sys.objects]]></category>
		<category><![CDATA[table columns]]></category>

		<guid isPermaLink="false">http://sqlkeys.wordpress.com/?p=510</guid>
		<description><![CDATA[There is no way to write a SQL Server SELECT statement, using the wildcard, to select all columns in a table except for any number of particular columns.   For example, if you wanted to return all fields from the Person.Address table except for the City field, you cannot write a query like this: SELECT [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=510&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p><strong><span style="color:#0000ff;">There is no way to write a SQL Server SELECT statement, using the wildcard, to select all columns in a table except for any number of particular columns</span>.  </strong></p>
<p>For example, if you wanted to return all fields from the Person.Address table except for the City field, you <strong>cannot</strong> write a query like this:</p>
<pre>SELECT ( * -City ) FROM Person.Address</pre>
<p>If you need to select 99 of 100 columns in a table, you have to list all 99.  This can be a painful task.  For now, the best way to accomplish this is to <strong>generate a list of field names from the table</strong>, and copy them into your query.  Here&#8217;s how to do it.</p>
<p>Every database in SQL Server 2008 contains system views named <strong>sys.obects</strong> and<strong> sys.columns</strong>.  Sys.objects contains <span style="text-decoration:underline;">all database objects</span>: system tables, user tables, constraints, views, etc.  Sys.columns contains <span style="text-decoration:underline;">all columns from every table</span>, along with the same object_id of it&#8217;s parent object (the table).  By joining these system tables together, you can get a quick list of columns that you can copy and paste into your query:</p>
<pre>USE AdventureWorks
GO
SELECT sc.name ColumnName
FROM sys.objects so
JOIN sys.columns sc
ON so.object_id = sc.object_id
WHERE so.name = 'Contact'</pre>
<p style="text-align:center;"><a href="http://sqlkeys.files.wordpress.com/2012/02/sql12-e1328884509791.png?w=364"><img class="aligncenter size-large wp-image-611" title="sql1" src="http://sqlkeys.files.wordpress.com/2012/02/sql12-e1328884509791.png?w=364&#038;h=527" alt="" width="364" height="527" /></a></p>
<p><strong>This is certainly one query to memorize!</strong></p>
<p>To list columns from <strong>ANY</strong> user table, drop the table specification, add a filter on sys.objects for &#8216;type&#8217;, and include the table name by using the <strong>OBJECT_NAME</strong> function:</p>
<pre>USE AdventureWorks
GO
SELECT OBJECT_NAME(sc.OBJECT_ID) TableName, sc.name ColumnName
FROM sys.objects so
JOIN sys.columns sc
ON so.object_id = sc.object_id
WHERE so.type = 'U'</pre>
<p style="text-align:center;"><a href="http://sqlkeys.files.wordpress.com/2012/02/sql21-e1328884455459.png?w=595"><img class="aligncenter  wp-image-610" title="sql2" src="http://sqlkeys.files.wordpress.com/2012/02/sql21-e1328884455459.png?w=595&#038;h=747" alt="" width="595" height="747" /></a></p>
<p style="text-align:left;">This query can be taken even further by joining to the <strong>sys.types</strong> view, where <span style="text-decoration:underline;">data type metadata is stored</span>:</p>
<pre>USE AdventureWorks
GO
SELECT OBJECT_NAME(sc.OBJECT_ID) TableName, sc.name ColumnName,
t.name [DataType], sc.is_nullable Nullable, sc.is_identity Is_identity
FROM sys.objects so
JOIN sys.columns sc
ON so.object_id = sc.object_id
JOIN sys.types t
ON sc.user_type_id=t.user_type_id
WHERE so.type = 'U'</pre>
<p style="text-align:center;"><a href="http://sqlkeys.files.wordpress.com/2012/02/sql3-e1328884352386.png?w=653"><img class="aligncenter  wp-image-609" title="sql3" src="http://sqlkeys.files.wordpress.com/2012/02/sql3-e1328884352386.png?w=653&#038;h=754" alt="" width="653" height="754" /></a></p>
<p style="text-align:left;">EDIT:</p>
<p style="text-align:left;">One reader brought up an alternative method to get a list of columns in a table, by using one of the <a href="http://msdn.microsoft.com/en-us/library/ms186778.aspx">information schema views</a>.  I like his idea better than using sys.columns:</p>
<p>SELECT COLUMN_NAME<br />
FROM INFORMATION_SCHEMA.COLUMNS<br />
WHERE TABLE_CATALOG = &#8216;AdventureWorks&#8217;<br />
AND TABLE_NAME = &#8216;Contact&#8217;</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlkeys.wordpress.com/510/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlkeys.wordpress.com/510/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=510&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlkeys.wordpress.com/2012/02/10/get-list-of-columns-in-a-table/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b9f68ea70e072376ab12b8fc92d4fd1c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">SD</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/02/sql12-e1328884509791.png?w=364" medium="image">
			<media:title type="html">sql1</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/02/sql21-e1328884455459.png?w=595" medium="image">
			<media:title type="html">sql2</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2012/02/sql3-e1328884352386.png?w=653" medium="image">
			<media:title type="html">sql3</media:title>
		</media:content>
	</item>
		<item>
		<title>Grant permissions to SQL Server database users on specific objects</title>
		<link>http://sqlkeys.wordpress.com/2011/11/23/grant-permissions-to-sql-server-database-users-on-specific-objects/</link>
		<comments>http://sqlkeys.wordpress.com/2011/11/23/grant-permissions-to-sql-server-database-users-on-specific-objects/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 13:27:49 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://sqlkeys.wordpress.com/?p=491</guid>
		<description><![CDATA[Need to allow a user access to only specific tables or other database objects, but not to the entire database? Get granular with the GRANT statement. To apply granular permissions to a specific object, like a table, use the GRANT [permission] ON [object] TO [user] format. To grant select permissions on a table to a database user: [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=491&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<h3><span style="color:#000000;">Need to allow a user access to only specific tables or other database objects, but not to the entire database? <span style="color:#0000ff;"><strong>Get granular with the GRANT statement.</strong></span></span></h3>
<p>To apply granular permissions to a specific object, like a table, use the<strong> GRANT [permission] ON [object] TO [user]</strong> format.</p>
<div><strong>To grant select permissions on a table to a database user:</strong></div>
<div></div>
<div>
<pre>USE AdventureWorks2012
GO
GRANT SELECT ON OBJECT::[Person].[Password] TO [dba_test]
GO</pre>
</div>
<div></div>
<div>If your login has no User Mapping to any database user, you will receive an error like this:</div>
<div></div>
<div><span style="color:#ff0000;">Msg 15151, Level 16, State 1, Line 1</span></div>
<div><span style="color:#ff0000;">Cannot find the user &#8216;dba_test&#8217;, because it does not exist or you do not have permission</span>.</div>
<div></div>
<div>&#8230; and a database user <strong>needs to be created for the login:</strong></div>
<div></div>
<div>
<pre>CREATE USER [dba_test] FOR LOGIN [BUILTIN\Users]
GO</pre>
</div>
<div><strong>___________________________________________________________________________________</strong></div>
<div><strong><br />
</strong></div>
<div><strong>To grant execute permissions on a stored procedure to a database user:</strong></div>
<div></div>
<div>
<pre>USE AdventureWorks2012
GO
GRANT EXECUTE ON OBJECT::dbo.uspGetBillOfMaterials TO [dba_test]
GO</pre>
</div>
<div><strong><br />
</strong></div>
<div><a href="http://msdn.microsoft.com/en-us/library/ms187965.aspx">http://msdn.microsoft.com/en-us/library/ms187965.aspx</a></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlkeys.wordpress.com/491/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlkeys.wordpress.com/491/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=491&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlkeys.wordpress.com/2011/11/23/grant-permissions-to-sql-server-database-users-on-specific-objects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b9f68ea70e072376ab12b8fc92d4fd1c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">SD</media:title>
		</media:content>
	</item>
		<item>
		<title>Increasing the mimimum memory used per query</title>
		<link>http://sqlkeys.wordpress.com/2011/10/27/increasing-the-mimimum-memory-used-per-query/</link>
		<comments>http://sqlkeys.wordpress.com/2011/10/27/increasing-the-mimimum-memory-used-per-query/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 12:39:39 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[min memory sql server]]></category>
		<category><![CDATA[minimum memory per query]]></category>

		<guid isPermaLink="false">http://sqlkeys.wordpress.com/?p=441</guid>
		<description><![CDATA[SQL Server&#8217;s sp_configure settings contains a setting named &#8216;min memory per query (KB)&#8217;. This setting allows you to set the default minimum amount of RAM that&#8217;s allocated for each query &#8212; but should be used with care. Like many SQL Server settings, the default setting is best for most situations. However, if you have RAM [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=441&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>SQL Server&#8217;s <strong>sp_configure</strong> settings contains a setting named &#8216;min memory per query (KB)&#8217;. This setting allows you to set the default minimum amount of RAM that&#8217;s allocated for each query &#8212; <span style="text-decoration:underline;">but should be used with care</span>. Like many SQL Server settings, the default setting is best for most situations. However, <strong>if you have RAM to burn</strong>, <a href="http://msdn.microsoft.com/en-us/library/ms181047.aspx">BOL</a> offers that increasing this setting could have a positive impact on small queries:</p>
<blockquote><p><span style="color:#ff0000;"><strong>&#8220;Increasing the value of min memory per query may improve performance for some small to medium-sized queries, but doing so could lead to increased competition for memory resources.&#8221;</strong></span></p></blockquote>
<p>Updates to the &#8216;min memory per query (KB)&#8217; setting can be done like this:</p>
<pre>--first, turn on advanced options
SP_CONFIGURE 'show advanced options', 1
GO
RECONFIGURE
GO
--then, set new value - default value is 1024KB, this increases it to 2MB
SP_CONFIGURE 'min memory per query (KB)',2048
GO
RECONFIGURE WITH OVERRIDE
GO
--now, display new value
SP_CONFIGURE 'min memory per query (KB)'
GO
RECONFIGURE WITH OVERRIDE
GO
___________________________________________________________________________________________________</pre>
<p><a href="http://sqlkeys.files.wordpress.com/2011/10/capture.png"><img class="size-large wp-image-443 alignleft" title="Capture" src="http://sqlkeys.files.wordpress.com/2011/10/capture.png?w=300&#038;h=96" alt="" width="300" height="96" /></a></p>
<p><a href="http://sqlkeys.files.wordpress.com/2011/10/capture1.png"><img class="size-large wp-image-445 alignleft" title="Capture" src="http://sqlkeys.files.wordpress.com/2011/10/capture1.png?w=300&#038;h=49" alt="" width="300" height="49" /></a></p>
<p><a href="http://msdn.microsoft.com/en-us/library/ms181047.aspx">http://msdn.microsoft.com/en-us/library/ms181047.aspx</a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlkeys.wordpress.com/441/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlkeys.wordpress.com/441/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=441&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlkeys.wordpress.com/2011/10/27/increasing-the-mimimum-memory-used-per-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b9f68ea70e072376ab12b8fc92d4fd1c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">SD</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2011/10/capture.png?w=300" medium="image">
			<media:title type="html">Capture</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2011/10/capture1.png?w=300" medium="image">
			<media:title type="html">Capture</media:title>
		</media:content>
	</item>
		<item>
		<title>Copy table schema only</title>
		<link>http://sqlkeys.wordpress.com/2011/06/29/copy-table-schema-only/</link>
		<comments>http://sqlkeys.wordpress.com/2011/06/29/copy-table-schema-only/#comments</comments>
		<pubDate>Wed, 29 Jun 2011 19:05:19 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://sqlkeys.resistancenet.com/?p=283</guid>
		<description><![CDATA[Need to duplicate a table&#8217;s schema?  Use a SELECT&#8230;INTO&#8230;FROM  statement: SELECT * INTO new_tbl FROM old_tbl This will create a copy of the table, data and all.  What if you don&#8217;t want the data copied with the schema?  My friend Alessandro Basso uses this method: SELECT * INTO new_tbl FROM old_tbl WHERE 1=2 Genius!<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=283&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Need to duplicate a table&#8217;s schema?  Use a SELECT&#8230;INTO&#8230;FROM  statement:</p>
<blockquote><p>SELECT * INTO new_tbl<br />
FROM old_tbl</p></blockquote>
<p>This will create a copy of the table, data and all.  What if you don&#8217;t want the data copied with the schema?  My friend <a href="http://www.alessandrobasso.tk/">Alessandro Basso</a> uses this method:</p>
<blockquote><p>SELECT * INTO new_tbl<br />
FROM old_tbl<br />
WHERE 1=2</p></blockquote>
<div>Genius!</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlkeys.wordpress.com/283/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlkeys.wordpress.com/283/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=283&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlkeys.wordpress.com/2011/06/29/copy-table-schema-only/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b9f68ea70e072376ab12b8fc92d4fd1c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">SD</media:title>
		</media:content>
	</item>
		<item>
		<title>Grouping with ROW_NUMBER</title>
		<link>http://sqlkeys.wordpress.com/2011/06/16/grouping-by-row_number/</link>
		<comments>http://sqlkeys.wordpress.com/2011/06/16/grouping-by-row_number/#comments</comments>
		<pubDate>Thu, 16 Jun 2011 17:04:21 +0000</pubDate>
		<dc:creator>Seth</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://sqlkeys.resistancenet.com/?p=257</guid>
		<description><![CDATA[Want to group a result set by only one field, but include others in the result set?  You can use the ROW_NUMBER function with its PARTITION BY parameter. To play with this, let&#8217;s create some sample data: Now we have a table with sample data: Using the data we just created, we want to show [&#8230;]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=257&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Want to group a result set by only one field, but include others in the result set?  You can use the <strong>ROW_NUMBER</strong> function with its <strong>PARTITION BY</strong> parameter.</p>
<p>To play with this, let&#8217;s create some sample data:</p>
<p><a href="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-3-07-08-pm1.png"><img class="alignnone size-full wp-image-274" title="Screen shot 2011-06-16 at 3.07.08 PM" src="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-3-07-08-pm1.png?w=604" alt=""   /></a></p>
<p><a href="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-30-34-pm12.png"><img class="alignnone size-full wp-image-264" title="Screen shot 2011-06-16 at 2.30.34 PM" src="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-30-34-pm12.png?w=604" alt=""   /></a></p>
<p>Now we have a table with sample data:</p>
<p><a href="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-34-29-pm1.png"><img class="alignnone size-full wp-image-265" title="Screen shot 2011-06-16 at 2.34.29 PM" src="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-34-29-pm1.png?w=604" alt=""   /></a></p>
<p>Using the data we just created, we want to show only the most recent transaction for EACH customer.   We can do a simple GROUP BY, but if we include TransactionID in the result set, we are forced to get a record for each TransactionID, since it has to be contained in the GROUP BY&#8217;s list of non-aggregates:</p>
<p><a href="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-50-53-pm1.png"><img class="alignnone size-full wp-image-270" title="Screen shot 2011-06-16 at 2.50.53 PM" src="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-50-53-pm1.png?w=604" alt=""   /></a></p>
<p><a href="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-50-59-pm1.png"><img class="alignnone size-full wp-image-271" title="Screen shot 2011-06-16 at 2.50.59 PM" src="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-50-59-pm1.png?w=604" alt=""   /></a></p>
<p>We get the full result set &#8211; not what we want.  To include TransactionID (or any other field that may be in the table) in the result set, but grouping only on CustID, we can include the ROW_NUMBER function in the select list, using the PARTITION BY clause:</p>
<p><a href="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-3-07-16-pm1.png"><img class="alignnone size-full wp-image-275" title="Screen shot 2011-06-16 at 3.07.16 PM" src="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-3-07-16-pm1.png?w=604" alt=""   /></a></p>
<p>We get this:</p>
<p><a href="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-46-46-pm1.png"><img class="alignnone size-full wp-image-268" title="Screen shot 2011-06-16 at 2.46.46 PM" src="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-46-46-pm1.png?w=604" alt=""   /></a></p>
<p>Using the ROW_NUMBER function in the select list (with out the PARTITION BY parameter) simply gives us the unique row number for each record.  When we use the PARTITION BY clause, it groups on CustID.  The ORDER BY clause is required, and necessary for our results &#8211; this allows us to make the most recent SaleDate = row 1.</p>
<p>&nbsp;</p>
<p><em>I give credit to Dan Humphries for showing examples of ROW_NUMBER <a href="http://www.sqlservercentral.com/Forums/Topic1126587-149-1.aspx">here</a>.</em></p>
<p><em><a href="http://msdn.microsoft.com/en-us/library/ms186734.aspx">http://msdn.microsoft.com/en-us/library/ms186734.aspx</a><br />
</em></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/sqlkeys.wordpress.com/257/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/sqlkeys.wordpress.com/257/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=sqlkeys.wordpress.com&#038;blog=24907369&#038;post=257&#038;subd=sqlkeys&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://sqlkeys.wordpress.com/2011/06/16/grouping-by-row_number/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b9f68ea70e072376ab12b8fc92d4fd1c?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">SD</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-3-07-08-pm1.png" medium="image">
			<media:title type="html">Screen shot 2011-06-16 at 3.07.08 PM</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-30-34-pm12.png" medium="image">
			<media:title type="html">Screen shot 2011-06-16 at 2.30.34 PM</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-34-29-pm1.png" medium="image">
			<media:title type="html">Screen shot 2011-06-16 at 2.34.29 PM</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-50-53-pm1.png" medium="image">
			<media:title type="html">Screen shot 2011-06-16 at 2.50.53 PM</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-50-59-pm1.png" medium="image">
			<media:title type="html">Screen shot 2011-06-16 at 2.50.59 PM</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-3-07-16-pm1.png" medium="image">
			<media:title type="html">Screen shot 2011-06-16 at 3.07.16 PM</media:title>
		</media:content>

		<media:content url="http://sqlkeys.files.wordpress.com/2011/06/screen-shot-2011-06-16-at-2-46-46-pm1.png" medium="image">
			<media:title type="html">Screen shot 2011-06-16 at 2.46.46 PM</media:title>
		</media:content>
	</item>
	</channel>
</rss>
