set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE [dbo].[page_proc]
@selectstr varchar(2048), --所取字段
@tbname varchar(50), --所查询的表的名称
@querystr varchar(2048), --查询条件
@pkey varchar(50), --主键(排序段)
@ordertype bit=1, --排序规则 0:升序,1:降序
@pagesize int, --每页数量
@page int output, --指定返回页
@rscount int output, --返回总数
@pagecount int output --返回总页数
as
declare @sqlstr nvarchar(2048)
declare @sqlcount nvarchar(2048)
declare @l_order varchar(255)
declare @l_compare varchar(255)
declare @l_tquery varchar(2048)
declare @l_query varchar(2048)
/*指定数据返回开始*/
if @ordertype=1 --降序
begin
set @l_order='desc'
set @l_compare=@pkey+'<(SELECT MIN('+@pkey+')'
end
else --升序
begin
set @l_order=''
set @l_compare=@pkey+'>(SELECT MAX('+@pkey+')'
end
if @querystr is null or rtrim(@querystr)=''
begin
set @l_tquery=''
set @l_query=''
end
else
begin
set @l_tquery='where ('+@querystr+')'
set @l_query='and ('+@querystr+')'
end
set nocount on
/*计算返回数据总数和页数*/
set @sqlcount='select @rscount=count('+@pkey+') from '+@tbname+' '+@l_tquery
exec sp_executesql @sqlcount,N'@rscount int OUTPUT',@rscount output
set @pagecount=ceiling(@rscount/cast(@pagesize as float))
if @page>@pagecount
set @page=@pagecount
if @page<1
set @page=1
/*页数计算结束*/
if @page>1
set @sqlstr='select top '+cast(@pagesize as varchar)+' '+@selectstr+' from '+@tbname+' where '+@l_compare+' from (select top '+cast(@pagesize*(@page-1) as varchar)+' '+@Pkey+' from '+@tbname+' '+@l_tquery+' order by '+@Pkey+' '+@l_order+') as tb) '+@l_query+' order by '+@pkey+' '+@l_order
else
set @sqlstr='select top '+cast(@pagesize as varchar)+' '+@selectstr+' from '+@tbname+' '+@l_tquery+' order by '+@pkey+' '+@l_order
exec(@sqlstr)
set nocount off
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE [dbo].[page_proc]
@selectstr varchar(2048), --所取字段
@tbname varchar(50), --所查询的表的名称
@querystr varchar(2048), --查询条件
@pkey varchar(50), --主键(排序段)
@ordertype bit=1, --排序规则 0:升序,1:降序
@pagesize int, --每页数量
@page int output, --指定返回页
@rscount int output, --返回总数
@pagecount int output --返回总页数
as
declare @sqlstr nvarchar(2048)
declare @sqlcount nvarchar(2048)
declare @l_order varchar(255)
declare @l_compare varchar(255)
declare @l_tquery varchar(2048)
declare @l_query varchar(2048)
/*指定数据返回开始*/
if @ordertype=1 --降序
begin
set @l_order='desc'
set @l_compare=@pkey+'<(SELECT MIN('+@pkey+')'
end
else --升序
begin
set @l_order=''
set @l_compare=@pkey+'>(SELECT MAX('+@pkey+')'
end
if @querystr is null or rtrim(@querystr)=''
begin
set @l_tquery=''
set @l_query=''
end
else
begin
set @l_tquery='where ('+@querystr+')'
set @l_query='and ('+@querystr+')'
end
set nocount on
/*计算返回数据总数和页数*/
set @sqlcount='select @rscount=count('+@pkey+') from '+@tbname+' '+@l_tquery
exec sp_executesql @sqlcount,N'@rscount int OUTPUT',@rscount output
set @pagecount=ceiling(@rscount/cast(@pagesize as float))
if @page>@pagecount
set @page=@pagecount
if @page<1
set @page=1
/*页数计算结束*/
if @page>1
set @sqlstr='select top '+cast(@pagesize as varchar)+' '+@selectstr+' from '+@tbname+' where '+@l_compare+' from (select top '+cast(@pagesize*(@page-1) as varchar)+' '+@Pkey+' from '+@tbname+' '+@l_tquery+' order by '+@Pkey+' '+@l_order+') as tb) '+@l_query+' order by '+@pkey+' '+@l_order
else
set @sqlstr='select top '+cast(@pagesize as varchar)+' '+@selectstr+' from '+@tbname+' '+@l_tquery+' order by '+@pkey+' '+@l_order
exec(@sqlstr)
set nocount off