smithalovesdotnet


I am trying to order by the field and direction as provided by input parameters @COLTOSORTBY and @DIR while using a CTE and assigning Row_Number, but am running into syntax errors.

Say I have a table called myTable with columns col1,col2,col3,

Here's what I'm trying to do

with myCTE AS
(

Select

col1

,col2

,col3

,row_number() over (order by

case when(@DIR = 'ASC') then

case when @COLTOSORTBY='col1' then col1 asc

when @COLTOSORTBY='col2' then col2 asc

else col3 asc

end

else

case when @COLTOSORTBY='col1' then col1 desc

when @COLTOSORTBY='col2' then col2 desc

else col3 desc

end

end

from myTable

)

Please let me know what i can do with minimal code repetition and achive my goal of dynamically sorting column and direction. I do not want to use dynamic SQL under any circumstance.

Thanks.





Re: using case statement to determine Order by field and direction (asc or desc) when using Row_number

Manivannan.D.Sekaran


You have to use the following syntax,

Code Snippet

Declare @COLTOSORTBY as varchar(100);

Declare @DIR as varchar(10);

Set @COLTOSORTBY = 'id';

Set @DIR = 'asc';

with myCTE AS

(

Select

id,

name,

xtype,

row_number() over (

order by

case when(@DIR = 'ASC') then case when @COLTOSORTBY='id' then id end end,

case when(@DIR = 'ASC') then case when @COLTOSORTBY='name' then name end end,

case when(@DIR = 'ASC') then case when @COLTOSORTBY='xtype' then xtype end end,

case when(@DIR <> 'ASC') then case when @COLTOSORTBY='id' then id end end desc,

case when(@DIR <> 'ASC') then case when @COLTOSORTBY='name' then name end end desc,

case when(@DIR <> 'ASC') then case when @COLTOSORTBY='xtype' then xtype end end desc

) as Rid

from

Sysobjects

)

select * from myCTE







Re: using case statement to determine Order by field and direction (asc or desc) when using Row_number

oj

you're almost there.

Code Snippet

;with myCTE AS
(

Select

col1

,col2

,col3

,row_number() over (order by

case when(@DIR = 'ASC') then

case when @COLTOSORTBY='col1' then col1

when @COLTOSORTBY='col2' then col2

else col3

end

end asc,

case when(@DIR <> 'ASC') then

case when @COLTOSORTBY='col1' then col1

when @COLTOSORTBY='col2' then col2

else col3

end

end desc)

from myTable

)

select * from myCTE







Re: using case statement to determine Order by field and direction (asc or desc) when using Row_number

Manivannan.D.Sekaran

I agree with OJ only if all the columns(col1, col2, col3) have same datatype.

If these datatypes are different then you will be forced to cast it. The casting to common datatype might affect your sorting behaviour.

The query above I posted will be common for all.






Re: using case statement to determine Order by field and direction (asc or desc) when using Row_number

oj

Good point, Mani.






Re: using case statement to determine Order by field and direction (asc or desc) when using Row_number

smithalovesdotnet

Thanks Mani and OJ. I started writing a solution and this helped me confirm that I need to break down into several case statements this way. Also yeah I need it to be compatible with various datatypes while sorting.

Thanks again!






Re: using case statement to determine Order by field and direction (asc or desc) when using Row_number

Manivannan.D.Sekaran

You can have something like this..

One case statement for each datatype...

As per my example Name & xType are varchar but id is int so the query might look like below,

Code Snippet

Set @COLTOSORTBY = 'xtype';

Set @DIR = 'asc';

with myCTE AS

(

Select

id,

name,

xtype,

row_number() over (

order by

case when(@DIR = 'ASC') then case when @COLTOSORTBY='id' then id end end,

case when(@DIR = 'ASC') then case

when @COLTOSORTBY='name' then name

when @COLTOSORTBY='xtype' then xtype

end

end,

case when(@DIR <> 'ASC') then case when @COLTOSORTBY='id' then id end end desc,

case when(@DIR <> 'ASC') then case

when @COLTOSORTBY='name' then name

when @COLTOSORTBY='xtype' then xtype

end

end desc

) as Rid

from

Sysobjects

)

select * from myCTE