Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
ORDER BY CASE
Message
 
 
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Titre:
Divers
Thread ID:
01436398
Message ID:
01436400
Vues:
57
I'm not sure what you're telling us here? Do you mean to use this technique then the Order By field is passed as a parameter?

If yes, then

case @OrderBy when 'DateField' then DateField when 'SomeOtherField' then SomeOtherField end

Also, what about DESC / ASC ?

>I just stumbled across this little gem
>
>
>declare @x table ( i1 int, i2 int )
>insert into @x values ( 1, 2 )
>insert into @x values ( 2, 1 )
>
>select *
>   from @x
>   order by
>       case when 1 = 1
>          then i1
>          else i2
>       end
>
>select *
>   from @x
>   order by
>       case when 1 != 1
>          then i1
>          else i2
>       end
>
>
>Works in 2000 and 2005. The example is a bogus when clause, more practically it'll be a parameter to your stored proc to pick the column to use for the output order. Go purge all that dynamic SQL you have just to get alternate output order.
If it's not broken, fix it until it is.


My Blog
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform