create procedure pagination3
(@tables varchar(1000), --表名稱,檢視
@primarykey varchar(100),--主關鍵字
@sort varchar(200) = 'bookid desc',--排序語句,不帶order by 比如:newsid desc,orderrows asc
@currentpage int = 1,--當前頁碼
@pagesize int = 10,--分頁尺寸
@fields varchar(1000) = '*',-- 查詢字段
@filter varchar(1000) = null,--過濾語句,不帶where
@group varchar(1000) = null --group語句,不帶group by)as
/*預設排序*/
if @sort is null or @sort = ''
set @sort = @primarykey
declare @sorttable varchar(100)
declare @sortname varchar(100)
declare @strsortcolumn varchar(200)
declare @operator char(2)
declare @type varchar(100)
declare @prec int
/*設定排序語句.*/
if charindex('desc',@sort)>0
begin
set @strsortcolumn = replace(@sort, 'desc', '')
set @operator = '<='
endelse
begin
if charindex('asc', @sort) = 0
set @strsortcolumn = replace(@sort, 'asc', '')
set @operator = '>='
endif charindex('.', @strsortcolumn) > 0
begin
set @sorttable = substring(@strsortcolumn, 0, charindex('.',@strsortcolumn))
set @sortname = substring(@strsortcolumn, charindex('.',@strsortcolumn) + 1, len(@strsortcolumn))
endelse
begin
set @sorttable = @tables
set @sortname = @strsortcolumn
endselect @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 @strpagesize varchar(50)
declare @strstartrow varchar(50)
declare @strfilter varchar(1000)
declare @str******filter varchar(1000)
declare @strgroup varchar(1000)
/*預設當前頁*/
if @currentpage < 1
set @currentpage = 1
/*設定分頁引數.*/
set @strpagesize = cast(@pagesize as varchar(50))
set @strstartrow = cast(((@currentpage - 1)*@pagesize + 1) as varchar(50))
/*篩選以及分組語句.*/
if @filter is not null and @filter != ''
begin
set @strfilter = ' where ' + @filter + ' '
set @str******filter = ' and ' + @filter + ' '
endelse
begin
set @str******filter = ''
set @strfilter = ''
endif @group is not null and @group != ''
set @strgroup = ' group by ' + @group + ' '
else
set @strgroup = ''
/*執行查詢語句*/
exec(
'declare @sortcolumn ' + @type + '
set rowcount ' + @strstartrow + '
select @sortcolumn=' + @strsortcolumn + ' from ' + @tables + @strfilter + ' ' + @strgroup + ' order by ' + @sort + '
set rowcount ' + @strpagesize + '
select ' + @fields + ' from ' + @tables + ' where ' + @strsortcolumn + @operator + ' @sortcolumn ' + @str******filter + ' ' + @strgroup + ' order by ' + @sort + '')
go
資料分頁 儲存過程
if exists select from sys.objects where name pagination drop procedure pagination gocreate procedure pagination tblname nvarchar 255 表名 strgetfields n...
資料分頁 儲存過程
if exists select from sys.objects where name pagination drop procedure pagination gocreate procedure pagination tblname nvarchar 255 表名 strgetfields n...
分頁儲存過程 分頁儲存過程
分頁儲存過程 alter proc dbo p pageshow pagesize int,每頁大小 currentpage int out,當前頁 housename nvarchar 50 房產名稱 totalcount int out,總記錄數 totalpage int out 總頁數 as...