日期:2014-05-18  浏览次数:20490 次

求:存储过程根据参数不同来排序
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

go
create proc pro_tracy
@P1 int
as
if @P1=1
begin
Select a, b, c from table1 order by a
end
if @P1=2
begin
Select a, b, c from table1 order by b
end
if @P1=3
begin
Select a, b, c from table1 order by c
end

------解决方案--------------------
还可以使用order by case when @P1=1 then a when @P1=2 then b when @P1=3 then c end