Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

Dynamic ORDER BY

Question posted by: yakatz (Newbie) on July 2nd, 2008 01:23 PM
I am working on adding sorting functionality to an online store.
The query works fine when it has a hard coded ORDER BY, but not when the ORDER BY is a variable.

Expand|Select|Wrap|Line Numbers
  1. declare @CatID Int
  2. declare @PageSize Int
  3. declare @PageNumber Int
  4. declare @OrderBy VarChar
  5. declare @EntityName VarChar
  6.  
  7. set @CatID = 3158
  8. set @PageSize = 10
  9. set @PageNumber = 1
  10. set @OrderBy = 'p.SalePrice'
  11. set @EntityName = 'Category';
  12.  
  13. WITH ProductPaging AS (
  14.                 SELECT
  15.                     ROW_NUMBER() OVER ( ORDER BY @OrderBy ) AS RowNumber,
  16.                     p.ProductID AS ProductID,
  17.                     p.Name AS Name,
  18.                     p.SKU AS SKU,
  19.                     p.HidePriceUntilCart AS HidePrice,
  20.                     pc.CategoryID AS CategoryID,
  21.                     pc.DisplayOrder AS pcDisplayOrder,
  22.                     p.Price AS Price,
  23.                     p.SalePrice AS SalePrice
  24.                 FROM 
  25.                     dbo.Product AS p INNER JOIN
  26.                     dbo.ProductCategory AS pc ON p.ProductID = pc.ProductID
  27.                 WHERE
  28.                     (pc.CategoryID = @CatID) AND 
  29.                     (p.Published = 1) AND 
  30.                     (p.Deleted = 0)
  31.                 )
  32.                 SELECT * 
  33.                 FROM
  34.                 ProductPaging
  35.                 WHERE RowNumber BETWEEN (@PageSize*(@PageNumber-1))+1 AND (@PageSize * @PageNumber)
  36.                 ORDER BY RowNumber
debasisdas's Avatar
debasisdas
Moderator
6,576 Posts
July 2nd, 2008
01:37 PM
#2

Re: Dynamic ORDER BY
Better use the sorting functionality from the frontend.

Reply
yakatz's Avatar
yakatz
Newbie
2 Posts
July 2nd, 2008
02:38 PM
#3

Re: Dynamic ORDER BY
Quote:
Better use the sorting functionality from the frontend.

I'm sorry, but I did not understand your answer.
I am writing a package that will create the sorting method seen from the front-end.
I have an xml package that runs a sql query and formats the results.
It will take request parameters from the query string and put them in a sql variables. i have the following query string: product.aspx?sort=1
which is converted by a static list to the required columns to sort (in this case p.SalePrice). sql will not honor the orderby for the variable.

Last edited by yakatz : July 2nd, 2008 at 02:41 PM. Reason: add details
Reply
ck9663's Avatar
ck9663
Expert
1,352 Posts
July 3rd, 2008
12:49 AM
#4

Re: Dynamic ORDER BY
Use dynamic query..

-- CK

Reply
deepuv04's Avatar
deepuv04
Expert
199 Posts
July 3rd, 2008
06:05 AM
#5

Re: Dynamic ORDER BY
Quote:
I'm sorry, but I did not understand your answer.
I am writing a package that will create the sorting method seen from the front-end.
I have an xml package that runs a sql query and formats the results.
It will take request parameters from the query string and put them in a sql variables. i have the following query string: product.aspx?sort=1
which is converted by a static list to the required columns to sort (in this case p.SalePrice). sql will not honor the orderby for the variable.


Hi,
You can use CASE statement in ORDER BY clause for dynamic sorting.

code is :
[code = sql]

declare @CatID Int
declare @PageSize Int
declare @PageNumber Int
declare @OrderBy VarChar
declare @EntityName VarChar
declare @sort int


set @CatID = 3158
set @PageSize = 10
set @PageNumber = 1
set @OrderBy = Rank() OVER (ORDER BY P.SalePrice)
set @EntityName = 'Category';

Set @sort = 1 --( assign the value passed as a parameter)



WITH ProductPaging AS (
SELECT
ROW_NUMBER() OVER ( ORDER BY @OrderBy ) AS RowNumber,
p.ProductID AS ProductID,
p.Name AS Name,
p.SKU AS SKU,
p.HidePriceUntilCart AS HidePrice,
pc.CategoryID AS CategoryID,
pc.DisplayOrder AS pcDisplayOrder,
p.Price AS Price,
p.SalePrice AS SalePrice
FROM
dbo.Product AS p INNER JOIN
dbo.ProductCategory AS pc ON p.ProductID = pc.ProductID
WHERE
(pc.CategoryID = @CatID) AND
(p.Published = 1) AND
(p.Deleted = 0)
)
SELECT *
FROM
ProductPaging
WHERE RowNumber BETWEEN (@PageSize*(@PageNumber-1))+1 AND (@PageSize * @PageNumber)
ORDER BY
(CASE @Sort WHEN 1 THEN Rank() OVER (ORDER BY P.SalePrice)
WHEN 2 THEN Rank() OVER (ORDER BY P.Price)
WHEN 3 THEN Rank() OVER (ORDER BY Pc.DisplayORder)
WHEN 4 THEN Rank() OVER (ORDER BY Pc.CategoryID)
WHEN 5 THEN Rank() OVER (ORDER BY P.HidePriceUntilCart)
WHEN 4 THEN Rank() OVER (ORDER BY P.SKU)
WHEN 5 THEN Rank() OVER (ORDER BY P.Name)
ELSE Rank() OVER (ORDER BY P.ProductID) end )
[/code]

Thanks

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,759 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top Microsoft SQL Server Contributors