<?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; TSQL</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/tsql/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>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('p1287code6'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12876"><td class="code" id="p1287code6"><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('p1287code7'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12877"><td class="code" id="p1287code7"><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('p1287code8'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12878"><td class="code" id="p1287code8"><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('p1287code9'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p12879"><td class="code" id="p1287code9"><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('p1287code10'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128710"><td class="code" id="p1287code10"><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('p1286code12'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128612"><td class="code" id="p1286code12"><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('p1285code16'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128516"><td class="code" id="p1285code16"><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('p1285code17'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128517"><td class="code" id="p1285code17"><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('p1285code18'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128518"><td class="code" id="p1285code18"><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('p1283code20'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128320"><td class="code" id="p1283code20"><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('p1281code23'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128123"><td class="code" id="p1281code23"><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('p1281code24'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128124"><td class="code" id="p1281code24"><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('p1280code26'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p128026"><td class="code" id="p1280code26"><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('p1279code28'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p127928"><td class="code" id="p1279code28"><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('p1278code30'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p127830"><td class="code" id="p1278code30"><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>
		<item>
		<title>The APP_NAME TSQL Function</title>
		<link>http://www.youdidwhatwithtsql.com/the-app_name-tsql-function/1277</link>
		<comments>http://www.youdidwhatwithtsql.com/the-app_name-tsql-function/1277#comments</comments>
		<pubDate>Mon, 18 Jul 2011 11:15:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[APP_NAME]]></category>
		<category><![CDATA[Application Name]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/the-app_name-tsql-function/1277</guid>
		<description><![CDATA[The APP_NAME function returns the name of the application for the current database connection if the application has set it. Run the following in SSMS; ?View Code TSQLSELECT APP_NAME&#40;&#41;; This will return; Microsoft SQL Server Management Studio - Query There&#8217;s no method of setting this value within TSQL. To set this value you need to [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/the-app_name-tsql-function/1277">The APP_NAME TSQL Function</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://msdn.microsoft.com/en-us/library/ms189770.aspx" target="_blank">APP_NAME</a> function returns the name of the application for the current database connection if the application has set it. Run the following 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('p1277code32'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p127732"><td class="code" id="p1277code32"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">SELECT</span> <span style="color: #FF00FF;">APP_NAME</span><span style="color: #808080;">&#40;</span><span style="color: #808080;">&#41;</span>;</pre></td></tr></table></div>

<p>This will return;</p>
<pre>Microsoft SQL Server Management Studio - Query</pre>
<p>There&#8217;s no method of setting this value within TSQL. To set this value you need to include an <a title="Application Name in a connection string" href="http://www.connectionstrings.com/Articles/Show/use-application-name-sql-server" target="_blank">Application Name parameter</a> in your connection string. For example;</p>
<pre>Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;Application Name=MyAppName;</pre>
<p><map name='google_ad_map_1277_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1277?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_1277_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1277&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fthe-app_name-tsql-function%2F1277' title="The APP NAME TSQL Function" alt=" The APP NAME TSQL Function" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/the-app_name-tsql-function/1277">The APP_NAME TSQL Function</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/the-app_name-tsql-function/1277" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/the-app_name-tsql-function/1277/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Column is nullable but contains no nulls</title>
		<link>http://www.youdidwhatwithtsql.com/column-is-nullable-but-contains-no-nulls/1074</link>
		<comments>http://www.youdidwhatwithtsql.com/column-is-nullable-but-contains-no-nulls/1074#comments</comments>
		<pubDate>Wed, 23 Mar 2011 17:11:00 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[nullable]]></category>
		<category><![CDATA[nulls]]></category>
		<category><![CDATA[TSQL]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/column-is-nullable-but-contains-no-nulls/1074</guid>
		<description><![CDATA[We&#8217;re currently busy reviewing some of the historical database design decisions taken in our organisation. We&#8217;ve noticed quite a lot of columns, that are specified as nullable, but do not actually contain any nulls. Obviously this fact makes the column a possible candidate for changing to NOT NULL. I wanted to make this task a [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/column-is-nullable-but-contains-no-nulls/1074">Column is nullable but contains no nulls</a></p>
]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re currently busy reviewing some of the historical database design decisions taken in our organisation. We&#8217;ve noticed quite a lot of columns, that are specified as nullable, but do not actually contain any nulls. Obviously this fact makes the column a possible candidate for changing to NOT NULL.</p>
<p>I wanted to make this task a little easier so I knocked up a quick TSQL script using the <a href="http://msdn.microsoft.com/en-us/library/ms186778(v=SQL.90).aspx" target="_blank">INFORMATION_SCHEMA</a> views. This script uses these views to identify all the nullable columns and then executes a count of nulls in this column. This could potentially run a large number of resource-intensive queries so do not run this on any live system.</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('p1074code34'); return false;">View Code</a> TSQL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p107434"><td class="code" id="p1074code34"><pre class="tsql" style="font-family:monospace;"><span style="color: #0000FF;">DECLARE</span> @TABLE_SCHEMA <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>,
        @TABLE_NAME <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>,
        @COLUMN_NAME <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">100</span><span style="color: #808080;">&#41;</span>,
        @<span style="color: #0000FF;">sql</span> <span style="color: #0000FF;">NVARCHAR</span><span style="color: #808080;">&#40;</span><span style="color: #000;">1000</span><span style="color: #808080;">&#41;</span>,
        @<span style="color: #FF00FF;">count</span> <span style="color: #0000FF;">INTEGER</span>;
&nbsp;
<span style="color: #0000FF;">DECLARE</span> columnCursor <span style="color: #0000FF;">CURSOR</span> <span style="color: #0000FF;">LOCAL</span> FAST_F<span style="color: #808080;">OR</span>WARD <span style="color: #0000FF;">FOR</span> <span style="color: #0000FF;">SELECT</span> c.<span style="color: #202020;">TABLE_SCHEMA</span>,
                                                          c.<span style="color: #202020;">TABLE_NAME</span>,
                                                          c.<span style="color: #202020;">COLUMN_NAME</span>
						   <span style="color: #0000FF;">FROM</span> <span style="color: #808080;">IN</span>F<span style="color: #808080;">OR</span>MATION_SCHEMA.<span style="color: #202020;">COLUMNS</span> c
                                                   <span style="color: #0000FF;">INNER</span> <span style="color: #808080;">JOIN</span> <span style="color: #808080;">IN</span>F<span style="color: #808080;">OR</span>MATION_SCHEMA.<span style="color: #202020;">TABLES</span> t 
                                                       <span style="color: #0000FF;">ON</span> t.<span style="color: #202020;">TABLE_CATALOG</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">TABLE_CATALOG</span>
                                                       <span style="color: #808080;">AND</span> t.<span style="color: #202020;">TABLE_SCHEMA</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">TABLE_SCHEMA</span>
                                                       <span style="color: #808080;">AND</span> t.<span style="color: #202020;">TABLE_NAME</span> <span style="color: #808080;">=</span> c.<span style="color: #202020;">TABLE_NAME</span>
                                                   <span style="color: #0000FF;">WHERE</span> c.<span style="color: #202020;">IS_NULLABLE</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'YES'</span>
                                                   <span style="color: #808080;">AND</span> t.<span style="color: #202020;">TABLE_TYPE</span> <span style="color: #808080;">=</span> <span style="color: #FF0000;">'BASE TABLE'</span>;
&nbsp;
<span style="color: #0000FF;">OPEN</span> columnCursor;
<span style="color: #0000FF;">FETCH</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">FROM</span> columnCursor <span style="color: #0000FF;">INTO</span> @TABLE_SCHEMA,
                                  @TABLE_NAME,
                                  @COLUMN_NAME;
&nbsp;
<span style="color: #0000FF;">WHILE</span> <span style="color: #808080;">&#40;</span><span style="color: #FF00FF;">@@FETCH_STATUS</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
<span style="color: #0000FF;">BEGIN</span>
&nbsp;
                <span style="color: #0000FF;">SET</span> @<span style="color: #0000FF;">sql</span> <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'SELECT @count = COUNT(*) FROM ['</span> <span style="color: #808080;">+</span> @TABLE_SCHEMA <span style="color: #808080;">+</span> <span style="color: #FF0000;">'].['</span> <span style="color: #808080;">+</span> @TABLE_NAME <span style="color: #808080;">+</span> <span style="color: #FF0000;">'] WHERE ['</span> <span style="color: #808080;">+</span> @COLUMN_NAME <span style="color: #808080;">+</span> <span style="color: #FF0000;">'] IS NULL'</span>;
&nbsp;
                <span style="color: #0000FF;">EXECUTE</span> <span style="color: #AF0000;">sp_executesql</span> @query <span style="color: #808080;">=</span> @<span style="color: #0000FF;">sql</span>,
                                      @params <span style="color: #808080;">=</span> N<span style="color: #FF0000;">'@count INTEGER OUTPUT'</span>,
                                      @<span style="color: #FF00FF;">count</span> <span style="color: #808080;">=</span> @<span style="color: #FF00FF;">count</span> <span style="color: #0000FF;">OUTPUT</span>;
&nbsp;
                <span style="color: #0000FF;">IF</span><span style="color: #808080;">&#40;</span>@<span style="color: #FF00FF;">count</span> <span style="color: #808080;">=</span> <span style="color: #000;">0</span><span style="color: #808080;">&#41;</span>
                <span style="color: #0000FF;">BEGIN</span>
&nbsp;
                        <span style="color: #0000FF;">PRINT</span> @COLUMN_NAME <span style="color: #808080;">+</span> <span style="color: #FF0000;">' in '</span> <span style="color: #808080;">+</span> @TABLE_SCHEMA <span style="color: #808080;">+</span> <span style="color: #FF0000;">'.'</span> <span style="color: #808080;">+</span> @TABLE_NAME <span style="color: #808080;">+</span> <span style="color: #FF0000;">' is nullable but contains no nulls.'</span>;
&nbsp;
                <span style="color: #0000FF;">END</span>;
&nbsp;
                <span style="color: #0000FF;">FETCH</span> <span style="color: #0000FF;">NEXT</span> <span style="color: #0000FF;">FROM</span> columnCursor <span style="color: #0000FF;">INTO</span> @TABLE_SCHEMA,
                                                  @TABLE_NAME,
                                                  @COLUMN_NAME
<span style="color: #0000FF;">END</span>;
&nbsp;
<span style="color: #0000FF;">CLOSE</span> columnCursor;
<span style="color: #0000FF;">DEALLOCATE</span> columnCursor;</pre></td></tr></table></div>

<p>The script will print a report of all the columns set to allow nulls that do not actually contain any. Here&#8217;s what it prints out when run against the <a title="AdventureWorks sample database" href="http://msftdbprodsamples.codeplex.com/" target="_blank">AdventureWorks</a> database.</p>
<pre>SalesPersonID in Sales.Store is nullable but contains no nulls.
Demographics in Sales.Store is nullable but contains no nulls.
ThumbNailPhoto in Production.ProductPhoto is nullable but contains no nulls.
ThumbnailPhotoFileName in Production.ProductPhoto is nullable but contains no nulls.
LargePhoto in Production.ProductPhoto is nullable but contains no nulls.
LargePhotoFileName in Production.ProductPhoto is nullable but contains no nulls.
Comments in Production.ProductReview is nullable but contains no nulls.
LastReceiptCost in Purchasing.ProductVendor is nullable but contains no nulls.
LastReceiptDate in Purchasing.ProductVendor is nullable but contains no nulls.
EmailAddress in Person.Contact is nullable but contains no nulls.
Phone in Person.Contact is nullable but contains no nulls.
ShipDate in Purchasing.PurchaseOrderHeader is nullable but contains no nulls.
EndDate in Production.WorkOrder is nullable but contains no nulls.
ActualStartDate in Production.WorkOrderRouting is nullable but contains no nulls.
ActualEndDate in Production.WorkOrderRouting is nullable but contains no nulls.
ActualResourceHrs in Production.WorkOrderRouting is nullable but contains no nulls.
ActualCost in Production.WorkOrderRouting is nullable but contains no nulls.
TerritoryID in Sales.Customer is nullable but contains no nulls.
ShipDate in Sales.SalesOrderHeader is nullable but contains no nulls.
AccountNumber in Sales.SalesOrderHeader is nullable but contains no nulls.
TerritoryID in Sales.SalesOrderHeader is nullable but contains no nulls.
Document in Production.Document is nullable but contains no nulls.
Diagram in Production.Illustration is nullable but contains no nulls.
Demographics in Sales.Individual is nullable but contains no nulls.
Resume in HumanResources.JobCandidate is nullable but contains no nulls.
Schema in dbo.DatabaseLog is nullable but contains no nulls.
Object in dbo.DatabaseLog is nullable but contains no nulls.
ErrorSeverity in dbo.ErrorLog is nullable but contains no nulls.
ErrorState in dbo.ErrorLog is nullable but contains no nulls.
ErrorProcedure in dbo.ErrorLog is nullable but contains no nulls.
ErrorLine in dbo.ErrorLog is nullable but contains no nulls.</pre>
<p><map name='google_ad_map_1074_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/1074?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_1074_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=1074&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fcolumn-is-nullable-but-contains-no-nulls%2F1074' title="Column is nullable but contains no nulls" alt=" Column is nullable but contains no nulls" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/column-is-nullable-but-contains-no-nulls/1074">Column is nullable but contains no nulls</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/column-is-nullable-but-contains-no-nulls/1074" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/column-is-nullable-but-contains-no-nulls/1074/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

