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
 Transact-SQL (2000)
 Dynamic ORDER BY with variable sort

Author  Topic 

brubaker
Starting Member

15 Posts

Posted - 2001-11-26 : 19:01:51
I've read the article on dynamic ORDER BYs, but I can't get a dynamic sort to work.

I want to sort by ASC for some fields, but DESC for others. Think of a golf example: sort by top money winners should be descending, but rank or handicap should be ascending.

I've successfully implemented a CASE statement to dynamically choose which field to order by, but if I then use a case statement for the sort order, I get an error.

Any thoughts?

royv
Constraint Violating Yak Guru

455 Posts

Posted - 2001-11-27 : 09:48:09
You cannot dynamically control whether it will be ASC or DESC. You will have to either use dynamic sql or you can use 2 stored procedures, one for ASC or one for DESC. For my situation I used 3 stored procedures, 1 stored procedure to accept all parameters and then decide based on what type of sort, it calls one of the other 2 stored procedures.

*************************
Just trying to get things done
Go to Top of Page

Arnold Fribble
Yak-finder General

1961 Posts

Posted - 2001-11-27 : 11:03:37
If they're all numeric, just negate the value.


Go to Top of Page
   

- Advertisement -