![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hello, I need to provide sorting ability across multiple column. Data which would be sorted are retrieved with stored procedure. I have two solutions: 1. if statement depending of sort type I must create diferent select statement in case many columns there would be so many variations (also, there must be considered sort direction ASC or DESC what increase variations double) 2. dynamic sql Is there any additional way, which one is better? Please advice, Best regards |
#3
| |||
| |||
|
#4
| |||
| |||
|
|
Hello, I need to provide sorting ability across multiple column. Data which would be sorted are retrieved with stored procedure. I have two solutions: 1. if statement depending of sort type I must create diferent select statement in case many columns there would be so many variations (also, there must be considered sort direction ASC or DESC what increase variations double) 2. dynamic sql Is there any additional way, which one is better? Please advice, Best regards |
#5
| |||
| |||
|
|
You can also use CASE expressions. For example, if the data type of the columns is the same: ORDER BY CASE @column WHEN 'column1' THEN column1 WHEN 'column2' THEN column2 WHEN 'column3' THEN column3 END If the data types are different then you have to use multiple CASE expressions: ORDER BY CASE @column WHEN 'column1' THEN column1 END, CASE @column WHEN 'column2' THEN column2 END, CASE @column WHEN 'column3' THEN column3 END But using dynamic SQL may be best as appropriate indexes on the ordering columns can be used more efficiently. -- Plamen Ratchev http://www.SQLStudio.com |
![]() |
| Thread Tools | |
| Display Modes | |
| |