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

<channel>
	<title>youdidwhatwithtsql.com &#187; T-SQL</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/category/t-sql/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>TSQL to generate date lookup table data</title>
		<link>http://www.youdidwhatwithtsql.com/tsql-generate-date-lookup-table-data/1387</link>
		<comments>http://www.youdidwhatwithtsql.com/tsql-generate-date-lookup-table-data/1387#comments</comments>
		<pubDate>Thu, 10 Nov 2011 13:20:43 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/?p=1387</guid>
		<description><![CDATA[I needed to generate a range of data about dates for a lookup table. There&#8217;s an elegant solution using a recursive cte that does the job; ?View Code TSQLWITH daysCte &#40; d &#41; AS &#40; SELECT CONVERT&#40;DATETIME, '1 January 2011'&#41; AS d -- starting date UNION ALL SELECT DATEADD&#40;D, 1, d&#41; FROM daysCte WHERE DATEPART&#40;yyyy, d&#41; [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/tsql-generate-date-lookup-table-data/1387">TSQL to generate date lookup table data</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I needed to generate a range of data about dates for a lookup table. There&#8217;s an elegant solution using a <a title="Recurive common table expression" href="http://msdn.microsoft.com/en-us/library/ms186243.aspx" target="_blank">recursive cte</a> that does the job;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1387code2'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13872"><td class="code" id="p1387code2"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">WITH</span> daysCte
<span style="color: #808080;">&#40;</span>
	d 
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">DATETIME</span>, <span style="color: #FF0000;">'1 January 2011'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> d <span style="color: #008080;">-- starting date</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">DATEADD</span><span style="color: #808080;">&#40;</span>D, <span style="color: #000;">1</span>, d<span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">FROM</span> daysCte
	<span style="color: #0000FF;">WHERE</span> <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>yyyy, d<span style="color: #808080;">&#41;</span> <span style="color: #808080;">&lt;=</span> <span style="color: #000;">2012</span> <span style="color: #008080;">-- stop year</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> d, 
	   <span style="color: #FF00FF;">DATEPART</span><span style="color: #808080;">&#40;</span>wk, d<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> week_number,
	   <span style="color: #FF00FF;">DATENAME</span><span style="color: #808080;">&#40;</span>dw, d<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> day_name,
	   <span style="color: #FF00FF;">DATENAME</span><span style="color: #808080;">&#40;</span>m, d<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> month_name,
	   <span style="color: #FF00FF;">DATENAME</span><span style="color: #808080;">&#40;</span>q, d<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span>quarter<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> daysCte 
<span style="color: #0000FF;">OPTION</span> <span style="color: #808080;">&#40;</span>MAXRECURSION <span style="color: #000;">800</span><span style="color: #808080;">&#41;</span>; <span style="color: #008080;">-- set &gt; number of days you want data for</span></pre></td></tr></table></div>

<p>This will display something looking like below;</p>
<pre>
d			week_number	day_name	month_name	quarter
2011-01-01 00:00:00.000	1		Saturday	January		1
2011-01-02 00:00:00.000	2		Sunday		January		1
2011-01-03 00:00:00.000	2		Monday		January		1
2011-01-04 00:00:00.000	2		Tuesday		January		1
2011-01-05 00:00:00.000	2		Wednesday	January		1
2011-01-06 00:00:00.000	2		Thursday	January		1
2011-01-07 00:00:00.000	2		Friday		January		1
</pre>
<p><map name='google_ad_map_1387_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1387?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1387_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1387&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ftsql-generate-date-lookup-table-data%2F1387' title="TSQL to generate date lookup table data" alt=" TSQL to generate date lookup table data" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/tsql-generate-date-lookup-table-data/1387">TSQL to generate date lookup table data</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/tsql-generate-date-lookup-table-data/1387" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/tsql-generate-date-lookup-table-data/1387/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Audit VLFs on your SQL Server</title>
		<link>http://www.youdidwhatwithtsql.com/audit-vlfs-on-your-sql-server/1358</link>
		<comments>http://www.youdidwhatwithtsql.com/audit-vlfs-on-your-sql-server/1358#comments</comments>
		<pubDate>Tue, 04 Oct 2011 20:47:55 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[Powershell]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Virtual Log Files]]></category>
		<category><![CDATA[VLF]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/audit-vlfs-on-your-sql-server/1358</guid>
		<description><![CDATA[I&#8217;ve been reading a bit about VLFs (Virtual Log Files) this week. I&#8217;ve found quite a few interesting links, especially this one, informing us that there&#8217;s such a thing as too few or too many VLFs. We can view details about VLFs using the DBCC LOGINFO TSQL command. This only works against the current database [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/audit-vlfs-on-your-sql-server/1358">Audit VLFs on your SQL Server</a></p>
]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been <a href="http://sqlblog.com/blogs/linchi_shea/archive/2009/02/09/performance-impact-a-large-number-of-virtual-log-files-part-i.aspx">reading a bit about VLFs</a> (Virtual Log Files) this week. I&#8217;ve found quite a few interesting links, especially this one, informing us that there&#8217;s such a thing as <a href="http://sqlskills.com/BLOGS/KIMBERLY/post/Transaction-Log-VLFs-too-many-or-too-few.aspx">too few or too many VLFs.</a> </p>
<p>We can view details about VLFs using the DBCC LOGINFO TSQL command. This only works against the current database context but I decided to make this process less tedious, and human-readable, so I knocked up a quick script;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1358code5'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13585"><td class="code" id="p1358code5"><pre class="tsql" style="font-family:monospace;"><span style="color: #008080;">-- Create a temp table to hold log info</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #tmp_log_info
<span style="color: #808080;">&#40;</span>
      FileId <span style="color: #0000FF;">INTEGER</span>,
      FileSize <span style="color: #0000FF;">BIGINT</span>,
      StartOffSet <span style="color: #0000FF;">BIGINT</span>,
      <span style="color: #808080;">&#91;</span>Status<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INTEGER</span>,
      FSeqNo <span style="color: #0000FF;">INTEGER</span>,
      Parity <span style="color: #0000FF;">SMALLINT</span>,
      CreateLSN <span style="color: #0000FF;">NUMERIC</span><span style="color: #808080;">&#40;</span><span style="color: #000;">38</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #008080;">-- Same as above but with database name</span>
<span style="color: #008080;">-- Can insert in one statement</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> #log_info
<span style="color: #808080;">&#40;</span>
      FileId <span style="color: #0000FF;">INTEGER</span>,
      FileSize <span style="color: #0000FF;">BIGINT</span>,
      StartOffSet <span style="color: #0000FF;">BIGINT</span>,
      <span style="color: #808080;">&#91;</span>Status<span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">INTEGER</span>,
      FSeqNo <span style="color: #0000FF;">INTEGER</span>,
      Parity <span style="color: #0000FF;">SMALLINT</span>,
      CreateLSN <span style="color: #0000FF;">NUMERIC</span><span style="color: #808080;">&#40;</span><span style="color: #000;">38</span>,<span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>,
      <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Database</span><span style="color: #808080;">&#93;</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">128</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>;
&nbsp;
<span style="color: #0000FF;">EXEC</span> sp_MsForEachDb <span style="color: #FF0000;">'INSERT INTO #tmp_log_info EXEC('</span><span style="color: #FF0000;">'DBCC LOGINFO(?)'</span><span style="color: #FF0000;">');
                              INSERT INTO #log_info
                               SELECT *, '</span><span style="color: #FF0000;">'?'</span><span style="color: #FF0000;">'
                              FROM #tmp_log_info;
                              TRUNCATE TABLE #tmp_log_info;'</span>;
&nbsp;
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Database</span><span style="color: #808080;">&#93;</span>,
            <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> Vlf_count,
            <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MAX</span><span style="color: #808080;">&#40;</span>FileSize<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">1048576</span>  <span style="color: #0000FF;">AS</span> biggest_vlf_MB,
            <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">MIN</span><span style="color: #808080;">&#40;</span>FileSize<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">FLOAT</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">/</span> <span style="color: #000;">1048576</span> <span style="color: #0000FF;">AS</span> smallest_vlf_MB
<span style="color: #0000FF;">FROM</span> #log_info
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Database</span><span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> <span style="color: #FF00FF;">COUNT</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">*</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">DESC</span>;
&nbsp;
&nbsp;
<span style="color: #008080;">-- Clean up</span>
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #tmp_log_info;
<span style="color: #0000FF;">DROP</span> <span style="color: #0000FF;">TABLE</span> #log_info;</pre></td></tr></table></div>

<p>This will display a summary report for all databases on the instance of SQL Server.</p>
<p><strong>Database &#8211; </strong>The database the log file belongs to.</p>
<p><strong>Vlf_count &#8211; </strong>The total number of VLFs.</p>
<p><strong>biggest_vlf_MB &#8211; </strong>The biggest virtual log file in MB.</p>
<p><strong>smallest_vlf_MB &#8211; </strong>The smallest virtual log file in MB.</p>
<pre>Database	Vlf_count	biggest_vlf_MB	smallest_vlf_MB
msdb		26		0.4453125	0.2421875
master		5		0.25		0.2421875
model		3		0.25		0.2421875
tempdb		2		0.25		0.2421875
ft_test		2		0.3125		0.2421875</pre>
<p><strong>UPDATE: </strong>Just found a <a href="http://sqlserverpedia.com/blog/sql-server-bloggers/find-the-number-of-vlfs-for-all-databases/">very similar post here</a>.</p>
<p><strong>UPDATE: </strong>Thought I&#8217;d try to bring something to the table and come up with a pure-powershell way to do this but it doesn&#8217;t seem that the VLF information is exposed;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1358code6'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p13586"><td class="code" id="p1358code6"><pre class="powershell" style="font-family:monospace;"><span style="color: #800080;">$server</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;localhost&quot;</span>;
<span style="color: #008000;"># Load SMO </span>
<span style="color: #000000;">&#91;</span><span style="color: #008080;">System.Reflection.Assembly</span><span style="color: #000000;">&#93;</span>::<span style="color: #800000;">LoadWithPartialName</span><span style="color: #000000;">&#40;</span><span style="color: #800000;">&quot;Microsoft.SqlServer.Smo&quot;</span><span style="color: #000000;">&#41;</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Out-Null</span>; 
<span style="color: #008000;"># create server objects </span>
<span style="color: #800080;">$srv</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">New-Object</span> Microsoft.SqlServer.Management.SMO.Server <span style="color: #800080;">$server</span>;
<span style="color: #800080;">$db</span> <span style="color: pink;">=</span> <span style="color: #800080;">$srv</span>.Databases<span style="color: #000000;">&#91;</span><span style="color: #800000;">&quot;tempdb&quot;</span><span style="color: #000000;">&#93;</span>;
&nbsp;
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$log</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$db</span>.LogFiles<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$log</span>.Name;
	<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800000;">&quot;=================&quot;</span>;
	<span style="color: #008000;"># Output each property</span>
	<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$p</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$log</span>.Properties<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-weight: bold;">Write-Host</span> <span style="color: #800080;">$p</span>.Name;
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Exposed logfile properties&#8230;</p>
<pre>
templog
=================
FileName
Growth
GrowthType
ID
MaxSize
Size
UsedSpace
BytesReadFromDisk
BytesWrittenToDisk
IsOffline
IsReadOnly
IsReadOnlyMedia
IsSparse
NumberOfDiskReads
NumberOfDiskWrites
PolicyHealthState
VolumeFreeSpace
</pre>
<p>If anyone knows a way I&#8217;d be interested to hear. Cheers</p>
<p><map name='google_ad_map_1358_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1358?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1358_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1358&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Faudit-vlfs-on-your-sql-server%2F1358' title="Audit VLFs on your SQL Server" alt=" Audit VLFs on your SQL Server" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/audit-vlfs-on-your-sql-server/1358">Audit VLFs on your SQL Server</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/audit-vlfs-on-your-sql-server/1358" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/audit-vlfs-on-your-sql-server/1358/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>TRIGGER_NESTLEVEL TSQL Function</title>
		<link>http://www.youdidwhatwithtsql.com/trigger_nestlevel-tsql-function/1287</link>
		<comments>http://www.youdidwhatwithtsql.com/trigger_nestlevel-tsql-function/1287#comments</comments>
		<pubDate>Wed, 10 Aug 2011 12:27:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[TRIGGER_NESTLEVEL]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/trigger_nestlevel-tsql-function/1287</guid>
		<description><![CDATA[This function is used to determine the current nest level or number of triggers that fired the current one. This could be used to prevent triggers from firing when fired by others. Here&#8217;s an example that does that; we have two tables with triggers, that fire AFTER INSERT, and insert into the other table. The [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/trigger_nestlevel-tsql-function/1287">TRIGGER_NESTLEVEL TSQL Function</a></p>
]]></description>
			<content:encoded><![CDATA[<p>This function is used to determine the current nest level or number of triggers that fired the current one. This could be used to prevent triggers from firing when fired by others. Here&#8217;s an example that does that; we have two tables with triggers, that fire AFTER INSERT, and insert into the other table. The use of <a title="TRIGGER_NESTLEVEL TSQL Function" href="http://msdn.microsoft.com/en-us/library/ms182737.aspx" target="_blank">TRIGGER_NESTLEVEL</a> allows us to control the flow gracefully.</p>
<p>First create the tables and triggers;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1287code12'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128712"><td class="code" id="p1287code12"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Table1</span>
<span style="color: #808080;">&#40;</span>
	Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	<span style="color: #0000FF;">Value</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TABLE</span> dbo.<span style="color: #202020;">Table2</span>
<span style="color: #808080;">&#40;</span>
	Id <span style="color: #0000FF;">INTEGER</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span> <span style="color: #0000FF;">PRIMARY</span> <span style="color: #0000FF;">KEY</span> <span style="color: #0000FF;">CLUSTERED</span>,
	<span style="color: #0000FF;">Value</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">20</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">NOT</span> <span style="color: #808080;">NULL</span>
<span style="color: #808080;">&#41;</span>;
GO
&nbsp;
<span style="color: #008080;">-- Trigger on table 1</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TRIGGER</span> dbo.<span style="color: #202020;">trg_Table1</span> <span style="color: #0000FF;">ON</span> dbo.<span style="color: #202020;">Table1</span>
<span style="color: #0000FF;">AFTER</span> <span style="color: #0000FF;">INSERT</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #0000FF;">DECLARE</span> @nest <span style="color: #0000FF;">INTEGER</span>;
	<span style="color: #0000FF;">SET</span> @nest <span style="color: #808080;">=</span> TRIGGER_NESTLEVEL<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;
&nbsp;
	<span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span>@nest <span style="color: #808080;">=</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">BEGIN</span>
		<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Table2</span>
		<span style="color: #808080;">&#40;</span>
			Id,
			<span style="color: #0000FF;">Value</span>
		<span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">SELECT</span> i.<span style="color: #202020;">Id</span>,
			   i.<span style="color: #0000FF;">Value</span>
		<span style="color: #0000FF;">FROM</span> inserted i;
	<span style="color: #0000FF;">END</span>
	<span style="color: #0000FF;">ELSE</span>
	<span style="color: #0000FF;">BEGIN</span>
		<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'Trigger 1: Insert aborted trigger_nestlevel is '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@nest <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>;
	<span style="color: #0000FF;">END</span>
<span style="color: #0000FF;">END</span>
GO
&nbsp;
<span style="color: #008080;">-- Trigger on table 2</span>
<span style="color: #0000FF;">CREATE</span> <span style="color: #0000FF;">TRIGGER</span> dbo.<span style="color: #202020;">trg_Table2</span> <span style="color: #0000FF;">ON</span> dbo.<span style="color: #202020;">Table2</span>
<span style="color: #0000FF;">AFTER</span> <span style="color: #0000FF;">INSERT</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #0000FF;">BEGIN</span>
	<span style="color: #0000FF;">DECLARE</span> @nest <span style="color: #0000FF;">INTEGER</span>;
	<span style="color: #0000FF;">SET</span> @nest <span style="color: #808080;">=</span> TRIGGER_NESTLEVEL<span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;
&nbsp;
	<span style="color: #0000FF;">IF</span> <span style="color: #808080;">&#40;</span>@nest <span style="color: #808080;">=</span> <span style="color: #000;">1</span><span style="color: #808080;">&#41;</span>
	<span style="color: #0000FF;">BEGIN</span>
		<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Table1</span>
		<span style="color: #808080;">&#40;</span>
			Id,
			<span style="color: #0000FF;">Value</span>
		<span style="color: #808080;">&#41;</span>
		<span style="color: #0000FF;">SELECT</span> i.<span style="color: #202020;">Id</span>,
			   i.<span style="color: #0000FF;">Value</span>
		<span style="color: #0000FF;">FROM</span> inserted i;
	<span style="color: #0000FF;">END</span>
	<span style="color: #0000FF;">ELSE</span>
	<span style="color: #0000FF;">BEGIN</span>
		<span style="color: #0000FF;">PRINT</span> <span style="color: #FF0000;">'Trigger 2: Insert aborted trigger_nestlevel is '</span> <span style="color: #808080;">+</span> <span style="color: #0000FF;">CAST</span><span style="color: #808080;">&#40;</span>@nest <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">10</span><span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>;
	<span style="color: #0000FF;">END</span>	
<span style="color: #0000FF;">END</span>
GO</pre></td></tr></table></div>

<p>Now insert data into <strong>dbo.Table1</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1287code13'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128713"><td class="code" id="p1287code13"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Table1</span>
<span style="color: #808080;">&#40;</span>
	Id,
	<span style="color: #0000FF;">Value</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #000;">1</span>,
	<span style="color: #FF0000;">'Blah'</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<pre>Trigger 2: Insert aborted trigger_nestlevel is 2

(1 row(s) affected)

(1 row(s) affected)</pre>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1287code14'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128714"><td class="code" id="p1287code14"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Table1</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> 
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Table2</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/TRIGGER_NESTLEVEL-TSQL-Function_B5A6/table1_trigger_nestlevel.png"><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="table1 trigger_nestlevel TSQL function" border="0" alt="table1 trigger nestlevel thumb TRIGGER NESTLEVEL TSQL Function" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/TRIGGER_NESTLEVEL-TSQL-Function_B5A6/table1_trigger_nestlevel_thumb.png" width="194" height="91" /></a></p>
<p>Now insert data into <strong>dbo.Table2</strong>.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1287code15'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128715"><td class="code" id="p1287code15"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> dbo.<span style="color: #202020;">Table2</span>
<span style="color: #808080;">&#40;</span>
	Id,
	<span style="color: #0000FF;">Value</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">VALUES</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #000;">2</span>,
	<span style="color: #FF0000;">'Blah'</span>
<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<pre>Trigger 1: Insert aborted trigger_nestlevel is 2

(1 row(s) affected)

(1 row(s) affected)</pre>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1287code16'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128716"><td class="code" id="p1287code16"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Table1</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span> 
<span style="color: #0000FF;">FROM</span> dbo.<span style="color: #202020;">Table2</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/TRIGGER_NESTLEVEL-TSQL-Function_B5A6/table2_trigger_nestlevel.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="table2 trigger_nestlevel TSQL Function" border="0" alt="table2 trigger nestlevel thumb TRIGGER NESTLEVEL TSQL Function" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/TRIGGER_NESTLEVEL-TSQL-Function_B5A6/table2_trigger_nestlevel_thumb.png" width="176" height="125" /></a></p>
<p>Of course this situation is best avoided in the first place but it&#8217;s nice to have an awareness of some of these lesser known functions.</p>
<p><map name='google_ad_map_1287_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1287?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1287_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1287&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Ftrigger_nestlevel-tsql-function%2F1287' title="TRIGGER NESTLEVEL TSQL Function" alt=" TRIGGER NESTLEVEL TSQL Function" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/trigger_nestlevel-tsql-function/1287">TRIGGER_NESTLEVEL TSQL Function</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/trigger_nestlevel-tsql-function/1287" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/trigger_nestlevel-tsql-function/1287/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The CONNECTIONPROPERTY TSQL Function</title>
		<link>http://www.youdidwhatwithtsql.com/the-connectionproperty-tsql-function/1286</link>
		<comments>http://www.youdidwhatwithtsql.com/the-connectionproperty-tsql-function/1286#comments</comments>
		<pubDate>Mon, 08 Aug 2011 11:53:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[CONNECTIONPROPERTY]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/the-connectionproperty-tsql-function/1286</guid>
		<description><![CDATA[The CONNECTIONPROPERTY function can be used to obtain information about the current connection. The information available is similar to the sys.dm_exec_connections system view. ?View Code TSQLSELECT 'Net Transport' AS Property, CONNECTIONPROPERTY&#40;'net_transport'&#41; AS Value UNION ALL SELECT 'Protocol type', CONNECTIONPROPERTY&#40;'protocol_type'&#41; UNION ALL SELECT 'Auth scheme', CONNECTIONPROPERTY&#40;'auth_scheme'&#41; UNION ALL SELECT 'Local net address', CONNECTIONPROPERTY&#40;'local_net_address'&#41; UNION ALL SELECT [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/the-connectionproperty-tsql-function/1286">The CONNECTIONPROPERTY TSQL Function</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The CONNECTIONPROPERTY function can be used to obtain information about the current connection. The information available is similar to the <a title="sys.dm_exec_connections system view" href="http://msdn.microsoft.com/en-us/library/ms181509.aspx" target="_blank">sys.dm_exec_connections</a> system view.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1286code18'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128618"><td class="code" id="p1286code18"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Net Transport'</span> <span style="color: #0000FF;">AS</span> Property, CONNECTIONPROPERTY<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'net_transport'</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #0000FF;">Value</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Protocol type'</span>, CONNECTIONPROPERTY<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'protocol_type'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Auth scheme'</span>, CONNECTIONPROPERTY<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'auth_scheme'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Local net address'</span>, CONNECTIONPROPERTY<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'local_net_address'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Local TCP port'</span>, CONNECTIONPROPERTY<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'local_tcp_port'</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Client net address'</span>, CONNECTIONPROPERTY<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'client_net_address'</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/The-CONNECTIONPROPERTY-TSQL-Function_B1E4/connectionproperty_tsql_function.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="connectionproperty tsql function" border="0" alt="connectionproperty tsql function thumb The CONNECTIONPROPERTY TSQL Function" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/The-CONNECTIONPROPERTY-TSQL-Function_B1E4/connectionproperty_tsql_function_thumb.png" width="236" height="167" /></a></p>
<p><map name='google_ad_map_1286_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1286?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1286_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1286&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fthe-connectionproperty-tsql-function%2F1286' title="The CONNECTIONPROPERTY TSQL Function" alt=" The CONNECTIONPROPERTY TSQL Function" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/the-connectionproperty-tsql-function/1286">The CONNECTIONPROPERTY TSQL Function</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/the-connectionproperty-tsql-function/1286" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/the-connectionproperty-tsql-function/1286/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Encryption with TSQL</title>
		<link>http://www.youdidwhatwithtsql.com/encryption-with-tsql/1285</link>
		<comments>http://www.youdidwhatwithtsql.com/encryption-with-tsql/1285#comments</comments>
		<pubDate>Wed, 03 Aug 2011 13:42:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[Encryption]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/encryption-with-tsql/1285</guid>
		<description><![CDATA[SQL Server has a bunch of encryption functionality at its disposal. The EncryptByPassphrase allows us to quickly encrypt data using a password. This function uses the Triple DES algorithm to protect data from prying eyes. To encrypt a section of text we supply a password and the text to the function; ?View Code TSQLSELECT ENCRYPTBYPASSPHRASE&#40;'secret', [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/encryption-with-tsql/1285">Encryption with TSQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>SQL Server has a bunch of <a href="http://technet.microsoft.com/en-us/library/bb510663.aspx" target="_blank">encryption functionality</a> at its disposal. The <a title="EncryptByPassphrase TSQL Function" href="http://technet.microsoft.com/en-us/library/ms190357.aspx" target="_blank">EncryptByPassphrase</a> allows us to quickly encrypt data using a password. This function uses the <a title="Triple DES" href="http://en.wikipedia.org/wiki/Triple_DES" target="_blank">Triple DES</a> algorithm to protect data from prying eyes. To encrypt a section of text we supply a password and the text to the function;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1285code22'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128522"><td class="code" id="p1285code22"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> ENCRYPTBYPASSPHRASE<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'secret'</span>, <span style="color: #FF0000;">'My very secret text'</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>This returns an encrypted version of our text.</p>
<pre>0x01000000409190B7ABDB55FE3724888C854ADBF33DA0BDF6F8AD0DCB0DC76746A2122D515B96DA4DCEF14FFA</pre>
<p>Of course there is also a <a title="DecryptByPassphrase TSQL function" href="http://technet.microsoft.com/en-us/library/ms188910.aspx" target="_blank">DecryptByPassphrase</a> we can use to decrypt the data.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1285code23'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128523"><td class="code" id="p1285code23"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> DECRYPTBYPASSPHRASE<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'secret'</span>, 0x010000006CF61B39AAF0030FDCED9942BEEEE946CB4EDB0AF2C0CDBCFB0B0882A7B32B4975974D20C8A3C82D<span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<pre>0x4D792076657279207365637265742074657874</pre>
<p>What? That&#8217;s not our original text. What&#8217;s going on here? This is our original text it&#8217;s just in <a title="VARBINARY TSQL Function" href="http://msdn.microsoft.com/en-us/library/ms188362.aspx" target="_blank">VARBINARY</a> format. Cast it to a char data type to make it human readable;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1285code24'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128524"><td class="code" id="p1285code24"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #0000FF;">CONVERT</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>, DECRYPTBYPASSPHRASE<span style="color: #808080;">&#40;</span><span style="color: #FF0000;">'secret'</span>, 0x010000006CF61B39AAF0030FDCED9942BEEEE946CB4EDB0AF2C0CDBCFB0B0882A7B32B4975974D20C8A3C82D<span style="color: #808080;">&#41;</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/The-ENCRYPTBYPASSPHRASE-TSQL-Function_CC34/tsql_encryption.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="tsql encryption" border="0" alt="tsql encryption thumb Encryption with TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/The-ENCRYPTBYPASSPHRASE-TSQL-Function_CC34/tsql_encryption_thumb.png" width="644" height="92" /></a></p>
<p><map name='google_ad_map_1285_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1285?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1285_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1285&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fencryption-with-tsql%2F1285' title="Encryption with TSQL" alt=" Encryption with TSQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/encryption-with-tsql/1285">Encryption with TSQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/encryption-with-tsql/1285" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/encryption-with-tsql/1285/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Check for bad SQL Server login passwords</title>
		<link>http://www.youdidwhatwithtsql.com/check-bad-sql-server-login-passwords/1283</link>
		<comments>http://www.youdidwhatwithtsql.com/check-bad-sql-server-login-passwords/1283#comments</comments>
		<pubDate>Mon, 01 Aug 2011 13:28:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[PWDCOMPARE]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[sql login]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/check-for-bad-sql-server-login-passwords/1283</guid>
		<description><![CDATA[The PWDCOMPARE function is really handy for further securing your SQL Servers by checking for a range of blank or common passwords. If you google for common password list you&#8217;ll probably recognise several if you&#8217;ve been working in IT for any reasonable amount of time. Fortunately you can use this function, in conjunction with the [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/check-bad-sql-server-login-passwords/1283">Check for bad SQL Server login passwords</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The <a title="TSQL PWDCOMPARE Function" href="http://msdn.microsoft.com/en-us/library/dd822792.aspx" target="_blank">PWDCOMPARE</a> function is really handy for further securing your SQL Servers by checking for a range of blank or common passwords. If you google for <a title="Common password list" href="http://www.google.co.uk/search?rlz=1C1SVEC_enGB381GB381&amp;aq=f&amp;sourceid=chrome&amp;ie=UTF-8&amp;q=common+password+list" target="_blank">common password list</a> you&#8217;ll probably recognise several if you&#8217;ve been working in IT for any reasonable amount of time. Fortunately you can use this function, in conjunction with the <a title="sys.sql_logins system view" href="http://msdn.microsoft.com/en-us/library/ms174355.aspx" target="_blank">sys.sql_logins</a> view, to check an instance for bad passwords.</p>
<p>I&#8217;m only checking <a title="Ten common passwords" href="http://modernl.com/article/top-10-most-common-passwords" target="_blank">ten common passwords</a> with the addition of a blank one. The list is based on a study of UK passwords so it&#8217;s not going to suitable for all corners of the globe. For a live system check you&#8217;ll probably want to use a more comprehensive list of passwords.</p>
<p>This TSQL will list any sql logins with a bad password.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1283code26'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128326"><td class="code" id="p1283code26"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @passwords <span style="color: #0000FF;">TABLE</span>
<span style="color: #808080;">&#40;</span>
	pwd <span style="color: #0000FF;">VARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>
<span style="color: #808080;">&#41;</span>
&nbsp;
<span style="color: #0000FF;">INSERT</span> <span style="color: #0000FF;">INTO</span> @passwords
<span style="color: #808080;">&#40;</span>
	pwd
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'thomas'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'arsenal'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'monkey'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'charlie'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'qwerty'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'123456'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'letmein'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'liverpool'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'password'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'123'</span>
<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">''</span>;
&nbsp;
<span style="color: #008080;">-- List bad users</span>
<span style="color: #0000FF;">SELECT</span> l.<span style="color: #808080;">&#91;</span>name<span style="color: #808080;">&#93;</span>,
	   l.<span style="color: #808080;">&#91;</span>sid<span style="color: #808080;">&#93;</span>,
	   p.<span style="color: #202020;">pwd</span>
<span style="color: #0000FF;">FROM</span> sys.<span style="color: #202020;">sql_logins</span> l
<span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> @passwords p
	<span style="color: #0000FF;">ON</span> PWDCOMPARE<span style="color: #808080;">&#40;</span>p.<span style="color: #202020;">pwd</span>, l.<span style="color: #202020;">password_hash</span><span style="color: #808080;">&#41;</span> <span style="color: #808080;">=</span> <span style="color: #000;">1</span>;</pre></td></tr></table></div>

<p>Hopefully you&#8217;ll get no results from your check but if you do it may look something like this;</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/Using-the-PWDCOMPARE-function-to-search-_C553/tsql_pwdcompare_sql_login_bad_password.png"><img style="background-image: none; padding-left: 0px; padding-right: 0px; display: inline; padding-top: 0px; border: 0px;" title="tsql_pwdcompare_sql_login_bad_password" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/Using-the-PWDCOMPARE-function-to-search-_C553/tsql_pwdcompare_sql_login_bad_password_thumb.png" alt="tsql pwdcompare sql login bad password thumb Check for bad SQL Server login passwords" width="644" height="164" border="0" /></a></p>
<p><map name='google_ad_map_1283_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1283?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1283_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1283&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcheck-bad-sql-server-login-passwords%2F1283' title="Check for bad SQL Server login passwords" alt=" Check for bad SQL Server login passwords" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/check-bad-sql-server-login-passwords/1283">Check for bad SQL Server login passwords</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/check-bad-sql-server-login-passwords/1283" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/check-bad-sql-server-login-passwords/1283/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Statistical System functions in TSQL</title>
		<link>http://www.youdidwhatwithtsql.com/statistical-system-functions-in-tsql/1281</link>
		<comments>http://www.youdidwhatwithtsql.com/statistical-system-functions-in-tsql/1281#comments</comments>
		<pubDate>Thu, 28 Jul 2011 12:55:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[fn_virtualfilestats]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/statistical-system-functions-in-tsql/1281</guid>
		<description><![CDATA[TSQL has a bunch of statistical system functions that can be used to return information about the system. This includes details about the number of connection attempts, cpu time, and total reads and writes and more. Many of these functions will be useful for performance monitoring. All of these functions return values that indicate cumulative [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/statistical-system-functions-in-tsql/1281">Statistical System functions in TSQL</a></p>
]]></description>
			<content:encoded><![CDATA[<p>TSQL has a bunch of <a href="http://msdn.microsoft.com/en-us/library/ms177520.aspx" target="_blank">statistical system functions</a> that can be used to return information about the system. This includes details about the number of connection attempts, cpu time, and total reads and writes and more.</p>
<p>Many of these functions will be useful for performance monitoring. All of these functions return values that indicate cumulative activity since the last restart of SQL Server.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1281code29'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128129"><td class="code" id="p1281code29"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">@@CONNECTIONS</span> <span style="color: #0000FF;">AS</span> ConnectionAttempts,
	   <span style="color: #FF00FF;">@@CPU_BUSY</span> <span style="color: #0000FF;">AS</span> CpuTime,
	   <span style="color: #FF00FF;">@@IDLE</span> <span style="color: #0000FF;">AS</span> IdleTime,
	   <span style="color: #FF00FF;">@@IO_BUSY</span> <span style="color: #0000FF;">AS</span> IoTime,
	   <span style="color: #FF00FF;">@@PACKET_ERRORS</span> <span style="color: #0000FF;">AS</span> PacketErrors,
	   <span style="color: #FF00FF;">@@PACK_RECEIVED</span> <span style="color: #0000FF;">AS</span> PacketsReceived,
	   <span style="color: #FF00FF;">@@PACK_SENT</span> <span style="color: #0000FF;">AS</span> PacketsSent,
	   <span style="color: #FF00FF;">@@TIMETICKS</span> <span style="color: #0000FF;">AS</span> MicrosecondsPerTick, <span style="color: #008080;">-- Computer dependant</span>
	   <span style="color: #FF00FF;">@@TOTAL_ERRORS</span> <span style="color: #0000FF;">AS</span> TotalDiskWriteErrors,
	   <span style="color: #FF00FF;">@@TOTAL_READ</span> <span style="color: #0000FF;">AS</span> TotalDiskReads,
	   <span style="color: #FF00FF;">@@TOTAL_WRITE</span> <span style="color: #0000FF;">AS</span> TotalDiskWrites;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/Statistical-System-function-in-TSQL_BF61/tsql_statistical_system_functions.png"><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="tsql statistical system functions" border="0" alt="tsql statistical system functions thumb Statistical System functions in TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/Statistical-System-function-in-TSQL_BF61/tsql_statistical_system_functions_thumb.png" width="644" height="53" /></a></p>
<p>An additional interesting function is <a title="fn_virtualfilestats" href="http://msdn.microsoft.com/en-us/library/ms187309.aspx" target="_blank">fn_virtualfilestats</a> which can return I/O stats about a database or particular database file. This is essentially the same as the <a href="http://msdn.microsoft.com/en-us/library/ms190326.aspx" target="_blank">sys.dm_io_virtual_file_stats</a> dmv. To retrieve information about a particular database run the following TSQL;</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1281code30'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128130"><td class="code" id="p1281code30"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #808080;">*</span>
<span style="color: #0000FF;">FROM</span> <span style="color: #AF0000;">fn_virtualfilestats</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">DB_ID</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>, <span style="color: #808080;">NULL</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/Statistical-System-function-in-TSQL_BF61/fn_virtualfilestats_TSQL_Function.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="fn_virtualfilestats_TSQL_Function" border="0" alt="fn virtualfilestats TSQL Function thumb Statistical System functions in TSQL" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/Statistical-System-function-in-TSQL_BF61/fn_virtualfilestats_TSQL_Function_thumb.png" width="644" height="64" /></a></p>
<p><map name='google_ad_map_1281_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1281?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1281_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1281&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fstatistical-system-functions-in-tsql%2F1281' title="Statistical System functions in TSQL" alt=" Statistical System functions in TSQL" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/statistical-system-functions-in-tsql/1281">Statistical System functions in TSQL</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/statistical-system-functions-in-tsql/1281" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/statistical-system-functions-in-tsql/1281/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>The NTILE TSQL Function</title>
		<link>http://www.youdidwhatwithtsql.com/the-ntile-tsql-function/1280</link>
		<comments>http://www.youdidwhatwithtsql.com/the-ntile-tsql-function/1280#comments</comments>
		<pubDate>Wed, 27 Jul 2011 12:15:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[NTILE]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/the-ntile-tsql-function/1280</guid>
		<description><![CDATA[The NTILE is used to assigned records into the desired number of groups. NTILE assigns a number to each record indicating the group it belongs to. The number of records in each group will be the same if possible, otherwise some groups will have less than the others. This function may be useful for assigning [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/the-ntile-tsql-function/1280">The NTILE TSQL Function</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The <a title="NTILE TSQL Function" href="http://msdn.microsoft.com/en-us/library/ms175126.aspx" target="_blank">NTILE</a> is used to assigned records into the desired number of groups. NTILE assigns a number to each record indicating the group it belongs to. The number of records in each group will be the same if possible, otherwise some groups will have less than the others.</p>
<p>This function may be useful for assigning a number of sales leads to a certain number of groups. Run the following TSQL in <a title="SQL Server Management Studio" href="http://msdn.microsoft.com/en-us/library/ms174173.aspx" target="_blank">SSMS</a>. </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1280code32'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128032"><td class="code" id="p1280code32"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">WITH</span> MyCTE
<span style="color: #808080;">&#40;</span>
	Company,
	Telephone
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'ACME LTD'</span>, <span style="color: #FF0000;">'0123456789'</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Big Corp'</span>, <span style="color: #FF0000;">'0987654321'</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Bobs Bits'</span>, <span style="color: #FF0000;">'9999999999'</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Daves Hardware'</span>, <span style="color: #FF0000;">'000000000000'</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Maestrosoft'</span>, <span style="color: #FF0000;">'111111111111'</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Boracle Corp'</span>, <span style="color: #FF0000;">'333333333333'</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'White Dwarf Microsystems'</span>, <span style="color: #FF0000;">'5555555555555'</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Big Telecom'</span>, <span style="color: #FF0000;">'4444444444444'</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'DOL'</span>, <span style="color: #FF0000;">'666666666666'</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span> 
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'London Media Ltd'</span>, <span style="color: #FF0000;">'888888888888'</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> Company,
	   Telephone,
	   NTILE<span style="color: #808080;">&#40;</span><span style="color: #000;">3</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">OVER</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> Company<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> <span style="color: #808080;">&#91;</span><span style="color: #0000FF;">Group</span><span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">FROM</span> MyCTE;</pre></td></tr></table></div>

<p>Note how there are four records in group 1 and the remaining groups get three each.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/The-NTILE-TSQL-Function_B66A/ntile_tsql_function.png"><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="ntile tsql function" border="0" alt="ntile tsql function thumb The NTILE TSQL Function" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/The-NTILE-TSQL-Function_B66A/ntile_tsql_function_thumb.png" width="644" height="461" /></a></p>
<p><map name='google_ad_map_1280_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1280?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1280_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1280&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fthe-ntile-tsql-function%2F1280' title="The NTILE TSQL Function" alt=" The NTILE TSQL Function" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/the-ntile-tsql-function/1280">The NTILE TSQL Function</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/the-ntile-tsql-function/1280" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/the-ntile-tsql-function/1280/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The GROUPING_ID TSQL Function</title>
		<link>http://www.youdidwhatwithtsql.com/the-grouping_id-tsql-function/1279</link>
		<comments>http://www.youdidwhatwithtsql.com/the-grouping_id-tsql-function/1279#comments</comments>
		<pubDate>Mon, 25 Jul 2011 11:57:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[GROUPING_ID]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/the-grouping_id-tsql-function/1279</guid>
		<description><![CDATA[The GROUPING_ID function computes the level of grouping in a resultset. It can be used in the SELECT, HAVING or ORDER BY clauses when used along with GROUP BY. The expression used in this function must match what has been used in the GROUP BY clause. This function can be usefully deployed to order a [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/the-grouping_id-tsql-function/1279">The GROUPING_ID TSQL Function</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The <a title="GROUPING_ID TSQL Function" href="http://msdn.microsoft.com/en-us/library/bb510624.aspx" target="_blank">GROUPING_ID</a> function computes the level of grouping in a resultset. It can be used in the SELECT, HAVING or ORDER BY clauses when used along with GROUP BY. The expression used in this function must match what has been used in the GROUP BY clause.</p>
<p>This function can be usefully deployed to order a resultset according to it&#8217;s grouping hierarchy. For example we may wish to order aggregated sales results according to period.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1279code34'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p127934"><td class="code" id="p1279code34"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">WITH</span> MyCTE
<span style="color: #808080;">&#40;</span>
	Division,
	<span style="color: #808080;">&#91;</span>Quarter<span style="color: #808080;">&#93;</span>,
	<span style="color: #0000FF;">Value</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 1'</span>, <span style="color: #000;">1</span>, <span style="color: #000;">0.12</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 1'</span>, <span style="color: #000;">1</span>, <span style="color: #000;">0.34</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 1'</span>, <span style="color: #000;">2</span>, <span style="color: #000;">0.45</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 2'</span>, <span style="color: #000;">3</span>, <span style="color: #000;">0.77</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 2'</span>, <span style="color: #000;">1</span>, <span style="color: #000;">0.55</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 1'</span>, <span style="color: #000;">3</span>, <span style="color: #000;">0.78</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 2'</span>, <span style="color: #000;">2</span>, <span style="color: #000;">0.45</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 2'</span>, <span style="color: #000;">4</span>, <span style="color: #000;">0.67</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 1'</span>, <span style="color: #000;">4</span>, <span style="color: #000;">0.77</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> Division,
	   <span style="color: #808080;">&#91;</span>Quarter<span style="color: #808080;">&#93;</span>,
	   <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Value</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> SummedValue,
	   <span style="color: #0000FF;">GROUPING</span><span style="color: #808080;">&#40;</span>Division<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> IsDivisionGroup,
	   <span style="color: #0000FF;">GROUPING</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Quarter<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> IsQuarterGroup,
	   GROUP<span style="color: #808080;">IN</span>G_ID<span style="color: #808080;">&#40;</span>Division, <span style="color: #808080;">&#91;</span>Quarter<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">FROM</span> MyCTE
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> Division,
		 <span style="color: #808080;">&#91;</span>Quarter<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">WITH</span> <span style="color: #0000FF;">ROLLUP</span>
<span style="color: #0000FF;">ORDER</span> <span style="color: #0000FF;">BY</span> GROUP<span style="color: #808080;">IN</span>G_ID<span style="color: #808080;">&#40;</span>Division, <span style="color: #808080;">&#91;</span>Quarter<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>Note how the resultset is ordered showing increasingly aggregated rows at the end.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/The-GROUPING_ID-TSQL-Function_B366/grouping_id_tsql_function.png"><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="grouping_id tsql function" border="0" alt="grouping id tsql function thumb The GROUPING ID TSQL Function" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/The-GROUPING_ID-TSQL-Function_B366/grouping_id_tsql_function_thumb.png" width="644" height="325" /></a></p>
<p><map name='google_ad_map_1279_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1279?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1279_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1279&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fthe-grouping_id-tsql-function%2F1279' title="The GROUPING ID TSQL Function" alt=" The GROUPING ID TSQL Function" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/the-grouping_id-tsql-function/1279">The GROUPING_ID TSQL Function</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/the-grouping_id-tsql-function/1279" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/the-grouping_id-tsql-function/1279/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The GROUPING TSQL Function</title>
		<link>http://www.youdidwhatwithtsql.com/the-grouping-tsql-function/1278</link>
		<comments>http://www.youdidwhatwithtsql.com/the-grouping-tsql-function/1278#comments</comments>
		<pubDate>Wed, 20 Jul 2011 11:43:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[GROUPING]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/the-grouping-tsql-function/1278</guid>
		<description><![CDATA[You can use the GROUPING function to indicate if a column in a resultset has been aggregated or not. A value of 1 will be returned if the result is aggregated, otherwise 0 is returned. It is best used to identify the additional rows returned when a query uses the ROLLUP, CUBE or GROUPING_SETS clauses. [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/the-grouping-tsql-function/1278">The GROUPING TSQL Function</a></p>
]]></description>
			<content:encoded><![CDATA[<p>You can use the <a title="TSQL GROUPING Function" href="http://msdn.microsoft.com/en-us/library/ms178544.aspx" target="_blank">GROUPING</a> function to indicate if a column in a resultset has been aggregated or not. A value of 1 will be returned if the result is aggregated, otherwise 0 is returned. It is best used to identify the additional rows returned when a query uses the ROLLUP, CUBE or GROUPING_SETS clauses.</p>
<p>To see this in action run the below TSQL in <a title="SQL Server Management Studio" href="http://msdn.microsoft.com/en-us/library/ms174173.aspx" target="_blank">SSMS</a>. </p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p1278code36'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p127836"><td class="code" id="p1278code36"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">WITH</span> MyCTE
<span style="color: #808080;">&#40;</span>
	Division,
	<span style="color: #808080;">&#91;</span>Quarter<span style="color: #808080;">&#93;</span>,
	<span style="color: #0000FF;">Value</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">AS</span>
<span style="color: #808080;">&#40;</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 1'</span>, <span style="color: #000;">1</span>, <span style="color: #000;">0.12</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 1'</span>, <span style="color: #000;">1</span>, <span style="color: #000;">0.34</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 1'</span>, <span style="color: #000;">2</span>, <span style="color: #000;">0.45</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 2'</span>, <span style="color: #000;">3</span>, <span style="color: #000;">0.77</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 2'</span>, <span style="color: #000;">1</span>, <span style="color: #000;">0.55</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 1'</span>, <span style="color: #000;">3</span>, <span style="color: #000;">0.78</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 2'</span>, <span style="color: #000;">2</span>, <span style="color: #000;">0.45</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 2'</span>, <span style="color: #000;">4</span>, <span style="color: #000;">0.67</span>
	<span style="color: #0000FF;">UNION</span> <span style="color: #808080;">ALL</span>
	<span style="color: #0000FF;">SELECT</span> <span style="color: #FF0000;">'Sales 1'</span>, <span style="color: #000;">4</span>, <span style="color: #000;">0.77</span>
<span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">SELECT</span> Division,
	   <span style="color: #808080;">&#91;</span>Quarter<span style="color: #808080;">&#93;</span>,
	   <span style="color: #FF00FF;">SUM</span><span style="color: #808080;">&#40;</span><span style="color: #0000FF;">Value</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> SummedValue,
	   <span style="color: #0000FF;">GROUPING</span><span style="color: #808080;">&#40;</span>Division<span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> IsDivisionGroup,
	   <span style="color: #0000FF;">GROUPING</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#91;</span>Quarter<span style="color: #808080;">&#93;</span><span style="color: #808080;">&#41;</span> <span style="color: #0000FF;">AS</span> IsQuarterGroup
<span style="color: #0000FF;">FROM</span> MyCTE
<span style="color: #0000FF;">GROUP</span> <span style="color: #0000FF;">BY</span> Division,
		 <span style="color: #808080;">&#91;</span>Quarter<span style="color: #808080;">&#93;</span>
<span style="color: #0000FF;">WITH</span> <span style="color: #0000FF;">ROLLUP</span>;</pre></td></tr></table></div>

<p>This query will return the following resultset. Note how <strong>IsQuarterGroup</strong> is flagged for each division total as well as the grand total row where <strong>IsQuarterGroup </strong>and <strong>IsDivisionGroup</strong> is also flagged.</p>
<p><a href="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/The-GROUPING-TSQL-Function_AD6B/grouping_tsql_function.png"><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="grouping tsql function" border="0" alt="grouping tsql function thumb The GROUPING TSQL Function" src="http://www.youdidwhatwithtsql.com/wp-content/uploads/2011/The-GROUPING-TSQL-Function_AD6B/grouping_tsql_function_thumb.png" width="644" height="395" /></a></p>
<p><map name='google_ad_map_1278_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1278?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_1278_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1278&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fthe-grouping-tsql-function%2F1278' title="The GROUPING TSQL Function" alt=" The GROUPING TSQL Function" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/the-grouping-tsql-function/1278">The GROUPING TSQL Function</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/the-grouping-tsql-function/1278" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/the-grouping-tsql-function/1278/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

