create 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) --排序附加語句
--------------排序-----------------1是降序,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 ' + @tbname + 'where '+ @strwhere
endelse
begin
set @strsqlcount='select @totalcout=count(*) from ' + @tbname
end--------------如果是第一頁------------
if @pageindex =1
begin
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-1)*@pagesize) + ' '+@orderfield +' from '+@tbname
+ @strwhere+ ' '+ @strorder +') as tb)'+ @strorder
endelse
begin
set @strsql='select top ' + str(@pagesize) + ' ' + @tbfields
+' from ' + @tbname + ' where '+@orderfield + ' '
+ @strtemp+ ' ' +@orderfield+ ') from (select top'
+str((@pageindex-1)*@pagesize) + ' '+@orderfield +' from '+@tbname
+ ' '+ @strorder +') as tb)'+ @strorder
endend
exec sp_executesql @strsqlcount,n'@totalcout int output',@total output
exec(@strsql)
sql 儲存過程分頁
create proc myx prpagerecordset querystr nvarchar 1000 keyfield nvarchar 200 pagesize int,pagenumber int as begin declare sqltext as nvarchar 4000 dec...
SQL 儲存過程 分頁
1.俄羅斯儲存過程 的改良版 create procedure pagination1 pagesize int,頁面大小,如每頁儲存20條記錄 pageindex int 當前頁碼 as set nocount on begin declare indextable table id int id...
SQL 分頁儲存過程
create procedure splitpage sql nvarchar 4000 不帶排序語句的sql語句 page int,頁碼 recsperpage int,每頁容納的記錄數 id varchar 255 需要排序的不重複的id號 sort varchar 255 排序欄位及規則 as...