我有以下格式的存储过程
SELECT key1 AS FirstKey, key2 AS SecondKey FROM table
ORDER BY
CASE @sortOrderParam WHEN 'ASC' THEN
CASE UPPER(@sortColumn)
WHEN 'blabla' THEN FirstKey
WHEN 'blibli' THEN FirstKey, SecondKey --Syntax error
END
END ASC,
CASE @sortOrderParam WHEN 'DESC' THEN
...
END DESC
但是,在某些情况下尝试按两列排序时出现语法错误。我怎么能这样做?
谢谢!
答案 0 :(得分:1)
Case只能返回一个特定值,而不是一组列。你需要以不同的方式分解它,或许像:
SELECT key1 AS FirstKey, key2 AS SecondKey FROM table
ORDER BY
FirstKey,
CASE @sortOrderParam WHEN 'ASC' THEN
CASE UPPER(@sortColumn)
WHEN 'blibli' THEN SecondKey --only relevant when blibli
END
END ASC,
CASE @sortOrderParam WHEN 'DESC' THEN
...
END DESC
您还可以通过附加适当的数据来构建排序列。