create procedure usp_paginglarge
@tablenames varchar(300), --表名,可以是多個表,但不能用別名
@primarykey varchar(100), --主鍵,可以為空,但@order為空時該值不能為空
@fields varchar(350), --要取出的字段,可以是多個表的字段,可以為空,為空表示select *
@pagesize int, --每頁記錄數
@currentpage int, --當前頁,0表示第1頁
@filter varchar(200) = '', --條件,可以為空,不用填 where
@group varchar(200) = '', --分組依據,可以為空,不用填 group by
@order varchar(200) = '', --排序,可以為空,為空缺省按主鍵公升序排列,不用填 order by
@recordcount int = 0 output
asbegin
declare @sortcolumn varchar(200)
declare @operator char(2)
declare @sorttable varchar(200)
declare @sortname varchar(200)
declare @tmpselect nvarchar(200)
if @fields = ''
set @fields = '*'
if @filter = ''
set @filter = 'where 1=1'
else
set @filter = 'where ' + @filter
if @group <>''
set @group = 'group by ' + @group
if @order <> ''
begin
declare @pos1 int, @pos2 int
set @order = replace(replace(@order, ' asc', ' asc'), ' desc', ' desc')
if charindex(' desc', @order) > 0
if charindex(' asc', @order) > 0
begin
if charindex(' desc', @order) < charindex(' asc', @order)
set @operator = '<='
else
set @operator = '>='
endelse
set @operator = '<='
else
set @operator = '>='
set @sortcolumn = replace(replace(replace(@order, ' asc', ''), ' desc', ''), ' ', '')
set @pos1 = charindex(',', @sortcolumn)
if @pos1 > 0
set @sortcolumn = substring(@sortcolumn, 1, @pos1-1)
set @pos2 = charindex('.', @sortcolumn)
if @pos2 > 0
begin
set @sorttable = substring(@sortcolumn, 1, @pos2-1)
if @pos1 > 0
set @sortname = substring(@sortcolumn, @pos2+1, @pos1-@pos2-1)
else
set @sortname = substring(@sortcolumn, @pos2+1, len(@sortcolumn)-@pos2)
endelse
begin
set @sorttable = @tablenames
set @sortname = @sortcolumn
endend
else
begin
set @sortcolumn = @primarykey
set @sorttable = @tablenames
set @sortname = @sortcolumn
set @order = @sortcolumn
set @operator = '>='
enddeclare @type varchar(50)
declare @prec int
select @type=t.name, @prec=c.prec
from sysobjects o
join syscolumns c on o.id=c.id
join systypes t on c.xusertype=t.xusertype
where o.name = @sorttable and c.name = @sortname
if charindex('char', @type) > 0
set @type = @type + '(' + cast(@prec as varchar) + ')'
declare @toprows int
set @toprows = @pagesize * @currentpage + 1
print @toprows
print @operator
exec('
declare @sortcolumnbegin ' + @type + '
set rowcount ' + @toprows + '
select @sortcolumnbegin=' + @sortcolumn + ' from ' + @tablenames + ' ' + @filter + ' ' + @group + ' order by ' + @order + '
set rowcount ' + @pagesize + '
select ' + @fields + ' from ' + @tablenames + ' ' + @filter + ' and ' + @sortcolumn + '' + @operator + '@sortcolumnbegin ' + @group + ' order by ' + @order + '
')declare @str_count_sql nvarchar(500)
set @str_count_sql= 'select @totalcount=count('+@primarykey+') from ' + @tablenames + ' ' + @filter
exec sp_executesql @str_count_sql,n'@totalcount int=0 output',@recordcount output
endgo
呼叫例子
declare @allcount int
exec usp_paginglarge 'online_course_menu','id','creatid,courseid,mtype,class,sortname',10,1,'','','id desc',@allcount output
select @allcount
分頁儲存過程 分頁儲存過程
分頁儲存過程 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...