<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/" version="2.0">
    <channel>
        <title>Jeff's SQL Server Blog</title>
        <link>http://weblogs.sqlteam.com/jeffs/Default.aspx</link>
        <description>Random Thoughts &amp; Cartesian Products with Microsoft SQL Server</description>
        <language>en-US</language>
        <copyright>Jeff Smith</copyright>
        <generator>Subtext Version 2.5.1.0</generator>
        <image>
            <title>Jeff's SQL Server Blog</title>
            <url>http://weblogs.sqlteam.com/images/RSS2Image.gif</url>
            <link>http://weblogs.sqlteam.com/jeffs/Default.aspx</link>
            <width>77</width>
            <height>60</height>
        </image>
        <atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/JeffsSqlServerWeblog" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="jeffssqlserverweblog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">JeffsSqlServerWeblog</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><item>
            <title>Why to avoid SELECT * from tables in your Views</title>
            <category>T-SQL</category>
            <category>Techniques</category>
            <category>SQL Server 2008</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2012/05/11/why-to-avoid-select-from-tables-in-your-views.aspx</link>
            <description>&lt;font face="Courier New"&gt;
&lt;p&gt; -- clean up any messes left over from before:&lt;br /&gt;
if OBJECT_ID('AllTeams') is not null&lt;br /&gt;
 drop view AllTeams&lt;br /&gt;
go&lt;/p&gt;
&lt;p&gt;if OBJECT_ID('Teams') is not null&lt;br /&gt;
 drop table Teams&lt;br /&gt;
go&lt;/p&gt;
&lt;p&gt;-- sample table:&lt;br /&gt;
create table Teams&lt;br /&gt;
(&lt;br /&gt;
 id int primary key,&lt;br /&gt;
 City varchar(20),&lt;br /&gt;
 TeamName varchar(20)&lt;br /&gt;
)&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt;-- sample data:&lt;br /&gt;
insert into Teams (id, City, TeamName )&lt;br /&gt;
select 1,'Boston','Red Sox' union all&lt;br /&gt;
select 2,'New York','Yankees'&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt;create view AllTeams&lt;br /&gt;
as&lt;br /&gt;
 select * from Teams&lt;/p&gt;
&lt;p&gt;go&lt;/p&gt;
&lt;p&gt;select * from AllTeams &lt;/p&gt;
&lt;p&gt;--Results:&lt;br /&gt;
--&lt;br /&gt;
--id          City                 TeamName&lt;br /&gt;
------------- -------------------- --------------------&lt;br /&gt;
--1           Boston               Red Sox&lt;br /&gt;
--2           New York             Yankees&lt;/p&gt;
&lt;p&gt;&lt;br /&gt;
-- Now, add a new column to the Teams table:&lt;/p&gt;
&lt;p&gt;alter table Teams&lt;br /&gt;
add League varchar(10)&lt;br /&gt;
go&lt;/p&gt;
&lt;p&gt;-- put some data in there:&lt;br /&gt;
update Teams&lt;br /&gt;
set League='AL'&lt;/p&gt;
&lt;p&gt;-- run it again&lt;/p&gt;
&lt;p&gt;select * from AllTeams &lt;/p&gt;
&lt;p&gt;--Results:&lt;br /&gt;
--&lt;br /&gt;
--id          City                 TeamName&lt;br /&gt;
------------- -------------------- --------------------&lt;br /&gt;
--1           Boston               Red Sox&lt;br /&gt;
--2           New York             Yankees&lt;/p&gt;
&lt;p&gt;-- Notice that League is not displayed!&lt;/p&gt;
&lt;p&gt;-- Here's an even worse scenario, when the table gets altered in ways beyond adding columns:&lt;br /&gt;
drop table Teams &lt;br /&gt;
go&lt;/p&gt;
&lt;p&gt;-- recreate table putting the League column before the City:&lt;br /&gt;
-- (i.e., simulate re-ordering and/or inserting a column)&lt;br /&gt;
create table Teams&lt;br /&gt;
(&lt;br /&gt;
 id int primary key,&lt;br /&gt;
 League varchar(10),&lt;br /&gt;
 City varchar(20),&lt;br /&gt;
 TeamName varchar(20)&lt;br /&gt;
)&lt;br /&gt;
go&lt;/p&gt;
&lt;p&gt;-- put in some data:&lt;br /&gt;
insert into Teams (id,League,City,TeamName)&lt;br /&gt;
select 1,'AL','Boston','Red Sox' union all&lt;br /&gt;
select 2,'AL','New York','Yankees'&lt;/p&gt;
&lt;p&gt;-- Now, Select again for our view:&lt;br /&gt;
select * from AllTeams &lt;/p&gt;
&lt;p&gt;--Results:&lt;br /&gt;
--&lt;br /&gt;
--id          City       TeamName&lt;br /&gt;
------------- ---------- --------------------&lt;br /&gt;
--1           AL         Boston&lt;br /&gt;
--2           AL         New York&lt;/p&gt;
&lt;p&gt;-- The column labeled "City" in the View is actually the League, and the column labelled TeamName is actually the City!&lt;/p&gt;
&lt;p&gt;go&lt;br /&gt;
-- clean up:&lt;br /&gt;
drop view AllTeams&lt;br /&gt;
drop table Teams &lt;br /&gt;
&lt;/p&gt;
&lt;/font&gt;&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/61412.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=X6Z2qVnSMfQ:P6Rtf_uMCBE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=X6Z2qVnSMfQ:P6Rtf_uMCBE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=X6Z2qVnSMfQ:P6Rtf_uMCBE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=X6Z2qVnSMfQ:P6Rtf_uMCBE:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=X6Z2qVnSMfQ:P6Rtf_uMCBE:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=X6Z2qVnSMfQ:P6Rtf_uMCBE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=X6Z2qVnSMfQ:P6Rtf_uMCBE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=X6Z2qVnSMfQ:P6Rtf_uMCBE:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2012/05/11/why-to-avoid-select-from-tables-in-your-views.aspx</guid>
            <pubDate>Fri, 11 May 2012 14:10:51 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2012/05/11/why-to-avoid-select-from-tables-in-your-views.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/61412.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/61412.aspx</trackback:ping>
        </item>
        <item>
            <title>How to calculate Median in SQL Server</title>
            <category>T-SQL</category>
            <category>Techniques</category>
            <category>SQL Server 2005</category>
            <category>GROUP BY</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2010/08/30/sql-server-calculate-median.aspx</link>
            <description>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.&lt;br /&gt;
