I am trying to select rows into a temporary table with a CASE statement in the ORDER BY clause but records are not being sorted on insert.
Declare @orderby varchar(10) , @direction varchar(10)
set @orderby = 'col1'
set @direction = 'desc'
select identity (int) as autoid, *
into #temp
from table
order by case when @direction = 'desc' and @orderby = 'co1' then col1 end desc
declare @startrow int
declare @maxrows int
set @starrow = 19
set @maxrow = 30
set rowcount @maxrows
select * from #temp
where autoid > @startrow
You can achieve this not using #temp tables insted use normal table temp. Later when you are done with your process you can drop it at the end.