我们说我有这个SP:
ALTER PROCEDURE [dbo].[testsp]
-- Add the parameters for the stored procedure here
@start datetime,
@end datetime,
@sort int = 1
AS
BEGIN
set nocount on;
select
t1.Name,
sum(t2.sum) as Salary
from
persons t1 inner join
payments t2 on(t1.ID = t2.PersonID)
where
t1.Active = 1 and
t2.TimeOfTransfer between @start and @end
group by
t1.Name
order by
[Salary] desc
END
如何编写我的订单,让它根据@sort中的值进行升序或降序排序?
答案 0 :(得分:1)
...
ORDER
BY CASE WHEN @sort = 1 THEN Salary END ASC
, CASE WHEN @sort = 0 THEN Salary END DESC
P.S。我可能会将您的@sort
参数的定义更改为位(仅限值0或1)或 char(4)(对于“ASC”或'DESC'):-)