Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Dynamic Order By

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2001-12-11 : 09:46:51
Amit writes "Hello,

I have a table containing 10 columns...col1, col2, ....col9, col10.
I need to sort by 6 columns in the table, but the sort order changes every time...
i.e : the first time it is ORDER BY col5, col6, col7, col8, col9, col10 DESC
the next time i run the procedure, it could be
ORDER BY col7, col10, col5, col9, col8, col6 DESC..

How do i implement such a scenario using the Order By clause?"

andre
Constraint Violating Yak Guru

259 Posts

Posted - 2001-12-11 : 10:02:34
You have a couple of options:
1) Use a case statement in the ORDER BY Clause

ORDER BY
CASE WHEN <condition 1> THEN col1,col2,col3,...
WHEN <condition 2> THEN col2,col1,col3...
ELSE col6,col5,col4

2) Use dynamic SQL.

Here's an article that will help:
[url]http://www.sqlteam.com/item.asp?ItemID=2209[/url]

Go to Top of Page
   

- Advertisement -