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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:33:18+00:00 2026-05-26T10:33:18+00:00

All stored procedures which return data to the presentation layer of the webservice I

  • 0

All stored procedures which return data to the presentation layer of the webservice I am currently developing take as parameters two integers, @StartingRow and @MaximumRows, so that I can paginate results without retrieving the whole result list everytime. Now I would like to introduce sorting in these procedures, but from what I see everyone uses dynamic SQL to do the ordering:

EXEC ( 'WITH [Results] AS 
        ( SELECT * , 
             ROW_NUMBER() OVER ( ORDER BY ' + @SortExpression + @Direction + ')  AS 'RowNumber' 
          FROM [SomeTable] ) 
        SELECT [Column1] , [Column2] 
        WHERE ( [RowNumber] BETWEEN ' + @StartingRow + 
                          ' AND ( ' + @StartingRow + ' + ' + @MaximumRows + ' - 1) )' )

The problem with this approach is that I can’t use dynamic SQL due to customer’s demand, so that I can’t specify the column according to which the results should be sorted. What are my options, then?

  • 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-26T10:33:18+00:00Added an answer on May 26, 2026 at 10:33 am

    You could use a case. This example sorts on Col1 when @SortParameterequals 1. For parameter 5, it sorts by Col2 in descending order.

    order by
            case 
            when @SortParameter = 1 then Col1
            when @SortParameter = 2 then Col2
            ...
            end
    ,       case 
            when @SortParameter = 4 then Col1
            when @SortParameter = 5 then Col2
            ...
            end DESC
    

    SQL Server cannot use indexes with this approach. Which is the prime reason for using dynamic SQL instead.

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

Sidebar

Related Questions

Our DBA requires us to return all tabular data from stored procedures in a
We have lots of stored procedures which all contain a lot of comments. Is
What query can return the names of all the stored procedures in a SQL
I have several stored procedures which return a strongly typed result set. I've learned
I'm trying to call an Oracle Stored Procedure which returns XMLType data, but all
I have an Access 2003 project in which all data is stored in SQL
Is there an easy way to copy all stored procedures from one database to
I have been wondering how to put all the stored procedures on a SQL
I want a query that returns a list of all the (user) stored procedures
I want to convert all of my db stored procedures to linq to sql

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.