<?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"
	>

<channel>
	<title>SQL Frenzy</title>
	<atom:link href="http://www.sqlfrenzy.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.sqlfrenzy.com</link>
	<description>The Smart Guide to SQL</description>
	<pubDate>Mon, 30 Aug 2010 04:00:00 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<item>
		<title>How to calculate Median in SQL Server 2005 or SQL Server 2008</title>
		<link>http://www.sqlfrenzy.com/2010/08/how-to-calculate-median-in-sql-server-2005-or-sql-server-2008/</link>
		<comments>http://www.sqlfrenzy.com/2010/08/how-to-calculate-median-in-sql-server-2005-or-sql-server-2008/#comments</comments>
		<pubDate>Mon, 30 Aug 2010 04:00:00 +0000</pubDate>
		<dc:creator>Jeff Smith</dc:creator>
		
		<category><![CDATA[Miscellaneous]]></category>

		<category><![CDATA[SQL Server 2005]]></category>

		<category><![CDATA[T-SQL]]></category>

		<category><![CDATA[Techniques]]></category>

		<category><![CDATA[group by]]></category>

		<guid isPermaLink="false">http://weblogs.sqlteam.com/jeffs/archive/2010/08/30/sql-server-calculate-median.aspx</guid>
		<description><![CDATA[Nothing earth-shattering here, I was just helping out a colleague with this so I thought I'd post up the example I gave him.<br />
<br />
<br />
<div style="40px;"><span style="Courier New;">-- sample table:</span><br />
<span style="Courier New;">create table People</span><br />
<span style="Courier New;">(</span><br />
<span style="Courier New;">    Person varchar(1) primary key,</span><br />
<span style="Courier New;">    City varchar(10),</span><br />
<span style="Courier New;">    Age int</span><br />
<span style="Courier New;">)</span><br />
<br />
<span style="Courier New;">go</span><br />
<br />
<span style="Courier New;">-- with some sample data:</span><br />
<br />
<span style="Courier New;">insert into People </span><br />
<span style="Courier New;">select 'A','Boston',23 union all  -- odd #</span><br />
<span style="Courier New;">select 'B','Boston',43 union all</span><br />
<span style="Courier New;">select 'C','Boston',29 union all</span><br />
<br />
<span style="Courier New;">select 'D','Chicago',15 union all -- single #</span><br />
<br />
<span style="Courier New;">select 'E','NY',12 union all  -- even #</span><br />
<span style="Courier New;">select 'F','NY',55 union all</span><br />
<span style="Courier New;">select 'G','NY',57 union all</span><br />
<span style="Courier New;">select 'H','NY',61</span><br />
<br />
<br />
<span style="Courier New;">go</span><br />
<br />
<span style="Courier New;">-- here's our query, showing median age per city:</span><br />
<br />
<span style="Courier New;">select city,</span><br />
<span style="Courier New;">    AVG(age) as MedianAge</span><br />
<span style="Courier New;">from </span><br />
<span style="Courier New;">(</span><br />
<span style="Courier New;">    select City, Person, Age, </span><br />
<span style="Courier New;">        ROW_NUMBER() over (partition by City order by Age ASC) as AgeRank,</span><br />
<span style="Courier New;">        COUNT(*) over (partition by City) as CityCount</span><br />
<span style="Courier New;">    from</span><br />
<span style="Courier New;">        People</span><br />
<span style="Courier New;">) x</span><br />
<span style="Courier New;">where</span><br />
<span style="Courier New;">    x.AgeRank in (x.CityCount/2+1, (x.CityCount+1)/2)    </span><br />
<span style="Courier New;">group by</span><br />
<span style="Courier New;">    x.City     </span><br />
<span style="Courier New;">    </span><br />
<br />
<span style="Courier New;">go</span><br />
<br />
<span style="Courier New;">-- clean it all up</span><br />
<span style="Courier New;">drop table People<br />
<br />
</span></div>
<p><span style="Arial;">And here's the result:</span></p>
<div style="40px;"><span style="Courier New;"><br />
city       MedianAge<br />
---------- -----------<br />
Boston     29<br />
Chicago    15<br />
NY         56<br />
<br />
(3 row(s) affected)<br />
<br />
</span></div>
<p>Simple remove "City" from the SELECT clause and the GROUP BY clause to get the median age for all.  <br />
</p>
<p>There may be more efficient tricks out there, but this is certainly the shortest and simplest technique I am aware of.</p>
<p> </p><img src="http://weblogs.sqlteam.com/jeffs/aggbug/61191.aspx" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/C570nWp_rnXaAAx8HndjFoGCFpY/0/da"><img src="http://feedads.g.doubleclick.net/~a/C570nWp_rnXaAAx8HndjFoGCFpY/0/di" border="0"></img></a><br />
<a href="http://feedads.g.doubleclick.net/~a/C570nWp_rnXaAAx8HndjFoGCFpY/1/da"><img src="http://feedads.g.doubleclick.net/~a/C570nWp_rnXaAAx8HndjFoGCFpY/1/di" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:P8itKDj-SAg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:P8itKDj-SAg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Yl_4zzkdk6g:P8itKDj-SAg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:P8itKDj-SAg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Yl_4zzkdk6g:P8itKDj-SAg:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:P8itKDj-SAg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Yl_4zzkdk6g:P8itKDj-SAg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:P8itKDj-SAg:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"></img></a>
</div>]]></description>
			<content:encoded><![CDATA[Nothing earth-shattering here, I was just helping out a colleague with this so I thought I'd post up the example I gave him.<br />
<br />
<br />
<div><span>-- sample table:</span><br />
<span>create table People</span><br />
<span>(</span><br />
<span>    Person varchar(1) primary key,</span><br />
<span>    City varchar(10),</span><br />
<span>    Age int</span><br />
<span>)</span><br />
<br />
<span>go</span><br />
<br />
<span>-- with some sample data:</span><br />
<br />
<span>insert into People </span><br />
<span>select 'A','Boston',23 union all  -- odd #</span><br />
<span>select 'B','Boston',43 union all</span><br />
<span>select 'C','Boston',29 union all</span><br />
<br />
<span>select 'D','Chicago',15 union all -- single #</span><br />
<br />
<span>select 'E','NY',12 union all  -- even #</span><br />
<span>select 'F','NY',55 union all</span><br />
<span>select 'G','NY',57 union all</span><br />
<span>select 'H','NY',61</span><br />
<br />
<br />
<span>go</span><br />
<br />
<span>-- here's our query, showing median age per city:</span><br />
<br />
<span>select city,</span><br />
<span>    AVG(age) as MedianAge</span><br />
<span>from </span><br />
<span>(</span><br />
<span>    select City, Person, Age, </span><br />
<span>        ROW_NUMBER() over (partition by City order by Age ASC) as AgeRank,</span><br />
<span>        COUNT(*) over (partition by City) as CityCount</span><br />
<span>    from</span><br />
<span>        People</span><br />
<span>) x</span><br />
<span>where</span><br />
<span>    x.AgeRank in (x.CityCount/2+1, (x.CityCount+1)/2)    </span><br />
<span>group by</span><br />
<span>    x.City     </span><br />
<span>    </span><br />
<br />
<span>go</span><br />
<br />
<span>-- clean it all up</span><br />
<span>drop table People<br />
<br />
</span></div>
<p><span>And here's the result:</span></p>
<div><span><br />
city       MedianAge<br />
---------- -----------<br />
Boston     29<br />
Chicago    15<br />
NY         56<br />
<br />
(3 row(s) affected)<br />
<br />
</span></div>
<p>Simple remove "City" from the SELECT clause and the GROUP BY clause to get the median age for all.  <br />
</p>
<p>There may be more efficient tricks out there, but this is certainly the shortest and simplest technique I am aware of.</p>
<p> </p><img src="http://weblogs.sqlteam.com/jeffs/aggbug/61191.aspx" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/C570nWp_rnXaAAx8HndjFoGCFpY/0/da"><img src="http://feedads.g.doubleclick.net/~a/C570nWp_rnXaAAx8HndjFoGCFpY/0/di" border="0"></img></a><br>
<a href="http://feedads.g.doubleclick.net/~a/C570nWp_rnXaAAx8HndjFoGCFpY/1/da"><img src="http://feedads.g.doubleclick.net/~a/C570nWp_rnXaAAx8HndjFoGCFpY/1/di" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:P8itKDj-SAg:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:P8itKDj-SAg:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Yl_4zzkdk6g:P8itKDj-SAg:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:P8itKDj-SAg:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Yl_4zzkdk6g:P8itKDj-SAg:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:P8itKDj-SAg:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Yl_4zzkdk6g:P8itKDj-SAg:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:P8itKDj-SAg:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.sqlfrenzy.com/2010/08/how-to-calculate-median-in-sql-server-2005-or-sql-server-2008/feed/</wfw:commentRss>
		</item>
		<item>
		<title>An interesting take on &#8220;Stored Procedures&#8221; (link)</title>
		<link>http://www.sqlfrenzy.com/2009/11/an-interesting-take-on-stored-procedures-link/</link>
		<comments>http://www.sqlfrenzy.com/2009/11/an-interesting-take-on-stored-procedures-link/#comments</comments>
		<pubDate>Mon, 16 Nov 2009 18:42:45 +0000</pubDate>
		<dc:creator>Jeff Smith</dc:creator>
		
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://weblogs.sqlteam.com/jeffs/archive/2009/11/16/61054.aspx</guid>
		<description><![CDATA[Another great post SQL-related post over at <a href="http://thedailywtf.com">TheDailyWTF</a> regarding a, umm, "creative" use of "Stored Procedures":<br />
<br />
<a href="http://thedailywtf.com/Articles/For-the-Ease-of-Maintenance.aspx">http://thedailywtf.com/Articles/For-the-Ease-of-Maintenance.aspx</a><br />
<br />
<br /><img src="http://weblogs.sqlteam.com/jeffs/aggbug/61054.aspx" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/7zo3VkiyCNqd4crdNN_ccgE3-Yc/0/da"><img src="http://feedads.g.doubleclick.net/~a/7zo3VkiyCNqd4crdNN_ccgE3-Yc/0/di" border="0"></img></a><br />
<a href="http://feedads.g.doubleclick.net/~a/7zo3VkiyCNqd4crdNN_ccgE3-Yc/1/da"><img src="http://feedads.g.doubleclick.net/~a/7zo3VkiyCNqd4crdNN_ccgE3-Yc/1/di" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:WF5pKKUmHO8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:WF5pKKUmHO8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RptS5omfvbQ:WF5pKKUmHO8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:WF5pKKUmHO8:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RptS5omfvbQ:WF5pKKUmHO8:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:WF5pKKUmHO8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RptS5omfvbQ:WF5pKKUmHO8:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:WF5pKKUmHO8:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"></img></a>
</div>]]></description>
			<content:encoded><![CDATA[Another great post SQL-related post over at <a href="http://thedailywtf.com">TheDailyWTF</a> regarding a, umm, "creative" use of "Stored Procedures":<br />
<br />
<a href="http://thedailywtf.com/Articles/For-the-Ease-of-Maintenance.aspx">http://thedailywtf.com/Articles/For-the-Ease-of-Maintenance.aspx</a><br />
<br />
<br /><img src="http://weblogs.sqlteam.com/jeffs/aggbug/61054.aspx" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/7zo3VkiyCNqd4crdNN_ccgE3-Yc/0/da"><img src="http://feedads.g.doubleclick.net/~a/7zo3VkiyCNqd4crdNN_ccgE3-Yc/0/di" border="0"></img></a><br>
<a href="http://feedads.g.doubleclick.net/~a/7zo3VkiyCNqd4crdNN_ccgE3-Yc/1/da"><img src="http://feedads.g.doubleclick.net/~a/7zo3VkiyCNqd4crdNN_ccgE3-Yc/1/di" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:WF5pKKUmHO8:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:WF5pKKUmHO8:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RptS5omfvbQ:WF5pKKUmHO8:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:WF5pKKUmHO8:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RptS5omfvbQ:WF5pKKUmHO8:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:WF5pKKUmHO8:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RptS5omfvbQ:WF5pKKUmHO8:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:WF5pKKUmHO8:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.sqlfrenzy.com/2009/11/an-interesting-take-on-stored-procedures-link/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Programming is like dreaming?</title>
		<link>http://www.sqlfrenzy.com/2009/11/programming-is-like-dreaming/</link>
		<comments>http://www.sqlfrenzy.com/2009/11/programming-is-like-dreaming/#comments</comments>
		<pubDate>Mon, 02 Nov 2009 19:52:38 +0000</pubDate>
		<dc:creator>Jeff Smith</dc:creator>
		
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://weblogs.sqlteam.com/jeffs/archive/2009/11/02/61035.aspx</guid>
		<description><![CDATA[This is from March, so it's a little old, but I just stumbled upon it and found it a bit interesting. I never thought of programming this way before, but it does make sense:<br />
<br />
<a href="http://www.independentdeveloper.com/archive/2009/03/17/programming-is-like-a-dream">Programming is like a dream</a><br /><img src="http://weblogs.sqlteam.com/jeffs/aggbug/61035.aspx" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/3Jky2NYreqGtV_T83xhoLbWz-0c/0/da"><img src="http://feedads.g.doubleclick.net/~a/3Jky2NYreqGtV_T83xhoLbWz-0c/0/di" border="0"></img></a><br />
<a href="http://feedads.g.doubleclick.net/~a/3Jky2NYreqGtV_T83xhoLbWz-0c/1/da"><img src="http://feedads.g.doubleclick.net/~a/3Jky2NYreqGtV_T83xhoLbWz-0c/1/di" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:n81Zb-h-o1A:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:n81Zb-h-o1A:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=jc0HtIULhGw:n81Zb-h-o1A:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:n81Zb-h-o1A:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=jc0HtIULhGw:n81Zb-h-o1A:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:n81Zb-h-o1A:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=jc0HtIULhGw:n81Zb-h-o1A:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:n81Zb-h-o1A:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"></img></a>
</div>]]></description>
			<content:encoded><![CDATA[This is from March, so it's a little old, but I just stumbled upon it and found it a bit interesting. I never thought of programming this way before, but it does make sense:<br />
<br />
<a href="http://www.independentdeveloper.com/archive/2009/03/17/programming-is-like-a-dream">Programming is like a dream</a><br /><img src="http://weblogs.sqlteam.com/jeffs/aggbug/61035.aspx" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/3Jky2NYreqGtV_T83xhoLbWz-0c/0/da"><img src="http://feedads.g.doubleclick.net/~a/3Jky2NYreqGtV_T83xhoLbWz-0c/0/di" border="0"></img></a><br>
<a href="http://feedads.g.doubleclick.net/~a/3Jky2NYreqGtV_T83xhoLbWz-0c/1/da"><img src="http://feedads.g.doubleclick.net/~a/3Jky2NYreqGtV_T83xhoLbWz-0c/1/di" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:n81Zb-h-o1A:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:n81Zb-h-o1A:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=jc0HtIULhGw:n81Zb-h-o1A:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:n81Zb-h-o1A:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=jc0HtIULhGw:n81Zb-h-o1A:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:n81Zb-h-o1A:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=jc0HtIULhGw:n81Zb-h-o1A:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:n81Zb-h-o1A:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.sqlfrenzy.com/2009/11/programming-is-like-dreaming/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Is it just me &#8230;</title>
		<link>http://www.sqlfrenzy.com/2009/10/is-it-just-me/</link>
		<comments>http://www.sqlfrenzy.com/2009/10/is-it-just-me/#comments</comments>
		<pubDate>Tue, 27 Oct 2009 16:50:54 +0000</pubDate>
		<dc:creator>Jeff Smith</dc:creator>
		
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://weblogs.sqlteam.com/jeffs/archive/2009/10/27/61031.aspx</guid>
		<description><![CDATA[... or is about time I got back to some blogging?<img src="http://weblogs.sqlteam.com/jeffs/aggbug/61031.aspx" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/oVjmqFcrQwvMjOuKYzQvqdgM3fM/0/da"><img src="http://feedads.g.doubleclick.net/~a/oVjmqFcrQwvMjOuKYzQvqdgM3fM/0/di" border="0"></img></a><br />
<a href="http://feedads.g.doubleclick.net/~a/oVjmqFcrQwvMjOuKYzQvqdgM3fM/1/da"><img src="http://feedads.g.doubleclick.net/~a/oVjmqFcrQwvMjOuKYzQvqdgM3fM/1/di" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:mrHM3p56kuE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:mrHM3p56kuE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=it5higQ-UX8:mrHM3p56kuE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:mrHM3p56kuE:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=it5higQ-UX8:mrHM3p56kuE:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:mrHM3p56kuE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=it5higQ-UX8:mrHM3p56kuE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:mrHM3p56kuE:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"></img></a>
</div>]]></description>
			<content:encoded><![CDATA[... or is about time I got back to some blogging?<img src="http://weblogs.sqlteam.com/jeffs/aggbug/61031.aspx" width="1" height="1" />
<p><a href="http://feedads.g.doubleclick.net/~a/oVjmqFcrQwvMjOuKYzQvqdgM3fM/0/da"><img src="http://feedads.g.doubleclick.net/~a/oVjmqFcrQwvMjOuKYzQvqdgM3fM/0/di" border="0"></img></a><br>
<a href="http://feedads.g.doubleclick.net/~a/oVjmqFcrQwvMjOuKYzQvqdgM3fM/1/da"><img src="http://feedads.g.doubleclick.net/~a/oVjmqFcrQwvMjOuKYzQvqdgM3fM/1/di" border="0"></img></a></p><div class="feedflare">
<a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:mrHM3p56kuE:yIl2AUoC8zA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:mrHM3p56kuE:V_sGLiPBpWU"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=it5higQ-UX8:mrHM3p56kuE:V_sGLiPBpWU" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:mrHM3p56kuE:gIN9vFwOqvQ"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=it5higQ-UX8:mrHM3p56kuE:gIN9vFwOqvQ" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:mrHM3p56kuE:F7zBnMyn0Lo"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=it5higQ-UX8:mrHM3p56kuE:F7zBnMyn0Lo" border="0"></img></a> <a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:mrHM3p56kuE:7Q72WNTAKBA"><img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.sqlfrenzy.com/2009/10/is-it-just-me/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Processing an OLAP cube with a T-SQL Stored Procedure</title>
		<link>http://www.sqlfrenzy.com/2008/10/processing-an-olap-cube-with-a-t-sql-stored-procedure/</link>
		<comments>http://www.sqlfrenzy.com/2008/10/processing-an-olap-cube-with-a-t-sql-stored-procedure/#comments</comments>
		<pubDate>Mon, 27 Oct 2008 19:26:05 +0000</pubDate>
		<dc:creator>Jeff Smith</dc:creator>
		
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://weblogs.sqlteam.com/jeffs/archive/2008/10/27/process-olap-cube-with-t-sql.aspx</guid>
		<description><![CDATA[Here's a simple SQL Server stored procedure that you can call to process an OLAP cube using T-SQL.  The parameters should be self-explanatory.  To me, this is a little easier and more flexible than processing using DTS packages.<br />
<br />
<div style="40px;">
<p style="0px;"><span style="blue;">create procedure </span>ProcessCube</p>
<p style="0px;">    @Database <span style="blue;">varchar</span>(100),</p>
<p style="0px;">    @Cube <span style="blue;">varchar</span>(100),</p>
<p style="0px;">    @Partition <span style="blue;">varchar</span>(100)  = <span style="blue;">null</span>, <span style="green;">-- If NULL, process the entire Cube</span></p>
<p style="0px;">    @Server <span style="blue;">varchar</span>(100) = <span style="rgb(163, 21, 21);">'localhost'</span></p>
<p style="0px;"><span style="blue;">as</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="green;">/* </span></p>
<p style="0px;">    <span style="green;">Author:    </span>    <span style="green;">Jeff Smith</span></p>
<p style="0px;">    <span style="green;">Version:    10/27/2008</span></p>
<p style="0px;"><span style="green;">*/</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="green;">/* variables used to store object handles */</span></p>
<p style="0px;"><span style="blue;">declare </span>@o_svr <span style="blue;">int</span>, @o_db <span style="blue;">int</span>, @o_cube <span style="blue;">int</span>, @o_part <span style="blue;">int</span>, @o_mds <span style="blue;">int</span></p>
<p style="0px;"><span style="blue;">declare </span>@hr <span style="blue;">int</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="green;">/* different cube processing options. </span> <span style="green;">This SP uses "default" */</span></p>
<p style="0px;"><span style="blue;">declare </span>@PROCESS_DEFAULT <span style="blue;">int</span></p>
<p style="0px;"><span style="blue;">declare </span>@PROCESS_FULL <span style="blue;">int</span></p>
<p style="0px;"><span style="blue;">declare </span>@PROCESS_REFRESH_DATA <span style="blue;">int</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="blue;">set </span>@PROCESS_DEFAULT = 0</p>
<p style="0px;"><span style="blue;">set </span>@PROCESS_FULL = 1</p>
<p style="0px;"><span style="blue;">set </span>@PROCESS_REFRESH_DATA = 2</p>
<p style="0px;"> </p>
<p style="0px;"><span style="green;">-- create a DSO.Server object:</span></p>
<p style="0px;"><span style="blue;">exec </span>@hr = sp_OACreate <span style="rgb(163, 21, 21);">'DSO.Server'</span>, @o_svr out</p>
<p style="0px;"><span style="blue;">if </span>@hr &#60;&#62; 0</p>
<p style="0px;">    <span style="blue;">begin</span></p>
<p style="0px;">        <span style="blue;">print </span><span style="rgb(163, 21, 21);">'Error at create server:'</span></p>
<p style="0px;">        <span style="blue;">exec </span>sp_OAGetErrorInfo @o_svr</p>
<p style="0px;">        <span style="blue;">goto </span>cleanup</p>
<p style="0px;">    <span style="blue;">end</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="green;">-- Connect to the server:</span></p>
<p style="0px;"><span style="blue;">exec </span>@hr = sp_OAMethod @o_svr, <span style="rgb(163, 21, 21);">'Connect'</span>, <span style="blue;">null</span>, @Server</p>
<p style="0px;"><span style="blue;">if </span>@hr &#60;&#62; 0</p>
<p style="0px;">    <span style="blue;">begin</span></p>
<p style="0px;">        <span style="blue;">print </span><span style="rgb(163, 21, 21);">'Error at connect to server:'</span></p>
<p style="0px;">        <span style="blue;">exec </span>sp_OAGetErrorInfo @o_svr</p>
<p style="0px;">        <span style="blue;">goto </span>cleanup</p>
<p style="0px;">    <span style="blue;">end</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="green;">-- Get the MDStores property from the Server:</span></p>
<p style="0px;"><span style="blue;">exec </span>@hr = sp_OAGetProperty @o_svr,<span style="rgb(163, 21, 21);">'MDStores'</span>, @o_mds OUT</p>
<p style="0px;"><span style="blue;">if </span>@hr &#60;&#62; 0</p>
<p style="0px;">    <span style="blue;">begin</span></p>
<p style="0px;">        <span style="blue;">print </span><span style="rgb(163, 21, 21);">'Error at get getting Server MDStores:'</span></p>
<p style="0px;">        <span style="blue;">exec </span>sp_OAGetErrorInfo @o_svr</p>
<p style="0px;">        <span style="blue;">goto </span>cleanup</p>
<p style="0px;">    <span style="blue;">end</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="green;">-- Get the database from the MDStores:</span></p>
<p style="0px;"><span style="blue;">exec </span>@hr = sp_OAGetProperty @o_mds,<span style="rgb(163, 21, 21);">'Item'</span>,@o_db OUT,@Database</p>
<p style="0px;"><span style="blue;">if </span>@hr &#60;&#62; 0</p>
<p style="0px;">    <span style="blue;">begin</span></p>
<p style="0px;">        <span style="blue;">print </span><span style="rgb(163, 21, 21);">'Error at get database:'</span></p>
<p style="0px;">        <span style="blue;">exec </span>sp_OAGetErrorInfo @o_mds</p>
<p style="0px;">        <span style="blue;">goto </span>cleanup</p>
<p style="0px;">    <span style="blue;">end</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="green;">-- get the MDStores property from the database:</span></p>
<p style="0px;"><span style="blue;">exec </span>sp_OADestroy @o_mds</p>
<p style="0px;"><span style="blue;">exec </span>@hr = sp_OAGetProperty @o_db,<span style="rgb(163, 21, 21);">'MDStores'</span>, @o_mds OUT</p>
<p style="0px;"><span style="blue;">if </span>@hr &#60;&#62; 0</p>
<p style="0px;">    <span style="blue;">begin</span></p>
<p style="0px;">        <span style="blue;">print </span><span style="rgb(163, 21, 21);">'Error at get database MDStores:'</span></p>
<p style="0px;">        <span style="blue;">exec </span>sp_OAGetErrorInfo @o_db</p>
<p style="0px;">        <span style="blue;">goto </span>cleanup</p>
<p style="0px;">    <span style="blue;">end</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="green;">-- get the Cube from the MDStores</span></p>
<p style="0px;"><span style="blue;">exec </span>@hr = sp_OAGetProperty @o_mds,<span style="rgb(163, 21, 21);">'Item'</span>,@o_cube OUT, @Cube</p>
<p style="0px;"><span style="blue;">if </span>@hr &#60;&#62; 0</p>
<p style="0px;">    <span style="blue;">begin</span></p>
<p style="0px;">        <span style="blue;">print </span><span style="rgb(163, 21, 21);">'Error at get Cube:'</span></p>
<p style="0px;">        <span style="blue;">exec </span>sp_OAGetErrorInfo @o_mds</p>
<p style="0px;">        <span style="blue;">goto </span>cleanup</p>
<p style="0px;">    <span style="blue;">end</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="blue;">if </span>@Partition <span style="blue;">is null </span> <span style="green;">-- Process the entire Cube, not just a single partition</span></p>
<p style="0px;">    <span style="blue;">begin</span></p>
<p style="0px;">        <span style="blue;">exec </span>@hr = sp_OAMethod @o_cube, <span style="rgb(163, 21, 21);">'Process'</span>, <span style="blue;">null</span>, @PROCESS_DEFAULT</p>
<p style="0px;">        <span style="blue;">if </span>@hr &#60;&#62; 0</p>
<p style="0px;">            <span style="blue;">begin</span></p>
<p style="0px;">                <span style="blue;">print </span><span style="rgb(163, 21, 21);">'Error at process Cube:'</span></p>
<p style="0px;">                <span style="blue;">exec </span>sp_OAGetErrorInfo @o_cube</p>
<p style="0px;">                <span style="blue;">goto </span>cleanup</p>
<p style="0px;">            <span style="blue;">end</span></p>
<p style="0px;">    <span style="blue;">end</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="blue;">else    </span><span style="green;">-- just process the specified Partition</span></p>
<p style="0px;"> </p>
<p style="0px;">    <span style="blue;">begin</span></p>
<p style="0px;">        <span style="green;">-- Get the MDStores property of the Cube:</span></p>
<p style="0px;">        <span style="blue;">exec </span>sp_OADestroy @o_mds</p>
<p style="0px;">        <span style="blue;">exec </span>@hr = sp_OAGetProperty @o_cube,<span style="rgb(163, 21, 21);">'MDStores'</span>, @o_mds OUT</p>
<p style="0px;">        <span style="blue;">if </span>@hr &#60;&#62; 0</p>
<p style="0px;">            <span style="blue;">begin</span></p>
<p style="0px;">                <span style="blue;">print </span><span style="rgb(163, 21, 21);">'Error at get Cube MDStores:'</span></p>
<p style="0px;">                <span style="blue;">exec </span>sp_OAGetErrorInfo @o_cube</p>
<p style="0px;">                <span style="blue;">goto </span>cleanup</p>
<p style="0px;">            <span style="blue;">end</span></p>
<p style="0px;"> </p>
<p style="0px;">        <span style="green;">-- </span> <span style="green;">Get the partition to process:</span></p>
<p style="0px;">        <span style="blue;">exec </span>@hr = sp_OAGetProperty @o_mds,<span style="rgb(163, 21, 21);">'Item'</span>,@o_part OUT, @Partition</p>
<p style="0px;">        <span style="blue;">if </span>@hr &#60;&#62; 0</p>
<p style="0px;">            <span style="blue;">begin</span></p>
<p style="0px;">                <span style="blue;">print </span><span style="rgb(163, 21, 21);">'Error at get Parition:'</span></p>
<p style="0px;">                <span style="blue;">exec </span>sp_OAGetErrorInfo @o_mds</p>
<p style="0px;">                <span style="blue;">goto </span>cleanup</p>
<p style="0px;">            <span style="blue;">end</span></p>
<p style="0px;"> </p>
<p style="0px;">        <span style="green;">-- Process the partition:</span></p>
<p style="0px;">        <span style="blue;">exec </span>@hr = sp_OAMethod @o_part, <span style="rgb(163, 21, 21);">'Process'</span>, <span style="blue;">null</span>, @PROCESS_DEFAULT</p>
<p style="0px;">        <span style="blue;">if </span>@hr &#60;&#62; 0</p>
<p style="0px;">            <span style="blue;">begin</span></p>
<p style="0px;">                <span style="blue;">print </span><span style="rgb(163, 21, 21);">'Error at process Partition:'</span></p>
<p style="0px;">                <span style="blue;">exec </span>sp_OAGetErrorInfo @o_part</p>
<p style="0px;">                <span style="blue;">goto </span>cleanup</p>
<p style="0px;">            <span style="blue;">end</span></p>
<p style="0px;">    <span style="blue;">end</span></p>
<p style="0px;"> </p>
<p style="0px;"><span style="green;">-- And unlock all objects on the server:</span></p>
<p style="0px;"><span style="blue;">exec </span>@hr = sp_OAMethod @o_svr, <span style="rgb(163, 21, 21);">'UnlockAllObjects'</span></p>
<p style="0px;"><span style="blue;">if </span>@hr &#60;&#62; 0</p>
<p style="0px;">    <span style="blue;">begin</span></p>
<p style="0px;">        <span style="blue;">print </span><span style="rgb(163, 21, 21);">'Error at unlock all server objects:'</span></p>
<p style="0px;">        <span style="blue;">exec </span>sp_OAGetErrorInfo @o_svr</p>
<p style="0px;">        <span style="blue;">goto </span>cleanup</p>
<p style="0px;">    <span style="blue;">end</span></p>
<p style="0px;"> </p>
<p style="0px;">cleanup:</p>
<p style="0px;"> </p>
<p style="0px;"><span style="blue;">if </span>@o_mds <span style="blue;">is not null exec </span>sp_OADestroy @o_mds</p>
<p style="0px;"><span style="blue;">if </span>@o_Part <span style="blue;">is not null exec </span>sp_OADestroy @o_Part</p>
<p style="0px;"><span style="blue;">if </span>@o_cube <span style="blue;">is not null exec </span>sp_OADestroy @o_cube</p>
<p style="0px;"><span style="blue;">if </span>@o_db <span style="blue;">is not null exec </span>sp_OADestroy @o_db</p>
<p style="0px;"><span style="blue;">if </span>@o_svr <span style="blue;">is not null exec </span>sp_OADestroy @o_svr</p>
</div><img src="http://weblogs.sqlteam.com/jeffs/aggbug/60746.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=POg6M"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=POg6M" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=lfPDm"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=lfPDm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=l4DFm"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=l4DFm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=uS3fm"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=uS3fm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=psTsM"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=psTsM" border="0"></img></a>
</div>]]></description>
			<content:encoded><![CDATA[Here's a simple SQL Server stored procedure that you can call to process an OLAP cube using T-SQL.  The parameters should be self-explanatory.  To me, this is a little easier and more flexible than processing using DTS packages.<br />
<br />
<div>
<p><span>create procedure </span>ProcessCube</p>
<p>    @Database <span>varchar</span>(100),</p>
<p>    @Cube <span>varchar</span>(100),</p>
<p>    @Partition <span>varchar</span>(100)  = <span>null</span>, <span>-- If NULL, process the entire Cube</span></p>
<p>    @Server <span>varchar</span>(100) = <span>'localhost'</span></p>
<p><span>as</span></p>
<p> </p>
<p><span>/* </span></p>
<p>    <span>Author:    </span>    <span>Jeff Smith</span></p>
<p>    <span>Version:    10/27/2008</span></p>
<p><span>*/</span></p>
<p> </p>
<p><span>/* variables used to store object handles */</span></p>
<p><span>declare </span>@o_svr <span>int</span>, @o_db <span>int</span>, @o_cube <span>int</span>, @o_part <span>int</span>, @o_mds <span>int</span></p>
<p><span>declare </span>@hr <span>int</span></p>
<p> </p>
<p><span>/* different cube processing options. </span> <span>This SP uses "default" */</span></p>
<p><span>declare </span>@PROCESS_DEFAULT <span>int</span></p>
<p><span>declare </span>@PROCESS_FULL <span>int</span></p>
<p><span>declare </span>@PROCESS_REFRESH_DATA <span>int</span></p>
<p> </p>
<p><span>set </span>@PROCESS_DEFAULT = 0</p>
<p><span>set </span>@PROCESS_FULL = 1</p>
<p><span>set </span>@PROCESS_REFRESH_DATA = 2</p>
<p> </p>
<p><span>-- create a DSO.Server object:</span></p>
<p><span>exec </span>@hr = sp_OACreate <span>'DSO.Server'</span>, @o_svr out</p>
<p><span>if </span>@hr &lt;&gt; 0</p>
<p>    <span>begin</span></p>
<p>        <span>print </span><span>'Error at create server:'</span></p>
<p>        <span>exec </span>sp_OAGetErrorInfo @o_svr</p>
<p>        <span>goto </span>cleanup</p>
<p>    <span>end</span></p>
<p> </p>
<p><span>-- Connect to the server:</span></p>
<p><span>exec </span>@hr = sp_OAMethod @o_svr, <span>'Connect'</span>, <span>null</span>, @Server</p>
<p><span>if </span>@hr &lt;&gt; 0</p>
<p>    <span>begin</span></p>
<p>        <span>print </span><span>'Error at connect to server:'</span></p>
<p>        <span>exec </span>sp_OAGetErrorInfo @o_svr</p>
<p>        <span>goto </span>cleanup</p>
<p>    <span>end</span></p>
<p> </p>
<p><span>-- Get the MDStores property from the Server:</span></p>
<p><span>exec </span>@hr = sp_OAGetProperty @o_svr,<span>'MDStores'</span>, @o_mds OUT</p>
<p><span>if </span>@hr &lt;&gt; 0</p>
<p>    <span>begin</span></p>
<p>        <span>print </span><span>'Error at get getting Server MDStores:'</span></p>
<p>        <span>exec </span>sp_OAGetErrorInfo @o_svr</p>
<p>        <span>goto </span>cleanup</p>
<p>    <span>end</span></p>
<p> </p>
<p><span>-- Get the database from the MDStores:</span></p>
<p><span>exec </span>@hr = sp_OAGetProperty @o_mds,<span>'Item'</span>,@o_db OUT,@Database</p>
<p><span>if </span>@hr &lt;&gt; 0</p>
<p>    <span>begin</span></p>
<p>        <span>print </span><span>'Error at get database:'</span></p>
<p>        <span>exec </span>sp_OAGetErrorInfo @o_mds</p>
<p>        <span>goto </span>cleanup</p>
<p>    <span>end</span></p>
<p> </p>
<p><span>-- get the MDStores property from the database:</span></p>
<p><span>exec </span>sp_OADestroy @o_mds</p>
<p><span>exec </span>@hr = sp_OAGetProperty @o_db,<span>'MDStores'</span>, @o_mds OUT</p>
<p><span>if </span>@hr &lt;&gt; 0</p>
<p>    <span>begin</span></p>
<p>        <span>print </span><span>'Error at get database MDStores:'</span></p>
<p>        <span>exec </span>sp_OAGetErrorInfo @o_db</p>
<p>        <span>goto </span>cleanup</p>
<p>    <span>end</span></p>
<p> </p>
<p><span>-- get the Cube from the MDStores</span></p>
<p><span>exec </span>@hr = sp_OAGetProperty @o_mds,<span>'Item'</span>,@o_cube OUT, @Cube</p>
<p><span>if </span>@hr &lt;&gt; 0</p>
<p>    <span>begin</span></p>
<p>        <span>print </span><span>'Error at get Cube:'</span></p>
<p>        <span>exec </span>sp_OAGetErrorInfo @o_mds</p>
<p>        <span>goto </span>cleanup</p>
<p>    <span>end</span></p>
<p> </p>
<p><span>if </span>@Partition <span>is null </span> <span>-- Process the entire Cube, not just a single partition</span></p>
<p>    <span>begin</span></p>
<p>        <span>exec </span>@hr = sp_OAMethod @o_cube, <span>'Process'</span>, <span>null</span>, @PROCESS_DEFAULT</p>
<p>        <span>if </span>@hr &lt;&gt; 0</p>
<p>            <span>begin</span></p>
<p>                <span>print </span><span>'Error at process Cube:'</span></p>
<p>                <span>exec </span>sp_OAGetErrorInfo @o_cube</p>
<p>                <span>goto </span>cleanup</p>
<p>            <span>end</span></p>
<p>    <span>end</span></p>
<p> </p>
<p><span>else    </span><span>-- just process the specified Partition</span></p>
<p> </p>
<p>    <span>begin</span></p>
<p>        <span>-- Get the MDStores property of the Cube:</span></p>
<p>        <span>exec </span>sp_OADestroy @o_mds</p>
<p>        <span>exec </span>@hr = sp_OAGetProperty @o_cube,<span>'MDStores'</span>, @o_mds OUT</p>
<p>        <span>if </span>@hr &lt;&gt; 0</p>
<p>            <span>begin</span></p>
<p>                <span>print </span><span>'Error at get Cube MDStores:'</span></p>
<p>                <span>exec </span>sp_OAGetErrorInfo @o_cube</p>
<p>                <span>goto </span>cleanup</p>
<p>            <span>end</span></p>
<p> </p>
<p>        <span>-- </span> <span>Get the partition to process:</span></p>
<p>        <span>exec </span>@hr = sp_OAGetProperty @o_mds,<span>'Item'</span>,@o_part OUT, @Partition</p>
<p>        <span>if </span>@hr &lt;&gt; 0</p>
<p>            <span>begin</span></p>
<p>                <span>print </span><span>'Error at get Parition:'</span></p>
<p>                <span>exec </span>sp_OAGetErrorInfo @o_mds</p>
<p>                <span>goto </span>cleanup</p>
<p>            <span>end</span></p>
<p> </p>
<p>        <span>-- Process the partition:</span></p>
<p>        <span>exec </span>@hr = sp_OAMethod @o_part, <span>'Process'</span>, <span>null</span>, @PROCESS_DEFAULT</p>
<p>        <span>if </span>@hr &lt;&gt; 0</p>
<p>            <span>begin</span></p>
<p>                <span>print </span><span>'Error at process Partition:'</span></p>
<p>                <span>exec </span>sp_OAGetErrorInfo @o_part</p>
<p>                <span>goto </span>cleanup</p>
<p>            <span>end</span></p>
<p>    <span>end</span></p>
<p> </p>
<p><span>-- And unlock all objects on the server:</span></p>
<p><span>exec </span>@hr = sp_OAMethod @o_svr, <span>'UnlockAllObjects'</span></p>
<p><span>if </span>@hr &lt;&gt; 0</p>
<p>    <span>begin</span></p>
<p>        <span>print </span><span>'Error at unlock all server objects:'</span></p>
<p>        <span>exec </span>sp_OAGetErrorInfo @o_svr</p>
<p>        <span>goto </span>cleanup</p>
<p>    <span>end</span></p>
<p> </p>
<p>cleanup:</p>
<p> </p>
<p><span>if </span>@o_mds <span>is not null exec </span>sp_OADestroy @o_mds</p>
<p><span>if </span>@o_Part <span>is not null exec </span>sp_OADestroy @o_Part</p>
<p><span>if </span>@o_cube <span>is not null exec </span>sp_OADestroy @o_cube</p>
<p><span>if </span>@o_db <span>is not null exec </span>sp_OADestroy @o_db</p>
<p><span>if </span>@o_svr <span>is not null exec </span>sp_OADestroy @o_svr</p>
</div><img src="http://weblogs.sqlteam.com/jeffs/aggbug/60746.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=POg6M"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=POg6M" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=lfPDm"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=lfPDm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=l4DFm"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=l4DFm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=uS3fm"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=uS3fm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=psTsM"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=psTsM" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.sqlfrenzy.com/2008/10/processing-an-olap-cube-with-a-t-sql-stored-procedure/feed/</wfw:commentRss>
		</item>
		<item>
		<title>I&#8217;m back &#8230; with some news!</title>
		<link>http://www.sqlfrenzy.com/2008/10/im-back-with-some-news/</link>
		<comments>http://www.sqlfrenzy.com/2008/10/im-back-with-some-news/#comments</comments>
		<pubDate>Thu, 16 Oct 2008 20:04:35 +0000</pubDate>
		<dc:creator>Jeff Smith</dc:creator>
		
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://weblogs.sqlteam.com/jeffs/archive/2008/10/16/60734.aspx</guid>
		<description><![CDATA[I apologize for not posting any new content in quite some time, but now I am back and will soon start posting on a (hopefully!) regular basis once again.<br />
<br />
The reason for my hiatus was a pretty good one, though: I recently became a father with the birth of my son Benjamin on October 2, 2008!   He is doing great and already knows that he should always do his formatting at the client and never within the database.  He's a natural!<br />
<br />
As if that wasn't enough, I also found out recently I have been named as a <span style="bold;">2009 SQL Server MVP</span>!  Now I can <span style="italic;">finally </span>get a decent seat at a restaurant by pulling the "do you know who I am?" routine!  In all seriousness, though, I am very honored and excited by the award and I intend to live up to it in 2009 by continuing to update this blog and help users out in the <a href="void(0);/*1224187349496*/">SQLTeam forums</a>.<br />
<br />
Thank you to everyone who reads this blog and participates in the discussions, and I promise more updates are coming soon.  If you have any specific topics or ideas for a blog post that you think I may be able to cover effectively, just <a href="void(0);/*1224187234283*/">let me know</a>.<br />
<br />
Best Regards,<br />
<br />
Jeff Smith<br />
SQL Server MVP<br />
 <br />
(Hey -- I like the way that looks!)<img src="http://weblogs.sqlteam.com/jeffs/aggbug/60734.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=urtiM"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=urtiM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=Bvigm"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=Bvigm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=w3ggm"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=w3ggm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=E7Lum"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=E7Lum" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=IjfLM"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=IjfLM" border="0"></img></a>
</div>]]></description>
			<content:encoded><![CDATA[I apologize for not posting any new content in quite some time, but now I am back and will soon start posting on a (hopefully!) regular basis once again.<br />
<br />
The reason for my hiatus was a pretty good one, though: I recently became a father with the birth of my son Benjamin on October 2, 2008!   He is doing great and already knows that he should always do his formatting at the client and never within the database.  He's a natural!<br />
<br />
As if that wasn't enough, I also found out recently I have been named as a <span>2009 SQL Server MVP</span>!  Now I can <span>finally </span>get a decent seat at a restaurant by pulling the "do you know who I am?" routine!  In all seriousness, though, I am very honored and excited by the award and I intend to live up to it in 2009 by continuing to update this blog and help users out in the <a href="void(0);/*1224187349496*/">SQLTeam forums</a>.<br />
<br />
Thank you to everyone who reads this blog and participates in the discussions, and I promise more updates are coming soon.  If you have any specific topics or ideas for a blog post that you think I may be able to cover effectively, just <a href="void(0);/*1224187234283*/">let me know</a>.<br />
<br />
Best Regards,<br />
<br />
Jeff Smith<br />
SQL Server MVP<br />
 <br />
(Hey -- I like the way that looks!)<img src="http://weblogs.sqlteam.com/jeffs/aggbug/60734.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=urtiM"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=urtiM" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=Bvigm"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=Bvigm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=w3ggm"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=w3ggm" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=E7Lum"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=E7Lum" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=IjfLM"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=IjfLM" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.sqlfrenzy.com/2008/10/im-back-with-some-news/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Foreign Key Constraints: SET NULL and SET DEFAULT</title>
		<link>http://www.sqlfrenzy.com/2008/08/foreign-key-constraints-set-null-and-set-default/</link>
		<comments>http://www.sqlfrenzy.com/2008/08/foreign-key-constraints-set-null-and-set-default/#comments</comments>
		<pubDate>Wed, 13 Aug 2008 13:56:49 +0000</pubDate>
		<dc:creator>Jeff Smith</dc:creator>
		
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://weblogs.sqlteam.com/jeffs/archive/2008/08/13/sql-server-set-null-set-default-foreign-key-constraints.aspx</guid>
		<description><![CDATA[Most people know about cascading updates and deletes, but did you know there are two other foreign key constraint options you can use to maintain referential integrity?<br />
<br />
Read all about them in <a href="http://www.sqlteam.com/article/using-set-null-and-set-default-with-foreign-key-constraints">my latest article over at SQLTeam.com</a>.<br />
<br />
These features, introduced with SQL Server 2005, haven't got a lot of publicity, but they can be very useful.  I just used the SET NULL option recently for the first time (inspiring me to put together an article on it) and it works great.<img src="http://weblogs.sqlteam.com/jeffs/aggbug/60682.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=1Ht61K"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=1Ht61K" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=6mrlQk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=6mrlQk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=ATEIVk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=ATEIVk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=dINnwk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=dINnwk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=UDfDxK"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=UDfDxK" border="0"></img></a>
</div>]]></description>
			<content:encoded><![CDATA[Most people know about cascading updates and deletes, but did you know there are two other foreign key constraint options you can use to maintain referential integrity?<br />
<br />
Read all about them in <a href="http://www.sqlteam.com/article/using-set-null-and-set-default-with-foreign-key-constraints">my latest article over at SQLTeam.com</a>.<br />
<br />
These features, introduced with SQL Server 2005, haven't got a lot of publicity, but they can be very useful.  I just used the SET NULL option recently for the first time (inspiring me to put together an article on it) and it works great.<img src="http://weblogs.sqlteam.com/jeffs/aggbug/60682.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=1Ht61K"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=1Ht61K" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=6mrlQk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=6mrlQk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=ATEIVk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=ATEIVk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=dINnwk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=dINnwk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=UDfDxK"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=UDfDxK" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.sqlfrenzy.com/2008/08/foreign-key-constraints-set-null-and-set-default/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Database Column Names != Report Headings</title>
		<link>http://www.sqlfrenzy.com/2008/08/database-column-names-report-headings/</link>
		<comments>http://www.sqlfrenzy.com/2008/08/database-column-names-report-headings/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 15:43:26 +0000</pubDate>
		<dc:creator>Jeff Smith</dc:creator>
		
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://weblogs.sqlteam.com/jeffs/archive/2008/08/06/60669.aspx</guid>
		<description><![CDATA[Always remember that the column names returned in a result set do not have to be the same as what you eventually output at your presentation layer.   <br />
<br />
For example, suppose you have stored procedure that accepts a @CurrentYear parameter and returns a sales variance between the current year and the previous year for each customer.  I often see programmers struggling with writing dynamic SQL to produce output like this:<br />
<br />
<div style="margin-left: 40px;"><span style="font-family: Courier New;">CustomerID   2008 Total    2007 Total   Variance</span><br style="font-family: Courier New;" />
<span style="font-family: Courier New;">----------   ----------    ----------   --------</span><br style="font-family: Courier New;" />
<span style="font-family: Courier New;">ABC          $100          $50          $50</span><br style="font-family: Courier New;" />
<span style="font-family: Courier New;">DEF          $200          $250         -$50</span><br style="font-family: Courier New;" />
</div>
<br />
That is, the names of the columns <span style="font-style: italic;">vary </span>based on the data; that is <span style="font-style: italic;">not </span>a good way to return data from your database!  A much better result set to return is simply this:<br />
<br style="font-family: Courier New;" />
<div style="margin-left: 40px;"><span style="font-family: Courier New;">CustomerID   CurrentYear   PrevYear   Variance</span><br style="font-family: Courier New;" />
<span style="font-family: Courier New;">----------   ----------    ---------  --------</span><br style="font-family: Courier New;" />
<span style="font-family: Courier New;">ABC          $100          $50        $50</span><br style="font-family: Courier New;" />
<span style="font-family: Courier New;">DEF          $200          $250       -$50</span><br />
</div>
<br />
Notice that with that set of columns, no dynamic SQL is needed, and the column names returned are always constant regardless of the value of the @CurrYear parameter.   <br />
<br />
As mentioned, the fact that your data set has columns labelled "CurrentYear" and "PrevYear" does not mean that you cannot re-label them any way that you like on your report or web page.  <br />
<br />
If your client code called the stored procedure and provided a @CurrentYear parameter, then it knows exactly what "CurrentYear" and "PrevYear" represent, and you can easily label the columns in the final result exactly as needed with simple formulas or a few lines of code.  <br />
<br />
Remember that in the world of relational database programming, table names and column names should be constant -- only the data itself should change.   Focus on returning consistently structured data from your database, and let your client applications handle the labeling of columns to make them look nice. <img src="http://weblogs.sqlteam.com/jeffs/aggbug/60669.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=qv3LSK"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=qv3LSK" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=RIe2Pk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=RIe2Pk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=j3IQNk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=j3IQNk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=cJ5WVk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=cJ5WVk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=sS99PK"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=sS99PK" border="0"></img></a>
</div>]]></description>
			<content:encoded><![CDATA[Always remember that the column names returned in a result set do not have to be the same as what you eventually output at your presentation layer.   <br />
<br />
For example, suppose you have stored procedure that accepts a @CurrentYear parameter and returns a sales variance between the current year and the previous year for each customer.  I often see programmers struggling with writing dynamic SQL to produce output like this:<br />
<br />
<div ><span >CustomerID   2008 Total    2007 Total   Variance</span><br  />
<span >----------   ----------    ----------   --------</span><br  />
<span >ABC          $100          $50          $50</span><br  />
<span >DEF          $200          $250         -$50</span><br  />
</div>
<br />
That is, the names of the columns <span >vary </span>based on the data; that is <span >not </span>a good way to return data from your database!  A much better result set to return is simply this:<br />
<br  />
<div ><span >CustomerID   CurrentYear   PrevYear   Variance</span><br  />
<span >----------   ----------    ---------  --------</span><br  />
<span >ABC          $100          $50        $50</span><br  />
<span >DEF          $200          $250       -$50</span><br />
</div>
<br />
Notice that with that set of columns, no dynamic SQL is needed, and the column names returned are always constant regardless of the value of the @CurrYear parameter.   <br />
<br />
As mentioned, the fact that your data set has columns labelled "CurrentYear" and "PrevYear" does not mean that you cannot re-label them any way that you like on your report or web page.  <br />
<br />
If your client code called the stored procedure and provided a @CurrentYear parameter, then it knows exactly what "CurrentYear" and "PrevYear" represent, and you can easily label the columns in the final result exactly as needed with simple formulas or a few lines of code.  <br />
<br />
Remember that in the world of relational database programming, table names and column names should be constant -- only the data itself should change.   Focus on returning consistently structured data from your database, and let your client applications handle the labeling of columns to make them look nice. <img src="http://weblogs.sqlteam.com/jeffs/aggbug/60669.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=qv3LSK"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=qv3LSK" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=RIe2Pk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=RIe2Pk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=j3IQNk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=j3IQNk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=cJ5WVk"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=cJ5WVk" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=sS99PK"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=sS99PK" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.sqlfrenzy.com/2008/08/database-column-names-report-headings/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How To Calculate the Number of Week Days Between two Dates</title>
		<link>http://www.sqlfrenzy.com/2008/07/how-to-calculate-the-number-of-week-days-between-two-dates/</link>
		<comments>http://www.sqlfrenzy.com/2008/07/how-to-calculate-the-number-of-week-days-between-two-dates/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 16:39:43 +0000</pubDate>
		<dc:creator>Jeff Smith</dc:creator>
		
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://weblogs.sqlteam.com/jeffs/archive/2008/07/31/week-days-between-two-dates.aspx</guid>
		<description><![CDATA[If the start date and end date are both week days, then the total number of week days in between is simply:<br />
<br />
<div style="margin-left: 40px;"><span style="font-family: Courier New;">(total difference in days) - (total difference in weeks) * 2</span><br />
</div>
<br />
or<br />
<div style="margin-left: 40px;"><span style="font-family: Courier New;"> DateDiff(dd, @start, @end) - DateDiff(ww, @start, @end)*2</span><br />
</div>
<br />
... since the DateDiff() function with weeks returns the number of week "boundaries" that are crossed; i.e., the number of weekends.<br />
<br />
If you have a table of holidays, then you can simply subtract them out as well:<br />
<br />
<div style="margin-left: 40px; font-family: Courier New;">DateDiff(dd, @start, @end) - <br />
DateDiff(ww, @start, @end)*2 -  <br />
(select count(*) from holidays where holiday_date between @start and @end)<br />
</div>
<br />
Now, what if the start day or the end day is on a weekend?  In that case, you need to define what to do in those situations in your requirements. <br />
<br />
For example, if the start date is Sunday, Nov 20th, and the end day is Monday, Nov 21st -- how many week days are between those dates? There's no universal correct answer; it could be 0, or 1, or perhaps even "undefined" (null) depending on your needs.<img src="http://weblogs.sqlteam.com/jeffs/aggbug/60662.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=qTtcBJ"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=qTtcBJ" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=21NWDj"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=21NWDj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=mARZ8j"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=mARZ8j" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=JOswYj"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=JOswYj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=tA9RHJ"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=tA9RHJ" border="0"></img></a>
</div>]]></description>
			<content:encoded><![CDATA[If the start date and end date are both week days, then the total number of week days in between is simply:<br />
<br />
<div ><span >(total difference in days) - (total difference in weeks) * 2</span><br />
</div>
<br />
or<br />
<div ><span > DateDiff(dd, @start, @end) - DateDiff(ww, @start, @end)*2</span><br />
</div>
<br />
... since the DateDiff() function with weeks returns the number of week "boundaries" that are crossed; i.e., the number of weekends.<br />
<br />
If you have a table of holidays, then you can simply subtract them out as well:<br />
<br />
<div >DateDiff(dd, @start, @end) - <br />
DateDiff(ww, @start, @end)*2 -  <br />
(select count(*) from holidays where holiday_date between @start and @end)<br />
</div>
<br />
Now, what if the start day or the end day is on a weekend?  In that case, you need to define what to do in those situations in your requirements. <br />
<br />
For example, if the start date is Sunday, Nov 20th, and the end day is Monday, Nov 21st -- how many week days are between those dates? There's no universal correct answer; it could be 0, or 1, or perhaps even "undefined" (null) depending on your needs.<img src="http://weblogs.sqlteam.com/jeffs/aggbug/60662.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=qTtcBJ"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=qTtcBJ" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=21NWDj"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=21NWDj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=mARZ8j"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=mARZ8j" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=JOswYj"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=JOswYj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=tA9RHJ"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=tA9RHJ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.sqlfrenzy.com/2008/07/how-to-calculate-the-number-of-week-days-between-two-dates/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Convert input explicitly at your client; don&#8217;t rely on the database to &#8220;figure it out&#8221;</title>
		<link>http://www.sqlfrenzy.com/2008/07/convert-input-explicitly-at-your-client-dont-rely-on-the-database-to-figure-it-out/</link>
		<comments>http://www.sqlfrenzy.com/2008/07/convert-input-explicitly-at-your-client-dont-rely-on-the-database-to-figure-it-out/#comments</comments>
		<pubDate>Thu, 24 Jul 2008 13:12:33 +0000</pubDate>
		<dc:creator>Jeff Smith</dc:creator>
		
		<category><![CDATA[Miscellaneous]]></category>

		<guid isPermaLink="false">http://weblogs.sqlteam.com/jeffs/archive/2008/07/24/60657.aspx</guid>
		<description><![CDATA[<p>A common mistake beginners make when working with SQL is trying to format their output at the database layer, as opposed to simply doing this at the presentation layer (i.e., client application, reporting tool, web page, etc).  I've covered that <a href="http://weblogs.sqlteam.com/jeffs/archive/2007/08/29/SQL-Dates-and-Times.aspx">quite</a> <a href="http://weblogs.sqlteam.com/jeffs/archive/2007/04/13/format-date-sql-server.aspx">a bit</a> in various blog posts, but I've only <a href="http://weblogs.sqlteam.com/jeffs/archive/2006/07/21/10728.aspx">touched upon</a> another similar issue which I feel is equally as important and also commonly mishandled.</p>
<p>In the <a href="http://www.sqlteam.com/forums/">SqlTeam forums</a>, I often see code that accepts DateTime input in the form of a string value (say, from a TextBox on a web form) and uploads that value to the database written like this:<span style="font-family: Courier New;"><br />
</span></p>
<p style="margin-left: 40px;"><span style="font-family: Courier New;">SqlCommand c = new SqlCommand();</span><br style="font-family: Courier New;" />
<span style="font-family: Courier New;">c.CommandText = "insert into SomeTable (DateCol) values ('" + txtDate.Text + "')";</span><br style="font-family: Courier New;" />
<span style="font-family: Courier New;">c.ExecuteNonQuery();</span><br />
</p>
<p>Now, I think that hopefully even most beginners will agree that this is bad code.   The primary issue, of course, is <a href="http://en.wikipedia.org/wiki/SQL_injection">SQL Injection</a>.  Avoiding SQL Injection is very easy to do using Parameters.  So, let's say that you rewrite this code using parameters like this:</p>
<p style="margin-left: 40px; font-family: Courier New;">SqlCommand c = new SqlCommand();<br />
c.CommandText = "insert into SomeTable (DateCol) values (@DateVal)";<br />
c.Parameters.AddWithValue("@DateVal",txtDate.Text);<br />
c.ExecuteNonQuery();</p>
<p>Looking at that, it seems we have done quite a bit better and should be happy with the code.  It works well, and no injection is possible.  But there is still an issue!  Why?  The txtDate.Text property returns a <em>string</em>, not a DateTime!   And, since we are not setting the data type of the parameter explicitly, the parameter being passed is a string (i.e., VARCHAR or NVARCHAR) value, <em>not a true DateTime value</em>.  This means that SQL Server must implicitly cast your string to a DateTime to store it in your table, and this may or may not work successfully, or as expected, depending on how the string is formatted.  </p>
<p>I've said it over and over and I'll say it again:  The concept of formatting dates should never be something that your database code should ever worry about.  The database layer should be accepting DateTime data from clients, and returning DateTime data to your clients. Where and how the client got the data before passing it to the database, or what the client does with the data in terms of formatting after receiving it from the database is of no concern to the database itself.  </p>
<p>So, we might decide that to fix this, we can simply declare the data type of the parameter explicitly:</p>
<p style="margin-left: 40px; font-family: Courier New;">SqlCommand c = new SqlCommand();<br />
c.CommandText = "insert into SomeTable (DateCol) values (@DateVal)";<br />
c.Parameters.Add("@DateVal", SqlDbType.DateTime).Value = txtDate.Text;<br />
c.ExecuteNonQuery();</p>
<p>It appears that now we are in good shape, right?  Actually -- no! There is <em>still </em>an implicit conversion happening, because we are still passing a string value -- the txtDate.Text property -- to the parameter, not a true DateTime!</p>
<p>Let's try one more time.  How can we avoid these implicit conversions?  The answer that question is always the same: Convert explicitly!  Your client application is fully capable of handling the parsing, validation, and conversion of that string to a true DateTime value, so go ahead and do it:</p>
<p style="margin-left: 40px;"><font face="Courier New">DateTime dateval = DateTime.Parse(txtDate.Text); // plus more code to validate, of course</font></p>
<p style="margin-left: 40px;"><font face="Courier New">SqlCommand c = new SqlCommand();<br />
c.CommandText = "insert into SomeTable (DateCol) values (@DateVal)";<br />
c.Parameters.Add("@DateVal", SqlDbType.DateTime).Value = dateval;<br />
c.ExecuteNonQuery();</font></p>
<p>Now we are in business!  Before we even create the SqlCommand object, we have a true DateTime value that we are ready to pass along to SQL Server.  Our SQL code doesn't need to worry about formatting, parsing, converting, or anything -- it is being passed a completely valid piece of data with the correct type.   In short, we can now be sure that whatever value we came up with for the date in our client code is <em>exactly </em>the value that will be stored in our database.  That's the idea, right?</p>
<p>So, please, don't rely on your database code to validate your input.    Don't just pass along generic string data and "hope" that at the end of the day the database can "handle it".  Eliminate the chance of anything going wrong and write your code to explicitly cast and convert and validate any and all input before the database even comes into the picture.   <br />
</p><img src="http://weblogs.sqlteam.com/jeffs/aggbug/60657.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=p2i7bJ"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=p2i7bJ" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=GNs6ej"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=GNs6ej" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=KEcpIj"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=KEcpIj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=FJnMij"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=FJnMij" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=FkiShJ"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=FkiShJ" border="0"></img></a>
</div>]]></description>
			<content:encoded><![CDATA[<p>A common mistake beginners make when working with SQL is trying to format their output at the database layer, as opposed to simply doing this at the presentation layer (i.e., client application, reporting tool, web page, etc).  I've covered that <a href="http://weblogs.sqlteam.com/jeffs/archive/2007/08/29/SQL-Dates-and-Times.aspx">quite</a> <a href="http://weblogs.sqlteam.com/jeffs/archive/2007/04/13/format-date-sql-server.aspx">a bit</a> in various blog posts, but I've only <a href="http://weblogs.sqlteam.com/jeffs/archive/2006/07/21/10728.aspx">touched upon</a> another similar issue which I feel is equally as important and also commonly mishandled.</p>
<p>In the <a href="http://www.sqlteam.com/forums/">SqlTeam forums</a>, I often see code that accepts DateTime input in the form of a string value (say, from a TextBox on a web form) and uploads that value to the database written like this:<span ><br />
</span></p>
<p ><span >SqlCommand c = new SqlCommand();</span><br  />
<span >c.CommandText = "insert into SomeTable (DateCol) values ('" + txtDate.Text + "')";</span><br  />
<span >c.ExecuteNonQuery();</span><br />
</p>
<p>Now, I think that hopefully even most beginners will agree that this is bad code.   The primary issue, of course, is <a href="http://en.wikipedia.org/wiki/SQL_injection">SQL Injection</a>.  Avoiding SQL Injection is very easy to do using Parameters.  So, let's say that you rewrite this code using parameters like this:</p>
<p >SqlCommand c = new SqlCommand();<br />
c.CommandText = "insert into SomeTable (DateCol) values (@DateVal)";<br />
c.Parameters.AddWithValue("@DateVal",txtDate.Text);<br />
c.ExecuteNonQuery();</p>
<p>Looking at that, it seems we have done quite a bit better and should be happy with the code.  It works well, and no injection is possible.  But there is still an issue!  Why?  The txtDate.Text property returns a <em>string</em>, not a DateTime!   And, since we are not setting the data type of the parameter explicitly, the parameter being passed is a string (i.e., VARCHAR or NVARCHAR) value, <em>not a true DateTime value</em>.  This means that SQL Server must implicitly cast your string to a DateTime to store it in your table, and this may or may not work successfully, or as expected, depending on how the string is formatted.  </p>
<p>I've said it over and over and I'll say it again:  The concept of formatting dates should never be something that your database code should ever worry about.  The database layer should be accepting DateTime data from clients, and returning DateTime data to your clients. Where and how the client got the data before passing it to the database, or what the client does with the data in terms of formatting after receiving it from the database is of no concern to the database itself.  </p>
<p>So, we might decide that to fix this, we can simply declare the data type of the parameter explicitly:</p>
<p >SqlCommand c = new SqlCommand();<br />
c.CommandText = "insert into SomeTable (DateCol) values (@DateVal)";<br />
c.Parameters.Add("@DateVal", SqlDbType.DateTime).Value = txtDate.Text;<br />
c.ExecuteNonQuery();</p>
<p>It appears that now we are in good shape, right?  Actually -- no! There is <em>still </em>an implicit conversion happening, because we are still passing a string value -- the txtDate.Text property -- to the parameter, not a true DateTime!</p>
<p>Let's try one more time.  How can we avoid these implicit conversions?  The answer that question is always the same: Convert explicitly!  Your client application is fully capable of handling the parsing, validation, and conversion of that string to a true DateTime value, so go ahead and do it:</p>
<p ><font face="Courier New">DateTime dateval = DateTime.Parse(txtDate.Text); // plus more code to validate, of course</font></p>
<p ><font face="Courier New">SqlCommand c = new SqlCommand();<br />
c.CommandText = "insert into SomeTable (DateCol) values (@DateVal)";<br />
c.Parameters.Add("@DateVal", SqlDbType.DateTime).Value = dateval;<br />
c.ExecuteNonQuery();</font></p>
<p>Now we are in business!  Before we even create the SqlCommand object, we have a true DateTime value that we are ready to pass along to SQL Server.  Our SQL code doesn't need to worry about formatting, parsing, converting, or anything -- it is being passed a completely valid piece of data with the correct type.   In short, we can now be sure that whatever value we came up with for the date in our client code is <em>exactly </em>the value that will be stored in our database.  That's the idea, right?</p>
<p>So, please, don't rely on your database code to validate your input.    Don't just pass along generic string data and "hope" that at the end of the day the database can "handle it".  Eliminate the chance of anything going wrong and write your code to explicitly cast and convert and validate any and all input before the database even comes into the picture.   <br />
</p><img src="http://weblogs.sqlteam.com/jeffs/aggbug/60657.aspx" width="1" height="1" /><div class="feedflare">
<a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=p2i7bJ"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=p2i7bJ" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=GNs6ej"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=GNs6ej" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=KEcpIj"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=KEcpIj" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=FJnMij"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=FJnMij" border="0"></img></a> <a href="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?a=FkiShJ"><img src="http://feeds.feedburner.com/~f/JeffsSqlServerWeblog?i=FkiShJ" border="0"></img></a>
</div>]]></content:encoded>
			<wfw:commentRss>http://www.sqlfrenzy.com/2008/07/convert-input-explicitly-at-your-client-dont-rely-on-the-database-to-figure-it-out/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
