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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:06:33+00:00 2026-05-10T18:06:33+00:00

Let’s suppose I’m using the Northwind database and I would like to run a

  • 0

Let’s suppose I’m using the Northwind database and I would like to run a query via a stored procedure that contains, among other parameters, the following:

  • @Offset to indicate where the pagination starts,
  • @Limit to indicate the page size,
  • @SortColumn to indicate the column used for sorting purposes,
  • @SortDirection, to indicate ascendant or descendant sorting.

The idea is to do the pagination on the database, as the result set contains thousands of rows so caching is not an option (and using VIEWSTATE is not even considered as, IMO, sucks).

As you may know SQL Server 2005 provides the function ROW_NUMBER which returns the sequential number of a row within a partition of a result set, starting at 1 for the first row in each partition.

We need sorting on every returned column (five in this example) and dynamic SQL is not an option, so we have two possibilities: using plenty of IF ... ELSE ... and having 10 queries, which is a hell to maintain, or having a query like the following:

WITH PaginatedOrders AS (     SELECT         CASE (@SortColumn + ':' + @SortDirection)             WHEN 'OrderID:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID ASC)             WHEN 'OrderID:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID DESC)             WHEN 'CustomerID:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.CustomerID ASC)             WHEN 'CustomerID:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.CustomerID DESC)             WHEN 'EmployeeID:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.EmployeeID ASC)             WHEN 'EmployeeID:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.EmployeeID DESC)             WHEN 'OrderDate:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderDate ASC)             WHEN 'OrderDate:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderDate DESC)             WHEN 'ShippedDate:A' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID ASC)             WHEN 'ShippedDate:D' THEN ROW_NUMBER() OVER (ORDER BY Orders.OrderID DESC)         END AS RowNumber,         OrderID, CustomerID, EmployeeID, OrderDate, ShippedDate     FROM Orders     -- WHERE clause goes here ) SELECT     RowNumber, OrderID, CustomerID, EmployeeID, OrderDate, ShippedDate,     @Offset, @Limit, @SortColumn, @SortDirection FROM PaginatedOrders WHERE RowNumber BETWEEN @Offset AND (@Offset + @Limit - 1) ORDER BY RowNumber 

I’ve tried the query several times, with different arguments, and its performance it is quite good actually, but it stills looks like it might be optimized some other way.

Is anything wrong with this query or you would do it this way? Do you propose a different approach?

  • 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. 2026-05-10T18:06:34+00:00Added an answer on May 10, 2026 at 6:06 pm

    Simple:

    SELECT   OrderID, CustomerID, EmployeeID, OrderDate, ShippedDate,   @Offset, @Limit, @SortColumn, @SortDirection FROM   Orders WHERE   ROW_NUMBER() OVER    (     ORDER BY       /* same expression as in the ORDER BY of the whole query */   ) BETWEEN (@PageNum - 1) * @PageSize + 1 AND @PageNum * @PageSize    /* AND more conditions ... */ ORDER BY   CASE WHEN @SortDirection = 'A' THEN     CASE @SortColumn        WHEN 'OrderID'    THEN OrderID       WHEN 'CustomerID' THEN CustomerID       /* more... */     END   END,   CASE WHEN @SortDirection = 'D' THEN     CASE @SortColumn        WHEN 'OrderID'    THEN OrderID       WHEN 'CustomerID' THEN CustomerID       /* more... */     END    END DESC 

    This will sort on NULL (DESC) if ASC order is selected, or vice versa.

    Let the ROW_NUMBER() function work over the same ORDER BY expression.

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

Sidebar

Ask A Question

Stats

  • Questions 103k
  • Answers 103k
  • 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 I would do it by splitting articles on chuks when… May 11, 2026 at 8:26 pm
  • Editorial Team
    Editorial Team added an answer I've got it to work. 1 of 2 things worked… May 11, 2026 at 8:26 pm
  • Editorial Team
    Editorial Team added an answer It's Nontaxable. It's just an ordinary word. Non is not… May 11, 2026 at 8:26 pm

Related Questions

Let's say you create a wizard in an HTML form. One button goes back,
Let's say I'm building a data access layer for an application. Typically I have
Let's say you have a class called Customer, which contains the following fields: UserName
Let me try to explain what I need. I have a server that is
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>

Trending Tags

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

Top Members

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.