Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 order by

Author  Topic 

matrixr
Starting Member

26 Posts

Posted - 2006-07-21 : 02:50:48
at the moment im using the paging technique described http://www.aspfaq.com/show.asp?id=2120 (ROW COUNT method) all works fine now what i want to do is order a certain page by a column asc/desc

this is how i do it now
declare @t table (...columns...)
insert into @t select ...columns from ...
select * from @t order by xyz

i cannot use a sub query because mssql says order by is not valid for a subquery, i cannot use the top 100 percent trick because then it screws up the paging technique

there must be a better way to do this, any ideas?

thanks

RyanRandall
Master Smack Fu Yak Hacker

1074 Posts

Posted - 2006-07-21 : 05:10:56
Maybe this will be useful?

http://weblogs.sqlteam.com/jeffs/archive/2003/12/22/672.aspx


Ryan Randall
www.monsoonmalabar.com London-based IT consultancy

Solutions are easy. Understanding the problem, now, that's the hard part.
Go to Top of Page

matrixr
Starting Member

26 Posts

Posted - 2006-07-21 : 06:00:43
perfect

declare @page int, @pageSize int, @totalRows int, @offset int
select @page = 0, @pageSize = 5

select @totalRows = count(*) from table [optional where clause]
select @offset = @totalRows - (@pageSize * @page)

execute ('
select * from
(select top ' + @pageSize + ' * from
(select top ' + @offset + ' * from
table [optional where clause] order by [column(s)] desc) a
order by [same column(s) as above] asc) b
order by [same column(s) as above] asc')

that did the trick.

thanks
Go to Top of Page
   

- Advertisement -