&lt;br /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;-- sample table:&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;create table People&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;(&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    Person varchar(1) primary key,&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    City varchar(10),&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    Age int&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;go&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;-- with some sample data:&lt;/span&gt;&lt;br /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;insert into People &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;select 'A','Boston',23 union all  -- odd #&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;select 'B','Boston',43 union all&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;select 'C','Boston',29 union all&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;select 'D','Chicago',15 union all -- single #&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;select 'E','NY',12 union all  -- even #&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;select 'F','NY',55 union all&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;select 'G','NY',57 union all&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;select 'H','NY',61&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;go&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;-- here's our query, showing median age per city:&lt;/span&gt;&lt;br /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;select city,&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    AVG(age) as MedianAge&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;from &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;(&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    select City, Person, Age, &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        ROW_NUMBER() over (partition by City order by Age ASC) as AgeRank,&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        COUNT(*) over (partition by City) as CityCount&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    from&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        People&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;) x&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;where&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    x.AgeRank in (x.CityCount/2+1, (x.CityCount+1)/2)    &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;group by&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    x.City     &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;go&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;-- clean it all up&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;drop table People&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;&lt;span style="font-family: Arial;"&gt;And here's the result:&lt;/span&gt;&lt;/p&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;&lt;br /&gt;
city       MedianAge&lt;br /&gt;
---------- -----------&lt;br /&gt;
Boston     29&lt;br /&gt;
Chicago    15&lt;br /&gt;
NY         56&lt;br /&gt;
&lt;br /&gt;
(3 row(s) affected)&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;p&gt;Simply remove "City" from the SELECT clause and the GROUP BY clause to get the median age for all.  &lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;There may be more efficient tricks out there, but this is certainly the shortest and simplest technique I am aware of.&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/61191.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:g5tkhHzC6qU:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:g5tkhHzC6qU:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Yl_4zzkdk6g:g5tkhHzC6qU:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:g5tkhHzC6qU:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Yl_4zzkdk6g:g5tkhHzC6qU:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:g5tkhHzC6qU:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Yl_4zzkdk6g:g5tkhHzC6qU:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Yl_4zzkdk6g:g5tkhHzC6qU:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2010/08/30/sql-server-calculate-median.aspx</guid>
            <pubDate>Mon, 30 Aug 2010 04:00:00 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2010/08/30/sql-server-calculate-median.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/61191.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/61191.aspx</trackback:ping>
        </item>
        <item>
            <title>An interesting take on "Stored Procedures" (link)</title>
            <category>Humor</category>
            <category>Links</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2009/11/16/61054.aspx</link>
            <description>Another great post SQL-related post over at &lt;a href="http://thedailywtf.com"&gt;TheDailyWTF&lt;/a&gt; regarding a, umm, "creative" use of "Stored Procedures":&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://thedailywtf.com/Articles/For-the-Ease-of-Maintenance.aspx"&gt;http://thedailywtf.com/Articles/For-the-Ease-of-Maintenance.aspx&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/61054.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:e9-FNbSVXYg:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:e9-FNbSVXYg:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RptS5omfvbQ:e9-FNbSVXYg:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:e9-FNbSVXYg:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RptS5omfvbQ:e9-FNbSVXYg:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:e9-FNbSVXYg:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RptS5omfvbQ:e9-FNbSVXYg:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RptS5omfvbQ:e9-FNbSVXYg:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2009/11/16/61054.aspx</guid>
            <pubDate>Mon, 16 Nov 2009 18:42:45 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2009/11/16/61054.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/61054.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/61054.aspx</trackback:ping>
        </item>
        <item>
            <title>Programming is like dreaming?</title>
            <category>Links</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2009/11/02/61035.aspx</link>
            <description>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:&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.independentdeveloper.com/archive/2009/03/17/programming-is-like-a-dream"&gt;Programming is like a dream&lt;/a&gt;&lt;br /&gt;&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/61035.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:W_rWodiCvrw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:W_rWodiCvrw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=jc0HtIULhGw:W_rWodiCvrw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:W_rWodiCvrw:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=jc0HtIULhGw:W_rWodiCvrw:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:W_rWodiCvrw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=jc0HtIULhGw:W_rWodiCvrw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=jc0HtIULhGw:W_rWodiCvrw:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2009/11/02/61035.aspx</guid>
            <pubDate>Mon, 02 Nov 2009 19:52:38 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2009/11/02/61035.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/61035.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/61035.aspx</trackback:ping>
        </item>
        <item>
            <title>Is it just me ...</title>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2009/10/27/61031.aspx</link>
            <description>... or is about time I got back to some blogging?&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/61031.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:H9t5URjqpHI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:H9t5URjqpHI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=it5higQ-UX8:H9t5URjqpHI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:H9t5URjqpHI:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=it5higQ-UX8:H9t5URjqpHI:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:H9t5URjqpHI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=it5higQ-UX8:H9t5URjqpHI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=it5higQ-UX8:H9t5URjqpHI:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2009/10/27/61031.aspx</guid>
            <pubDate>Tue, 27 Oct 2009 16:50:54 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2009/10/27/61031.aspx#feedback</comments>
            <slash:comments>7</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/61031.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/61031.aspx</trackback:ping>
        </item>
        <item>
            <title>Processing an OLAP cube with a T-SQL Stored Procedure</title>
            <category>OLAP</category>
            <category>T-SQL</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2008/10/27/process-olap-cube-with-t-sql.aspx</link>
            <description>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.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="background: white none repeat scroll 0% 0%; font-family: Verdana; font-size: 8pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial; margin-left: 40px;"&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;create procedure &lt;/span&gt;ProcessCube&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    @Database &lt;span style="color: blue;"&gt;varchar&lt;/span&gt;(100),&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    @Cube &lt;span style="color: blue;"&gt;varchar&lt;/span&gt;(100),&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    @Partition &lt;span style="color: blue;"&gt;varchar&lt;/span&gt;(100)  = &lt;span style="color: blue;"&gt;null&lt;/span&gt;, &lt;span style="color: green;"&gt;-- If NULL, process the entire Cube&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    @Server &lt;span style="color: blue;"&gt;varchar&lt;/span&gt;(100) = &lt;span style="color: rgb(163, 21, 21);"&gt;'localhost'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;as&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;/* &lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: green;"&gt;Author:    &lt;/span&gt;    &lt;span style="color: green;"&gt;Jeff Smith&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: green;"&gt;Version:    10/27/2008&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;*/&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;/* variables used to store object handles */&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;declare &lt;/span&gt;@o_svr &lt;span style="color: blue;"&gt;int&lt;/span&gt;, @o_db &lt;span style="color: blue;"&gt;int&lt;/span&gt;, @o_cube &lt;span style="color: blue;"&gt;int&lt;/span&gt;, @o_part &lt;span style="color: blue;"&gt;int&lt;/span&gt;, @o_mds &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;declare &lt;/span&gt;@hr &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;/* different cube processing options. &lt;/span&gt; &lt;span style="color: green;"&gt;This SP uses "default" */&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;declare &lt;/span&gt;@PROCESS_DEFAULT &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;declare &lt;/span&gt;@PROCESS_FULL &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;declare &lt;/span&gt;@PROCESS_REFRESH_DATA &lt;span style="color: blue;"&gt;int&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;set &lt;/span&gt;@PROCESS_DEFAULT = 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;set &lt;/span&gt;@PROCESS_FULL = 1&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;set &lt;/span&gt;@PROCESS_REFRESH_DATA = 2&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;-- create a DSO.Server object:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;exec &lt;/span&gt;@hr = sp_OACreate &lt;span style="color: rgb(163, 21, 21);"&gt;'DSO.Server'&lt;/span&gt;, @o_svr out&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@hr &amp;lt;&amp;gt; 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;print &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;'Error at create server:'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OAGetErrorInfo @o_svr&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;goto &lt;/span&gt;cleanup&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;-- Connect to the server:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;exec &lt;/span&gt;@hr = sp_OAMethod @o_svr, &lt;span style="color: rgb(163, 21, 21);"&gt;'Connect'&lt;/span&gt;, &lt;span style="color: blue;"&gt;null&lt;/span&gt;, @Server&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@hr &amp;lt;&amp;gt; 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;print &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;'Error at connect to server:'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OAGetErrorInfo @o_svr&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;goto &lt;/span&gt;cleanup&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;-- Get the MDStores property from the Server:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;exec &lt;/span&gt;@hr = sp_OAGetProperty @o_svr,&lt;span style="color: rgb(163, 21, 21);"&gt;'MDStores'&lt;/span&gt;, @o_mds OUT&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@hr &amp;lt;&amp;gt; 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;print &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;'Error at get getting Server MDStores:'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OAGetErrorInfo @o_svr&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;goto &lt;/span&gt;cleanup&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;-- Get the database from the MDStores:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;exec &lt;/span&gt;@hr = sp_OAGetProperty @o_mds,&lt;span style="color: rgb(163, 21, 21);"&gt;'Item'&lt;/span&gt;,@o_db OUT,@Database&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@hr &amp;lt;&amp;gt; 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;print &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;'Error at get database:'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OAGetErrorInfo @o_mds&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;goto &lt;/span&gt;cleanup&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;-- get the MDStores property from the database:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OADestroy @o_mds&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;exec &lt;/span&gt;@hr = sp_OAGetProperty @o_db,&lt;span style="color: rgb(163, 21, 21);"&gt;'MDStores'&lt;/span&gt;, @o_mds OUT&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@hr &amp;lt;&amp;gt; 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;print &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;'Error at get database MDStores:'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OAGetErrorInfo @o_db&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;goto &lt;/span&gt;cleanup&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;-- get the Cube from the MDStores&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;exec &lt;/span&gt;@hr = sp_OAGetProperty @o_mds,&lt;span style="color: rgb(163, 21, 21);"&gt;'Item'&lt;/span&gt;,@o_cube OUT, @Cube&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@hr &amp;lt;&amp;gt; 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;print &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;'Error at get Cube:'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OAGetErrorInfo @o_mds&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;goto &lt;/span&gt;cleanup&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@Partition &lt;span style="color: blue;"&gt;is null &lt;/span&gt; &lt;span style="color: green;"&gt;-- Process the entire Cube, not just a single partition&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;@hr = sp_OAMethod @o_cube, &lt;span style="color: rgb(163, 21, 21);"&gt;'Process'&lt;/span&gt;, &lt;span style="color: blue;"&gt;null&lt;/span&gt;, @PROCESS_DEFAULT&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;if &lt;/span&gt;@hr &amp;lt;&amp;gt; 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;print &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;'Error at process Cube:'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OAGetErrorInfo @o_cube&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;goto &lt;/span&gt;cleanup&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;else    &lt;/span&gt;&lt;span style="color: green;"&gt;-- just process the specified Partition&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: green;"&gt;-- Get the MDStores property of the Cube:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OADestroy @o_mds&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;@hr = sp_OAGetProperty @o_cube,&lt;span style="color: rgb(163, 21, 21);"&gt;'MDStores'&lt;/span&gt;, @o_mds OUT&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;if &lt;/span&gt;@hr &amp;lt;&amp;gt; 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;print &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;'Error at get Cube MDStores:'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OAGetErrorInfo @o_cube&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;goto &lt;/span&gt;cleanup&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: green;"&gt;-- &lt;/span&gt; &lt;span style="color: green;"&gt;Get the partition to process:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;@hr = sp_OAGetProperty @o_mds,&lt;span style="color: rgb(163, 21, 21);"&gt;'Item'&lt;/span&gt;,@o_part OUT, @Partition&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;if &lt;/span&gt;@hr &amp;lt;&amp;gt; 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;print &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;'Error at get Parition:'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OAGetErrorInfo @o_mds&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;goto &lt;/span&gt;cleanup&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: green;"&gt;-- Process the partition:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;@hr = sp_OAMethod @o_part, &lt;span style="color: rgb(163, 21, 21);"&gt;'Process'&lt;/span&gt;, &lt;span style="color: blue;"&gt;null&lt;/span&gt;, @PROCESS_DEFAULT&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;if &lt;/span&gt;@hr &amp;lt;&amp;gt; 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;print &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;'Error at process Partition:'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OAGetErrorInfo @o_part&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;                &lt;span style="color: blue;"&gt;goto &lt;/span&gt;cleanup&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;            &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: green;"&gt;-- And unlock all objects on the server:&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;exec &lt;/span&gt;@hr = sp_OAMethod @o_svr, &lt;span style="color: rgb(163, 21, 21);"&gt;'UnlockAllObjects'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@hr &amp;lt;&amp;gt; 0&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;begin&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;print &lt;/span&gt;&lt;span style="color: rgb(163, 21, 21);"&gt;'Error at unlock all server objects:'&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;exec &lt;/span&gt;sp_OAGetErrorInfo @o_svr&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;        &lt;span style="color: blue;"&gt;goto &lt;/span&gt;cleanup&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;    &lt;span style="color: blue;"&gt;end&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;cleanup:&lt;/p&gt;
&lt;p style="margin: 0px;"&gt; &lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@o_mds &lt;span style="color: blue;"&gt;is not null exec &lt;/span&gt;sp_OADestroy @o_mds&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@o_Part &lt;span style="color: blue;"&gt;is not null exec &lt;/span&gt;sp_OADestroy @o_Part&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@o_cube &lt;span style="color: blue;"&gt;is not null exec &lt;/span&gt;sp_OADestroy @o_cube&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@o_db &lt;span style="color: blue;"&gt;is not null exec &lt;/span&gt;sp_OADestroy @o_db&lt;/p&gt;
&lt;p style="margin: 0px;"&gt;&lt;span style="color: blue;"&gt;if &lt;/span&gt;@o_svr &lt;span style="color: blue;"&gt;is not null exec &lt;/span&gt;sp_OADestroy @o_svr&lt;/p&gt;
&lt;/div&gt;&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60746.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=WyHNddoZo1U:rhZAKBUxeYo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=WyHNddoZo1U:rhZAKBUxeYo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=WyHNddoZo1U:rhZAKBUxeYo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=WyHNddoZo1U:rhZAKBUxeYo:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=WyHNddoZo1U:rhZAKBUxeYo:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=WyHNddoZo1U:rhZAKBUxeYo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=WyHNddoZo1U:rhZAKBUxeYo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=WyHNddoZo1U:rhZAKBUxeYo:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2008/10/27/process-olap-cube-with-t-sql.aspx</guid>
            <pubDate>Mon, 27 Oct 2008 19:26:05 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2008/10/27/process-olap-cube-with-t-sql.aspx#feedback</comments>
            <slash:comments>12</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60746.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60746.aspx</trackback:ping>
        </item>
        <item>
            <title>I'm back ... with some news!</title>
            <category>Miscellaneous</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2008/10/16/60734.aspx</link>
            <description>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.&lt;br /&gt;
&lt;br /&gt;
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!&lt;br /&gt;
&lt;br /&gt;
As if that wasn't enough, I also found out recently I have been named as a &lt;span style="font-weight: bold;"&gt;2009 SQL Server MVP&lt;/span&gt;!  Now I can &lt;span style="font-style: italic;"&gt;finally &lt;/span&gt;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 &lt;a href="http://www.sqlteam.com/forums"&gt;SQLTeam forums&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
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 &lt;a href="http://weblogs.sqlteam.com/jeffs/contact.aspx"&gt;let me know&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Best Regards,&lt;br /&gt;
&lt;br /&gt;
Jeff Smith&lt;br /&gt;
SQL Server MVP&lt;br /&gt;
 &lt;br /&gt;
(Hey -- I like the way that looks!)&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60734.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RN9ugEkWTbE:4vJqlyLvzQ0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RN9ugEkWTbE:4vJqlyLvzQ0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RN9ugEkWTbE:4vJqlyLvzQ0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RN9ugEkWTbE:4vJqlyLvzQ0:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RN9ugEkWTbE:4vJqlyLvzQ0:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RN9ugEkWTbE:4vJqlyLvzQ0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=RN9ugEkWTbE:4vJqlyLvzQ0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=RN9ugEkWTbE:4vJqlyLvzQ0:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2008/10/16/60734.aspx</guid>
            <pubDate>Thu, 16 Oct 2008 20:04:35 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2008/10/16/60734.aspx#feedback</comments>
            <slash:comments>9</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60734.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60734.aspx</trackback:ping>
        </item>
        <item>
            <title>SQL Server 2005 Foreign Key Constraints: SET NULL and SET DEFAULT</title>
            <category>Database Design</category>
            <category>Joins/Relations</category>
            <category>Links</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2008/08/13/sql-server-set-null-set-default-foreign-key-constraints.aspx</link>
            <description>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?&lt;br /&gt;
&lt;br /&gt;
Read all about them in &lt;a href="http://www.sqlteam.com/article/using-set-null-and-set-default-with-foreign-key-constraints"&gt;my latest article over at SQLTeam.com&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
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.&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60682.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=OfF3Sao4srY:SsIn8pxFnCI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=OfF3Sao4srY:SsIn8pxFnCI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=OfF3Sao4srY:SsIn8pxFnCI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=OfF3Sao4srY:SsIn8pxFnCI:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=OfF3Sao4srY:SsIn8pxFnCI:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=OfF3Sao4srY:SsIn8pxFnCI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=OfF3Sao4srY:SsIn8pxFnCI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=OfF3Sao4srY:SsIn8pxFnCI:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2008/08/13/sql-server-set-null-set-default-foreign-key-constraints.aspx</guid>
            <pubDate>Wed, 13 Aug 2008 13:56:49 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2008/08/13/sql-server-set-null-set-default-foreign-key-constraints.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60682.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60682.aspx</trackback:ping>
        </item>
        <item>
            <title>Database Column Names != Report Headings</title>
            <category>Efficiency</category>
            <category>Report Writing</category>
            <category>Techniques</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2008/08/06/60669.aspx</link>
            <description>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.   &lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;CustomerID   2008 Total    2007 Total   Variance&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;----------   ----------    ----------   --------&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;ABC          $100          $50          $50&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;DEF          $200          $250         -$50&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;/div&gt;
&lt;br /&gt;
That is, the names of the columns &lt;span style="font-style: italic;"&gt;vary &lt;/span&gt;based on the data; that is &lt;span style="font-style: italic;"&gt;not &lt;/span&gt;a good way to return data from your database!  A much better result set to return is simply this:&lt;br /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;CustomerID   CurrentYear   PrevYear   Variance&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;----------   ----------    ---------  --------&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;ABC          $100          $50        $50&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;DEF          $200          $250       -$50&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
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.   &lt;br /&gt;
&lt;br /&gt;
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.  &lt;br /&gt;
&lt;br /&gt;
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.  &lt;br /&gt;
&lt;br /&gt;
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. &lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60669.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=EANkcwAJVqQ:0-cKz8D754Y:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=EANkcwAJVqQ:0-cKz8D754Y:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=EANkcwAJVqQ:0-cKz8D754Y:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=EANkcwAJVqQ:0-cKz8D754Y:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=EANkcwAJVqQ:0-cKz8D754Y:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=EANkcwAJVqQ:0-cKz8D754Y:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=EANkcwAJVqQ:0-cKz8D754Y:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=EANkcwAJVqQ:0-cKz8D754Y:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2008/08/06/60669.aspx</guid>
            <pubDate>Wed, 06 Aug 2008 15:43:26 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2008/08/06/60669.aspx#feedback</comments>
            <slash:comments>3</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60669.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60669.aspx</trackback:ping>
        </item>
        <item>
            <title>How To Calculate the Number of Week Days Between two Dates</title>
            <category>T-SQL</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2008/07/31/week-days-between-two-dates.aspx</link>
            <description>If the start date and end date are both week days, then the total number of week days in between is simply:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;(total difference in days) - (total difference in weeks) * 2&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
or&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt; DateDiff(dd, @start, @end) - DateDiff(ww, @start, @end)*2&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
... since the DateDiff() function with weeks returns the number of week "boundaries" that are crossed; i.e., the number of weekends.&lt;br /&gt;
&lt;br /&gt;
If you have a table of holidays, then you can simply subtract them out as well:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px; font-family: Courier New;"&gt;DateDiff(dd, @start, @end) - &lt;br /&gt;
DateDiff(ww, @start, @end)*2 -  &lt;br /&gt;
(select count(*) from holidays where holiday_date between @start and @end)&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
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.&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60662.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=ePvsVrWKlrQ:7MzoUVU3FFs:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=ePvsVrWKlrQ:7MzoUVU3FFs:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=ePvsVrWKlrQ:7MzoUVU3FFs:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=ePvsVrWKlrQ:7MzoUVU3FFs:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=ePvsVrWKlrQ:7MzoUVU3FFs:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=ePvsVrWKlrQ:7MzoUVU3FFs:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=ePvsVrWKlrQ:7MzoUVU3FFs:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=ePvsVrWKlrQ:7MzoUVU3FFs:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2008/07/31/week-days-between-two-dates.aspx</guid>
            <pubDate>Thu, 31 Jul 2008 16:39:43 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2008/07/31/week-days-between-two-dates.aspx#feedback</comments>
            <slash:comments>13</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60662.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60662.aspx</trackback:ping>
        </item>
        <item>
            <title>Convert input explicitly at your client; don't rely on the database to "figure it out"</title>
            <category>.NET (C# / VB)</category>
            <category>DateTime Data</category>
            <category>Techniques</category>
            <category>T-SQL</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2008/07/24/60657.aspx</link>
            <description>&lt;p&gt;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 &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/08/29/SQL-Dates-and-Times.aspx"&gt;quite&lt;/a&gt; &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/04/13/format-date-sql-server.aspx"&gt;a bit&lt;/a&gt; in various blog posts, but I've only &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2006/07/21/10728.aspx"&gt;touched upon&lt;/a&gt; another similar issue which I feel is equally as important and also commonly mishandled.&lt;/p&gt;
&lt;p&gt;In the &lt;a href="http://www.sqlteam.com/forums/"&gt;SqlTeam forums&lt;/a&gt;, 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:&lt;span style="font-family: Courier New;"&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/p&gt;
&lt;p style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;SqlCommand c = new SqlCommand();&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;c.CommandText = "insert into SomeTable (DateCol) values ('" + txtDate.Text + "')";&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;c.ExecuteNonQuery();&lt;/span&gt;&lt;br /&gt;
&lt;/p&gt;
&lt;p&gt;Now, I think that hopefully even most beginners will agree that this is bad code.   The primary issue, of course, is &lt;a href="http://en.wikipedia.org/wiki/SQL_injection"&gt;SQL Injection&lt;/a&gt;.  Avoiding SQL Injection is very easy to do using Parameters.  So, let's say that you rewrite this code using parameters like this:&lt;/p&gt;
&lt;p style="margin-left: 40px; font-family: Courier New;"&gt;SqlCommand c = new SqlCommand();&lt;br /&gt;
c.CommandText = "insert into SomeTable (DateCol) values (@DateVal)";&lt;br /&gt;
c.Parameters.AddWithValue("@DateVal",txtDate.Text);&lt;br /&gt;
c.ExecuteNonQuery();&lt;/p&gt;
&lt;p&gt;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 &lt;em&gt;string&lt;/em&gt;, 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, &lt;em&gt;not a true DateTime value&lt;/em&gt;.  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.  &lt;/p&gt;
&lt;p&gt;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.  &lt;/p&gt;
&lt;p&gt;So, we might decide that to fix this, we can simply declare the data type of the parameter explicitly:&lt;/p&gt;
&lt;p style="margin-left: 40px; font-family: Courier New;"&gt;SqlCommand c = new SqlCommand();&lt;br /&gt;
c.CommandText = "insert into SomeTable (DateCol) values (@DateVal)";&lt;br /&gt;
c.Parameters.Add("@DateVal", SqlDbType.DateTime).Value = txtDate.Text;&lt;br /&gt;
c.ExecuteNonQuery();&lt;/p&gt;
&lt;p&gt;It appears that now we are in good shape, right?  Actually -- no! There is &lt;em&gt;still &lt;/em&gt;an implicit conversion happening, because we are still passing a string value -- the txtDate.Text property -- to the parameter, not a true DateTime!&lt;/p&gt;
&lt;p&gt;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:&lt;/p&gt;
&lt;p style="margin-left: 40px;"&gt;&lt;font face="Courier New"&gt;DateTime dateval = DateTime.Parse(txtDate.Text); // plus more code to validate, of course&lt;/font&gt;&lt;/p&gt;
&lt;p style="margin-left: 40px;"&gt;&lt;font face="Courier New"&gt;SqlCommand c = new SqlCommand();&lt;br /&gt;
c.CommandText = "insert into SomeTable (DateCol) values (@DateVal)";&lt;br /&gt;
c.Parameters.Add("@DateVal", SqlDbType.DateTime).Value = dateval;&lt;br /&gt;
c.ExecuteNonQuery();&lt;/font&gt;&lt;/p&gt;
&lt;p&gt;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 &lt;em&gt;exactly &lt;/em&gt;the value that will be stored in our database.  That's the idea, right?&lt;/p&gt;
&lt;p&gt;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.   &lt;br /&gt;
&lt;/p&gt;&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60657.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=yTEyFOamv_I:mobsZWzbQBg:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=yTEyFOamv_I:mobsZWzbQBg:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=yTEyFOamv_I:mobsZWzbQBg:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=yTEyFOamv_I:mobsZWzbQBg:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=yTEyFOamv_I:mobsZWzbQBg:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=yTEyFOamv_I:mobsZWzbQBg:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=yTEyFOamv_I:mobsZWzbQBg:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=yTEyFOamv_I:mobsZWzbQBg:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2008/07/24/60657.aspx</guid>
            <pubDate>Thu, 24 Jul 2008 13:12:33 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2008/07/24/60657.aspx#feedback</comments>
            <slash:comments>11</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60657.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60657.aspx</trackback:ping>
        </item>
        <item>
            <title>The MailBag --- Super-Sized Edition!  String Parsing, Crosstabs, SQL Injection, and more.</title>
            <category>Miscellaneous</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2008/07/16/60652.aspx</link>
            <description>OK, boys and girls, it's time for the &lt;a href="http://weblogs.sqlteam.com/jeffs/contact.aspx"&gt;mailbag&lt;/a&gt;!  There's lots of stuff to cover, so let's get to it!&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;---&lt;br /&gt;
&lt;br /&gt;
Greg E &lt;/span&gt;writes:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;Hello Jeff,&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;br style="font-style: italic;" /&gt;
&lt;span style="font-style: italic;"&gt;I just found your blog and wanted to know if you could point me in the right direction or possibly toss me a solution.&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;br style="font-style: italic;" /&gt;
&lt;span style="font-style: italic;"&gt;I am looking at a badly formed telelphone number column in a MS SQL Server db. Entries contain '(555) 555-1212' or '555.555.1212, etc. Do you know how I would go about stripping out unwanted characters from the telephone number?&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;br style="font-style: italic;" /&gt;
&lt;span style="font-style: italic;"&gt;Thanks for the brain cycles.&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;/div&gt;
&lt;br /&gt;
Greg -- A simple UDF should do the trick for you.  For example, something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;create function NumbersOnly(@txt varchar(1000))&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;returns varchar(100)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;as&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;begin&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    declare @i int&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    declare @ret varchar(100)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    select @i = 1, @ret = ''&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    while (@i &amp;lt;= len(@txt))&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        select @ret = @ret + case when substring(@txt,@i,1) like '[0-9]' &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                                  then substring(@txt,@i,1) else '' &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;                             end,&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;               @i = @i + 1&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    return @ret&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;end&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;/div&gt;
&lt;br /&gt;
With that, you can write something like this:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;select ID, dbo.NumbersOnly(PhoneColumn) as PhoneNumbersOnly&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;from YourTable&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;/div&gt;
&lt;br /&gt;
Over at &lt;a href="http://www.sqlteam.com/forums/forum.asp?FORUM_ID=11"&gt;SQLTeam's script library forum&lt;/a&gt;, there is a &lt;a href="http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=79083"&gt;thread&lt;/a&gt; with a bunch of parsing functions that you may find useful if your needs are more complex.&lt;br /&gt;
&lt;br /&gt;
And, in case you missed it, be sure to read &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2008/06/11/golden-rule-of-data-manipulation.aspx"&gt;this post&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
In response to my &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/06/26/60240.aspx"&gt;blog post on passing arrays to stored procedures&lt;/a&gt;, &lt;span style="font-weight: bold;"&gt;Juan &lt;/span&gt;writes:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;I know is not the right solution, but I have to say it for the sake of completeness of the discussion: if the amount of items in your "array parameter" is limited (say, for example 5 or 10 items), you can always use optional parameters (i.e. assign them to null when declaring them in the SP), then insert them in a temp table or do whatever you want with them, without using dynamic,nor xml, nor string manipulation.&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
Great point, something I missed in my article entirely.  Sometimes, it may make sense to declare @Val1, @Val2, ... @ValN parameters if there aren't too many and there's a clearly defined limit.  Thanks for bringing that up, Juan.  The simplest solution is usually the best, and in some cases that's probably all you need.  You still have clearly defined parameters with strong typing and no parsing, and those are the main issues with CSV parameters that I wanted to avoid.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt; Marc &lt;/span&gt;writes:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px; font-style: italic;"&gt;We have three tables.  They all share the same "type" of primary key: let's say ActivityCode.  I need to pull data using an ActivityCode, but there is a catch.  If table 1 has the data, I want to use it.  If table 2 has the data and Table 1 does not I want to use Table 2.  If table 3 has the data and Table 1 and Table 2 does not, I want to use that.  The ActivityCode can be found in both Table 1 and Table 2.  Once I determine which table i am using I will need to do several other inner and/or outer joins with other tables.  I am using JDBC.  I want to be able to do this using a single SQL statement, but I am willing to use multiple statements if it makes more sense.  I just need to keep it to a single transaction under JDBC.&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
Marc -- I think what you are looking for is described &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/04/03/Conditional-Joins.aspx"&gt;here&lt;/a&gt;.  The key is to OUTER JOIN to all of your tables, and then use a CASE expression to determine which of those joined tables has the data you need.&lt;br /&gt;
&lt;br /&gt;
--&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Mary &lt;/span&gt;writes:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;I struggled with a thorny SQL problem all day yesterday and found your &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/04/30/60192.aspx"&gt;post on set based thinking&lt;/a&gt; very helpful.  I needed to write an update query that updated a table with many records with the same key from a table with the key and the corresponding new value.  The table with new values didn't exist - I had to derive it from a different table showing the key, new value and date (the new value changed over time.)&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;br style="font-style: italic;" /&gt;
&lt;span style="font-style: italic;"&gt;Your observations that one needs to break the problem down into its simplest components helped me realize something else. &lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;br style="font-style: italic;" /&gt;
&lt;span style="font-style: italic;"&gt;I made the classic rookie error of grabbing some code that did a similar type of update and try to hack it into my solution.  When I finally realized I was going in the wrong direction (because my solution was getting messier and messier), I went back to the beginning.&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;br style="font-style: italic;" /&gt;
&lt;span style="font-style: italic;"&gt;I defined the problem in its simplest terms and learned I could do a simple "update  A set A.value = B.value from A join B on B.key = A.key" .&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;br style="font-style: italic;" /&gt;
&lt;span style="font-style: italic;"&gt;I didn't realize I could update from but once the problem was simply defined a quick question to one of our senior engineers resulted in a quick answer leading to an elegant solution.  The whole thing was completed in less than half an hour.&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;br style="font-style: italic;" /&gt;
&lt;span style="font-style: italic;"&gt;The moral of the story:  Define the problem first!  Don't even think about syntax until you have written a clear, concise spec from the problem just defined.  Then if you find yourself spending an inordinate amount of time and/or the solution seems too messy or seems to run too long - google or talk to your colleagues.&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;br style="font-style: italic;" /&gt;
&lt;span style="font-style: italic;"&gt;Thanks for a great blog; your post made me realize it's more about how we think than throwing code at the problem - the code should be the last thing!&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
Thanks, Mary!  I'm glad I would be of assistance.  The "moral" that you wrote says it all. 90% of programming isn't writing code at all, it is simply defining what your code will do -- and that's always the hardest part!  &lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
In &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/09/10/group-by-month-sql.aspx"&gt;response to my post on grouping by month&lt;/a&gt;, &lt;span style="font-weight: bold;"&gt;Mark &lt;/span&gt;writes:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px; font-style: italic;"&gt;I'm so close! I've tried all the things in this article, but can't seem to do what I want to do. I've been tearing my hair out for days! Here's what I'm trying to do.&lt;br /&gt;
&lt;br /&gt;
Basically I need a sql procedure that looks at an invoicing table that totals amounts by month/year and quarter at the same time. Here's how my table looks:&lt;br /&gt;
&lt;br /&gt;
Project ID  Date      Amount&lt;br /&gt;
1                3/11/08    10.00&lt;br /&gt;
1                4/18/08    10.00&lt;br /&gt;
1                6/22/08    10.00&lt;br /&gt;
2                3/01/08    10.00&lt;br /&gt;
2                9/15/08    10.00&lt;br /&gt;
&lt;br /&gt;
I would like the output to have dynamic columns, so an output may look like:&lt;br /&gt;
Project ID  Jan'08  Feb'08  Mar'08  Q1'08  Apr'08  May'08  Jun'08  Q2'08  Jul'08  Aug'08  Sep'08  Q3'08&lt;br /&gt;
1              0.00    0.00      10.00    10.00  10.00    0.00      10.00  20.00  0.00    0.00      0.00      0.00&lt;br /&gt;
2              0.00    0.00      10.00    10.00  0.00      0.00      0.00    0.00  0.00    0.00      10.00    10.00&lt;br /&gt;
&lt;br /&gt;
I would like the query to know if there was no value in Jan &amp;amp;amp; Feb'08, but still list all the months in Q1.&lt;br /&gt;
&lt;br /&gt;
I'm not opposed to using a calendar table, but would like to try to avoid it if possible.&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;Any help would be greatly appreciated!&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
Hi Mark -- First off, never be afraid to use a calendar table!  There is nothing hacky or unusual or tricky about them, they can make your life much easier, your code much shorter, and everything much more efficient.  If grouping by month or some other time period is important to your reporting,  then defining those months in a permanent, nicely indexed table makes perfect sense.&lt;br /&gt;
&lt;br /&gt;
In this case, though, since you are outputting one &lt;span style="font-style: italic;"&gt;column &lt;/span&gt;per month for a single year, I recommend to simply use CASE expressions to "cross tab" your data.  You can alias your columns as M1,M2,M3...M12 and Q1-Q4 so that no matter what year you are running the report for, your columns will be consistently named, and you can let your presentation layer handle outputting nice column headers with the current year/month for each one.&lt;br /&gt;
&lt;br /&gt;
So, all you really need is something like this:&lt;br /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt; select projectID, Y as [Year],&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   sum(case when m=1 then amount else 0 end) as M1,&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   sum(case when m=2 then amount else 0 end) as M2,&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;   sum(case when m=3 then amount else 0 end) as M3,&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;   sum(case when m in (1,2,3) then amount else 0 end) as Q1,&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   ...&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   sum(case when m=12 then amount else 0 end) as M12,&lt;br /&gt;
  sum(amount) as Total&lt;br style="font-family: Courier New;" /&gt;
&lt;/span&gt; &lt;span style="font-family: Courier New;"&gt; from&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt; (&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   select projectID, Amount, DatePart(Month, [Date]) as M, DatePart(Year, [Date]) as Y&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;  from YourTable&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;   where [Date] &amp;gt;= @StartDate and Date &amp;lt; @EndDate&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt; ) x&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
Of course, you'd define @StartDate and @endDate as '01-01-2008' and '01-01-2009', respectively.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
In &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/12/13/select-distinct-order-by-error.aspx#39101"&gt;this comment&lt;/a&gt;, &lt;span style="font-weight: bold;"&gt;Stewy &lt;/span&gt;writes:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px; font-style: italic;"&gt;I have an issue with both DISTINCT and GROUP BY.&lt;br /&gt;
&lt;br /&gt;
The issue is that using either one, the results comes back ordered as if using order by.&lt;br /&gt;
&lt;br /&gt;
I need the unique results in the order they are in the database. How can I do this? Thanks&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
Stewy -- Relational databases have no obligation to store data in any specific order, or to keep track of the order that things were entered, or to return things "as they are in the database."  There is no such thing as getting data out "the way it is stored" because a relational database may move or re-order data temporarily to efficiently execute a query depending on indexes available.   You must always explicitly specify how you want your results using an ORDER BY clause.  If you want to keep track of the order that you added data to a table, you should have a "timestamp" column that records the exact moment each row was added via a DEFAULT value or a trigger.  Or, at the very least, you can use an IDENTITY.  Then, you can simply order by that column.  This is a very important concept to understand when working with relational databases.  Things are returned based on the data itself, not based on physical storage characteristics.  I hope this helps.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
In response to &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2006/07/21/10728.aspx"&gt;Always Use Parameters&lt;/a&gt;,&lt;span style="font-weight: bold;"&gt; Karuna &lt;/span&gt;writes:&lt;br /&gt;
&lt;br /&gt;
&lt;span id="apnlCommentsWrapper$RBS_Holder"&gt;&lt;span id="apnlCommentsWrapper" ajaxcall="async"&gt;&lt;/span&gt;&lt;/span&gt;
&lt;div style="margin-left: 40px; font-style: italic;"&gt;Hi Jeff,&lt;br /&gt;
Just wondering if I build the Sql in Stored Procedure (Dynamic Sql) based on the parameters passed to stored proc, will it still be a possible candidate for Sql Injection? Basically I want to build the Sql in the stored procedure instead of doing it in .Net code as displayed in the article.&lt;br /&gt;
&lt;br /&gt;
Dim cm As New SqlCommand("", YourConnection)&lt;br /&gt;
cm.CommandText = "DELETE FROM YourTable WHERE ID=@ID "&lt;br /&gt;
cm.Parameters.Add("@ID", SqlDbType.Int).Value = ID&lt;br /&gt;
&lt;br /&gt;
If Name &amp;lt;&amp;gt; "" Then&lt;br /&gt;
cm.CommandText &amp;amp;= " And Name=@name"&lt;br /&gt;
cm.Parameters.Add("@Name", SqlDbType.VarChar).Value = Name&lt;br /&gt;
End If&lt;br /&gt;
If TranDate &amp;lt;&amp;gt; DateTime.MinValue Then&lt;br /&gt;
cm.CommandText &amp;amp;= " And TranDate = @TranDate"&lt;br /&gt;
cm.Parameters.Add("@TranDate", SqlDbType.DateTime).Value = TranDate&lt;br /&gt;
&lt;a id="Comments_ascx_CommentList_ctl18_NameLink" title="karuna" target="_blank"&gt;&lt;br /&gt;
&lt;/a&gt;&lt;/div&gt;
&lt;span id="apnlCommentsWrapper$RBS_Holder"&gt;&lt;span id="apnlCommentsWrapper" ajaxcall="async"&gt;
&lt;div class="postfoot"&gt; 				&lt;/div&gt;
&lt;/span&gt;&lt;/span&gt;Hi Karuna -- you are absolutely 100% safe from SQL Injection by doing this.  Remember, SQL Injection is not about genereal SQL concatenation or about building a SQL statement dynamically.  It only can happen when you &lt;span style="font-weight: bold;"&gt;concatenate user input into a SQL string and execute it.  &lt;/span&gt;If you put together a big SQL statement via concatenation but you only incorporate user input via parameters, there's no need for scrubbing data or worrying in any way about SQL Injection -- it will never happen, under any circumstance.  &lt;br /&gt;
&lt;br /&gt;
Avoiding SQL Injection is the easiest thing in the world -- simply do things the easy and correct way and you'll never need to worry about it.   It's like if there was a big controversy in the news about thousands of people crashing their cars because they are driving them with decorative tin foil covering their windshields, and asking the experts "how can we solve this crisis?"   Should we cut holes in the tin foil, or add mirrors, or incorporate a camera and a tv monitor?  Uh .. no.  You should just take the tin foil off of your windshield and do things the easy, simple and correct way and don't make things over complicated.   That's basically what this whole SQL Injection thing is about -- bad programmers doing stupid things when all they need to do is write decent code the &lt;span style="font-weight: bold;"&gt;easy way&lt;/span&gt; -- simply by using parameters.&lt;br /&gt;
&lt;br /&gt;
---&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Gocs &lt;/span&gt;writes:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;I have tried to compute the number of hours based on the datetime in MS SQL 2005.  However, I am not sure the hours is correct.  Do you have any idea on how to do it correctly?&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
Gocs -- I think you really need to read &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2008/05/13/question-needed-not-answer.aspx"&gt;this&lt;/a&gt; very carefully.  I'll be waiting!&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60652.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=SjFLPJCDuQg:BNN2-GNI3b8:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=SjFLPJCDuQg:BNN2-GNI3b8:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=SjFLPJCDuQg:BNN2-GNI3b8:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=SjFLPJCDuQg:BNN2-GNI3b8:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=SjFLPJCDuQg:BNN2-GNI3b8:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=SjFLPJCDuQg:BNN2-GNI3b8:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=SjFLPJCDuQg:BNN2-GNI3b8:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=SjFLPJCDuQg:BNN2-GNI3b8:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2008/07/16/60652.aspx</guid>
            <pubDate>Wed, 16 Jul 2008 20:32:51 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2008/07/16/60652.aspx#feedback</comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60652.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60652.aspx</trackback:ping>
        </item>
        <item>
            <title>The Golden Rule of Data Manipulation</title>
            <category>Database Design</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2008/06/11/golden-rule-of-data-manipulation.aspx</link>
            <description>&lt;span style="font-weight: bold;"&gt;Introduction&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
There is a very simple rule when it comes to storing (and returning) data, which I see violated all the time, making life so much more complicated for everyone involved.  In case you haven't noticed, that's a common theme I discuss here on this blog -- different ways programmers make life more difficult for themselves, instead of simply following good practices and doing things the easy way.  This is yet another example of that situation.&lt;br /&gt;
&lt;br /&gt;
The "Golden Rule of Data Manipulation" is a simple, but important rule that you should always follow when designing a database,  writing database code, or really writing &lt;span style="font-style: italic;"&gt;any &lt;/span&gt;application code at all for that matter:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;"It is always easier and more flexible to combine data elements rather than to break them apart"&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
In other words: Concatenation is easy. Parsing is hard.  Often, &lt;span style="font-style: italic;"&gt;very&lt;/span&gt; hard -- or even impossible depending on the data.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Problems with Parsing&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
It is amazing how often I see people struggling with "difficult SQL problems" such as:&lt;br /&gt;
&lt;ul&gt;
    &lt;li&gt;Working with CSV lists of values in a single column, such as "1,3,56,2"&lt;/li&gt;
    &lt;li&gt;Breaking out a FirstName/MiddleName/LastName/Suffix from a single "Name" column&lt;/li&gt;
    &lt;li&gt;Parsing address strings into City/State/ZIP, or Number/Street/Unit&lt;/li&gt;
    &lt;li&gt;Parsing Phone Numbers to get just an area code, or to take different phone formats and present them all uniformly&lt;/li&gt;
    &lt;li&gt;Figuring out how to calculate the Day,Month, and/or Year from different string values such as "23-Jan-08", "2008-02", "20070303", "03032007"&lt;/li&gt;
&lt;/ul&gt;
And on and on it goes....&lt;br /&gt;
&lt;br /&gt;
Now, sometimes you inherit or import data that needs to be parsed -- that's a fact of life.  You've got to figure out how to do it, and the key in those cases is to accept that because the data itself is essentially random, nothing you can write will perfectly work 100% of the time on all of it.  Often, the best you can do is handle most of the data, and then do some manual clean up.  &lt;br /&gt;
&lt;br /&gt;
Parsing strings can be a very difficult task for any programmer, and the challenge isn't writing the code, it's coming up with the algorithm (another common theme on this blog).  Consider my new favorite example of why parsing a single Name column into a First/Middle/Last is not as easy as it seems:&lt;br /&gt;
&lt;br /&gt;
    &lt;span style="font-style: italic;"&gt;Oscar De La Hoya&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
How would your algorithm parse that one?  Never mind prefixes such as "Dr." and suffixes such as "Jr."!  &lt;br /&gt;
&lt;br /&gt;
Please don't interpret what I am saying as a programming challenge -- I understand that it is possible to write long code with a list of exceptions or rules and have that algorithm work pretty well in most cases.  The point is that writing that algorithm is a lot of work, running it will be inefficient, and it will never be exact because the data itself that is being processed is essentially random.  It's just like the old saying: "garbage in, garbage out".  Still one of my favorites, after all these years, and it still applies!&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;A Data Model that requires Parsing = A Poor Data Model&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
So, we need to accept that sometimes you've got to parse data like this.  And that's OK; it happens, it can be done, even if some manual work is often involved.&lt;br /&gt;
&lt;br /&gt;
However, there's no excuse when you design your database, your SQL code, or applications so that free-form data &lt;span style="font-style: italic;"&gt;must &lt;/span&gt;be parsed, when you can simply design it correctly in the first place and store your data already broken out into the smallest possible units with the correct data types.&lt;br /&gt;
&lt;br /&gt;
If breaking out a contact's name into First, Last, Middle, etc is important to your application, then you should force the point of data entry to accept input broken out into those columns.  The same goes for phone numbers, addresses, and so on.  Any time you have the option of accepting  input as clean, short, raw, segments of data you should always do it.   Once you have data at that smaller resolution, it is trivial to combine it any way that you want for presentation, formatting, filtering, and so on.&lt;br /&gt;
&lt;br /&gt;
It may seem like overkill to break out a phone number into 4 columns:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;AreaCode&lt;br /&gt;
Exchange&lt;br /&gt;
Number&lt;br /&gt;
Extension&lt;br /&gt;
&lt;/div&gt;
    &lt;br /&gt;
And, in fact, it might be more complicated than that if you need to deal with international phone numbers.  You may look at your tables, and your code, and even the UI that accepts these fields and think "that is way too precise and unnecessary, breaking out phone numbers like this sure makes things complicated!"&lt;br /&gt;
&lt;br /&gt;
But by doing this, and only accepting user input that follows precise rules of what is allowed in these fields, and storing each of them in their own column, you can now easily and efficiently:&lt;br /&gt;
&lt;ol&gt;
    &lt;li&gt;Sort these numbers any way you want, without worrying about extra characters like parenthesis or dashes, or leading 1s, messing things up&lt;/li&gt;
    &lt;li&gt;Filter quickly on an area code without the need to use LIKE, and again worrying about extra characters getting in the way&lt;/li&gt;
    &lt;li&gt;Present the phone number quickly and easily any way you want without any parsing, be it as 123.123.1345 x123 or "(123) 123-1345 extension 123", or anything you want. &lt;/li&gt;
    &lt;li&gt;Validate your phone numbers, ensuring you have all the necessary parts and they are the proper length, without worrying about parsing strings&lt;/li&gt;
&lt;/ol&gt;
Considering doing any of those things if your data is stored in random strings like:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;1-123-124-1234&lt;br /&gt;
(123) 124-1234&lt;br /&gt;
123-124-1234, ex. 123&lt;br /&gt;
123.124.1234 x123&lt;br /&gt;
(123)124.1234 ext. 123&lt;br /&gt;
1 123 124 1234 123&lt;br /&gt;
&lt;/div&gt;
    &lt;br /&gt;
and so on ...  Not so easy in that case, just as parsing simple "Name" columns into First/Last, or addresses into  Number/Street/Unit is not so easy as well.  &lt;br /&gt;
&lt;br /&gt;
Again, this is not a programming challenge -- I am sure it can done. (In fact, phone numbers are generally the easiest because you can usually just ignore anything other than digits.)  Most of us have done it before.  But designing something in such a way that parsing is &lt;span style="font-style: italic;"&gt;required&lt;/span&gt; to do simple filtering, sorting, or formatting, is a &lt;span style="font-style: italic;"&gt;bad design&lt;/span&gt;.  &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;It's Not About the UI&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;As I wrote &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/06/05/60223.aspx"&gt;here&lt;/a&gt;, you should never think "I want to display phone numbers like 123.123.1234, so I should store them and return them that way."  You should always think "How can I break this down into small, concrete parts that are easily validated and easy to combine any way I want at any time?"&lt;br /&gt;
&lt;br /&gt;
So, what if you need fine detail when storing addresses, but you don't want your UI to present Street Number, Street Name, Unit Type, Unit Number as different data entry fields for usability or aesthetic reasons?  That's fine, but that doesn't mean you should not set up your database properly.  Your UI can certainly still present that one single "Address" text box for the user to fill out, parse that text &lt;span style="font-style: italic;"&gt;at data entry&lt;/span&gt;, show the user the parsed result in multiple fields, and ask "Please verify for your address" or something along those lines.   Then, if not, the user can tweak the results and save it.  If you do things along those lines, and focus on getting the data parsed and stored correctly at the earliest point possible, every other part of your code will be that much more efficient.&lt;br /&gt;
&lt;br /&gt;
All of this applies not only to data storage, but to how data is returned and passed between tiers as well.  Again, if you just return separate columns to your client application, instead of focusing on making them "look nice" in your database code by returning nothing but long, "pre-formatted" strings, your client can simply concatenate and format those columns any way it needs.  And, different clients can format that same database output in different ways -- all without ever altering any database code!  &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Conclusion&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In short, remember that writing concatenation is easy, efficient, and exact.  Writing a parsing routine, on the other hand, is often &lt;span style="font-style: italic;"&gt;none &lt;/span&gt;of those things.   You may not always be able to control the design of the data you are working with, but be sure that when you can, you do it right.  If you find yourself using lots of LIKE expressions, or string parsing for simple data retrieval operations, something is wrong.   Time to fix up your database and your code, store the parsed and validated data permanently, and make things easier and cleaner for everyone.&lt;br /&gt;
&lt;br /&gt;
Whether you are designing a schema, writing a SELECT, or writing code in any other programming language, remember that the Golden Rule of Data Manipulation &lt;span style="font-style: italic;"&gt;always &lt;/span&gt;applies.  Accept this rule, learn from it, and practice it, and you might be surprised to find that programming isn't quite as hard as you thought it was.&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60624.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=pgTYYBIRmK4:3sodCSdnZoc:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=pgTYYBIRmK4:3sodCSdnZoc:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=pgTYYBIRmK4:3sodCSdnZoc:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=pgTYYBIRmK4:3sodCSdnZoc:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=pgTYYBIRmK4:3sodCSdnZoc:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=pgTYYBIRmK4:3sodCSdnZoc:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=pgTYYBIRmK4:3sodCSdnZoc:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=pgTYYBIRmK4:3sodCSdnZoc:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2008/06/11/golden-rule-of-data-manipulation.aspx</guid>
            <pubDate>Wed, 11 Jun 2008 15:48:41 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2008/06/11/golden-rule-of-data-manipulation.aspx#feedback</comments>
            <slash:comments>9</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60624.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60624.aspx</trackback:ping>
        </item>
        <item>
            <title>The Joy of Blog Feedback</title>
            <category>Miscellaneous</category>
            <category>Humor</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2008/06/06/the-joy-of-blog-feedback.aspx</link>
            <description>&lt;span style="font-weight: bold;"&gt;Introduction&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
I have been writing my little blog here for some time now, and my favorite part of doing this is of course the feedback.  It's always great to hear from the readers, to have mistakes corrected, to debate various topics and techniques, and to learn a lot about SQL and the various topics I discuss here.  &lt;br /&gt;
&lt;br /&gt;
At this point, I have received over 1,700 comments over the years, and while all of them are truly appreciated, I have noticed that unfortunately many of the, uh, less helpful comments do seem to consistently fall neatly into various categories.  &lt;br /&gt;
&lt;br /&gt;
Let's take a look at an example of a simple, typical blog post and some of the responses that often come back.  If you write a blog of your own, or often read the feedback from other blogs, many of these may seem familiar to you.  &lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;&lt;br /&gt;
A Typical Blog Post&lt;/span&gt;&lt;br style="font-weight: bold;" /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px; font-style: italic;"&gt;Today, I have a simple tip for beginner SQL programmers.  When writing a SELECT, you can add a WHERE clause to filter the results that are returned.&lt;br /&gt;
&lt;br /&gt;
For example, to only return rows for CustomerID 345, you can write:&lt;br /&gt;
&lt;br /&gt;
SELECT ...&lt;br /&gt;
FROM YourTable&lt;br /&gt;
WHERE CustomerID = 345&lt;br /&gt;
&lt;br /&gt;
As you can see, it is very simple. You can use any boolean expression to filter the results as needed. Try it out!  If you have any questions, let me know.&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;Some Typical Responses&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The subtle blog spammer (that you initially mistake for a nice complement):&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;Very helpful site! Good advice!  From Joe at www.some-random-site-that-has-nothing-to-do-with-sql.com&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
The person who doesn't seem to get it:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;Ummm ... what good does this do when I want to sort? &lt;/span&gt;&lt;span style="font-style: italic;"&gt;You should fix the code.&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
The person that &lt;span style="font-style: italic;"&gt;really &lt;/span&gt;doesn't get it:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;The problem with that is it will only return results for one customer. &lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
The person that somehow takes away the exact &lt;span style="font-style: italic;"&gt;opposite &lt;/span&gt;of what you wrote:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;I disagree, this will not return all customers other than 345 and this is definitely not something for advanced SQL Programmers, it's probably better for beginners.&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
The script kiddie (who just wants to cut and paste your code, not read or learn anything):&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;LOL, that doesnt even run 4 me!  I get errorz that sez "YourTable" does not exist!  Plz help!!  thnx!&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
The very clichéd, mindless "anti-Microsoft ranter":&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;You only have to use WHERE clauses because Bill Gates wants more $$, you are a shill!! Micro$oft sucks, you should use an iPhone for this!  MySQL automagically filters results for you!&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
The "skimmer" (who just skims the post missing most of it):&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;Nice, but is there any way to filter for just one customer? &lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
The "repeater" (who just repeats what you've already written):&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;A better solution is to write WHERE CustomerID = 345, it works better.  It is also fast because less rows are returned.  Using WHERE is a good way to filter a SELECT.&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;/div&gt;
&lt;br /&gt;
The "know-it-all complainer":&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;That is the stupidest advice I ever read, why would you want to ever do this? Just use a parameter, or an ORM tool-- this will not scale!  &lt;/span&gt;&lt;span style="font-style: italic;"&gt;I sure hope CustomerID isn't a VARCHAR -- then you have an implicit conversion happening, your indexes are shot, your server will overheat, and your wife will leave you for your mechanic.  Also, 345 is too large if CustomerID is a tinyint.&lt;/span&gt;&lt;br style="font-style: italic;" /&gt;
&lt;/div&gt;
&lt;br /&gt;
The random, unrelated question asker:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;Good advice. Thnx.  How to insert into the table?&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
The "misunderstander":&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px; font-style: italic;"&gt;If I add this to all of my scripts, only data for one customer will ever be returned.  I am not sure this is a good idea. Also, this code will not work in Java and doesn't follow the HTML 4.0 specification.  &lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
The very rare polite and helpful typo alerter:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;Hey there, you have a typo in the first sentence -- should be "filter", not "fitler"! Just letting you know, thanks for a great post!&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
The much more common typo alerter:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;You wrote FITLER not FILTER, your an idiot!! if you cannot write English how can you write SQL ???   Learn to spell!&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;span style="font-weight: bold;"&gt;Summary&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Please, don't misunderstand, I mean this all in good fun.  I love feedback, and please, keep it coming.  It's what makes this and every other blog a fun place to visit. &lt;span style="font-style: italic;"&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;In fact, I realize that I left out the most annoying feedback of all!  That's right, the Thin-Skinned, Overly-Defensive Blog Author Who Feels the Need to Respond to Everything:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-style: italic;"&gt;Did you even read what I wrote? I did not say that.  And, yes, I did spell "monkey" wrong, so sue me!  Remind me to fire my editor.... or maybe I should refund your subscription fee?  Oh, wait, this blog is free!  So what the heck are you complaining about?  Why don't you go bother some MySQL blogger?  I hear they usually write at a 5th grade level which is probably more appropriate for your intellect. Jerk!&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
Yeah, comments like those are &lt;span style="font-style: italic;"&gt;definitely &lt;/span&gt;the worst of all!  Thank &lt;span style="font-style: italic;"&gt;you &lt;/span&gt;for putting up with &lt;span style="font-style: italic;"&gt;my &lt;/span&gt;feedback, now that I think of it! &lt;span style="font-style: italic;"&gt;&lt;/span&gt;&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60620.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=QM9HTr5QD3I:kZO8FZ-tE00:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=QM9HTr5QD3I:kZO8FZ-tE00:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=QM9HTr5QD3I:kZO8FZ-tE00:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=QM9HTr5QD3I:kZO8FZ-tE00:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=QM9HTr5QD3I:kZO8FZ-tE00:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=QM9HTr5QD3I:kZO8FZ-tE00:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=QM9HTr5QD3I:kZO8FZ-tE00:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=QM9HTr5QD3I:kZO8FZ-tE00:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2008/06/06/the-joy-of-blog-feedback.aspx</guid>
            <pubDate>Fri, 06 Jun 2008 19:43:05 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2008/06/06/the-joy-of-blog-feedback.aspx#feedback</comments>
            <slash:comments>11</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60620.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60620.aspx</trackback:ping>
        </item>
        <item>
            <title>The Truth about "Cursor Busting" in SQL</title>
            <category>Efficiency</category>
            <category>T-SQL</category>
            <link>http://weblogs.sqlteam.com/jeffs/archive/2008/06/05/sql-server-cursor-removal.aspx</link>
            <description>Let's say you are called in to troubleshoot a stored procedure that is performing poorly.&lt;br /&gt;
&lt;br /&gt;
You dive in to investigate and this is what you find:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;create procedure ProcessProducts&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;as&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    declare @Products cursor, @ProductID int&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    set @Products = cursor for select ProductID from Products order by ProductID&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    open @Products&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    fetch next from @Products into @ProductID&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    while (@@FETCH_STATUS=0)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        begin&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;span style="font-family: Courier New;"&gt;        exec DoSomething @ProductID&lt;br /&gt;
&lt;/span&gt; &lt;span style="font-family: Courier New;"&gt;        fetch next from @Products into @ProductID&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        end&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    deallocate @Products&lt;/span&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
Ah ha! A &lt;a href="http://en.wikipedia.org/wiki/Cursor_(databases)" target="_blank"&gt;cursor&lt;/a&gt;!  It seems we have identified the bottleneck: Clearly, the performance problems are because the code is not doing things in a &lt;a href="http://weblogs.sqlteam.com/jeffs/archive/2007/04/30/60192.aspx" target="_blank"&gt;set-based&lt;/a&gt; manner, but rather by processing rows one at a time using a dreaded cursor.  This cursor is opening up the Products table, looping through the rows one at a time, and calling the "DoSomething" stored procedure for each ProductID.  As we all know, cursors are not the way to go when writing SQL code; this cursor should eliminated and replaced with a cleaner, more efficient (and more socially acceptable!) solution.&lt;br /&gt;
&lt;br /&gt;
So, how we do optimize this?  Well, a commonly suggested approach is to eliminate the CURSOR by replacing it with a WHILE loop:&lt;br /&gt;
&lt;br /&gt;
    &lt;span style="font-family: Courier New;"&gt;declare @ProductID int&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    set @ProductID = -99999&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    while (@ProductID is not null)&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        begin&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        set @ProductID = (select top 1 ProductID &lt;br /&gt;
                          from Products &lt;br /&gt;
                          where ProductID &amp;gt; @ProductID &lt;br /&gt;
                          order by ProductID asc)&lt;br /&gt;
&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        exec DoSomething @ProductID&lt;br /&gt;
&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;        end&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Instead of declaring a CURSOR to loop through the table, we now are using "set-based" code and our problems seem to be solved.  The cursor is gone, our code looks much cleaner, we've tested it and it works properly, so off to production it goes.  Another cursor has been busted!&lt;br /&gt;
&lt;br /&gt;
Right?&lt;br /&gt;
&lt;br /&gt;
Actually ... no. &lt;br /&gt;
&lt;br /&gt;
You see, eliminating cursors is &lt;span style="font-style: italic;"&gt;not about syntax&lt;/span&gt;.  It is not about searching for the word "cursor" in your code and just replacing it with a WHILE loop that does the same thing.  Optimizing and replacing cursors involves much more.  We can never optimize any cursor code until we look deeper into what exactly is happening when we "process" each of those rows.  In this case, we need to find out what that "DoSomething" procedure is actually doing.  &lt;br /&gt;
&lt;br /&gt;
Suppose the DoSomething procedure is generating a report and sending an email to the "Product Manager" for each product that contains status information, and then logging this email message into a table somewhere. &lt;br /&gt;
&lt;br /&gt;
If that is the case, what have we just gained by replacing our CURSOR?   &lt;br /&gt;
&lt;br /&gt;
Honestly -- not much,  if anything at all.  Because of the task at hand, we may very well need to process rows in the Product table one-by-one to send our emails and generate the report, and the bottleneck here is not the cursor code at all, but rather the report generation and maybe sending the email.   Eliminating the cursor code probably gains us nothing here.  If you need to process rows one at a time, go ahead and use a cursor -- that's what they are there for!   Replacing a perfectly fine, simple cursor with a WHILE loop might even make your code longer, or more confusing, or even less efficient depending on circumstances.  &lt;br /&gt;
&lt;br /&gt;
For example, what if we need to process the Products ordered by Region, then Product Name, for whatever reason.  Our cursor code is simple:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;set @Products = cursor for &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    select ProductID &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    from Products &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;/span&gt;&lt;span style="font-family: Courier New;"&gt;    order by Region, ProductName&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;
&lt;br /&gt;
All that we needed  to change was our ORDER BY clause.  Now, how would we write this as a WHILE loop?  Is it possible?  Sure.  Will it be as simple and clean as using a cursor?  No, it won't. (Though ROW_COUNT() makes this much easier than it used to be)&lt;br /&gt;
&lt;br /&gt;
Now, I am not here to say that cursors are "good", but if you &lt;span style="font-style: italic;"&gt;really need&lt;/span&gt; to process rows one by one, go ahead and proudly use a cursor.   Replacing cursors isn't about processing rows one-by-one in a &lt;span style="font-style: italic;"&gt;different way&lt;/span&gt; (i.e., using a WHILE loop instead), it is about &lt;span style="font-style: italic;"&gt;not &lt;/span&gt;processing rows one-by-one &lt;span style="font-style: italic;"&gt;at all&lt;/span&gt;!    &lt;br /&gt;
&lt;br /&gt;
Let's consider another scenario: What if the DoSomething stored procedure is checking to see if the Product's ExpireDate is greater than today's date, and if so, it is updates the Status column for that Product to 'X'.&lt;br /&gt;
&lt;br /&gt;
In that situation, what have we gained by rewriting ProcessProducts without a cursor, and using a WHILE loop instead?   The answer is, once again: &lt;span style="font-style: italic;"&gt;nothing!&lt;/span&gt;  In fact, we potentially have once again made our code more confusing or even less efficient than a cursor might be!  Remember, the bottleneck isn't the cursor syntax -- it is the fact that we are processing rows one at a time.  Replacing the cursor with the WHILE loop didn't solve this problem, did it?   &lt;br /&gt;
&lt;br /&gt;
So, looking now at both of the scenarios I presented for the DoSomething stored procedure, it should be clear that we did not fix anything by replacing the cursor in either case simply by writing a WHILE loop.  If that's all you are doing, &lt;span style="font-style: italic;"&gt;don't bother replacing the cursor at all.  &lt;/span&gt;You haven't optimized anything.&lt;br /&gt;
&lt;br /&gt;
As I said before, the art of replacing a cursor is not a find-and-replace syntax change operation -- it is a fundamental change in how you process your data.  As in the Product report generation and email example, it may be that we simply &lt;span style="font-style: italic;"&gt;need &lt;/span&gt;to process rows one by one, and thus no further optimization is possible from a SQL point of view.  In situations like updating the Product table, however, we do &lt;span style="font-style: italic;"&gt;not &lt;/span&gt;need to process the rows individually -- we can do everything in one single UPDATE statement.  Thus, in order to determine how to optimize the ProcessProducts stored procedure, we needed to dig deeper into entire process as a whole, which included examining the DoSomething stored procedure and determining the full scope of exactly what this "ProcessProducts" stored procedure is doing.  &lt;br /&gt;
&lt;br /&gt;
So, if "DoSomething" is updating the Products table as specified, we now know that a good replacement for our cursor code doesn't result in a WHILE loop and calling a separate stored procedure over and over at all -- it results a true, set-based solution:&lt;br /&gt;
&lt;br /&gt;
&lt;div style="margin-left: 40px;"&gt;&lt;span style="font-family: Courier New;"&gt;create procedure ProcessProducts&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;as&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    Update Products set Status='X' where ExpireDate &amp;gt; getdate()&lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;span style="font-family: Courier New;"&gt;    &lt;/span&gt;&lt;br style="font-family: Courier New;" /&gt;
&lt;/div&gt;
And &lt;span style="font-style: italic;"&gt;THAT &lt;/span&gt;is how you optimize a cursor! No loops, no calling of another stored procedure for each row in a table, no "find-and-replace" cursor code removal.  We examined the entire &lt;span style="font-style: italic;"&gt;process, &lt;/span&gt;and rewrote the entire process, to get it done quicker and shorter and faster without cursors &lt;span style="font-style: italic;"&gt;or &lt;/span&gt;loops.  &lt;br /&gt;
&lt;br /&gt;
Always remember: Replacing a cursor isn't about rewriting your syntax, it is about redesigning your algorithm.&lt;img src="http://weblogs.sqlteam.com/jeffs/aggbug/60616.aspx" width="1" height="1" /&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Ip45g-qE7YE:8UpyI9TlCd0:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Ip45g-qE7YE:8UpyI9TlCd0:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Ip45g-qE7YE:8UpyI9TlCd0:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Ip45g-qE7YE:8UpyI9TlCd0:gIN9vFwOqvQ"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Ip45g-qE7YE:8UpyI9TlCd0:gIN9vFwOqvQ" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Ip45g-qE7YE:8UpyI9TlCd0:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?i=Ip45g-qE7YE:8UpyI9TlCd0:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?a=Ip45g-qE7YE:8UpyI9TlCd0:7Q72WNTAKBA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/JeffsSqlServerWeblog?d=7Q72WNTAKBA" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;</description>
            <dc:creator>Jeff Smith</dc:creator>
            <guid>http://weblogs.sqlteam.com/jeffs/archive/2008/06/05/sql-server-cursor-removal.aspx</guid>
            <pubDate>Thu, 05 Jun 2008 14:56:02 GMT</pubDate>
            <comments>http://weblogs.sqlteam.com/jeffs/archive/2008/06/05/sql-server-cursor-removal.aspx#feedback</comments>
            <slash:comments>14</slash:comments>
            <wfw:commentRss>http://weblogs.sqlteam.com/jeffs/comments/commentRss/60616.aspx</wfw:commentRss>
            <trackback:ping>http://weblogs.sqlteam.com/jeffs/services/trackbacks/60616.aspx</trackback:ping>
        </item>
    </channel>
</rss>
