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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:42:40+00:00 2026-05-18T00:42:40+00:00

i have a question on stored procedures. I try to get a page of

  • 0

i have a question on stored procedures.

I try to get a page of result set and the record count of the whole set.

Each of this is working on it’s own, but I’m unable to combine it:

ALTER PROCEDURE dbo.pagingSCP
@PageStart INT, 
@PageSize INT, 
@RecordCount INT OUTPUT
AS
BEGIN
WITH AllRecords AS ( 
SELECT ROW_NUMBER() OVER (ORDER BY MATNR) 
AS Row, viewStyleColorInModul.*
FROM viewStyleColorInModul WHERE SPRAS = 'D'
) SELECT * FROM AllRecords WHERE Row between 
@PageStart and @PageStart + @PageSize
END

(50 row(s) returned)
@RecordCount = 0
@RETURN_VALUE = 0
Finished running [dbo].[pagingSCP].

ALTER PROCEDURE dbo.pagingSCP
@PageStart INT, 
@PageSize INT, 
@RecordCount INT OUTPUT
AS
BEGIN
WITH AllRecords AS ( 
SELECT ROW_NUMBER() OVER (ORDER BY MATNR) 
AS Row, viewStyleColorInModul.*
FROM viewStyleColorInModul WHERE SPRAS = 'D'
) SELECT @RecordCount = Count(*) From AllRecords
END

No rows affected.
(0 row(s) returned)
@RecordCount = 43770
@RETURN_VALUE = 0
Finished running [dbo].[pagingSCP].

Is is now somehow possible to get the 50 Rows and the total Recordcount
within the single query?

Thanks in advance.

  • 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-18T00:42:41+00:00Added an answer on May 18, 2026 at 12:42 am
    ALTER PROCEDURE dbo.pagingSCP
    @PageStart INT, 
    @PageSize INT, 
    @RecordCount INT OUTPUT
    AS
    BEGIN
      -- get record count
      WITH AllRecords AS ( 
        SELECT viewStyleColorInModul.*
      FROM viewStyleColorInModul WHERE SPRAS = 'D'
      ) SELECT @RecordCount = Count(*) From AllRecords;
    
      -- now get the records
      WITH AllRecords AS ( 
       SELECT ROW_NUMBER() OVER (ORDER BY MATNR) 
       AS Row, viewStyleColorInModul.*
       FROM viewStyleColorInModul WHERE SPRAS = 'D'
      ) SELECT * FROM AllRecords 
      WHERE Row between @PageStart and @PageStart + @PageSize;
    END
    

    You have two distinct queries, therefor eyou run two distinct SELECT and let the SQL optimizer optimize each individually. Even if trying to get both queries in one SELECT is possible, is highly counterproductive and sub-optimal.

    As a side note, in the client code any output parameter is available only after iterating all results returned.

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

Sidebar

Related Questions

Trying to get started with subsonic but have a simple question about stored procedures
This is a follow up to the question Nested stored procedures containing TRY CATCH
I have a question about a performance of stored procedures in the ADS. I
I am attempting to tune some stored procedures and have a question on indexes.
I have a database with some stored procedures. My question is: Is there a
I have stored procedures (they accept parameters) that return multiple result sets and I'd
I would like to get some clarification to this answer -> Nested stored procedures
I have a small question about stored procedures and the DateTime2 datatype in SQL
I have a stored procedure ( previous question ) but now I want to
We have a table (named QuestionBank) on which every question are stored : Id

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.