You can write several cases, even if they all have the same condition.
ORDER BY CASE @OrderByColumn WHEN 1 THEN Forename END DESC, CASE @OrderByColumn WHEN 1 THEN Date END DESC, CASE @OrderByColumn WHEN 1 THEN Location END DESC, CASE @OrderByColumn WHEN 2 THEN Surname END ASC
In fact, you are not specifying a column to sort, but an expression.
The case statement returns zero if the condition is not met, so this actually means:
CASE @OrderByColumn WHEN 1 THEN Forename ELSE NULL END
So if @OrderByColumn is not 1, then the operator always returns NULL. By the way, this does not exclude it from sorting, but combines all of these lines as a result, making SurName decisive sorting in this group of lines.
Goleztrol
source share