表中主鍵必須為標識列,[id] int identity (1,1)//每次自增一
1.分頁方案一:(利用not in和select top分頁)
語句形式:
select top 10 *
from testtable
where (id not in
(select top 20 id
from testtable
order by id))
order by id
select top 頁大小 *
from testtable
where (id not in
(select top 頁大小*(頁數-1) id
from 表
order by id))
order by id
2.分頁方案二:(利用id大於多少和select top分頁)
語句形式:
select top 10 *
from testtable
where (id >
(select max(id)
from (select top 20 id
from testtable
order by id) as t))
order by id
select top 頁大小 *
from testtable
where id >
(select max(id)
from (select top 頁大小*(頁數-1) id
from 表
order by id) as t)
order by id
3.分頁方案三:(利用sql的游標儲存過程分頁)
create procedure sqlpager
@sqlstr nvarchar(4000), --查詢字串
@currentpage int, --第n頁
@pagesize int --每頁行數
as set nocount on
declare @p1 int, --p1是游標的id
@rowcount int
exec sp_cursoropen @p1 output,@sqlstr,@scrollopt=1,@ccopt=1, @rowcount=@rowcount output
select ceiling(1.0*@rowcount/@pagesize) as 總頁數--,@rowcount as 總行數,@currentpage as 當前頁
set @currentpage=(@currentpage-1)*@pagesize+1
exec sp_cursorfetch @p1,16,@currentpage,@pagesize
exec sp_cursorclose @p1
set nocount off
4.create procedure framworkpage
@tablename varchar(50), --表名
@fields varchar(5000) = '*', --欄位名(全部欄位為*)
@orderfield varchar(5000), --排序字段(必須!支援多欄位)
@sqlwhere varchar(5000) = null,--條件語句(不用加where)
@pagesize int, --每頁多少條記錄
@pageindex int = 1 , --指定當前為第幾頁
@totalpage int output, --返回條數
@ordertype bit -- 設定排序型別,1 公升序 0 值則降序
as begin
declare @strorder varchar(400) -- 排序型別
begin tran --開始事務
declare @sql nvarchar(4000);
declare @totalrecord int;
--計算總記錄數
if (@sqlwhere ='''' or @sqlwhere='' or @sqlwhere is null)
set @sql = 'select @totalrecord = count(*) from ' + @tablename
else
set @sql = 'select @totalrecord = count(*) from ' + @tablename + ' where ' + @sqlwhere
exec sp_executesql @sql,n'@totalrecord int output',@totalrecord output--計算總記錄數
--計算總頁數
select @totalpage=@totalrecord --ceiling((@totalrecord+0.0)/@pagesize)
if @ordertype = 0
begin
set @strorder = ' order by [' + @orderfield +'] desc'
--如果@ordertype是0,就執行降序,這句很重要!
end
else
begin
set @strorder = ' order by [' + @orderfield +'] asc'
end
if (@sqlwhere ='''' or @sqlwhere='' or @sqlwhere is null)
set @sql = 'select * from (select row_number() over( '+@strorder+' ) as rowid,' + @fields + ' from ' + @tablename
else
set @sql = 'select * from (select row_number() over( '+@strorder+' ) as rowid,' + @fields + ' from ' + @tablename + ' where ' + @sqlwhere
--處理頁數超出範圍情況
if @pageindex<=0
set @pageindex = 1
if @pageindex>@totalpage
set @pageindex = @totalpage
--處理開始點和結束點
declare @startrecord int
declare @endrecord int
set @startrecord = (@pageindex-1)*@pagesize + 1
set @endrecord = @startrecord + @pagesize - 1
if @ordertype = 0
begin
set @strorder = ' order by rowid desc'
--如果@ordertype是0,就執行降序,這句很重要!
end
else
begin
set @strorder = ' order by rowid asc'
end
--繼續合成sql語句
set @sql = @sql + ') as ' + @tablename + ' where rowid between ' + convert(varchar(50),@startrecord) + ' and ' + convert(varchar(50),@endrecord) + ' '+@strorder
-- print @sql
exec(@sql)
if @@error <> 0
begin
rollback tran
return -1
end
else
begin
commit tran
return @totalrecord ---返回記錄總數
end
end
分頁儲存過程 分頁儲存過程
分頁儲存過程 alter proc dbo p pageshow pagesize int,每頁大小 currentpage int out,當前頁 housename nvarchar 50 房產名稱 totalcount int out,總記錄數 totalpage int out 總頁數 as...
分頁儲存過程
create proc p sobigo percentpage tblname varchar 255 t category 表名 strgetfields varchar 1000 需要返回的列 fldname varchar 255 排序的欄位名 pagesize int 10,頁尺寸 pag...
分頁儲存過程
create procedure pro select pageindex int,pagesize int as select student.sno,student.sname,student.s grade.math,grade.physics,grade.huaxue,grade.chine...