<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Ken McNamee</title><link>http://blogs.vertigosoftware.com/kenm/default.aspx</link><description>Senior Software Engineer</description><dc:language>en-US</dc:language><generator>CommunityServer 1.1 (Build: 1.1.0.50615)</generator><item><title>Dynamic ORDER BY Clause</title><link>http://blogs.vertigosoftware.com/kenm/archive/2005/12/06/Dynamic_ORDER_BY_Clause.aspx</link><pubDate>Tue, 06 Dec 2005 22:25:00 GMT</pubDate><guid isPermaLink="false">fcb82b5c-78c7-46a5-b6ff-1ef27e7d7271:1720</guid><dc:creator>kenm</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.vertigosoftware.com/kenm/comments/1720.aspx</comments><wfw:commentRss>http://blogs.vertigosoftware.com/kenm/commentrss.aspx?PostID=1720</wfw:commentRss><description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;I was curious whether you could sort the resultset of a sproc based on dynamic parameters. For instance, I may want to sort a list of Northwind.Employees by LastName in some cases or HireDate in others. There are a few different ways I could do this:&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Create different sprocs with the same SELECT and WHERE clauses but different ORDER BY clauses. This would be the most efficient from a database performace standpoint but the duplication of logic would be bad for code maintenance, especially if the sproc has very complex logic.&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Leave out the ORDER BY clause or give it the most commonly used sort column, then re-sort the data in the middle-tier or client. This is a viable alternative but can cause performance issues on a web server.&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;Pass a parameter into the sproc that tells it which column to sort on.&lt;/FONT&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;The last option seemed to me to be the best since SQL Server is much better optimized&amp;nbsp;at sorting data. However, I wasn't sure if it was possible to put conditional logic in the ORDER BY clause or how to do it. After a quick search I found this &lt;A href="http://www.4guysfromrolla.com/webtech/010704-1.shtml" target=_blank&gt;article&lt;/A&gt;&amp;nbsp;which described exactly what I needed but with one caveat: &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;"&lt;EM&gt;Dynamic ordering doesn't work for mixed data types. That is, if you have fields of differing data types that you want to order by - such as varchars and decimals, as is the case with LastName and Salary above - then in the CASE statement you need to use CONVERT to convert the mismatched data type to the same data type as the others.&lt;/EM&gt;"&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;This was not good because the casting and converting could cause my sort to be unreliable for some datatypes, such as ordering by datetimes that have been cast as varchars. I did a little testing and found that the article is wrong about this point. It &lt;STRONG&gt;is&lt;/STRONG&gt; possible to have the dynamic ORDER BY return different datatypes if you use a separate CASE statement for each possible option. Here's an example:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color=#0000ff&gt;DECLARE&lt;/FONT&gt; @orderBy varchar(50)&lt;BR&gt;&lt;FONT color=#0000ff&gt;SET&lt;/FONT&gt; @orderBy = &lt;FONT color=#ff0000&gt;'LastName'&lt;/FONT&gt;&lt;BR&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;SELECT&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#808080&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;*&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;FROM&lt;/FONT&gt;&lt;BR&gt;&lt;FONT size=+0&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/FONT&gt;Employees&lt;BR&gt;&lt;FONT color=#0000ff&gt;ORDER&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;BY&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CASE&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHEN&lt;/FONT&gt; @orderBy = &lt;FONT color=#ff0000&gt;'LastName'&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;THEN&lt;/FONT&gt; LastName&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;END&lt;/FONT&gt;&lt;FONT color=#808080&gt;,&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;CASE&lt;/FONT&gt;&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;WHEN&lt;/FONT&gt; @orderBy = &lt;FONT color=#ff0000&gt;'HireDate'&lt;/FONT&gt; &lt;FONT color=#0000ff&gt;THEN&lt;/FONT&gt; HireDate&lt;BR&gt;&lt;FONT color=#0000ff&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;END&lt;/FONT&gt;&lt;/FONT&gt; &lt;/P&gt;&lt;img src="http://blogs.vertigosoftware.com/aggbug.aspx?PostID=1720" width="1" height="1"&gt;</description></item></channel></rss>