<feed 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="http://www.w3.org/2005/Atom" xml:lang="en-US">
    <title>Graeme Reisinger</title>
    <link rel="self" type="application/xml" href="http://geekswithblogs.net/HighAltitudeCoder/Atom.aspx" />
    <subtitle type="html">Welcome to my Office.  My Other Office.</subtitle>
    <id>http://geekswithblogs.net/HighAltitudeCoder/Default.aspx</id>
    <author>
        <name>HighAltitudeCoder</name>
        <uri>http://geekswithblogs.net/HighAltitudeCoder/Default.aspx</uri>
    </author>
    <generator uri="http://subtextproject.com" version="Subtext Version 0.0.0.0">Subtext</generator>
    <updated>2011-12-16T18:51:24Z</updated>
    <entry>
        <title>The Dreaded Startup Repair Loop on Win 7</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/HighAltitudeCoder/archive/2011/02/27/dreaded-startup-repair-loop-on-win-7.aspx" />
        <id>http://geekswithblogs.net/HighAltitudeCoder/archive/2011/02/27/dreaded-startup-repair-loop-on-win-7.aspx</id>
        <published>2011-02-27T17:51:36-07:00:00</published>
        <updated>2011-04-11T19:06:47Z</updated>
        <content type="html">&lt;p&gt;For most people, upgrading to Windows 7 has been a relatively painless process. &lt;/p&gt;
