SQLServer2000分页存储过程
create procedure GetRecordByPage
(
@TableName nvarchar(100), --表名
@PrimaryKey nvarchar(50), --主键
@Fields nvarchar(1000), --字段
@CurrentPage int, --当前页数
@PageSize int, --每页记录数
@Filter nvarchar(1000), --条件
@Sort nvarchar(100) , --排序
@Counts int=0 output, --记录条数
@PageCount int=1 output --查询结果分页后的总页数
)
as
set nocount on
declare @sc1 nvarchar(1000),@sc2 nvarchar(1000)
declare @iasc int,@idesc int,@itype tinyint
declare @st1 nvarchar(1000),@st2 nvarchar(1000),@st3 nvarchar(1000),@st4 nvarchar(1000),@ssql nvarchar(4000)
set @CurrentPage = @CurrentPage +1;
/*----------------------判断where 条件是否空值-------------------*/
if len(@Filter)>2
select @sc1=' where '+@Filter+' ', @sc2=' where '+@Filter+' and '
else
begin
select @sc1='', @sc2=' where '
end
select @st1=upper(@Sort), @st2=@Sort, @itype=0, @Sort='', @st4=upper(@PrimaryKey)
/*-----------------------获取查询的数据行数---------------------*/
if len(@st2)>2
begin
select @iasc=0, @idesc=0
if @st4=substring(@st1,0,len(@st4)) --存在主建
begin
select @iasc=charindex('asc',@st1), @idesc=charindex('desc',@st1)
end
if (@iasc>0 and @idesc=0) or ((@iasc>0 and @idesc>0) and (@iasc<@idesc))
select @itype=1, @st3='>(select max('
else if (@iasc=0 and @idesc>0) or ((@iasc>0 and @idesc>0) and (@iasc>@idesc))
begin
select @itype=1, @st3='<(select min('
end
set @Sort=' order by '+@st2
end
/*-------------------------获取查询的数据行数----------------------*/
set @ssql='select @Counts=count(0) from '+@TableName+@sc1
exec sp_executesql @ssql,'@Counts int out',@Counts out
set @PageCount=(@Counts+@PageSize-1)/@PageSize
if @CurrentPage>@PageCount
set @CurrentPage=@PageCount
select @CurrentPage=(case when @Counts<(@CurrentPage-1)*@PageSize then ceiling(@Counts/@PageSize) when @CurrentPage<1 then 1 else @CurrentPage end)
if (@CurrentPage>1) and (@itype=1)
set @ssql='select top '+cast(@PageSize as nvarchar)+' '+@Fields+' from '+@TableName+@sc2+@PrimaryKey+@st3+@PrimaryKey+') from (select top '+cast((@CurrentPage-1)*@PageSize as nvarchar)+' '+@PrimaryKey+' from '+@TableName+@sc1+@Sort+') as tbtemp)'+@Sort
else if (@CurrentPage>1) and (@itype=0)
set @ssql='select '+@Fields+' from '+@TableName+@sc2+@PrimaryKey+' in (select top '+cast(@PageSize as nvarchar)+' '+@PrimaryKey+' from '+@TableName+@sc2+@PrimaryKey+' not in(select top '+cast((@CurrentPage-1)*@PageSize as nvarchar)+' '+@PrimaryKey+' from '+@TableName+@sc1+@Sort+')'+@Sort+')'+@Sort
else
begin
set @ssql='select top '+cast(@PageSize as nvarchar)+' '+@Fields+' from '+@TableName+@sc1+@Sort
end
exec(@ssql)
print @ssql
print @Counts
print @PageCount
set nocount off
go
(
@TableName nvarchar(100), --表名
@PrimaryKey nvarchar(50), --主键
@Fields nvarchar(1000), --字段
@CurrentPage int, --当前页数
@PageSize int, --每页记录数
@Filter nvarchar(1000), --条件
@Sort nvarchar(100) , --排序
@Counts int=0 output, --记录条数
@PageCount int=1 output --查询结果分页后的总页数
)
as
set nocount on
declare @sc1 nvarchar(1000),@sc2 nvarchar(1000)
declare @iasc int,@idesc int,@itype tinyint
declare @st1 nvarchar(1000),@st2 nvarchar(1000),@st3 nvarchar(1000),@st4 nvarchar(1000),@ssql nvarchar(4000)
set @CurrentPage = @CurrentPage +1;
/*----------------------判断where 条件是否空值-------------------*/
if len(@Filter)>2
select @sc1=' where '+@Filter+' ', @sc2=' where '+@Filter+' and '
else
begin
select @sc1='', @sc2=' where '
end
select @st1=upper(@Sort), @st2=@Sort, @itype=0, @Sort='', @st4=upper(@PrimaryKey)
/*-----------------------获取查询的数据行数---------------------*/
if len(@st2)>2
begin
select @iasc=0, @idesc=0
if @st4=substring(@st1,0,len(@st4)) --存在主建
begin
select @iasc=charindex('asc',@st1), @idesc=charindex('desc',@st1)
end
if (@iasc>0 and @idesc=0) or ((@iasc>0 and @idesc>0) and (@iasc<@idesc))
select @itype=1, @st3='>(select max('
else if (@iasc=0 and @idesc>0) or ((@iasc>0 and @idesc>0) and (@iasc>@idesc))
begin
select @itype=1, @st3='<(select min('
end
set @Sort=' order by '+@st2
end
/*-------------------------获取查询的数据行数----------------------*/
set @ssql='select @Counts=count(0) from '+@TableName+@sc1
exec sp_executesql @ssql,'@Counts int out',@Counts out
set @PageCount=(@Counts+@PageSize-1)/@PageSize
if @CurrentPage>@PageCount
set @CurrentPage=@PageCount
select @CurrentPage=(case when @Counts<(@CurrentPage-1)*@PageSize then ceiling(@Counts/@PageSize) when @CurrentPage<1 then 1 else @CurrentPage end)
if (@CurrentPage>1) and (@itype=1)
set @ssql='select top '+cast(@PageSize as nvarchar)+' '+@Fields+' from '+@TableName+@sc2+@PrimaryKey+@st3+@PrimaryKey+') from (select top '+cast((@CurrentPage-1)*@PageSize as nvarchar)+' '+@PrimaryKey+' from '+@TableName+@sc1+@Sort+') as tbtemp)'+@Sort
else if (@CurrentPage>1) and (@itype=0)
set @ssql='select '+@Fields+' from '+@TableName+@sc2+@PrimaryKey+' in (select top '+cast(@PageSize as nvarchar)+' '+@PrimaryKey+' from '+@TableName+@sc2+@PrimaryKey+' not in(select top '+cast((@CurrentPage-1)*@PageSize as nvarchar)+' '+@PrimaryKey+' from '+@TableName+@sc1+@Sort+')'+@Sort+')'+@Sort
else
begin
set @ssql='select top '+cast(@PageSize as nvarchar)+' '+@Fields+' from '+@TableName+@sc1+@Sort
end
exec(@ssql)
print @ssql
print @Counts
print @PageCount
set nocount off
go