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

  • Home
  • SEARCH
  • 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 6057025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:26:34+00:00 2026-05-23T08:26:34+00:00

ALTER PROCEDURE [dbo].[spGetMessages] @lastRow int AS BEGIN — Insert statements for procedure here DECLARE

  • 0
ALTER PROCEDURE [dbo].[spGetMessages]
    @lastRow int
AS
BEGIN
    -- Insert statements for procedure here
    DECLARE @StartRow INT,@EndRow INT
    SELECT @StartRow = (@lastRow + 1), @EndRow = (@lastRow + 6)

;WITH cte AS (SELECT ROW_NUMBER() OVER (ORDER BY
        CASE WHEN @sort = 'votes1' THEN m.votes END DESC,
        CASE WHEN @sort = 'votes2' THEN m.votes END ASC,
        CASE WHEN @sort = 'age1' THEN datediff(minute,m.timestamp, getdate()) END ASC,
        CASE WHEN @sort = 'age2' THEN datediff(minute,m.timestamp, getdate()) END DESC
      ) AS rows,
      m.message,
      m.messageId
    FROM
      tblMessages m
    WHERE
      m.deleted != 1
      )
     SELECT * 
     FROM cte WHERE ROWS BETWEEN @StartRow AND @EndRow
    ORDER BY rows
END

So this proc gets me a bunch of messages, but passing in the last row value so I can implement paging, and a ‘load more’ functionality on the front end.

If the proc returns less than 6 messages, I can disable ‘load more’ obviously, if it returns 6 messages however, I don’t know if there’s more in the database, or that those were the last 6 messages.

My thought was that if I passed back another DataTable containing either true or false that represented if there were more than these 6 messages left in the database using the last row id, I could use this flag to enable/disable the ‘load more’ button. Is this a good idea? If not, what’s a better plan?

If so, how would I modify this proc to pass that flag back?

  • 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-23T08:26:34+00:00Added an answer on May 23, 2026 at 8:26 am

    We have several procs like this. The easy way was to add an additional column in the output called “TotalCount” which returned a count of all rows.

    In your case it would look like:

    ;WITH cte AS (SELECT ROW_NUMBER() OVER (ORDER BY
            CASE WHEN @sort = 'votes1' THEN m.votes END DESC,
            CASE WHEN @sort = 'votes2' THEN m.votes END ASC,
            CASE WHEN @sort = 'age1' THEN datediff(minute,m.timestamp, getdate()) END ASC,
            CASE WHEN @sort = 'age2' THEN datediff(minute,m.timestamp, getdate()) END DESC      ) AS rows,
                m.message, m.messageId,
            TotalCount = COUNT(m.Id) OVER ( PARTITION BY NULL)
        FROM tblMessages m
        WHERE m.deleted != 1
    )
    SELECT *
    FROM cte 
    WHERE ROWS BETWEEN @StartRow AND @EndRow
    ORDER BY rows
    

    I’m assuming the tblMessages has a column called Id. The point is to simply count the unique id’s in that table.

    We did it this way so we wouldn’t have to run 2 queries. Of course, depending on the size of data this could have a possible performance impact. So test both ways.

    ======
    BTW, one that thing comes to mind. You might consider not using prefixes (like “tbl” and “sp”) That’s a very outdated way of doing things and generally completely unnecessary.

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

Sidebar

Related Questions

ALTER PROCEDURE [dbo].[getMessages] -- Add the parameters for the stored procedure here @lastRow int,
ALTER PROCEDURE dbo.ModeratorSpamDeleteComment DECLARE @CommentID int; AS BEGIN DELETE Comments WHERE CommentsID=@CommentID END It
Let say I have a simple Stored Procedure: ALTER PROCEDURE [dbo].[myProc] AS BEGIN SELECT
ALTER PROCEDURE [dbo].[test] @tour int, @tourname varchar(50) OUTPUT, @tourdepartures varchar(50) OUTPUT AS BEGIN --
I get error why? ALTER PROCEDURE dbo.verifGuids @value char(36), @message char(1)Output AS Begin select
ALTER PROCEDURE dbo.Articles_InsertArticle @Article nvarchar(MAX), @UsersID uniqueidentifier AS INSERT INTO Articles (UsersID,Article,PageNumber) SELECT @UsersID,
I've a stored procedure here ALTER PROCEDURE [dbo].[SortedReport] ( @ClientID INT, @RecordLimit, @FromDate DATETIME,
Take a look at this SP. ALTER PROCEDURE [dbo].[sp_GetRecTitleVeh] AS BEGIN select a.StockNo, c.ClaimNo,
Here's my SQL Server stored procedure : ALTER PROCEDURE [dbo].[SearchUser] (@Text NVARCHAR(100), @TotalRows INT
for example... ALTER PROCEDURE [dbo].[Reports_Dashboard_Get_Sav] -- Add the parameters for the stored procedure here

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.