在本文的帮助和 SO 专家最近的回答下,我得到了以下内容,这将帮助我有效地翻阅一组记录。
我想我的最后几个问题是
- 看看我如何
Total Number of Records
在名为“Total”的 SQL CTE 末尾包含有效负载。你会这样做吗? - 还有其他建议吗?更简洁或改进的潜在领域?
Return Total Number of Pages
DECLARE @page_size INT = 5;
DECLARE @page_nbr INT = 4;
DECLARE @search NVARCHAR(MAX) = '';
DECLARE @sort_order INT = 2;
WITH AllProducts
AS
(
SELECT *,
CASE @sort_order
WHEN 1 THEN ROW_NUMBER() OVER ( ORDER BY ProductID )
WHEN 2 THEN ROW_NUMBER() OVER ( ORDER BY ProductName )
END AS 'Seq'
FROM Products
),
Filtered
AS
(
SELECT * FROM AllProducts
WHERE ProductName like '%'+@search+'%'
OR
@search is null
)
SELECT (select COUNT(*) from Filtered) as 'Total', * FROM Filtered
WHERE seq > (@page_nbr - 1) * @page_size
AND seq <= @page_nbr * @page_size