alter procedure sqldatapaging@tbname varchar(255), --表名
@tbfields varchar(1000), --返回字段
@orderfield varchar(255), --排序的欄位名
@pagesize int, --頁尺寸
@pageindex int, --頁碼
@ordertype bit, --排序型別,1是公升序,0是降序
@strwhere varchar(1500), --查詢條件
@total int output --返回總記錄數
asdeclare @strsql varchar(5000) --主語句
declare @strorder varchar(200) --排序
declare @strsqlcount nvarchar(500)--查詢記錄總數主語句
declare @strtemp varchar(50) --排序附加語句
--------------排序-----------------0是降序,1未公升序
set @total=0
if @ordertype !=0
begin
set @strtemp= '>(select max('
set @strorder='order by ' + @orderfield + ' asc '
endelse
begin
set @strtemp= '<(select min('
set @strorder='order by '+ @orderfield + ' desc'
end--------------總記錄數---------------
if @strwhere !=''
begin
set @strsqlcount='select @totalcout=count(*) from (select * from ' + @tbname + ' where '+ @strwhere+') as tmp '
endelse
begin
set @strsqlcount='select @totalcout=count(*) from (select * from ' + @tbname+' ) as tmp '
endexec sp_executesql @strsqlcount,n'@totalcout int output',@total output
if @pageindex>(@total/@pagesize+1)
set @pageindex=(@total/@pagesize+1)
else if @pageindex<0
set @pageindex=0
if @pageindex>-1
--------------分頁為第一頁------------
begin
if @pageindex=0
begin
-- set @pageindex =0
if @strwhere !=''
begin
set @strsql = ' select top' +str(@pagesize)+ ' ' + @tbfields + ' from '+ @tbname +' where ' + @strwhere + ' ' + @strorder
endelse
begin
set @strsql = 'select top' +str(@pagesize)+ ' ' + @tbfields + ' from '+ @tbname + ' '+ @strorder
endend
--------------分頁不為第一頁------------
else
begin
if @strwhere !=''
begin
set @strsql='select top ' +str(@pagesize) + ' ' + @tbfields
+' from ' + @tbname + ' where '+@orderfield +' '
+ @strtemp+ ' ' +@orderfield+ ') from (select top '
+str(@pageindex*@pagesize) + ' '+@orderfield +' from '+@tbname+' where '+@strwhere
+ ' '+ @strorder +') as tb) and '+@strwhere+' '+ @strorder
endelse
begin
set @strsql='select top ' + str(@pagesize) + ' ' + @tbfields
+' from ' + @tbname + ' where '+@orderfield + ' '
+ @strtemp+ ' ' +@orderfield+ ') from (select top'
+str(@pageindex*@pagesize) + ' '+@orderfield +' from '+@tbname
+ ' '+ @strorder +') as tb)'+ @strorder
endend
exec (@strsql)
endelse
begin
print 'wrong page'
end
我的通用分頁儲存過程
考慮了好久,一直想不好用什麼方案好。綜合了各種方法寫了自己的分頁,準備在其他過程中呼叫。create procedure selectpagedsql sql nvarchar 512 indexfield nvarchar 100 pagesize int 10,pageindex int 1,s...
分頁儲存過程 分頁儲存過程
分頁儲存過程 alter proc dbo p pageshow pagesize int,每頁大小 currentpage int out,當前頁 housename nvarchar 50 房產名稱 totalcount int out,總記錄數 totalpage int out 總頁數 as...
分頁的儲存過程
alter procedure sql conn sort tblname varchar 255 表名 strgetfields varchar 1000 需要返回的列 fldname varchar 255 排序的欄位名 pagesize int 頁尺寸 pageindex int 頁碼 doc...