&lt;p&gt;Not me.  I am in the unlucky 1% or less who had a somewhat less pleasant experience.  First, I cloned my entire hard drive onto a larger (and much faster) solid state hard drive, only experiencing minimal problems.&lt;/p&gt;
&lt;p&gt;Then, I bought the Retail version of Windows 7 Ultimate, took a deep breath and... oh yeah, I almost forgot - BACK UP THE COMPUTER.  The next morning I upgraded to Win 7 and everything seemed fine, until...&lt;/p&gt;
&lt;p&gt;I rebooted the system, the nice Windows 7 launch graphics come up, it's about to launch and AWWW, are you kidding me?!?!  Back to the BIOS splash screen?  Next comes the sequence of failure - attempt repair - unable to repair - do you want to wipe your hard drive decisions.&lt;/p&gt;
&lt;p&gt;Because I purchased the retail version, a number is provided where I could call Microsoft Tech support.  When I did, they instructed me to click "Install" from my installation CD, which did not work.  When I tried the "Upgrade" option, it reaches an impasse, saying you have a newer version of Win 7, and thus cannot Upgrade.  If you choose "Install" you willl lose everything... files, programs, EVERYTHING.  Or at least this is what it tells you.  Not willing to take the risk, I took them at their word.&lt;/p&gt;
&lt;p&gt;To make things worse, I had installed a new antivirus software application before I realized my system was unstable (Trend Micro Titanium Internet Security), and this was causing additional problems.&lt;/p&gt;
&lt;p&gt;One interesting thing, and the only saving grace as it turns out, was that my system WOULD successfully reboot into the OS if I chose to restart it, rather than shut it down.  If I chose to shut down, I would have to go through the loop again until I was given the option to restart.&lt;/p&gt;
&lt;p&gt;As it turned out, I needed to update my BIOS.  I assumed that since I had updated my BIOS a long time ago to settings that were stable under Windows Vista Ultimate x64, Win 7 would adopt the same settings and didn't expect there to be any problems.  WRONG.&lt;/p&gt;
&lt;p&gt;My BIOS had a setting to halt the boot cycle if various kinds of errors were detected.  Windows Vista didn't care about this, but forget it under Windows 7.  Needless to say, I immediately corrected that BIOS setting.  Next, there were the two separate BIOS settings: enable USB mouse and enable USB keyboard.  The only sequence of events that would work were to start my reboot process over from stratch with a hard-wired non-usb keyboard and mouse.  Whent the system booted under these settings, it didn't detect any errors due to either the mouse or keyboard, and actually booted for the first time in a long while (let me tell ya, that's an aaamazing experience after fiddling with settings for two entire weekends!)&lt;/p&gt;
&lt;p&gt;Next step: leave your old mouse and keyboard connected, but also connect your other two devices (mouse, keyboard) that use USB connections.  During the boot cycle, the operating system will not fail due to missing requirements atstartup, and it will then pick up the new drivers necessary to use your new hardware.&lt;/p&gt;
&lt;p&gt;If you think you are in the clear here, you are wrong.  The next VERY IMPORTANT step is to remember to change your settings in the BIOS upon next startup.  Specifically, you will need to change your BIOS to enable USB mouse and USB keyboard input.  If you don't, Windows will detect an incompatibility upon the next startup, and you will be stuck once again in the endless cycle of reboot/Startup Repair/reboot/Startup Repair, without ever reaching a successful boot.&lt;/p&gt;
&lt;p&gt;Here's the thing - the BIOS and the drivers registered in Win 7 need to match.  If they don't, you're going to lose another weekend worrying and fiddling, all the while wondering if you've permanently damaged your hard drive beyond repair.&lt;/p&gt;
&lt;p&gt;(Sigh).  In the end, things worked out. &lt;/p&gt;
&lt;p&gt;I must note that it is saddening to see how many posts there are out there that recommend just doing a clean install, as if it's the only option.  How many countless poor souls have lost their data, their backups, their pictures and videos, all for nothing other than the fact that the person giving advice just didn't know what to do at that point?&lt;/p&gt;
&lt;p&gt;My advice to you, try having a look at your BIOS settings first and making sure Win 7 can detect the BIOS settings it expects, and also disabling in your BIOS anything that might halt your system boot-up process if it encounters errors.&lt;/p&gt;
&lt;p&gt;For additional information, have a look at MicroSoft's explanation of possible errors you may find in the SrtTrail.txt file, which logs boot problems and such:&lt;/p&gt;
&lt;p&gt;&lt;a href="http://technet.microsoft.com/en-us/library/dd744291(WS.10).aspx"&gt;http://technet.microsoft.com/en-us/library/dd744291(WS.10).aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;For me, all I found was:&lt;/p&gt;
&lt;p&gt;Root cause found: &lt;br /&gt;
---------------------------&lt;br /&gt;
Unspecified changes to system configuration might have caused the problem.&lt;/p&gt;&lt;img src="http://geekswithblogs.net/HighAltitudeCoder/aggbug/144133.aspx" width="1" height="1" /&gt;</content>
    </entry>
    <entry>
        <title>SQL Server Dynamic Where Clause Column Names</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/HighAltitudeCoder/archive/2010/12/29/sql-server-dynamic-where-clause-column-names.aspx" />
        <id>http://geekswithblogs.net/HighAltitudeCoder/archive/2010/12/29/sql-server-dynamic-where-clause-column-names.aspx</id>
        <published>2010-12-29T19:40:12-07:00:00</published>
        <updated>2011-03-28T20:12:34Z</updated>
        <content type="html">&lt;p&gt;Let me start this post by saying if you are searching for the capability to do this, there is a very good chance you have problems with your current database architecture. &lt;/p&gt;
&lt;p&gt;Having said that, I realize you may not have the option of normalizing your database, and have no choice but to work with what you've got.  If this is the case, you may find the following post a useful workaround to whatever problem is causing you to search for a dynamic WHERE clause.&lt;/p&gt;
&lt;p&gt;A dynamic WHERE clause might be useful if you wish to search for an ID or a GUID generically from a junction table, but don't know what that particular item or object type (column name) might be, until run time.  Or perhaps the information is coming from a denormalized table containing many types of objects, with similar column titles between each.  For example, you might need Part ID 345698 and the part name from the dbo.MyJunction table.  The next time you run the same query, you may need Assembly ID 43578 and the assembly name from the same dbo.MyJunction table. &lt;/p&gt;
&lt;p&gt;I'm going to show you how to accomplish this.&lt;/p&gt;
&lt;p&gt;The following example uses a Function, however if you need to use dynamic SQL along with the Execute statement, you will have to use some other kind of object, like maybe a stored proc.&lt;/p&gt;
&lt;table border="1" cellspacing="1" cellpadding="1" width="457" align="left" style="width: 457px; height: 753px"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;USE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; [YourDbName]&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;/****** Object: UserDefinedFunction [dbo].fnDynamicWhereClause ******/&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;SET&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;ANSI_NULLS&lt;/span&gt; &lt;span style="color: blue"&gt;ON&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;SET&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;QUOTED_IDENTIFIER&lt;/span&gt; &lt;span style="color: blue"&gt;ON&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;IF&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: gray"&gt;EXISTS&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;SELECT&lt;/span&gt; &lt;span style="color: gray"&gt;*&lt;/span&gt; &lt;span style="color: blue"&gt;FROM&lt;/span&gt; &lt;span style="color: green"&gt;SYS&lt;/span&gt;&lt;span style="color: gray"&gt;.&lt;/span&gt;&lt;span style="color: green"&gt;OBJECTS&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;WHERE&lt;/span&gt; name &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'fnDynamicWhereClause'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;AND&lt;/span&gt; &lt;span style="color: blue"&gt;type&lt;/span&gt; &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'TF'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;)&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;DROP&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;FUNCTION&lt;/span&gt; [dbo]&lt;span style="color: gray"&gt;.&lt;/span&gt;[fnDynamicWhereClause]&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;CREATE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;FUNCTION&lt;/span&gt; [dbo]&lt;span style="color: gray"&gt;.&lt;/span&gt;[fnDynamicWhereClause]&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      @ID                                 &lt;span style="color: blue"&gt;INT&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      @TableName                    &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;255&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;)&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;RETURNS&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; @Table &lt;span style="color: blue"&gt;TABLE &lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      ID                                  &lt;span style="color: blue"&gt;INT&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      Name                          &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;255&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;)&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;AS&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;BEGIN&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;DECLARE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; @SQL &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia"&gt;MAX&lt;/span&gt;&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;INSERT&lt;/span&gt; &lt;span style="color: blue"&gt;INTO&lt;/span&gt; @Table &lt;span style="color: blue"&gt;VALUES&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;      &lt;/span&gt;&lt;span style="color: gray; font-size: 10pt"&gt;(&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SELECT&lt;/span&gt; ID&lt;span style="color: gray"&gt;,&lt;/span&gt; Name&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;FROM&lt;/span&gt; SomeJunctionTable&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;WHERE&lt;/span&gt; @ID =&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;CASE&lt;/span&gt; &lt;span style="color: fuchsia"&gt;UPPER&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@TableName&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;                  &lt;span style="color: blue"&gt;WHEN&lt;/span&gt; &lt;span style="color: red"&gt;'ASSEMBLY'&lt;/span&gt; &lt;span style="color: blue"&gt;THEN&lt;/span&gt; &lt;span style="color: blue"&gt;Assembly&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;                  &lt;span style="color: blue"&gt;WHEN&lt;/span&gt; &lt;span style="color: red"&gt;'PART'&lt;/span&gt; &lt;span style="color: blue"&gt;THEN&lt;/span&gt; Part&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;                  &lt;span style="color: blue"&gt;WHEN&lt;/span&gt; &lt;span style="color: red"&gt;'MODULE'&lt;/span&gt; &lt;span style="color: blue"&gt;THEN&lt;/span&gt; Module&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;RETURN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="margin: 0in 0in 10pt"&gt;&lt;span style="line-height: 115%; color: blue; font-size: 10pt"&gt;END&lt;/span&gt;&lt;span style="line-height: 115%; font-size: 10pt"&gt; &lt;span style="color: green"&gt;-- End Function&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;The important thing here is the SQL contained in the WHERE clause.  In the end, the SQL executed will look like:&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #0000ff"&gt;SELECT &lt;/span&gt;ID, Name &lt;span style="color: #0000ff"&gt;FROM &lt;/span&gt;SomeJunctionTable &lt;span style="color: #0000ff"&gt;WHERE  &lt;/span&gt;345698 = Part&lt;/p&gt;
&lt;p&gt;- or -&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #0000ff"&gt;SELECT &lt;/span&gt;ID, Name &lt;span style="color: #0000ff"&gt;FROM &lt;/span&gt;SomeJunctionTable &lt;span style="color: #0000ff"&gt;WHERE  &lt;/span&gt;345698 = Assembly&lt;/p&gt;
&lt;p&gt;This is slightly different from what you're probably used to, where the column name is declared first, and then the search condition is on the right side of the equals sign:&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #0000ff"&gt;SELECT &lt;/span&gt;ID, Name &lt;span style="color: #0000ff"&gt;FROM &lt;/span&gt;SomeJunctionTable &lt;span style="color: #0000ff"&gt;WHERE  &lt;/span&gt;Part = 345698 &lt;/p&gt;
&lt;p&gt;If you attempted to reorder the INSERT statement so the column name comes first, you would receive an error at compile time because SQL Server is unable to determine which column name to associate with the value.  If you switch the two, SQL Server is ok with that because the item on the left-hand side of the equals sign is known (for example, a value of 345698), and the value on the right-hand side of the statement can be evaluated to a column name.&lt;/p&gt;
&lt;p&gt;While I didn't expect this to work when I was working on it for the first time, I was pleasantly surprised when it did!  It saved me a lot of work in the end, so I figured I'd share it with anyone else out there looking for this capability.&lt;/p&gt;&lt;img src="http://geekswithblogs.net/HighAltitudeCoder/aggbug/143275.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/HighAltitudeCoder/comments/143275.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/HighAltitudeCoder/comments/commentRss/143275.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/HighAltitudeCoder/services/trackbacks/143275.aspx</trackback:ping>
    </entry>
    <entry>
        <title>SQL Server Dynamic SQL - A Comprehensive Example</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/HighAltitudeCoder/archive/2010/06/15/sql-server-server-dynamic-sql.aspx" />
        <id>http://geekswithblogs.net/HighAltitudeCoder/archive/2010/06/15/sql-server-server-dynamic-sql.aspx</id>
        <published>2010-06-15T21:32:51-06:00:00</published>
        <updated>2010-10-17T19:19:53Z</updated>
        <content type="html">&lt;h3&gt;With Dynamic SQL, one stored proc can do the work of many...&lt;/h3&gt;
&lt;p&gt;Over the years, I have worked with a variety of different clients.  It seems that every client has unique and different needs.  In some situations, you may find that dynamic sql fills the void better than other solutions available to you.&lt;/p&gt;
&lt;p&gt;One scenario I have encountered in the past is one where the client wants the capability to run aggregate information at run time, but for any start month and number of time periods.  There are many ways to accomplish this, but assuming you are in the situation where you are required to accomplish this task in the SQL Server environment itself, I will show you how to do that, using the Adventureworks database as an example from which you might develop your own solution tailored to your business environment.&lt;/p&gt;
&lt;p&gt;Consider the following SQL:&lt;/p&gt;
&lt;table border="1" cellspacing="1" cellpadding="1" width="449" style="width: 449px; height: 117px"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;SELECT&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; OrderDate&lt;span style="color: gray"&gt;,&lt;/span&gt; SalesPersonID&lt;span style="color: gray"&gt;,&lt;/span&gt; &lt;span style="color: fuchsia"&gt;COUNT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;SalesPersonID&lt;span style="color: gray"&gt;)&lt;/span&gt; &lt;span style="color: blue"&gt;AS&lt;/span&gt; SalesBySalesPersonThisMonth&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;FROM&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; Sales&lt;span style="color: gray"&gt;.&lt;/span&gt;SalesOrderHeader&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;WHERE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; SalesPersonID &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NOT&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;GROUP&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;BY&lt;/span&gt; SalesPersonID&lt;span style="color: gray"&gt;,&lt;/span&gt; OrderDate&lt;/span&gt;&lt;/div&gt;
            &lt;div style="margin: 0in 0in 10pt"&gt;&lt;span style="line-height: 115%; color: blue; font-size: 10pt"&gt;ORDER&lt;/span&gt;&lt;span style="line-height: 115%; font-size: 10pt"&gt; &lt;span style="color: blue"&gt;BY&lt;/span&gt;    SalesPersonID&lt;span style="color: gray"&gt;,&lt;/span&gt; OrderDate&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;This will return a count of all sales made by all sales people within a given month, oriented vertically.  This isn't very human-friendly, however, as it requires the reader to pick through all the data, if for example he/she wants to see a comprehensive picture of all sales for sales person 278.&lt;/p&gt;
&lt;p&gt;A more readable format would be to display the data horizontally, as such:&lt;/p&gt;
&lt;table border="1" cellspacing="1" cellpadding="1" width="448" style="width: 448px; height: 23px"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;USE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; AdventureWorks&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;WITH&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; EmployeeSalesByMonth &lt;span style="color: gray"&gt;(&lt;/span&gt;OrderDate&lt;span style="color: gray"&gt;,&lt;/span&gt; SalesPersonID&lt;span style="color: gray"&gt;,&lt;/span&gt; SalesBySalesPersonThisMonth&lt;span style="color: gray"&gt;)&lt;/span&gt; &lt;span style="color: blue"&gt;AS&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;(&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;      SELECT&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; OrderDate&lt;span style="color: gray"&gt;,&lt;/span&gt; SalesPersonID&lt;span style="color: gray"&gt;,&lt;/span&gt; &lt;span style="color: fuchsia"&gt;COUNT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;SalesPersonID&lt;span style="color: gray"&gt;)&lt;/span&gt; &lt;span style="color: blue"&gt;AS&lt;/span&gt;  &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      SalesBySalesPersonThisMonth&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;FROM&lt;/span&gt; Sales&lt;span style="color: gray"&gt;.&lt;/span&gt;SalesOrderHeader&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;WHERE&lt;/span&gt; SalesPersonID &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NOT&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;GROUP&lt;/span&gt; &lt;span style="color: blue"&gt;BY&lt;/span&gt; SalesPersonID&lt;span style="color: gray"&gt;,&lt;/span&gt; OrderDate&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;)&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;SELECT&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: gray"&gt;*&lt;/span&gt; &lt;span style="color: blue"&gt;FROM&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;      (&lt;/span&gt;&lt;span style="color: blue; font-size: 10pt"&gt;SELECT&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; OrderDate&lt;span style="color: gray"&gt;,&lt;/span&gt; SalesPersonID&lt;span style="color: gray"&gt;,&lt;/span&gt; &lt;span style="color: fuchsia"&gt;COUNT&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;SalesPersonID&lt;span style="color: gray"&gt;)&lt;/span&gt; &lt;span style="color: blue"&gt;AS&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      SalesBySalesPersonThisMonth&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;FROM&lt;/span&gt; Sales&lt;span style="color: gray"&gt;.&lt;/span&gt;SalesOrderHeader&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;WHERE&lt;/span&gt; SalesPersonID &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NOT&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;GROUP&lt;/span&gt; &lt;span style="color: blue"&gt;BY&lt;/span&gt; SalesPersonID&lt;span style="color: gray"&gt;,&lt;/span&gt; OrderDate&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;HAVING&lt;/span&gt; &lt;span style="color: fuchsia"&gt;MIN&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;OrderDate&lt;span style="color: gray"&gt;)&lt;/span&gt; &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'7/1/2001'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: green"&gt;--ORDER BY SalesPersonID, OrderDate&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;)&lt;/span&gt; &lt;span style="color: blue"&gt;AS&lt;/span&gt; ESM1&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;LEFT&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: gray"&gt;OUTER&lt;/span&gt; &lt;span style="color: gray"&gt;JOIN&lt;/span&gt; EmployeeSalesByMonth &lt;span style="color: blue"&gt;AS&lt;/span&gt; ESM2&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;ON&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; ESM1&lt;span style="color: gray"&gt;.&lt;/span&gt;SalesPersonID &lt;span style="color: gray"&gt;=&lt;/span&gt; ESM2&lt;span style="color: gray"&gt;.&lt;/span&gt;SalesPersonID&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;AND&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: fuchsia"&gt;MONTH&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;ESM1&lt;span style="color: gray"&gt;.&lt;/span&gt;OrderDate&lt;span style="color: gray"&gt;)&lt;/span&gt; &lt;span style="color: gray"&gt;+&lt;/span&gt; 1 &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia"&gt;MONTH&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;ESM2&lt;span style="color: gray"&gt;.&lt;/span&gt;OrderDate&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;AND&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: fuchsia"&gt;YEAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;ESM1&lt;span style="color: gray"&gt;.&lt;/span&gt;OrderDate&lt;span style="color: gray"&gt;)&lt;/span&gt; &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia"&gt;YEAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;ESM2&lt;span style="color: gray"&gt;.&lt;/span&gt;OrderDate&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;&lt;span style="color: gray"&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;LEFT&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: gray"&gt;OUTER&lt;/span&gt; &lt;span style="color: gray"&gt;JOIN&lt;/span&gt; EmployeeSalesByMonth &lt;span style="color: blue"&gt;AS&lt;/span&gt; ESM3&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;ON&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; ESM2&lt;span style="color: gray"&gt;.&lt;/span&gt;SalesPersonID &lt;span style="color: gray"&gt;=&lt;/span&gt; ESM3&lt;span style="color: gray"&gt;.&lt;/span&gt;SalesPersonID&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;AND&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: fuchsia"&gt;MONTH&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;ESM2&lt;span style="color: gray"&gt;.&lt;/span&gt;OrderDate&lt;span style="color: gray"&gt;)&lt;/span&gt; &lt;span style="color: gray"&gt;+&lt;/span&gt; 1 &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia"&gt;MONTH&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;ESM3&lt;span style="color: gray"&gt;.&lt;/span&gt;OrderDate&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;AND&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: fuchsia"&gt;YEAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;ESM2&lt;span style="color: gray"&gt;.&lt;/span&gt;OrderDate&lt;span style="color: gray"&gt;)&lt;/span&gt; &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia"&gt;YEAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;ESM3&lt;span style="color: gray"&gt;.&lt;/span&gt;OrderDate&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;ORDER&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;BY&lt;/span&gt;    ESM1&lt;span style="color: gray"&gt;.&lt;/span&gt;SalesPersonID&lt;span style="color: gray"&gt;,&lt;/span&gt; ESM1&lt;span style="color: gray"&gt;.&lt;/span&gt;OrderDate&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Putting together your stored procedure in such a manner will be relatively straightforward from this point onward.  However what it is likely your client will show this new report to his manager, and the manager might request the same report, while allowing him to choose any number of months he wishes.  You could either create a stored procedure for every number of months imagineable (imagine creating the proc that returns five years of data!).  Or, you could create &lt;em&gt;one &lt;/em&gt;stored procedure that creates this information dynamically.&lt;/p&gt;
&lt;p&gt;To do this, you will need to accept as parameters a start date, and number of months the client wishes to run the report for.  Once you have this information, you will assemble the query within your stored procedure, and then finally execute it using the (implied) EXECUTE IMMEDIATE functionality of SQL Server:&lt;/p&gt;
&lt;table border="1" cellspacing="1" cellpadding="1" width="446" style="width: 446px; height: 23px"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt; 
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;USE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; AdventureWorks&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;IF&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: gray"&gt;EXISTS&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT&lt;/span&gt; &lt;span style="color: gray"&gt;*&lt;/span&gt; &lt;span style="color: blue"&gt;FROM&lt;/span&gt; &lt;span style="color: green"&gt;sys.objects&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;WHERE&lt;/span&gt; &lt;span style="color: blue"&gt;type&lt;/span&gt; &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'P'&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;AND&lt;/span&gt; &lt;span style="color: blue"&gt;name&lt;/span&gt; &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'ProcSalesReportDynamic'&lt;/span&gt;&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;DROP&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;PROCEDURE&lt;/span&gt; dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;ProcSalesReportDynamic&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;SET&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;ANSI_NULLS&lt;/span&gt; &lt;span style="color: blue"&gt;ON&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;SET&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;QUOTED_IDENTIFIER&lt;/span&gt; &lt;span style="color: blue"&gt;ON&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;CREATE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;PROCEDURE&lt;/span&gt; dbo&lt;span style="color: gray"&gt;.&lt;/span&gt;ProcSalesReportDynamic &lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      @StartDate                          &lt;span style="color: blue"&gt;DATETIME&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      @NumberOfMonths               &lt;span style="color: blue"&gt;INT&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;)&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;AS&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;BEGIN&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;SET&lt;/span&gt; &lt;span style="color: blue"&gt;NOCOUNT&lt;/span&gt; &lt;span style="color: blue"&gt;ON&lt;/span&gt;&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;DECLARE&lt;/span&gt; @SQL                        &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: fuchsia"&gt;MAX&lt;/span&gt;&lt;span style="color: gray"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;DECLARE&lt;/span&gt; @CurrentMonth   &lt;span style="color: blue"&gt;INT&lt;/span&gt;&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;SET&lt;/span&gt; @CurrentMonth &lt;span style="color: gray"&gt;=&lt;/span&gt; 1&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;SET&lt;/span&gt; @SQL &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'WITH EmployeeSalesByMonth (OrderDate, SalesPersonID, SalesBySalesPersonThisMonth) AS&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;      (&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;            SELECT OrderDate, SalesPersonID, COUNT(SalesPersonID) AS SalesBySalesPersonThisMonth&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;            FROM Sales.SalesOrderHeader&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;            WHERE SalesPersonID IS NOT NULL&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;            GROUP BY SalesPersonID, OrderDate&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;      --    ORDER BY    SalesPersonID, OrderDate&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;      )&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;SELECT * FROM&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;      (SELECT OrderDate, SalesPersonID, COUNT(SalesPersonID) AS SalesBySalesPersonThisMonth&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;      FROM Sales.SalesOrderHeader&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;      WHERE SalesPersonID IS NOT NULL&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;      GROUP BY SalesPersonID, OrderDate&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;      HAVING MIN(OrderDate) = '''&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: fuchsia"&gt;CAST&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@StartDate &lt;span style="color: blue"&gt;AS&lt;/span&gt; &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;50&lt;span style="color: gray"&gt;))&lt;/span&gt; &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: red"&gt;'''&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;      --ORDER BY SalesPersonID, OrderDate&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;      ) AS ESM1'&lt;/span&gt;&lt;span style="color: gray; font-size: 10pt"&gt;;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;WHILE&lt;/span&gt; @CurrentMonth &lt;span style="color: gray"&gt;&amp;lt;&lt;/span&gt; @NumberOfMonths&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @SQL &lt;span style="color: gray"&gt;=&lt;/span&gt; @SQL &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: red"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;LEFT OUTER JOIN EmployeeSalesByMonth AS ESM'&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: fuchsia"&gt;CAST&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@CurrentMonth &lt;span style="color: gray"&gt;+&lt;/span&gt; 1 &lt;span style="color: blue"&gt;AS&lt;/span&gt; &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;))&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: red"&gt;' &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;ON ESM'&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: fuchsia"&gt;CAST&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@CurrentMonth &lt;span style="color: blue"&gt;AS&lt;/span&gt; &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;))&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: red"&gt;'.SalesPersonID = ESM'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: fuchsia"&gt;CAST&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@CurrentMonth &lt;span style="color: gray"&gt;+&lt;/span&gt; 1 &lt;span style="color: blue"&gt;AS&lt;/span&gt; &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;))&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: red"&gt;'.SalesPersonID&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;AND MONTH(ESM'&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: fuchsia"&gt;CAST&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@CurrentMonth &lt;span style="color: blue"&gt;AS&lt;/span&gt; &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;))&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: red"&gt;'.OrderDate) + 1 = MONTH(ESM'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: fuchsia"&gt;CAST&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@CurrentMonth &lt;span style="color: gray"&gt;+&lt;/span&gt; 1 &lt;span style="color: blue"&gt;AS&lt;/span&gt; &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;))&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: red"&gt;'.OrderDate)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;AND YEAR(ESM'&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: fuchsia"&gt;CAST&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@CurrentMonth &lt;span style="color: blue"&gt;AS&lt;/span&gt; &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;))&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: red"&gt;'.OrderDate) = YEAR(ESM'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: fuchsia"&gt;CAST&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@CurrentMonth &lt;span style="color: gray"&gt;+&lt;/span&gt; 1 &lt;span style="color: blue"&gt;AS&lt;/span&gt; &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;2&lt;span style="color: gray"&gt;))&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: red"&gt;'.OrderDate)'&lt;/span&gt;&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @CurrentMonth &lt;span style="color: gray"&gt;=&lt;/span&gt; @CurrentMonth &lt;span style="color: gray"&gt;+&lt;/span&gt; 1&lt;span style="color: gray"&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt; &lt;span style="color: green"&gt;-- End WHILE &lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;SET&lt;/span&gt; @SQL &lt;span style="color: gray"&gt;=&lt;/span&gt; @SQL &lt;span style="color: gray"&gt;+&lt;/span&gt; &lt;span style="color: red"&gt;'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: red; font-size: 10pt"&gt;ORDER BY    ESM1.SalesPersonID, ESM1.OrderDate'&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;--PRINT @SQL;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;EXEC&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@SQL&lt;span style="color: gray"&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;END&lt;/span&gt;&lt;span style="color: gray; font-size: 10pt"&gt;;&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: green"&gt;--End dbo.ProcSalesReportDynamic&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="margin: 0in 0in 10pt"&gt;&lt;span style="line-height: 115%; font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;Not bad, right?  (At least it beats writing two, three, twelve, or sixty stored procedures!)&lt;/p&gt;
&lt;p&gt;When you execute this for many months, you may notice that the columns begin to become confusing.  Once a column name repeats, it contains the same name for the sequence to be repeated for the next month; the only way to tell which column is which is to look at the data itself.  I thought about correcting this, but for the sake of keeping this post as short as possible, as well as giving you the reader a chance to update the dynamically created SQL as you see fit, I thought it best to leave this correction out.&lt;/p&gt;
&lt;p&gt;I will give you a hint, though, and that is to change the dynamically created 'SELECT *' into an explicitly named column list.&lt;/p&gt;
&lt;p&gt;Lastly, beware the drawbacks of using dynamic SQL:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Your code can blow up at run time.  If you're using SQL Server 2000 or earlier, the max size limit of a VARCHAR is 8000.  That means the maximum size of your dyamically executed SQL will be 8000 characters long.  A client or consumer that attempts to run a proc that terminates prematurely because it has run out of storage space for the string will receive some sort of SQL error.  While you may be familiar with the error, the consumer or client will not.&lt;/li&gt;
    &lt;li&gt;If you are using SQL Server 2005 or later, you will have the VARCHAR(MAX) data type available to you.  This is both a good and a bad thing.  You will not have the size limitation you would normally encounter with SQL Server 2000.  (Well, the string can &lt;em&gt;only &lt;/em&gt;be 2Gb long - but is that &lt;em&gt;really a limitation&lt;/em&gt;?)  Instead, you could bump up against the limitation that your SQL takes one and a half years to execute; the client or consumer could hang indefinitely.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Either (perhaps both) of the scenarios described above are ones you should plan for, so you can avoid getting dirty looks from your boss.  If your SQL creeps into the territory where it is beginning to take a long time to run, you may find yourself researching ways to improve your statement either through indexing or more likely optimization of the query itself.&lt;/p&gt;&lt;img src="http://geekswithblogs.net/HighAltitudeCoder/aggbug/140439.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/HighAltitudeCoder/comments/140439.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/HighAltitudeCoder/comments/commentRss/140439.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/HighAltitudeCoder/services/trackbacks/140439.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Business Logic: Should it exist in the code or in the database?</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/HighAltitudeCoder/archive/2010/06/27/business-logic-should-it-exist-in-the-code-or-in.aspx" />
        <id>http://geekswithblogs.net/HighAltitudeCoder/archive/2010/06/27/business-logic-should-it-exist-in-the-code-or-in.aspx</id>
        <published>2010-06-27T12:56:06-06:00:00</published>
        <updated>2011-03-27T14:13:44Z</updated>
        <summary type="html">If your team is in this quandry, read on...</summary>
        <content type="html">&lt;p&gt;Some time ago, I found myself in the difficult situation of trying to explain why business logic shouldn't be placed in the database, but instead in the code (preferably in a Business Logic layer or Object Model layer).&lt;/p&gt;
&lt;p&gt;The situation seemed so clear to me.  It was striking how differently others in the team saw it. &lt;/p&gt;
&lt;p&gt;You may find yourself in a situation similar to this one, where you are working on a database that contains *many* business rules.  Perhaps it started out as a set of tables that very closely mirrored the Graphical User Interface presented to your users.  It may have additionally contained an initial set of rules that dictated whether additional rows in other tables were to be updated.&lt;/p&gt;
&lt;p&gt;Now, throw in to the mix the fact that the customer realized the application &lt;em&gt;almost&lt;/em&gt; meets their needs, but not quite.  Because of one small change in the business rules governing workflow in their ofice, the existing design your team had put into place now has to pretty much go out the window.  (Well, maybe just migrated to a better model and then later abandoned).&lt;/p&gt;
&lt;p&gt;You may find yourself thinking back to the original design, and how much more flexible it would be now to change, if only the database were used solely for the porpose of efficient storing and retrieval of data.  Additionally, you may have further complications to unravel due to the fact that the database more resembles the objects in the Graphical User Interface, rather than a relational model that is unknowing and uncaring about what ever GUI object may display the data.&lt;/p&gt;
&lt;p&gt;I did some searching around on the internet, and apparently this is a very common problem.  One article in particular by Chad Z. Hower caught my eye:&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://www.codeproject.com/KB/architecture/DudeWheresMyBusinessLogic.aspx"&gt;http://www.codeproject.com/KB/architecture/DudeWheresMyBusinessLogic.aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;Pay particular attention to his definition of 'Business Logic'.  If I were to try to capture his definition into one sentence, I would describe business logic as "any decision, formatting, additional work, or decision-making that can be encumbered upon a data transaction, outside of the normal constraints that exist with in a database".&lt;/p&gt;
&lt;p&gt;In his article, Chad outlines a detailed explanation of pros and cons associated with this very decision.  Surprisingly, in his post I actually found identically worded explanations (compared to those I have heard before) as to why placing business logic into the database is a good thing.  I must clarify: he DOES NOT espouse this practice (and neither do I!), but I'm saying I've heard the same explanations before, verbatim.&lt;/p&gt;
&lt;p&gt;This perspective (in favor of placing logic in the database) can be a very frustrating one to refute, in part because it seems so simple for people to list the obvious, more immediate benefits.  Warning: if you are reading this post right now, you have probably heard one or more of these benefits debated in discussions already at work.&lt;/p&gt;
&lt;p&gt;You may hear that putting the logic into the database:&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;Allows your team to save time by avoiding a testing cycle (for e.g. when the team delivers a software patch or version upgrade to existing components).&lt;/li&gt;
    &lt;li&gt;Is "easier" than having to locate and maintain it in the code base somewhere&lt;/li&gt;
    &lt;li&gt;Is less buggy, because it requires less testing&lt;/li&gt;
    &lt;li&gt;Gives an additional layer of protection against bugs, because it does not solely rely on a bug-free code base&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Pretty simple, obvious benefits, right?&lt;/p&gt;
&lt;p&gt;The cons are much more subtle.  And in my opinion, they are much more sinister than the pros, because their consequences can be paralyzing when it comes time to make changes to the code, the GUI, the database, or a combination of these.&lt;/p&gt;
&lt;ul&gt;
    &lt;li&gt;When logic is placed in the database, it is often in one (or several) additional locations outside of the database.  This is often the result of a "just in case" approach put into action by a developer, because he is unsure whether future changes might be applied to the database that can break the code segment he is currently working on.  He is thinking to himself, "Just to be sure, I better place the same logic in the code, in case the rule gets changed in the database..."&lt;/li&gt;
    &lt;li&gt;There may be several copies of the same logic, encapsulated in many database objects that serve data to different layers of the application.  Or perhaps several slightly different versions of the same object.  These things evolve over time - unfortunately, it's up to you to figure them out  :-/.&lt;/li&gt;
    &lt;li&gt;Rather than relying solely on constraints to uphold data integrity, unknown (probably countless) layers of subtle rules are being applied at run time to update the data in the database, as it is parsed.  Over time, these rules make the database fragile to the expectations placed upon the data model itself.  Meaning: "You should have known that you can only apply 'Y' for yes, 'N' for no, and 'M' for maybe."  When the paradigm shifts, the data model (and all its associated rules) become obsolete almost instantly.  What I mean by this is, the database becomes expectant to certain kinds of specific updates.  It will eventually end up breaking the business model when a new developer comes along, expecting nothing out of the ordinary when updating the database.  He didn't know that only data types of char(1) and only values of 'Y', 'N', or 'M' could be applied.&lt;/li&gt;
    &lt;li&gt;Which team member was it that worked on those stored procedures that tied all the logic together correctly?&lt;/li&gt;
    &lt;li&gt;Does that team member even still work here?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These cons build on each other.  If not realized, they can either paralyze a team that wishes to make changes to their application, or force them to abandon the old model and start anew, with a fresh application code base, and (sigh) a fresh database.&lt;/p&gt;
&lt;p&gt;The data will still need to be migrated however.&lt;/p&gt;
&lt;p&gt;Graeme&lt;/p&gt;
&lt;p&gt; &lt;/p&gt;&lt;img src="http://geekswithblogs.net/HighAltitudeCoder/aggbug/140644.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/HighAltitudeCoder/comments/140644.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/HighAltitudeCoder/comments/commentRss/140644.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/HighAltitudeCoder/services/trackbacks/140644.aspx</trackback:ping>
    </entry>
    <entry>
        <title>Stub out your project using Visual Studio's Class Diagram</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/HighAltitudeCoder/archive/2010/07/11/stub-out-your-project-using-visual-studios-class-diagram.aspx" />
        <id>http://geekswithblogs.net/HighAltitudeCoder/archive/2010/07/11/stub-out-your-project-using-visual-studios-class-diagram.aspx</id>
        <published>2010-07-11T12:00:51-06:00:00</published>
        <updated>2011-03-27T14:15:02Z</updated>
        <summary type="html">Use the Visual Studio Class Diagram to Enhance the Productivity of your Work Group.</summary>
        <content type="html">&lt;p&gt;A while back I was tasked with creating a class diagram for one of the applications created by my company.  While I knew there was a possibility I might have to manually create the diagram by hand, I knew this method would probably consume a lot of time.  I also knew there were third-party tools out there that automate some of these tasks.&lt;/p&gt;
&lt;p&gt;The problem was, we didn't have any of these third-party diagramming tools.  There are a myriad number of reasons why a company might choose not to purchase these kinds of productivity-enhancing tools; at my workplace the deciding factor seems to be lack of time and money.&lt;/p&gt;
&lt;p&gt;I decided to see if Visual Studio had anything to offer, expecting it to fall short in the richness of its features and capabilities.  I was pleasantly surprised to discover the opposite: Visual Studio offers quite a nice little package for class diagramming.  ***Note, the version of Visual Studio I'm referring to is VS 2005.  If I find better tools (third-party or inlcuded in subsequent releases of Visual Studio), I will update this post with my findings.&lt;/p&gt;
&lt;p&gt;Here are a few of the features offered by the Visual Studio 2005 class diagram tool:&lt;/p&gt;
&lt;ol&gt;
    &lt;li&gt;You can use it to stub out your code, or ...&lt;/li&gt;
    &lt;li&gt;You can capture all the class information for an already existing project without having to do any real work.  More on this later.&lt;/li&gt;
    &lt;li&gt;The class designer stays up-to-date in real time.  No need to fuss with keeping everything in synch all the time, excepting perhaps the latest version of class diagram on people's desks.&lt;/li&gt;
    &lt;li&gt;The VS class designer tool understands (and visually illustrates) all of the following:
    &lt;ul&gt;
        &lt;li&gt;Object Properties, members, methods, events, delegates, and nested types.&lt;/li&gt;
        &lt;li&gt;Object  inheritance.&lt;/li&gt;
        &lt;li&gt;Class types (abstract, interface, sealed)&lt;/li&gt;
        &lt;li&gt;Class metadata, for displaying (or creating) the documentation and comments associated with your code&lt;/li&gt;
        &lt;li&gt;Object relationships (I'm referring to the arrows depicted on most UML diagrams to express relationship between two or more objects)&lt;/li&gt;
    &lt;/ul&gt;
    &lt;/li&gt;
    &lt;li&gt;The class diagram you've created is exportable to .pdf or image types, so you can print and distrube copies of it freely, without having to install Visual Studio on every consumer's machine.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;The nice thing about this tool is it is a big time-saver.  Whether you're coming from the design side of things, or whether you're wanting a class diagram for an existing code base, Visual Studio greatly reduces the time and effort required to accomplish either of these tasks.&lt;/p&gt;
&lt;p&gt;I'll update this post later with pictures and a quick run-through of both processes.  For now, I hope you might find this article by Microsoft very helpful in the mean time.&lt;/p&gt;
&lt;p&gt;&lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/aa288743(VS.71).aspx"&gt;http://msdn.microsoft.com/en-us/library/aa288743(VS.71).aspx&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I hope you enjoy exploring the many features provided by Visual Studio class diagram tool!&lt;/p&gt;&lt;img src="http://geekswithblogs.net/HighAltitudeCoder/aggbug/140873.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/HighAltitudeCoder/comments/140873.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/HighAltitudeCoder/comments/commentRss/140873.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/HighAltitudeCoder/services/trackbacks/140873.aspx</trackback:ping>
    </entry>
    <entry>
        <title>SQL Server Date Comparison Functions</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/HighAltitudeCoder/archive/2010/06/13/sql-server-date-comparison-functions.aspx" />
        <id>http://geekswithblogs.net/HighAltitudeCoder/archive/2010/06/13/sql-server-date-comparison-functions.aspx</id>
        <published>2010-06-13T13:56:20-06:00:00</published>
        <updated>2010-06-16T21:11:13Z</updated>
        <summary type="html">When you need the earlier of or later of two dates and NULL handling capability.</summary>
        <content type="html">&lt;p&gt;A few months ago, I found myself working with a repetitive cursor that looped until the data had been manipulated enough times that it was finally correct.  The cursor was heavily dependent upon dates, every time requiring the earlier of two (or several) dates in one stored procedure, while requiring the later of two dates in another stored procedure.&lt;/p&gt;
&lt;p&gt;In short what I needed was a function that would allow me to perform the following evaluation:&lt;/p&gt;
&lt;p&gt;&lt;span style="color: #0000ff"&gt;WHERE&lt;/span&gt; &lt;span style="color: #ff00cc"&gt;MAX&lt;/span&gt;&lt;span style="color: #808080"&gt;(&lt;/span&gt;Date1, Date2&lt;span style="color: #808080"&gt;)&lt;/span&gt; &amp;lt; @SomeDate&lt;/p&gt;
&lt;p&gt;The problem is, the MAX() function in SQL Server does not perform this functionality. &lt;/p&gt;
&lt;p&gt;So, I set out to put these functions together.  They are titled: EarlierOf() and LaterOf().&lt;/p&gt;
&lt;table border="1" cellspacing="1" cellpadding="1" width="400" align="left"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;/**********************************************************&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;                            EarlierOf.sql&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;**********************************************************/&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;/**********************************************************&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;Return the later of two DATETIME variables.&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;Parameter 1: DATETIME1&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;Parameter 2: DATETIME2&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;Works for a variety of DATETIME or NULL values. Even &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;though comparisons with NULL are actually indeterminate, we &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;know conceptually that NULL is not earlier &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;or later than any other date provided.&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SYNTAX:&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SELECT dbo.EarlierOf('1/1/2000','12/1/2009')&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SELECT dbo.EarlierOf('2009-12-01 00:00:00.000','2009-12-01 00:00:00.521')&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SELECT dbo.EarlierOf('11/15/2000',NULL)&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SELECT dbo.EarlierOf(NULL,'1/15/2004')&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SELECT dbo.EarlierOf(NULL,NULL)&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;**********************************************************/&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;USE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; AdventureWorks&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;IF&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: gray"&gt;EXISTS&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT&lt;/span&gt; &lt;span style="color: gray"&gt;*&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;FROM&lt;/span&gt; sysobjects&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;WHERE&lt;/span&gt; &lt;span style="color: blue"&gt;name&lt;/span&gt; &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'EarlierOf'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;AND&lt;/span&gt; xtype &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'FN'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;BEGIN&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;DROP&lt;/span&gt; &lt;span style="color: blue"&gt;FUNCTION&lt;/span&gt; EarlierOf&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;END&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;CREATE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;FUNCTION&lt;/span&gt; EarlierOf &lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      @Date1                              &lt;span style="color: blue"&gt;DATETIME&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      @Date2                              &lt;span style="color: blue"&gt;DATETIME&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;)&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;RETURNS&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;DATETIME&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;AS&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;BEGIN&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;DECLARE&lt;/span&gt; @ReturnDate     &lt;span style="color: blue"&gt;DATETIME&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;IF&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@Date1 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt; &lt;span style="color: gray"&gt;AND&lt;/span&gt; @Date2 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NULL)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ReturnDate &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;GOTO&lt;/span&gt; EndOfFunction&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;ELSE&lt;/span&gt; &lt;span style="color: blue"&gt;IF&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@Date1 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt; &lt;span style="color: gray"&gt;AND&lt;/span&gt; @Date2 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NOT&lt;/span&gt; &lt;span style="color: gray"&gt;NULL)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ReturnDate &lt;span style="color: gray"&gt;=&lt;/span&gt; @Date2&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;GOTO&lt;/span&gt; EndOfFunction&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;ELSE&lt;/span&gt; &lt;span style="color: blue"&gt;IF&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@Date1 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NOT&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt; &lt;span style="color: gray"&gt;AND&lt;/span&gt; @Date2 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NULL)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ReturnDate &lt;span style="color: gray"&gt;=&lt;/span&gt; @Date1&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;GOTO&lt;/span&gt; EndOfFunction&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;ELSE&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ReturnDate &lt;span style="color: gray"&gt;=&lt;/span&gt; @Date1&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;IF&lt;/span&gt; @Date2 &lt;span style="color: gray"&gt;&amp;lt;&lt;/span&gt; @Date1&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;                  &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ReturnDate &lt;span style="color: gray"&gt;=&lt;/span&gt; @Date2&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;GOTO&lt;/span&gt; EndOfFunction&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      EndOfFunction&lt;span style="color: gray"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;RETURN&lt;/span&gt; @ReturnDate&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;END&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: green"&gt;-- End Function&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;---- Set Permissions&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;--GRANT SELECT ON EarlierOf TO UserRole1&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;--GRANT SELECT ON EarlierOf TO UserRole2&lt;/span&gt;&lt;/div&gt;
            &lt;div style="margin: 0in 0in 10pt"&gt;&lt;span style="line-height: 115%; color: green; font-size: 10pt"&gt;--GO&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;The inverse of this function is only slightly different.&lt;/p&gt;
&lt;table border="1" cellspacing="1" cellpadding="1" width="400" align="left"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;/**********************************************************&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;                            LaterOf.sql&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;**********************************************************/&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;/**********************************************************&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;Return the later of two DATETIME variables.&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;Parameter 1: DATETIME1&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;Parameter 2: DATETIME2&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;Works for a variety of DATETIME or NULL values. Even &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;though comparisons with NULL are actually indeterminate, we &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;know conceptually that NULL is not earlier &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;or later than any other date provided.&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SYNTAX:&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SELECT dbo.LaterOf('1/1/2000','12/1/2009')&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SELECT dbo.LaterOf('2009-12-01 00:00:00.000','2009-12-01 00:00:00.521')&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SELECT dbo.LaterOf('11/15/2000',NULL)&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SELECT dbo.LaterOf(NULL,'1/15/2004')&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;SELECT dbo.LaterOf(NULL,NULL)&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;**********************************************************/&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;USE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; AdventureWorks&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;IF&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: gray"&gt;EXISTS&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT&lt;/span&gt; &lt;span style="color: gray"&gt;*&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;FROM&lt;/span&gt; sysobjects&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;WHERE&lt;/span&gt; &lt;span style="color: blue"&gt;name&lt;/span&gt; &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'LaterOf'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;AND&lt;/span&gt; xtype &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'FN'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;BEGIN&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;DROP&lt;/span&gt; &lt;span style="color: blue"&gt;FUNCTION&lt;/span&gt; LaterOf&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;END&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;CREATE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;FUNCTION&lt;/span&gt; LaterOf &lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      @Date1                              &lt;span style="color: blue"&gt;DATETIME&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      @Date2                              &lt;span style="color: blue"&gt;DATETIME&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;)&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;RETURNS&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;DATETIME&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;AS&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;BEGIN&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;DECLARE&lt;/span&gt; @ReturnDate     &lt;span style="color: blue"&gt;DATETIME&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;IF&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@Date1 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt; &lt;span style="color: gray"&gt;AND&lt;/span&gt; @Date2 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NULL)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ReturnDate &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;GOTO&lt;/span&gt; EndOfFunction&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;ELSE&lt;/span&gt; &lt;span style="color: blue"&gt;IF&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@Date1 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt; &lt;span style="color: gray"&gt;AND&lt;/span&gt; @Date2 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NOT&lt;/span&gt; &lt;span style="color: gray"&gt;NULL)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ReturnDate &lt;span style="color: gray"&gt;=&lt;/span&gt; @Date2&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;GOTO&lt;/span&gt; EndOfFunction&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;ELSE&lt;/span&gt; &lt;span style="color: blue"&gt;IF&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@Date1 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NOT&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt; &lt;span style="color: gray"&gt;AND&lt;/span&gt; @Date2 &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NULL)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ReturnDate &lt;span style="color: gray"&gt;=&lt;/span&gt; @Date1&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;GOTO&lt;/span&gt; EndOfFunction&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;ELSE&lt;/span&gt; &lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ReturnDate &lt;span style="color: gray"&gt;=&lt;/span&gt; @Date1&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;IF&lt;/span&gt; @Date2 &lt;span style="color: gray"&gt;&amp;gt;&lt;/span&gt; @Date1&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;                  &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ReturnDate &lt;span style="color: gray"&gt;=&lt;/span&gt; @Date2&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;GOTO&lt;/span&gt; EndOfFunction&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      EndOfFunction&lt;span style="color: gray"&gt;:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;RETURN&lt;/span&gt; @ReturnDate&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;END&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: green"&gt;-- End Function&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt; &lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;---- Set Permissions&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;--GRANT SELECT ON LaterOf TO UserRole1&lt;/span&gt;&lt;/div&gt;
            &lt;div style="line-height: normal"&gt;&lt;span style="color: green; font-size: 10pt"&gt;--GRANT SELECT ON LaterOf TO UserRole2&lt;/span&gt;&lt;/div&gt;
            &lt;div style="margin: 0in 0in 10pt"&gt;&lt;span style="line-height: 115%; color: green; font-size: 10pt"&gt;--GO&lt;/span&gt;&lt;/div&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt; &lt;/p&gt;
&lt;p&gt;The interesting thing about these function is their simplicity and the built-in NULL handling functionality.  They are interesting, because it seems like something should already exist in SQL Server to provide this functionality.  From a different vantage point, if you create this functionality and it is easy to use (ideally, intuitive or self-explanatory), you have made a successful contribution.&lt;/p&gt;
&lt;p&gt;Interesting is good.  Self-explanatory, or intuitive is FAR better.  Happy coding!&lt;/p&gt;
&lt;p&gt;Graeme&lt;/p&gt;&lt;img src="http://geekswithblogs.net/HighAltitudeCoder/aggbug/140387.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/HighAltitudeCoder/comments/140387.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/HighAltitudeCoder/comments/commentRss/140387.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/HighAltitudeCoder/services/trackbacks/140387.aspx</trackback:ping>
    </entry>
    <entry>
        <title>SQL Server Split() Function</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/HighAltitudeCoder/archive/2010/06/06/sql-server-split-function.aspx" />
        <id>http://geekswithblogs.net/HighAltitudeCoder/archive/2010/06/06/sql-server-split-function.aspx</id>
        <published>2010-06-06T20:33:50-06:00:00</published>
        <updated>2010-10-17T18:52:11Z</updated>
        <summary type="html">Add flexibility to your database with parsing functions.</summary>
        <content type="html">&lt;table border="0" cellspacing="0" cellpadding="0" width="50%" align="left"&gt;
    &lt;tbody&gt;
        &lt;tr&gt;
            &lt;td&gt;
            &lt;h3&gt;Diversify Your Database!&lt;/h3&gt;
            &lt;p style="width: 530px"&gt; &lt;/p&gt;
            &lt;p style="width: 530px"&gt;Ever wanted a dbo.Split() function, but not had the time to debug it completely? &lt;/p&gt;
            &lt;p style="width: 530px"&gt;Let me guess - you are probably working on a stored procedure with 50 or more parameters; two or three of them are parameters of differing types, while the other 47 or so all of the same type (id1, id2, id3, id4, id5...).  Worse, you've found several other similar stored procedures with the ONLY DIFFERENCE being the number of like parameters taped to the end of the parameter list.&lt;/p&gt;
            &lt;p style="width: 530px"&gt;If this is the situation you find yourself in now, you may be wondering, "why am I working with all these different copies of what is basically the same stored procedure, and &lt;em&gt;why am I having to maintain changes in several different places&lt;/em&gt;?  Can't I have one stored procedure that accomplishes the job of all three?&lt;/p&gt;
            &lt;p style="width: 530px"&gt;My answer to you: YES! &lt;/p&gt;
            &lt;p style="width: 530px"&gt;Here is the Split() function I've created. &lt;/p&gt;
            &lt;table border="1" cellspacing="1" summary="The Split() Function is what SQL Server refers to as a 'table-valued-function'. In other words, it returns a table." cellpadding="1" width="430" align="left"&gt;
                &lt;caption&gt;&lt;/caption&gt;
                &lt;tbody&gt;
                    &lt;tr&gt;
                        &lt;td class="style1"&gt; 
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;/*********************************************************************&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;                                    Split.sql&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;*********************************************************************/&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;/*********************************************************************&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;Split a delimited string into sub-components and return them as a table.&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;Parameter 1: Input string which is to be split into parts.&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;Parameter 2: Delimiter which determines the split points in input string.&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;Works with space or spaces as delimiter. Split() is apostrophe-safe.&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;SYNTAX:&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;SELECT * FROM Split('Dvorak,Debussy,Chopin,Holst', ',')&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;SELECT * FROM Split('Denver|Seattle|San Diego|New York', '|')&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;SELECT * FROM Split('Denver is the super-awesomest city of them all.', ' ')&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;SELECT * FROM Split('abcdefghijklmnopqrstuvwxyz', 'hijk')&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;*********************************************************************/&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;USE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; AdventureWorks&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;IF&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: gray"&gt;EXISTS&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;span style="color: blue"&gt;SELECT&lt;/span&gt; &lt;span style="color: gray"&gt;*&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;FROM&lt;/span&gt; sysobjects&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;WHERE&lt;/span&gt; xtype &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'TF'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;AND&lt;/span&gt; &lt;span style="color: blue"&gt;name&lt;/span&gt; &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;'Split'&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;BEGIN&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;DROP&lt;/span&gt; &lt;span style="color: blue"&gt;FUNCTION&lt;/span&gt; Split&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;END&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;CREATE&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: blue"&gt;FUNCTION&lt;/span&gt; Split &lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      @InputString                  &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;8000&lt;span style="color: gray"&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      @Delimiter                    &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;50&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;)&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;RETURNS&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; @Items &lt;span style="color: blue"&gt;TABLE&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      Item                          &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;8000&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="color: gray; font-size: 10pt"&gt;)&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;AS&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;BEGIN&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;IF&lt;/span&gt; @Delimiter &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;' '&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @Delimiter &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;','&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @InputString &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia"&gt;REPLACE&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@InputString&lt;span style="color: gray"&gt;,&lt;/span&gt; &lt;span style="color: red"&gt;' '&lt;/span&gt;&lt;span style="color: gray"&gt;,&lt;/span&gt; @Delimiter&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;IF&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@Delimiter &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt; &lt;span style="color: gray"&gt;OR&lt;/span&gt; @Delimiter &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;''&lt;/span&gt;&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @Delimiter &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: red"&gt;','&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;--INSERT INTO @Items VALUES (@Delimiter) -- Diagnostic&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;--INSERT INTO @Items VALUES (@InputString) -- Diagnostic&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;DECLARE&lt;/span&gt; @Item                 &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;8000&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;DECLARE&lt;/span&gt; @ItemList       &lt;span style="color: blue"&gt;VARCHAR&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;8000&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;DECLARE&lt;/span&gt; @DelimIndex     &lt;span style="color: blue"&gt;INT&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ItemList &lt;span style="color: gray"&gt;=&lt;/span&gt; @InputString&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;SET&lt;/span&gt; @DelimIndex &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia"&gt;CHARINDEX&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@Delimiter&lt;span style="color: gray"&gt;,&lt;/span&gt; @ItemList&lt;span style="color: gray"&gt;,&lt;/span&gt; 0&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;WHILE&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@DelimIndex &lt;span style="color: gray"&gt;!=&lt;/span&gt; 0&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @Item &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia"&gt;SUBSTRING&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@ItemList&lt;span style="color: gray"&gt;,&lt;/span&gt; 0&lt;span style="color: gray"&gt;,&lt;/span&gt; @DelimIndex&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;INSERT&lt;/span&gt; &lt;span style="color: blue"&gt;INTO&lt;/span&gt; @Items &lt;span style="color: blue"&gt;VALUES&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@Item&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: green"&gt;-- Set @ItemList = @ItemList minus one less item&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @ItemList &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia"&gt;SUBSTRING&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@ItemList&lt;span style="color: gray"&gt;,&lt;/span&gt; @DelimIndex&lt;span style="color: gray"&gt;+&lt;/span&gt;1&lt;span style="color: gray"&gt;,&lt;/span&gt; &lt;span style="color: fuchsia"&gt;LEN&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@ItemList&lt;span style="color: gray"&gt;)-&lt;/span&gt;@DelimIndex&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @DelimIndex &lt;span style="color: gray"&gt;=&lt;/span&gt; &lt;span style="color: fuchsia"&gt;CHARINDEX&lt;/span&gt;&lt;span style="color: gray"&gt;(&lt;/span&gt;@Delimiter&lt;span style="color: gray"&gt;,&lt;/span&gt; @ItemList&lt;span style="color: gray"&gt;,&lt;/span&gt; 0&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt; &lt;span style="color: green"&gt;-- End WHILE&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;IF&lt;/span&gt; @Item &lt;span style="color: gray"&gt;IS&lt;/span&gt; &lt;span style="color: gray"&gt;NOT&lt;/span&gt; &lt;span style="color: gray"&gt;NULL&lt;/span&gt; &lt;span style="color: green"&gt;-- At least one delimiter was encountered in @InputString&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;BEGIN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;SET&lt;/span&gt; @Item &lt;span style="color: gray"&gt;=&lt;/span&gt; @ItemList&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;            &lt;span style="color: blue"&gt;INSERT&lt;/span&gt; &lt;span style="color: blue"&gt;INTO&lt;/span&gt; @Items &lt;span style="color: blue"&gt;VALUES&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@Item&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;END&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: green"&gt;-- No delimiters were encountered in @InputString, so just return @InputString&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;ELSE&lt;/span&gt; &lt;span style="color: blue"&gt;INSERT&lt;/span&gt; &lt;span style="color: blue"&gt;INTO&lt;/span&gt; @Items &lt;span style="color: blue"&gt;VALUES&lt;/span&gt; &lt;span style="color: gray"&gt;(&lt;/span&gt;@InputString&lt;span style="color: gray"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;      &lt;span style="color: blue"&gt;RETURN&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="color: blue; font-size: 10pt"&gt;END&lt;/span&gt;&lt;span style="font-size: 10pt"&gt; &lt;span style="color: green"&gt;-- End Function&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="font-size: 10pt"&gt;GO&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt; &lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;---- Set Permissions&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;--GRANT SELECT ON Split TO UserRole1&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="line-height: normal"&gt;&lt;span style="width: 500px; color: green; font-size: 10pt"&gt;--GRANT SELECT ON Split TO UserRole2&lt;/span&gt;&lt;/div&gt;
                        &lt;div style="margin: 0in 0in 10pt"&gt;&lt;span style="line-height: 115%; color: green; font-size: 10pt"&gt;--GO&lt;/span&gt;&lt;/div&gt;
                        &lt;/td&gt;
                    &lt;/tr&gt;
                &lt;/tbody&gt;
            &lt;/table&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;br /&gt;
            &lt;p style="width: 530px"&gt; &lt;/p&gt;
            &lt;p style="width: 530px"&gt;The syntax is basically as follows:&lt;/p&gt;
            &lt;p style="width: 530px"&gt;&lt;span style="color: #0000ff"&gt;SELECT &lt;font color="#000000"&gt;&amp;lt;fields&amp;gt;&lt;/font&gt;&lt;br /&gt;
            &lt;/span&gt;&lt;span style="color: #0000ff"&gt;FROM &lt;/span&gt;Table 1&lt;br /&gt;
            &lt;span style="color: #808080"&gt;JOIN &lt;/span&gt;Table 2&lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;ON &lt;/span&gt;...&lt;br /&gt;
            &lt;span style="color: #808080"&gt;JOIN &lt;/span&gt;Table 3&lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;ON &lt;/span&gt;... &lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;WHERE &lt;/span&gt;&lt;br /&gt;
            &lt;em&gt;LOGICAL CONDITION A&lt;br /&gt;
            &lt;/em&gt;&lt;span style="color: #808080"&gt;AND &lt;/span&gt;&lt;em&gt;LOGICAL CONDITION B&lt;br /&gt;
            &lt;/em&gt;&lt;span style="color: #808080"&gt;AND &lt;/span&gt;&lt;em&gt;LOGICAL CONDITION C&lt;br /&gt;
            &lt;/em&gt;&lt;span style="color: #808080"&gt;AND &lt;/span&gt;TABLE2.Id &lt;span style="color: #808080"&gt;IN &lt;/span&gt;(&lt;span style="color: #0000ff"&gt;SELECT &lt;/span&gt;* &lt;span style="color: #0000ff"&gt;FROM &lt;/span&gt;Split(@IdList, &lt;span style="color: #ff0000"&gt;','&lt;/span&gt;))&lt;/p&gt;
            &lt;p style="width: 530px"&gt;@IdList is a parameter passed into the function, and the comma (',') is the delimiter you have chosen to split the parameter list on.&lt;/p&gt;
            &lt;p style="width: 530px"&gt;&lt;br /&gt;
            You can also use it like this:&lt;/p&gt;
            &lt;p style="width: 530px"&gt;&lt;span style="color: #0000ff"&gt;SELECT &lt;/span&gt;&amp;lt;fields&amp;gt;&lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;FROM &lt;/span&gt;Table 1&lt;br /&gt;
            &lt;span style="color: #808080"&gt;JOIN &lt;/span&gt;Table 2&lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;ON &lt;/span&gt;...&lt;br /&gt;
            &lt;span style="color: #808080"&gt;JOIN &lt;/span&gt;Table 3&lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;ON &lt;/span&gt;... &lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;WHERE &lt;/span&gt;&lt;br /&gt;
            &lt;em&gt;LOGICAL CONDITION A&lt;/em&gt;&lt;br /&gt;
            &lt;span style="color: #808080"&gt;AND &lt;/span&gt;&lt;em&gt;LOGICAL CONDITION B&lt;/em&gt;&lt;br /&gt;
            &lt;span style="color: #808080"&gt;AND &lt;/span&gt;&lt;em&gt;LOGICAL CONDITION C&lt;/em&gt;&lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;HAVING &lt;/span&gt;&lt;span style="color: #ff00cc"&gt;COUNT&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;SELECT &lt;/span&gt;* &lt;span style="color: #0000ff"&gt;FROM &lt;/span&gt;Split(@IdList, &lt;span style="color: #ff0000"&gt;','&lt;/span&gt;)&lt;/p&gt;
            &lt;p style="width: 530px"&gt;&lt;br /&gt;
            Similarly, it can be used in other aggregate functions at run-time:&lt;/p&gt;
            &lt;p style="width: 530px"&gt;&lt;span style="color: #0000ff"&gt;SELECT &lt;/span&gt;&lt;span style="color: #ff00cc"&gt;MIN&lt;/span&gt;(&lt;span style="color: #0000ff"&gt;SELECT &lt;/span&gt;* &lt;span style="color: #0000ff"&gt;FROM &lt;/span&gt;Split(@IdList, &lt;span style="color: #ff0000"&gt;','&lt;/span&gt;), &amp;lt;fields&amp;gt;&lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;FROM &lt;/span&gt;Table 1&lt;br /&gt;
            &lt;span style="color: #808080"&gt;JOIN &lt;/span&gt;Table 2&lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;ON &lt;/span&gt;...&lt;br /&gt;
            &lt;span style="color: #808080"&gt;JOIN &lt;/span&gt;Table 3&lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;ON &lt;/span&gt;... &lt;br /&gt;
            &lt;span style="color: #0000ff"&gt;WHERE &lt;/span&gt;&lt;br /&gt;
            &lt;em&gt;LOGICAL CONDITION A&lt;br /&gt;
            &lt;/em&gt;&lt;span style="color: #808080"&gt;AND &lt;/span&gt;&lt;em&gt;LOGICAL CONDITION B&lt;/em&gt;&lt;br /&gt;
            &lt;span style="color: #808080"&gt;AND &lt;/span&gt;&lt;em&gt;LOGICAL CONDITION C&lt;br /&gt;
            &lt;/em&gt;&lt;span style="color: #0000ff"&gt;GROUP BY &lt;/span&gt;&amp;lt;fields&amp;gt;&lt;/p&gt;
            &lt;p style="width: 530px"&gt;&lt;br /&gt;
            Now that I've (hopefully effectively) explained the benefits to using this function and implementing it in one or more of your database objects, let me warn you of a caveat that you are likely to encounter.  You may have a team member who waits until the right moment to ask you a pointed question: "Doesn't this function just do the same thing as using the IN function?  Why didn't you just use that instead?  In other words, why bother with this function?"&lt;/p&gt;
            &lt;p style="width: 530px"&gt;What's happening is, one or more team members has failed to understand the reason for implementing this kind of function in the first place.  (Note: this is THE MOST IMPORTANT ASPECT OF THIS POST).&lt;/p&gt;
            &lt;p style="width: 530px"&gt;Allow me to outline a few pros to implementing this function, so you may effectively parry this question.  Touche.&lt;/p&gt;
            &lt;p style="width: 530px"&gt;1) Code consolidation.  You don't have to maintain what is basically the same code and logic, but with varying numbers of the same parameter in several SQL objects. &lt;/p&gt;
            &lt;p style="width: 530px"&gt;I'm not going to go into the cons related to using this function, because the afore mentioned team member is probably more than adept than I at pointing these out.  Remember, the real positive contribution is you are decreasing the liklihood that your team fails to update all (x) duplicate copies of what are basically the &lt;em&gt;same &lt;/em&gt;stored procedure, and so on...  This is the classic downside to duplicate code.  It is a virus, and you should kill it.&lt;/p&gt;
            &lt;p style="width: 530px"&gt;Rather than fending off criticism, you might be better off responding with your own question: "Would you rather maintain the same logic in multiple different stored procedures, and hope that the team doesn't forget to always update all of them at the same time?".  In his head, your team member might be thinking, "YES, I would like to maintain several different copies of the same stored procedure", although the response you get is anyone's guess.&lt;/p&gt;
            &lt;p style="width: 530px"&gt;2) Added flexibility - you can use the Split function elsewhere, and for splitting your data in different ways.  Plus, you can use any kind of delimiter you wish.  How can you know today the ways in which you might want to examine your data tomorrow?  Segue to my next point.&lt;/p&gt;
            &lt;p style="width: 530px"&gt;3) Because the function takes a delimiter parameter, you can split the data in any number of ways.  This greatly increases the utility of such a function and enables your team to work with the data in a variety of different ways in the future.  You can split on a single char, symbol, word, or group of words.  You can split on spaces.  (The list goes on... test it out).&lt;/p&gt;
            &lt;p style="width: 530px"&gt;Finally, you can dynamically define the behavior of a stored procedure (or other SQL object) at run time, through the use of this function.  Rather than have several objects that accomplish almost the same thing, why not have only one instead?&lt;/p&gt;
            &lt;/td&gt;
        &lt;/tr&gt;
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;p style="width: 530px"&gt; &lt;/p&gt;&lt;img src="http://geekswithblogs.net/HighAltitudeCoder/aggbug/140251.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/HighAltitudeCoder/comments/140251.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/HighAltitudeCoder/comments/commentRss/140251.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/HighAltitudeCoder/services/trackbacks/140251.aspx</trackback:ping>
    </entry>
    <entry>
        <title>High-Powered Sites for low Cost</title>
        <link rel="self" type="text/html" href="http://geekswithblogs.net/HighAltitudeCoder/archive/2010/05/23/high-powered-sites-for-low-cost.aspx" />
        <id>http://geekswithblogs.net/HighAltitudeCoder/archive/2010/05/23/high-powered-sites-for-low-cost.aspx</id>
        <published>2010-05-23T23:45:25-06:00:00</published>
        <updated>2011-03-27T14:15:47Z</updated>
        <summary type="html">Excellent learning for the job-seeker.</summary>
        <content type="html">&lt;p&gt;Ahh, I am experiencing the intimidation of my very first post - visible by the whole world.&lt;/p&gt;
&lt;p&gt;Ok, here goes.&lt;/p&gt;
&lt;p&gt;This first post is nothing exceptional.  It is simply a recommendation based (fittingly, I suppose) upon the job search you may be gearing up for.  I find myself in this very situation right now.  And, I will take my own recommendation after posting this entry.&lt;/p&gt;
&lt;p&gt;Job-Seekers:&lt;/p&gt;
&lt;p&gt;To the left you will notice two links under "Recommended Learning".  I have found these links to be invaluable when it comes to re-tooling, re-familiarizing, or otherwise resharping my skills when looking for that next job.&lt;/p&gt;
&lt;p&gt;Often, you will find job-postings with the text, usually posted after a laborious list of qualifications indicating the company's desire to hire candidates who know what they are doing: "...Looking for a candidate who can hit the ground running...".  The interesting thing about this post to me is I've encountered many individuals who, after speaking and working with them for some time, I've realized are perfectly capable of hitting the ground running - and FAST.  But what if they speed off in the wrong direction?&lt;/p&gt;
&lt;p&gt;The next time you spearhead a major task in your job, ask yourself: Am I headed in the wrong direction?  There are many ways to do this.  In fact, I've found in this new field there are more ways to steer your project in the wrong direction than there are good ones.  You might be carrying forward a pattern from an loder language (C++?) that does not work in the newer language.  Or, perhaps, you are following the advice of an 'expert' who prefers to stay within his comfort zone, rather than explore newer, more innovative or practical solutions. &lt;/p&gt;
&lt;p&gt;In my own personal past experiences, regardless of the reason I may have gotten started heading off into the weeds, the clue I learned to pay attention to more and more is that nagging voice in the back of my mind.  Though annoying, usually I end up giving in to what it suggesting, which is: "There has GOT to be a better way of doing this..."  I would compare this to the "&lt;a id="Code Smells" target="_blank" href="http://www.codinghorror.com/blog/2006/05/code-smells.html" name="CodeSmellsLink"&gt;bad smells&lt;/a&gt;"described in the SW development community.&lt;/p&gt;
&lt;p&gt;I don't want to suggest that every one of my posts will fall into the "right direction" category, however I do think a healthy dose of introspection of the pros and cons will always be beneficial before you set off.&lt;/p&gt;
&lt;p&gt;That said, allow me to expound on the previously mentioned links.&lt;/p&gt;
&lt;p&gt;These web sites are invaluable.  They demonstrate the capabilities of existing as well as new and upcoming tools available in several IDE's.  I've viewed many tutorials in LearnVisualStudio.NET, and only one or two so far in TrainingSpot, however I've been delighted in their simplicity and straightforward approach to proper usage of the particular tool or concept being discussed.  They have not (so far in my experience) demonstrated ways in which to use the tools that become cumbersome, impractical, or error-prone.&lt;/p&gt;
&lt;p&gt;Each website has step-by-step videos that can be paused, replayed, and most importantly, they are done in real time.  As the author is typing, the viewer gets to experience the coding experience from a first-person perspective, including syntax errors, unexpected behaviors, IDE setup idiosyncracies, everything.  A subtle value I've gained from these videos is that a certain degree of confusion and introspection is normal when working with new tools and exploring new paths. &lt;/p&gt;
&lt;p&gt;They (as well as your own experience) are not to be feared, but enjoyed.  I highly recommend them.&lt;/p&gt;
&lt;p&gt;Good work, guys!&lt;br /&gt;
 &lt;/p&gt;&lt;img src="http://geekswithblogs.net/HighAltitudeCoder/aggbug/140032.aspx" width="1" height="1" /&gt;</content>
        <wfw:comment>http://geekswithblogs.net/HighAltitudeCoder/comments/140032.aspx</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://geekswithblogs.net/HighAltitudeCoder/comments/commentRss/140032.aspx</wfw:commentRss>
        <trackback:ping>http://geekswithblogs.net/HighAltitudeCoder/services/trackbacks/140032.aspx</trackback:ping>
    </entry>
</feed>
