SELECT * FROM TableName WHERE ORDER BY CASE @OrderByColumn WHEN 1 THEN Forename WHEN 2 THEN Surname END;
我有一个上面的声明,让我动态选择如何订购查询的结果.但是,如何指定我想要Forename命令DESC和Surname ASC?
解决方法
您需要将ORDER BY拆分为两部分:
SELECT * FROM TableName WHERE ORDER BY (CASE @OrderByColumn WHEN 1 THEN Forename END) DESC -- Forename --> descending,(CASE @OrderByColumn WHEN 2 THEN Surname END) ASC -- Surname --> ascending