CREATE PROCEDURE GetSortedMovies
(
@SortExpression NVarChar(100),
@StartRowIndex INT,
@MaximumRows INT
)
AS
-- 创建一个临时表存储查询结果
CREATE TABLE #PageIndex
(
IndexId INT IDENTITY (1,1) NOT NULL,
RecordId INT
)
-- 插入临时表
INSERT INTO #PageIndex (RecordId)
SELECT Id FROM Movies
ORDER BY
CASE WHEN @SortExpression='Id' THEN Id END ASC,
CASE WHEN @SortExpression='Id DESC' THEN Id END DESC,
CASE WHEN @SortExpression='Title' THEN Title END ASC,
CASE WHEN @SortExpression='Title DESC' THEN Title END DESC,
CASE WHEN @SortExpression='Description' THEN Description END ASC,
CASE WHEN @SortExpression='Description DESC' THen Description END DESC
-- 得到页数
SELECT Id,Title,Description FROM Movies
INNER JOIN #PageIndex WITH (nolock)
ON Movies.Id = #PageIndex.RecordId
WHERE #PageIndex.IndexId >@StartRowIndex
AND #PageIndex.IndexId <(@StartRowIndex + @MaximunRows + 1)
ORDER BY #PageIndex.IndexId
SELECT t.字段名 FROM (SELECT 字段(可以是多表关联的字段) ,
row_number() over(order by 某个排序字段 desc) r
FROM 表名 where 过滤条件 ) t
where t.r <= (每页显示条数*要查询的页) and t.r >(每页显示条数*(要查询的页-1))
有疑问可以再问
望采纳
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)