我有以下 SP 用于对新闻文章列表进行分页。正如您可能猜到的,@count
是要返回的行数,@start
是从中选择行的索引(按内部查询排序),@orderby
表示要排序的列,并@orderdir
表示是按一个方向排序还是按另一个方向排序。在我添加参数之前,我的原始查询在
这里。@orderdir
ALTER PROCEDURE [mytable].[news_editor_paginate]
@count int,
@start int,
@orderby int,
@orderdir int
AS
BEGIN
SET NOCOUNT ON;
SELECT TOP (@count) * FROM
(
SELECT ne.*,n.publishstate,
(CASE WHEN @orderdir = 1 THEN
ROW_NUMBER() OVER (
ORDER BY
CASE WHEN @orderby = 0 THEN ne.[time] END DESC,
CASE WHEN @orderby = 1 THEN ne.lastedit END DESC,
CASE WHEN @orderby = 2 THEN ne.title END ASC
)
WHEN @orderdir = 2 THEN
ROW_NUMBER() OVER (
ORDER BY
CASE WHEN @orderby = 0 THEN ne.[time] END ASC,
CASE WHEN @orderby = 1 THEN ne.lastedit END ASC,
CASE WHEN @orderby = 2 THEN ne.title END DESC
)
END
) AS num
FROM news_edits AS ne
LEFT OUTER JOIN news AS n
ON n.editid = ne.id
)
AS a
WHERE num > @start
END
现在实际上没有任何问题,但@orderby
参数不起作用。如果提供 1 作为@orderdir
参数,它将给我完全相同的结果,就像我提供 2 作为该参数一样。