asp.net分頁的SQL語句及儲存過程

暖楓無敵發表於2011-10-26

SQL語句:

利用 not in    

  select top pageSize * from table where tid not in (select top (pageIndex-1)*pagesize tid from 表名) ;

  --------------或者    

  select * from ( select *,Row_Number() over(order by id) RowNumber from 表名) t where t.RowNumber>=2 and t.RowNumber<=10;  
  


儲存過程:

create  Procedure GetPageData(      

  @PageIndex  int =1,      

  @PageSize   int =10,      

  @RowCount  int output,      

  @PageCount  int  output,)

  as

  Declare  @sql nvarchar(225),@sqlcount nuachar(225)

  select @rowcount=count(sid),@pagecount=ceiling( count(sid)+0.0 )/@pagesize  from studentsset  

  @sql='select top '+str(@pagesize)+' * from students,classes where scid=cid and sid not in(select top) '  +str( (@pageIndex)-1 )*pagesize)  +'sid from students'

  print  @sql

  exec(@sql)

 --執行  
Declare  
@rowcount int, 
@pagecount int 
exec  GetPageData  1,3,@rowcount out ,@pagecount outputselect  rowcount,@pagecount





相關文章