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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:32:12+00:00 2026-05-27T21:32:12+00:00

Following is a Paging enabled Query for SQL server, it is working well without

  • 0

Following is a Paging enabled Query for SQL server, it is working well without dynamic order by @pOrderBy variable .

I want to implement two things in this query.

  1. I want to sort the rows according to the column name i m passing.
  2. I want to run another Query in it to get total number of rows so that I can display number of pages on my UI.

    ALTER PROCEDURE [dbo].[usp_get_all_groups] 
      -- Add the parameters for the stored procedure here
      @pStartIndex smallint,
      @pPageSize tinyint,
      @pOrderBy varchar(20)
    AS
    BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
    
     SELECT       GroupTable._id,
                  GroupTable.GroupCode,
                  GroupTable.Type,
                  GroupTable.Description
        From(
        Select ROW_NUMBER() OVER (
           ORDER BY UG._id,
                UG.GroupCode,
                UG.Type,
                UG.Description ) as [Row_Number],
                UG._id,
                UG.GroupCode,
                UG.Type,
                UG.Description
       From UserGroups as UG
       ) as GroupTable
       where GroupTable.[Row_Number] BETWEEN @pStartIndex AND @pStartIndex + @pPageSize             
       ORDER BY GroupTable.[Row_Number]
    
    END
    
  • 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-27T21:32:12+00:00Added an answer on May 27, 2026 at 9:32 pm

    I want to sort the rows according to the column name i m passing.

    You will need to use dynamic SQL for that – it is not possible to use a column name in a variable on a regular query.

    An alternative is to use CASE in the ORDER BY clause – checking the value of the variable and using the corresponding column. See the comment from @Dems.

    I want to run another Query in it to get total number of rows so that I can display number of pages on my UI.

    Use an output parameter or return value and assign the value of such a query to it in this stored procedure.

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

Sidebar

Related Questions

I'm using the following SQL Server query, which searches a full-text index and appears
I'm trying to implement a paging and sorting list in ASP.NET MVC without using
I am trying to implement a query on a RavenDB Lucene index and paging
I am working on an application in which I would like to implement paging.
I want to implement table paging using this method: SET @PageNum = 2; SET
Is there a way to implement paging logic in DB2 SQL, where records can
I'm new to MVC, and am not following how you'd do paging and sorting
Following up on this question, I'm working on a large Delphi 7 codebase which
i'd like a basic idea of how to go about formatting the following paging
I have a scrollView with paging enabled and a number N of pages, which

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.