CREATE PRocedure GetUsers (@pagesize int, @pageindex int, @docount bit) as set nocount on if(@docount=1) select count(userId) from Users else begin declare @indextable table(id int identity(1,1),nid int) declare @PageLowerBound int declare @PageUpperBound int set @PageLowerBound=(@pageindex-1)*@pagesize set @PageUpperBound=@PageLowerBound+@pagesize set rowcount @PageUpperBound insert into @indextable(nid) select userId from Users order by userName desc select O.* from Users O,@indextable t where O.userId=t.nid and t.id>@PageLowerBound and t.id<=@PageUpperBound order by t.id end set nocount off GO