Currently I have the following query in TSQL:
select *
from table
order by
col1, col2
But there’s a condition (@cond = 1)
, which if it is true, then I should rather sort by column col0 DESC
and then use the rest:
select *
from table
order by
case when @cond = 1 then col0 end DESC,
col1,
col2
which works fine when the condition is true. But if it’s false, then I have an error message (in that case I want to have the initial query).
How to make the trick here?