|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Stored Procedure: updating the "order by" clause
I have a stored procedure that returns a recordset which I would like to have sorted based on the inputs. For example, a product listing could be ordered by product number, product name, or price.
I have tried creating the whole SQL query in a variable and executing it, only to get an error because it can't find the stored procedure. What I'm looking for is something like (pseudocode) select * from Products where if SortByPrice is true then order by Products.Price ASC end if if SortByName is true then order by Products.Name ASC end if and so on. Thanks. |
|
#2
|
|||
|
|||
|
hey akmolteni
Try: Code:
CREATE PROC sp_Data
(
@SortBy int
)
AS
SELECT Table1.* FROM Table1
ORDER BY
CASE @SortBy WHEN 1 THEN Field1 END,
CASE @SortBy WHEN 2 THEN Field2 END,
CASE @SortBy WHEN 3 THEN Field3 END,
CASE @SortBy WHEN 4 THEN Field4 END
, Field5, Field6
|
![]() |
| Viewing: Dev Articles Community Forums > Databases > Microsoft SQL Server > Stored Procedure: updating the "order by" clause |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|