分类:
2008-10-15 16:27:55
Create PROCEDURE [dbo].[Basic_Pagination2000]
@tblName varchar(255), -- 表名
@fidlelist varchar(2000), --要查询字段
@fldName varchar(255), -- 排序字段
@PageSize int, -- 页尺寸
@PageIndex int, -- 页码
@IsReCount bit, -- 返回记录总数, 非 0 值则返回
@OrderType bit, -- 设置排序类型, 非 0 值则降序
@strWhere varchar(1000) -- 查询条件 (注意: 不要加 where)
AS
declare @strSQL varchar(6000) -- 主语句
declare @strTmp varchar(100),@tmpwhere varchar(200) -- 临时变量
declare @strOrder varchar(400) -- 排序类型
if @OrderType != 0
begin
set @strTmp = '<(select min'
set @strOrder = ' order by [' + @fldName +'] desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by [' + @fldName +'] asc'
end
set @tmpwhere='';
if(@strWhere!='')
begin
set @tmpwhere=' where '+@strWhere;
end
if @PageIndex = 1
begin
set @strSQL = 'select top ' + str(@PageSize) +' '+@fidlelist+' '+'from ['
+ @tblName + '] ' + @tmpwhere + ' ' + @strOrder
end
else
begin
set @strSQL = 'select top ' + str(@PageSize) + ' '+@fidlelist+' '+'from ['
+ @tblName + '] where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) from (select top ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] from [' + @tblName + '] ' + @tmpwhere + ' '
+ @strOrder + ') as tblTmp) ' + @tmpwhere + ' ' + @strOrder
end
exec(@strSQL)
if @IsReCount != 0
begin
set @strSQL = 'select count(*) as Total from [' + @tblName + ']'+ @strWhere
exec (@strSQL)
end