Click here to Skip to main content
15,947,389 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone,

In the following script I couldn't trace out how it is working.
Can anyone please help me?

Code goes like this:
select *,ROW_NUMBER() over (order by case  @sortorder
                                           when 'asc' then case @sortexpression 
                                           when 'empno' then empno
                                           when 'ename' then ename
                                           end end asc,
                                case @sortorder 
                                           when 'desc' then case @sortexpression
                                           when 'empno' then empno
                                           when 'ename' then ename  
                                           end end desc ) from emp


Supose if case is 'ASC' what happens to the case 'DESC'.what will be it's value?

Regards
Chaithanya M
Posted
Updated 2-May-11 0:42am
v7
Comments
Toniyo Jackson 21-Apr-11 3:23am    
Added pre tag
Dalek Dave 21-Apr-11 3:42am    
Edited for Grammar and Readability.
Corporal Agarn 21-Apr-11 14:53pm    
Edit to remove '

<pre lang="sql">declare @sortorder varchar(10) = 'desc';
declare @sortexpression varchar(10) = 'ename';
select *, ROW_NUMBER() over (order by case  @sortorder
                                           when 'asc' then case @sortexpression
-- changed to VARCHAR to match all sort types
                                           when 'empno' then CAST([EmployeeID] AS VARCHAR(10))
                                           when 'ename' then [LastName]
                                           end end asc,
                                case @sortorder
                                           when 'desc' then case @sortexpression
                                           when 'empno' then CAST([EmployeeID] AS VARCHAR(10))
                                           when 'ename' then [LastName]
                                           end end desc )
from [AdventureWorks2008R2].[dbo].[MyEmployees]



Try running this with various values for your variables. The sort is done in order first, second.

If you want to run with only one order value then the CASE statements would need changed.
 
Share this answer
 
Comments
M.CHAITHANYA 22-Apr-11 0:33am    
Hi,
I agree with you that the order is done in order first,second...But my concern here is suppose if we pass @sortorder as 'asc'..what will be the value of 2nd case ,I mean

"order by ename asc,?"----here what replaces question mark


Regards
Chaithanya M
The inner case will execute first and then the outer ones execute.
The best way to write such complex expressions is to put brackets - that always makes code easier to read.

When the case is asc then based on the expression @sortexpressions empno or ename is chosed.
Some conditions are applicable to desc.
 
Share this answer
 
Comments
M.CHAITHANYA 2-May-11 6:53am    
Hi Abhinav,

I agree with you that the order is done in order first,second...But my concern here is suppose if we pass @sortorder as 'asc'..what will be the value of 2nd case ,I mean

"order by ename asc,?"----here what replaces question mark


Regards
Chaithanya M

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900