I have the SQL stored procedure below:
ALTER PROCEDURE [dbo].[sp_getpaginatedusersbyglobalfilter]
@globalFilter varchar(max)
, @pageSize int
, @page int
, @totalRecords int output
, @totalFilteredRecords int output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
;WITH PaginatedUsers AS(
SELECT ROW_NUMBER() OVER(ORDER BY u.LastName) ID
, u.Username
, u.FirstName
, u.OtherNames
, u.LastName
, e.Nombre
, r.RoleName
, u.PhoneNumber
FROM webpages_Users u
INNER JOIN Establecimiento e
ON u.FacilityId = e.IDReporte
AND e.EstadoRegistro = 1
INNER JOIN webpages_UsersInRoles ur
ON u.UserId = ur.UserId
INNER JOIN webpages_Roles r
ON ur.RoleId = r.RoleId
WHERE u.Username LIKE '%' + @globalFilter + '%'
OR u.FirstName LIKE '%' + @globalFilter + '%'
OR u.OtherNames LIKE '%' + @globalFilter + '%'
OR u.LastName LIKE '%' + @globalFilter + '%'
OR e.Nombre LIKE '%' + @globalFilter + '%'
OR r.RoleName LIKE '%' + @globalFilter + '%'
OR u.PhoneNumber LIKE '%' + @globalFilter + '%'
)
SELECT pu.ID, pu.Username
, pu.FirstName
, pu.OtherNames
, pu.LastName
, pu.Nombre AS FacilityName
, pu.RoleName
, pu.PhoneNumber
FROM PaginatedUsers pu
WHERE pu.ID BETWEEN ((@page - 1) * @pageSize + 1)
AND @page * @pageSize
SELECT @totalFilteredRecords = COUNT(pu.ID)
FROM PaginatedUsers pu
SELECT @totalRecords = COUNT(u.UserId)
FROM webpages_Users u
END
The problem I have is with the section:
SELECT @totalFilteredRecords = COUNT(pu.ID)
FROM PaginatedUsers pu
The error I get is "Invalid object name 'PaginatedUsers'". As per my research, I cannot use the PaginatedUsers (resulting from the WITH) object more than once in the query. Then, How can I do to return the total number of records in the output variable @totalFilteredRecords? I've been thinking about inserting the result into a temporal table and then get the count from it as well as the final resultset, but I don't like that idea. What can I do?
Thanks.