I'm using a stored procedure with a CTE and doing some paging. I also want to return an output parameter with the total count of the returned query before my paging. 
My problem is that I get an error that "OrderedSet" is not a valid object name. 

```
    @ft INT,
    @page INT,
    @pagesize INT,
    @count INT OUTPUT
AS
BEGIN

DECLARE @offset INT
SET @offset = @page * @pagesize
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    WITH OrderedSet AS (
    SELECT *, ROW_NUMBER() OVER (ORDER BY Id DESC) AS 'Index'
    FROM tbl_BulkUploadFiles buf
    WHERE
    buf.FileType = @ft )
    SELECT * FROM OrderedSet WHERE [Index] BETWEEN @offset AND (@offset + @pagesize)
    SET @count = (SELECT COUNT(*) FROM OrderedSet)
END

```

So my issue is on the last line, error is that last OrderedSet is not a valid object name.

Thanks in advance for any help!