Home All Groups Group Topic Archive Search About

ASC/DESC as SP Keywords?

Author
12 Jan 2006 2:42 PM
Chris Ashley
Can I do something like this:

CASE WHEN @orderBy = 'ASC' THEN ASC ELSE DESC END

So I can order by asc or desc depending on a stored procedure parameter?

Author
12 Jan 2006 2:56 PM
Raymond D'Anjou
"Chris Ashley" <chris.ashl***@gmail.com> wrote in message
news:1137076942.622670.97210@g44g2000cwa.googlegroups.com...
> Can I do something like this:
>
> CASE WHEN @orderBy = 'ASC' THEN ASC ELSE DESC END
>
> So I can order by asc or desc depending on a stored procedure parameter?

Unless someone can see something wrong with this:

declare @orderBy char(3)
set @orderBy  = 'asc'

select .. from table order by
case @orderBy when 'asc' then colName end asc,
case @orderBy when 'des' then colName end desc
Author
12 Jan 2006 3:04 PM
Raymond D'Anjou
Show quote
"Raymond D'Anjou" <rdanjou@canatradeNOSPAM.com> wrote in message
news:O5vTIi4FGHA.3976@TK2MSFTNGP11.phx.gbl...
> "Chris Ashley" <chris.ashl***@gmail.com> wrote in message
> news:1137076942.622670.97210@g44g2000cwa.googlegroups.com...
>> Can I do something like this:
>>
>> CASE WHEN @orderBy = 'ASC' THEN ASC ELSE DESC END
>>
>> So I can order by asc or desc depending on a stored procedure parameter?
>
> Unless someone can see something wrong with this:
>
> declare @orderBy char(3)
> set @orderBy  = 'asc'
>
> select .. from table order by
> case @orderBy when 'asc' then colName end asc,
> case @orderBy when 'des' then colName end desc

This article explains it a lot better:
http://www.aspfaq.com/show.asp?id=2501

AddThis Social Bookmark Button