Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6574297
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:14:36+00:00 2026-05-25T15:14:36+00:00

I am trying to return a page of data and also row count of

  • 0

I am trying to return a page of data and also row count of all data in one stored procedure which looks like following:

WITH Props AS
(
    SELECT *,
    ROW_NUMBER() OVER (ORDER BY PropertyID) AS RowNumber
    FROM Property
    WHERE PropertyType = @PropertyType AND ...
)   

SELECT * FROM Props 
WHERE RowNumber BETWEEN ((@PageNumber - 1) * @PageSize) + 1 AND (@PageNumber * @PageSize);

I am unable to return the row count (highest row number).

I know this has already been discussed (I’ve seen this:
Efficient way of getting @@rowcount from a query using row_number) but when I add COUNT(x) OVER(PARTITION BY 1) in the CTE, the performance degrades and the query above that normally takes no time takes forever to execute. I reckon it’s because the count is calculated for each row? I seems that I can’t reuse the CTE in another query. Table Props has 100k records, CTE returns 5k records.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:14:37+00:00Added an answer on May 25, 2026 at 3:14 pm

    In T-SQL it should be

    ;WITH Props AS
    (
        SELECT *,
            ROW_NUMBER() OVER (ORDER BY PropertyID) AS RowNumber
        FROM Property
        WHERE PropertyType = @PropertyType AND ...
    )
    
    , Props2 AS
    (
        SELECT COUNT(*) CNT FROM Props
    )
    
    -- Now you can use even Props2.CNT
    SELECT * FROM Props, Props2
    WHERE RowNumber BETWEEN ((@PageNumber - 1) * @PageSize) + 1 AND (@PageNumber * @PageSize);
    

    now you have CNT in every line… Or you wanted something different? You wanted a second resultset with only the count? Then do it!

    -- This could be the second result-set of your query.
    SELECT COUNT(*) CNT
    FROM Property
    WHERE PropertyType = @PropertyType AND ...
    

    Note: reedited, the query 1 David was referencing now has been trashcanned, query 2 is now query 1.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been going bananas trying to get some data from Javascript on one page
I have a set links on a page like this <a href='' class='contact' data-index='1'>One</a>
I'm trying to return a custom 404 page using IIS6. (I have a site
I am trying to return a JSON object from an aspx page using Jayrock.JSON.
Hi I am trying to return a collection which may potentially have null date
Im trying to create a listing app that is core data backed.The first page
I'm trying to traverse an AJAX response, which contains a remote web page (an
Im trying to return a list to a display template containing data from two
I have been trying to get a jquery ajax function to return data from
Here is my issue. I am trying to call a page: foo.php?docID=bar and return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.