seajey: Electronic Knight (Default)
[personal profile] seajey
Как-то столкнулся с необходимостью, в процедуре, задать порядок сортировки результирующего запроса через входящую переменную. Динамический SQL - не выход, а другого способа, кроме как лесенки IF'ов и множественного повторения SELECT'а не придумалось.

Задача возникла снова, и в этот раз получилось найти более лаконичный, в тоже время вполне понятный вариант. И так:

DECLARE @SortDirection dShortString SET @SortDirection = 'DESC' 

SELECT * 
FROM wlmUsers
ORDER BY 
CASE 
    WHEN @SortDirection = 'ASC' 
        THEN wlmUserID 
END ASC,
CASE    
    WHEN @SortDirection = 'DESC' 
        THEN wlmUserID 
END DESC
From:
Anonymous( )Anonymous This account has disabled anonymous posting.
OpenID( )OpenID You can comment on this post while signed in with an account from many other sites, once you have confirmed your email address. Sign in using OpenID.
User
Account name:
Password:
If you don't have an account you can create one now.
Subject:
HTML doesn't work in the subject.

Message:

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org


 
Notice: This account is set to log the IP addresses of everyone who comments.
Links will be displayed as unclickable URLs to help prevent spam.

Profile

seajey: Electronic Knight (Default)
SeaJey

September 2013

S M T W T F S
123456 7
8 910 11121314
15161718192021
22232425262728
2930     

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags
Page generated Jul. 21st, 2017 08:41 am
Powered by Dreamwidth Studios