如何在ROW_NUMBER中计算总和

时间:2017-12-01 10:38:41

标签: sql-server stored-procedures

请帮帮我。如何计算ROW_NUMBER总记录数 我有一张表,CustTable:

ID   Name 
1    John
2    Mick
3    MD
4    Smith

如果用户输入的总行数超过总行数,那么我想进行分页

ALTER PROCEDURE CustPaginantion (@PageNum int,@TotalPages int)
AS
BEGIN
    SELECT ROW_NUMBER() OVER (ORDER BY CustId) AS UNIQID,
           COUNT(*) OVER(ORDER BY CustId) AS cnt,
           CustId,
           CustName,
           CustAddress
    FROM Customer
    ORDER BY UNIQID
    OFFSET (@PageNum-1)*@TotalPages ROWS
    FETCH NEXT @TotalPages ROWS ONLY
END

1 个答案:

答案 0 :(得分:1)

SELECT ROW_NUMBER() OVER (ORDER BY CustId) AS UNIQID,
       COUNT(*) OVER(ORDER BY CustId) AS cnt,
       CustId,
       CustName,
       CustAddress
FROM Customer
ORDER BY UNIQID
OFFSET (@PageNum-1)*@TotalPages ROWS
FETCH NEXT @TotalPages ROWS ONLY

IF @@ROWCOUNT > @TotalPages
    RAISERROR(' user enters more than the total rows',1,1)