且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 CASE WHEN 的 ORDER BY 子句中的错误

更新时间:2023-11-22 16:04:28

一些搜索这条线帮助我了解更多..

按表达式排序必须计算为常量

ordering by an expression must evaluate to a constant

正如拉马克指出的那样,1=0 计算结果为 false 并且您没有定义 else 条件..所以 null 是未定义的,它会引发错误

so as Lamak pointed out,1=0 evaluates to false and you didn't define an else condition..so null is undefined and it throws error

摆脱像下面这样的尝试

ORDER BY CASE WHEN 1 = 1 THEN
              MyColumn
         END DESC,
         CASE WHEN 1 = 0 THEN
              col2 else col2--not your column,added this to make example clearer
         END ASC

还要注意,order by 中的表达式必须是唯一的,因此您的查询将不起作用(即使它成功)并抛出不同的错误,您也可以使用 ISNULL

also beware ,expressions in order by must be unique,so your query won't work(even if it succeeds) and throws different error,you can use ISNULL as well