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 705799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:05:38+00:00 2026-05-14T04:05:38+00:00

Ok I have a table in my SQL Server database that stores comments. My

  • 0

Ok I have a table in my SQL Server database that stores comments. My desire is to be able to page though the records using [Back],[Next], page numbers & [Last] buttons in my data list. I figured the most efficient way was to use a stored procedure that only returns a certain number of rows within a particular range. Here is what I came up with

@PageIndex INT, 
@PageSize INT,
@postid int


AS
 SET NOCOUNT ON  
 begin

WITH tmp AS ( 
SELECT comments.*, ROW_NUMBER() OVER (ORDER BY dateposted ASC)  AS  Row
    FROM    comments
    WHERE     (comments.postid = @postid))

SELECT tmp.*
FROM tmp
WHERE Row between 

(@PageIndex - 1) * @PageSize + 1 and @PageIndex*@PageSize

end

RETURN 

Now everything works fine and I have been able implement [Next] and [Back] buttons in my data list pager. Now I need the total number of all comments (not in the current page) so that I can implement my page numbers and the[Last] button on my pager. In other words I want to return the total number of rows in my first select statement i.e

  WITH tmp AS ( 
    SELECT comments.*, ROW_NUMBER() OVER (ORDER BY dateposted ASC)  AS  Row
        FROM    comments
        WHERE     (comments.postid = @postid))
set @TotalRows = @@rowcount

@@rowcount doesn’t work and raises an error. I also cant get count.* to work either.

Is there another way to get the total amount of rows or is my approach doomed.

  • 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-14T04:05:39+00:00Added an answer on May 14, 2026 at 4:05 am

    I’ve dealt with this very problem and in the end I found a handful of solutions none of which are spectacular but do the job:

    1. Query twice
    2. Return the count as one of the columns
    3. Stuff the results into a temporary table while returning the count as a column

    In the first solution you would do something like:

        ...
        , @Count int OUTPUT
    AS 
    Select @Count = (
                    Select Count(*)
                    From comments
                    Where comments.postid = @postid
                        And Col1 = ... And Col2 = ...
                    )
    
    With NumberedResults As
        (
        Select ...
            , ROW_NUMBER() OVER( ORDER BY dateposted ASC ) As Num
        From comments
        Where Col1 = ... And Col2 = ...
        )
    Select ...
    From NumberedResults
    Where Num Between ( @PageIndex - 1 ) * @PageSize + 1 and @PageIndex * @PageSize
    

    The obvious downside is that if the query is expensive, you do it twice.

    In the second solution, you simply return the count as part of the results. You would then pick off the count from the first record in your business tier code. The advantage is that you only do an expensive query once. The downside is you return an extra four bytes for every row in the result.

    With NumberedResults As
        (
        Select ...
            , ROW_NUMBER() OVER( ORDER BY dateposted ASC ) As Num
        From comments
        Where Col1 = ... And Col2 = ...
        )
    Select ...
        , ( Select Count(*) From NumberedResults ) As TotalCount
    From NumberedResults
    Where Num Between ( @PageIndex - 1 ) * @PageSize + 1 and @PageIndex * @PageSize
    

    The third solution is a variant of the second in that you stuff the results into a temp table and set your out parameter from the first record

        ...
        , @TotalCount int OUTPUT
    AS
    
    Declare @PagedResults Table (
                                Col1 ...
                                , ...
                                , TotalCount int
                                )
    With NumberedResults As
        (
        Select ...
            , ROW_NUMBER() OVER( ORDER BY dateposted ASC ) As Num
        From comments
        )
    Insert @PagedResults( Col1...., TotalCount )
    Select ...
        , ( Select Count(*) From NumberedResults ) As TotalCount
    From NumberedResults
    Where Num Between ( @PageIndex - 1 ) * @PageSize + 1 and @PageIndex * @PageSize
    
    Set @TotalCount = ( Select TOP 1 TotalCount From @PagedResults )
    
    Select ...
    From @PagedResults
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 497k
  • Answers 497k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Well, it's not technically a CDN but you can link… May 16, 2026 at 12:07 pm
  • Editorial Team
    Editorial Team added an answer Some example data: import numpy as np lookup = np.array([[… May 16, 2026 at 12:07 pm
  • Editorial Team
    Editorial Team added an answer You can populate the DataTextField and DataValueField of the dropdownlist… May 16, 2026 at 12:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a table in SQL Server 2005 database that has following columns: Id,ProductName,Year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec
I have a table named t_Student in Microsoft SQL Server 2005 database. In that
I have a table in a SQL Server 2008 database. This table has a
I am having a very weird problem with building a SQL Server 2008 Database
We have mirroring set up between 2 SQL Server 2005 standard editions. There are
I have a very large database I need to diagram. The database is SQL
Im using the above technologies and have ran into what I presume is a
I need a status column that will have about a dozen possible values. Is
Please forgive my long question. I have an idea for a design that I
I have a stored procedure that does, among other stuff, some inserts in different

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.