求:存储过程根据参数不同来排序
Procedure @P1 int as
Select a, b, c from table1
order by ????
当P1=1 时按a排序,
当P1=2 时按b排序,
当P1=3 时按c排序。
怎么写? 谢谢
[解决办法]
Procedure @P1 int as
Select a, b, c from table1
order by case @P1 when 1 then a when 2 then b when 3 then c
[解决办法]
- SQL code
gocreate proc pro_tracy@P1 intasif @P1=1beginSelect a, b, c from table1 order by aendif @P1=2beginSelect a, b, c from table1 order by bendif @P1=3beginSelect a, b, c from table1 order by cend
[解决办法]
还可以使用order by case when @P1=1 then a when @P1=2 then b when @P1=3 then c end