我有一个带有输入参数的存储过程,现在根据此参数,我的“order by”语句将发生变化,就像如果输入参数是“ID”(int 类型列),则按 ID 排序,如果是“ProductType' 然后按 Producttype 排序,如果是 'IssueDate' 则应按 IssueDate 排序。
现在我在 SP 中添加了 2 个 if else 语句,但是这个解决方案不可扩展,所以我的问题是有没有更好的方法。
最佳答案
如果使用 SQL Server,您可以使用 case 语句
order by
case inputparameter
when 'id' then id
when 'productType' then ProductType
else defaultOrderBy
end
关于sql - 如何在 'order by' 中添加条件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4213270/