Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Conditional order by clause with multiple columns

User198296Jul 28 2011 — edited Jul 29 2011
I would like to know whether it is possible to include multiple columns in a conditional order by clause.
Ex: I have written the following PL / SQL :

CREATE PROCEDURE GetProducts
(
@OrderBy VARCHAR(50),
@Input2 VARCHAR(30)
)
AS
BEGIN
SET NOCOUNT ON

SELECT Id, ProductName, Description, Price, Quantity
FROM Products
WHERE ProductName LIKE @Input2
ORDER BY
CASE
WHEN @OrderBy = 'ProductNameAsc' THEN ProductName
END ASC,
CASE
WHEN @OrderBy = 'ProductNameDesc' THEN ProductName
END DESC

END

Now I want to include when the Orderby - "Productnameasc" then order by productname, price, stdate
else
orderby productname, crdate, category

Could this be done? I tried it, but couldn't get this to work.
Any help is greatly appreciated.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 26 2011
Added on Jul 28 2011
9 comments
7,119 views