当前位置: 代码迷 >> Sql Server >> 求:存储过程根据参数不同来排序,该怎么处理
  详细解决方案

求:存储过程根据参数不同来排序,该怎么处理

热度:74   发布时间:2016-04-27 13:41:08.0
求:存储过程根据参数不同来排序
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
  相关解决方案