使用 ROW_NUMBER() OVER 和 COUNT(*) 时,如何设置此 tsql proc 的 out @Total 参数?
ALTER proc [Generic].[proc_GetPartsForUser_BySupplier_ByCategory]
@UserID UNIQUEIDENTIFIER,
@SupplierID INT,
@CategoryID INT,
@StartIndex INT,
@PageSize INT,
@Total INT out
AS
SET NOCOUNT ON;
SET @StartIndex = @StartIndex + 1
BEGIN
SELECT *
FROM (
SELECT *, ROW_NUMBER() OVER (ORDER BY ID ASC) AS RowNum, COUNT(*) OVER() AS Total
FROM (
SELECT p.*,
s.Name'SupplierName',s.Email'SupplierEmail',s.Phone'SupplierPhone'
FROM [Generic].[Part] p WITH(NOLOCK) JOIN
Generic.Supplier s WITH(NOLOCK) ON p.SupplierID = s.ID
WHERE p.ID IN(SELECT up.PartID FROM Generic.GenericCatalog gc with(nolock) JOIN
Generic.UserPart up WITH(NOLOCK) ON up.GenericCatID = gc.ID
WHERE gc.UserID = @UserID)
AND
CategoryID = @CategoryID
AND
s.ID = @SupplierID
) AS firstt
) AS final
WHERE RowNum BETWEEN @StartIndex AND (@StartIndex + @pageSize) - 1
ORDER BY final.[Name] ASC;
END;
SET NOCOUNT OFF;