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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T10:58:44+00:00 2026-06-12T10:58:44+00:00

I am trying to create a paging query using CTE. It works fine when

  • 0

I am trying to create a paging query using CTE. It works fine when I execute it from Microsoft SQL Server Management Studio Query Editor. And the result set is perfectly sorted as I want. But when I modify it for a stored procedure it gives me a unsorted result and I don’t have any clue why.

Here is my Query,

with items as
(
select ROW_NUMBER() over (order by create_time desc) number
    , i.item_name item_name
    , i.create_time create_time
    , c.category_name category_name
    , i.category_id category_id
from cb_item i, cb_category c
where i.category_id = c.category_id
and c.category_id = '4E5248FE-05DD-4D01-ABBB-80C6E3BA5CDA' 
)
select item_name
, create_time
, category_name
, category_id
from items
where number between 1 and 25

And this is the Stored Procedure Version,

    create procedure ItemPage @category_id uniqueidentifier
    , @from int
    , @to int
    , @sortby nvarchar(50)
    as
    begin
    with items as
    (
    select ROW_NUMBER() over (order by @sortby) number
        , i.item_name item_name
        , i.create_time create_time
        , c.category_name category_name
        , i.category_id category_id
    from cb_item i, cb_category c
    where i.category_id = c.category_id
    and c.category_id = @category_id 
    )
    select item_name
    , create_time
    , category_name
    , category_id
    from items
    where number between @from and @to
    end


exec itempage '4E5248FE-05DD-4D01-ABBB-80C6E3BA5CDA' , 1, 25, 'create_time desc'

The first one gives me sorted result but procedure gives me unsorted result. I don’t know why?

  • 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-06-12T10:58:45+00:00Added an answer on June 12, 2026 at 10:58 am

    You have to use Case in order by statement where you can check passed value and accordingly can order by row.

    Your query should be like this:

     create procedure ItemPage @category_id uniqueidentifier
            , @from int
            , @to int
            , @sortby nvarchar(50)
            as
            begin
            with items as
            (
            select ROW_NUMBER() over (order by
                                        CASE @sortby
                                        WHEN 'create_time desc' THEN  create_time -- Here, I assume passed @sortby value is create_time , otherwise you can mapped with passed value
                                        END
                                        DESC,
                                        CASE @sortby
                                        WHEN 'create_time asc' THEN  create_time -- Here, I assume passed @sortby value is create_time , otherwise you can mapped with passed value
                                        END
                                        ASC
                                        ) number
                , i.item_name item_name
                , i.create_time create_time
                , c.category_name category_name
                , i.category_id category_id
            from cb_item i, cb_category c
            where i.category_id = c.category_id
            and c.category_id = @category_id 
            )
            select item_name
            , create_time
            , category_name
            , category_id
            from items
            where number between @from and @to
            end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to implement a paging feature using PDO's ability to create cursors.
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
Ok so I am trying create a login script, here I am using PHP5
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
hi I'm trying create chat using node.js I see example in http://chat.nodejs.org/ I have
I'm trying to write a linq to sql method that handles sorting, paging, and
I am trying create a simple WQL query where I only return mounted drives
I'm trying create an executable for Windows for a GUI application in tkinter using
I am trying create system in which I can login from php and then
I'm trying to create a menu (using UIScrollView) where the user can select images

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.