/************************ PageByTopMax************************/
IF EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID ( N'[dbo].[PageByTopMax]' ) AND type in ( N'P' , N'PC' ))
DROP PROCEDURE [dbo]. [PageByTopMax]
go
CREATE PROCEDURE [dbo]. [PageByTopMax]
@tableName varchar ( 255),
@fieldName varchar ( 1000) = '*' ,
@PageSize int = 10, -- 页尺寸 ( 每页记录数 )
@page int = 1, -- 页码
@fieldSort varchar ( 255)= '' , -- 排序的字段名
@sort bit = 0, -- 配置排序类型 , 非值则降序
@condition varchar ( 1500) = '' , -- 查询条件 ( 留心 : 不要加 where)
@pageCount int = 1 output , ---- 查询结果分页后的总页数
@recordCount int = 1 output , ---- 查询结果记录总数
@sql nvarchar ( 1000) = '' output
AS
declare @strSQL nvarchar ( 4000) -- 主语句
declare @strTmp varchar ( 110) -- 临时变量
declare @strOrder varchar ( 400) -- 排序类型
if @condition is null or @condition= '' -- 没有设置显示条件
begin
set @strSQL = 'select @recordCount=count(1) from ' + @tableName + ''
end
else
begin
set @strSQL = 'select @recordCount=count(1) from ' + @tableName + ' where ' + @condition
end
---- 取得查询结果总数量 -----
exec sp_executesql @strSQL, N'@recordCount int out ' , @recordCount out
set @pageCount=( @recordCount+ @pageSize- 1)/ @pageSize
if @sort != 0 -- 降序 (desc)
begin
set @strTmp = '<(select min'
set @strOrder = ' order by ' + @fieldSort + ' desc'
end
else
begin
set @strTmp = '>(select max'
set @strOrder = ' order by ' + @fieldSort + ' asc'
end
if @page = 1
begin
if @condition != ''
set @strSQL = 'select top ' + convert ( varchar ( 12), @PageSize)+ ' ' + @fieldName+ ' from ' + @tableName + ' where ' + @condition + ' ' + @strOrder
else
set @strSQL = 'select top ' + convert ( varchar ( 12), @PageSize)+ ' ' + @fieldName+ ' from ' + @tableName+ ' ' + @strOrder
end
else
begin
set @strSQL = 'select top ' + convert ( varchar ( 12), @PageSize)+ ' ' + @fieldName+ ' from ' + @tableName+ ' where ' + @fieldSort+ '' + @strTmp+ '(' + right( @fieldSort, len ( @fieldSort)- charindex ( '.' , @fieldSort))+ ') from (select top ' + convert ( varchar ( 12),( @page- 1)* @PageSize)+ ' ' + @fieldSort+ ' from ' + @tableName+ '' + @strOrder+ ') as tblTmp)' + @strOrder
if @condition != ''
set @strSQL = 'select top ' + convert ( varchar ( 12), @PageSize)+ ' ' + @fieldName+ ' from ' + @tableName+ ' where ' + @fieldSort+ '' + @strTmp+ '(' +right( @fieldSort, len ( @fieldSort)- charindex ( '.' , @fieldSort))+ ') from (select top ' + convert ( varchar ( 12),( @page- 1)* @PageSize) + ' ' + @fieldSort+ ' from ' + @tableName+ ' where ' + @condition+ ' ' + @strOrder+ ') as tblTmp) and ' + @condition+ ' ' + @strOrder
end
------ 返回查询结果 -----
set @sql = @strSQL
exec sp_executesql @strSQL
print @strSQL
go
/************************ PageByNotIn************************/
IF EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID ( N'[dbo].[PageByNotIn]' ) AND type in ( N'P' , N'PC' ))
DROP PROCEDURE [dbo]. [PageByNotIn]
go
CREATE procedure [dbo]. [PageByNotIn]
(
@tableName nvarchar ( 500), ---- 要显示的表或多个表的连接
@fieldName nvarchar ( 500) = '*' , ---- 要显示的字段列表
@pageSize int = 1, ---- 每页显示的记录个数
@page int = 10, ---- 要显示那一页的记录
@fieldSort nvarchar ( 200) = null, ---- 排序字段列表或条件
@sort bit = 0, ---- 排序方法,为升序,为降序 ( 如果是多字段排列 Sort 指代最后一个排序字段的排列顺序 ( 最后一个排序字段不加排序标记 )-- 程序传参如: ' SortA Asc,SortB Desc,SortC ')
@condition nvarchar ( 1000) = null, ---- 查询条件 , 不需 where, 以 And 开始
@id nvarchar ( 150), ---- 主表的主键
@distinct bit = 0 , ---- 是否添加查询字段的 DISTINCT 默认不添加 /1 添加
@pageCount int = 1 output , ---- 查询结果分页后的总页数
@recordCount int = 1 output , ---- 查询到的记录数
@sql nvarchar ( 1000) = '' output ----- 最后返回的 SQL 语句
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar ( 4000) ---- 存放动态生成的 SQL 语句
Declare @strTmp nvarchar ( 4000) ---- 存放取得查询结果总数的查询语句
Declare @strID nvarchar ( 1000) ---- 存放取得查询开头或结尾 ID 的查询语句
Declare @strSortType nvarchar ( 10) ---- 数据排序规则 A
Declare @strFSortType nvarchar ( 10) ---- 数据排序规则 B
Declare @SqlSelect nvarchar ( 50) ---- 对含有 DISTINCT 的查询进行 SQL 构造
Declare @SqlCounts nvarchar ( 50) ---- 对含有 DISTINCT 的总数查询进行 SQL 构造
if @distinct = 0
begin
set @SqlSelect = 'select '
set @SqlCounts = 'Count(*)'
end
else
begin
set @SqlSelect = 'select distinct '
set @SqlCounts = 'Count(DISTINCT ' + @id+ ')'
end
if @sort= 0
begin
set @strFSortType= ' ASC '
set @strSortType= ' DESC '
end
else
begin
set @strFSortType= ' DESC '
set @strSortType= ' ASC '
end
-------- 生成查询语句 --------
-- 此处 @strTmp 为取得查询结果数量的语句
if @condition is null or @condition= '' -- 没有设置显示条件
begin
set @sqlTmp = @fieldName + ' From ' + @tableName
set @strTmp = @SqlSelect+ ' @recordCount=' + @SqlCounts+ ' FROM ' + @tableName
set @strID = ' From ' + @tableName
end
else
begin
set @sqlTmp = + @fieldName + 'From ' + @tableName + ' where (1>0) and ' + @condition
set @strTmp = @SqlSelect+ ' @recordCount=' + @SqlCounts+ ' FROM ' + @tableName + ' where (1>0) and ' + @condition
set @strID = ' From ' + @tableName + ' where (1>0) and ' + @condition
end
---- 取得查询结果总数量 -----
exec sp_executesql @strTmp, N'@recordCount int out ' , @recordCount out
declare @tmpCounts int
if @recordCount = 0
set @tmpCounts = 1
else
set @tmpCounts = @recordCount
-- 取得分页总数
set @pageCount=( @tmpCounts+ @pageSize- 1)/ @pageSize
/**//** 当前页大于总页数取最后一页 **/
if @page> @pageCount
set @page= @pageCount
--/*----- 数据分页分处理 -------*/
declare @pageIndex int -- 总数 / 页大小
declare @lastcount int -- 总数 % 页大小
set @pageIndex = @tmpCounts/ @pageSize
set @lastcount = @tmpCounts% @pageSize
if @lastcount > 0
set @pageIndex = @pageIndex + 1
else
set @lastcount = @pagesize
--//*** 显示分页
--//**Begin 解决分页排序字段报错的问题
declare @fldSortContent varchar ( 200); -- 存储排序字段
declare @fldSortNum int ; -- 存储排序字段 '.' 的位置如果指定列名就有 '.'
set @fldSortContent= @fieldSort;
set @fldSortNum= charindex ( '.' , @fldSortContent);
if ( @fldSortNum> 0) -- 判断是否存在 '.' 如果有就去掉 '.' 之前的全部字符
begin
set @fldSortContent= Substring ( @fldSortContent, @fldSortNum+ 1, len ( @fldSortContent));
end
--//**End 解决分页排序字段报错的问题
if @condition is null or @condition= '' -- 没有设置显示条件
begin
if @pageIndex<= @pageIndex / 2 + @pageIndex % 2 -- 前半部分数据处理
begin
set @strTmp= @SqlSelect+ ' top ' + CAST ( @pageSize as VARCHAR ( 4))+ ' ' + @fieldName+ ' from ' + @tableName
+ ' where ' + @id+ ' not in(' + @SqlSelect+ ' top ' + CAST ( @pageSize*( @page- 1) as Varchar ( 20)) + ' ' + @id + ' from ' + @tableName
+ ' order by ' + @fieldSort + ' ' + @strFSortType+ ')'
+ ' order by ' + @fieldSort + ' ' + @strFSortType
end
else
begin
set @page = @pageIndex- @page+ 1 -- 后半部分数据处理
if @page <= 1 -- 最后一页数据显示
set @strTmp= @SqlSelect+ ' * from (' + @SqlSelect+ ' top ' + CAST ( @lastcount as VARCHAR ( 4))+ ' ' + @fieldName+ ' from ' + @tableName
+ ' order by ' + @fieldSort + ' ' + @strSortType+ ') AS TempTB' + ' order by ' + @fldSortContent + ' ' + @strFSortType
else
set @strTmp= @SqlSelect+ ' * from (' + @SqlSelect+ ' top ' + CAST ( @pageSize as VARCHAR ( 4))+ ' ' + @fieldName+ ' from ' + @tableName
+ ' where ' + @id+ ' not in(' + @SqlSelect+ ' top ' + CAST ( @pageSize*( @page- 2)+ @lastcount as Varchar ( 20)) + ' ' + @id + ' from ' + @tableName
+ ' order by ' + @fieldSort + ' ' + @strSortType+ ')'
+ ' order by ' + @fieldSort + ' ' + @strSortType+ ') AS TempTB' + ' order by ' + @fldSortContent + ' ' + @strFSortType
end
end
else -- 有查询条件
begin
if @pageIndex<= @pageIndex / 2 + @pageIndex % 2 -- 前半部分数据处理
begin
set @strTmp= @SqlSelect+ ' top ' + CAST ( @pageSize as VARCHAR ( 4))+ ' ' + @fieldName + ' from ' + @tableName
+ ' where ' + @id+ ' not in(' + @SqlSelect+ ' top ' + CAST ( @pageSize*( @page- 1) as Varchar ( 20)) + ' ' + @id + ' from ' + @tableName
+ ' Where (1>0) and ' + @condition + ' order by ' + @fieldSort + ' ' + @strFSortType+ ')'
+ ' And ' + @condition + ' order by ' + @fieldSort + ' ' + @strFSortType
end
else
begin
set @page = @pageIndex- @page+ 1 -- 后半部分数据处理
if @page <= 1 -- 最后一页数据显示
set @strTmp= @SqlSelect+ ' * from (' + @SqlSelect+ ' top ' + CAST ( @lastcount as VARCHAR ( 4))+ ' ' + @fieldName+ ' from ' + @tableName
+ ' where (1>0) and ' + @condition + ' order by ' + @fieldSort + ' ' + @strSortType+ ') AS TempTB' + ' order by ' + @fldSortContent + ' ' + @strFSortType
else
set @strTmp= @SqlSelect+ ' * from (' + @SqlSelect+ ' top ' + CAST ( @pageSize as VARCHAR ( 4))+ ' ' + @fieldName+ ' from ' + @tableName
+ ' where ' + @id+ ' not in(' + @SqlSelect+ ' top ' + CAST ( @pageSize*( @page- 2)+ @lastcount as Varchar ( 20)) + ' ' + @id + ' from ' + @tableName
+ ' where (1>0) and ' + @condition + ' order by ' + @fieldSort + ' ' + @strSortType+ ') and '
+ @condition + ' order by ' + @fieldSort + ' ' + @strSortType+ ') AS TempTB' + ' order by ' + @fldSortContent + ' ' + @strFSortType
end
end
------ 返回查询结果 -----
set @sql = @strTmp
exec sp_executesql @strTmp
print @strTmp
----
go
/************************ PageByInnerProc************************/
IF EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID ( N'[dbo].[PageByInnerProc]' ) AND type in ( N'P' , N'PC' ))
DROP PROCEDURE [dbo]. [PageByInnerProc]
go
create procedure PageByInnerProc
@sql nvarchar ( 4000), -- 要执行的 sql 语句
@currentpage int = 2, -- 要显示的页码
@pagesize int = 10, -- 每页的大小
@pagecount int = 0 output , -- 总页数
@rowcount int = 0 output
as
set nocount on
declare @p1 int
exec sp_cursoropen @p1 output , @sql, @scrollopt= 1, @ccopt= 1, @rowcount= @rowcount output
select @pagecount= ceiling ( 1.0* @rowcount/ @pagesize)
, @currentpage=( @currentpage- 1)* @pagesize+ 1
select @currentpage
exec sp_cursorfetch @p1, 16, @currentpage, @pagesize
exec sp_cursorclose @p1
go
/************************ PageByRowNumber************************/
IF EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID ( N'[dbo].[PageByRowNumber]' ) AND type in ( N'P' , N'PC' ))
DROP PROCEDURE [dbo]. [PageByRowNumber]
go
CREATE procedure PageByRowNumber
@TableName varchar ( 5000), -- 要进行分页的表,也可以用联接,如 dbo.employee 或 dbo.employee INNER JOIN dbo.jobs ON (dbo.employee.job_id=dbo.jobs.job_id)
@Fields varchar ( 5000), -- 表中的字段,可以使用 * 代替
@OrderField varchar ( 5000), -- 要排序的字段
@sqlWhere varchar ( 5000), --WHERE 子句
@pageSize int , -- 分页的大小
@pageIndex int , -- 要显示的页的索引
@PageCount int output , -- 页的总数
@rowcount int output -- 记录的总数
as
begin
Begin Tran
set nocount on
Declare @sql nvarchar ( 4000);
if ( @sqlWhere IS NULL or @sqlWhere = '' )
-- 在没有 WHERE 子句的情况下得到表中所有的记录总数
set @sql = 'select @rowcount = count(1) from ' + @TableName
else
-- 利用 WHERE 子句进行过滤
set @sql = 'select @rowcount = count(1) from ' + @TableName + ' where ' + @sqlWhere
-- 执行 sql 语句得到记录总数
EXEC sp_executesql @sql, N'@rowcount int OUTPUT' , @rowcount OUTPUT
select @PageCount= CEILING (( @rowcount+ 0.0)/ @PageSize)
-- 根据特定的排序字段为为行分配唯一 ROW_NUMBER 的顺序
if ( @sqlWhere IS NULL or @sqlWhere = '' )
set @sql = 'select * from (select ROW_NUMBER() over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName
else
set @sql = 'select * from (select ROW_NUMBER() over(order by ' + @OrderField + ') as rowId,' + @Fields + ' from ' + @TableName + ' where ' + @SqlWhere
-- 确保当前页的索引在合理的范围之内
--if @PageIndex<=0
--Set @pageIndex = 1
--if @pageIndex>@PageCount
--Set @pageIndex = @PageCount
-- 得到当前页在整个结果集中准确的 ROW_NUMBER 值
Declare @StartRecord int
Declare @EndRecord int
set @StartRecord = ( @pageIndex- 1)* @PageSize + 1
set @EndRecord = @StartRecord + @pageSize - 1
-- 输出当前页中的数据
set @Sql = @Sql + ') as t' + ' where rowId between ' + Convert ( varchar ( 50), @StartRecord) + ' and ' + Convert ( varchar ( 50), @EndRecord)
print @sql
Exec ( @Sql)
set nocount off
If @@Error <> 0
Begin
RollBack Tran
Return - 1
End
Else
Begin
Commit Tran
Return @rowcount
End
end
go
/************************ PageByRowNumber************************/
CREATE procedure [dbo].[PageByRowNumber]
@TableName varchar(5000), --表名
@SelectFields varchar(5000), --字段列表
@OrderField varchar(5000), --排序字段
@WhereStr varchar(5000), --WHERE子句
@PageSize int, --分页大小
@PageNo int, --页号
@IfCount bit,--是否统计页数
@PageCount int output, --总页数
@RowCount int output --总行数
as
SET LOCK_TIMEOUT 60000
SET NOCOUNT ON
select @PageCount=1,@RowCount=0
declare @Sql nvarchar(4000)
select @WhereStr=' 1=1 '+case when isnull(@WhereStr,'')='' then '' else (' and '+@WhereStr) end
select @Sql='select @RowCount = count(1) from ' + @TableName + ' where ' + @WhereStr
if(@IfCount=1)
begin
exec sp_executesql @Sql,N'@RowCount int out ',@RowCount out
select @PageCount=(@RowCount+@PageSize-1)/@PageSize
if(@PageNo>@PageCount) return
end
declare @RowStart int,@RowEnd int
select @RowStart=(@PageNo-1)*@PageSize + 1,@RowEnd=@RowStart+@PageSize-1
select @Sql='select * from (select '+@SelectFields+',row_number() over(order by '+@OrderField+') as RowNo from '+@TableName
+' where '+@WhereStr+') as t where RowNo between '+cast(@RowStart as nvarchar)+' and '+cast(@RowEnd as varchar)
--print @Sql
exec(@Sql)
go
/************************ PageByTopMaxDichotomy************************/
IF EXISTS ( SELECT * FROM sys.objects WHERE object_id = OBJECT_ID ( N'[dbo].[PageByTopMaxDichotomy]' ) AND type in ( N'P' , N'PC' ))
DROP PROCEDURE [dbo]. [PageByTopMaxDichotomy]
go
create PROCEDURE PageByTopMaxDichotomy
( @tblName nvarchar ( 200), ---- 要显示的表或多个表的连接
@fldName nvarchar ( 500) = '*' , ---- 要显示的字段列表
@fldSort nvarchar ( 200) = null, ---- 排序字段列表或条件
@Sort bit = 0, ---- 排序方法,为升序,为降序 ( 如果是多字段排列 Sort 指代最后一个排序字段的排列顺序 ( 最后一个排序字段不加排序标记 )-- 程序传参如: ' SortA Asc,SortB Desc,SortC ')
@strCondition nvarchar ( 1000) = null, ---- 查询条件 , 不需 where
@ID nvarchar ( 150), ---- 主表的主键
@Dist bit = 0, ---- 是否添加查询字段的 DISTINCT 默认不添加 /1 添加
@pageSize int = 10, ---- 每页显示的记录个数
@page int = 1, ---- 要显示那一页的记录
@pageCount int = 1 output , ---- 查询结果分页后的总页数
@Counts int = 1 output ---- 查询到的记录数
)
AS
SET NOCOUNT ON
Declare @sqlTmp nvarchar ( 1000) ---- 存放动态生成的 SQL 语句
Declare @strTmp nvarchar ( 1000) ---- 存放取得查询结果总数的查询语句
Declare @strID nvarchar ( 1000) ---- 存放取得查询开头或结尾 ID 的查询语句
Declare @strSortType nvarchar ( 10) ---- 数据排序规则 A
Declare @strFSortType nvarchar ( 10) ---- 数据排序规则 B
Declare @SqlSelect nvarchar ( 50) ---- 对含有 DISTINCT 的查询进行 SQL 构造
Declare @SqlCounts nvarchar ( 50) ---- 对含有 DISTINCT 的总数查询进行 SQL 构造
if @Dist = 0
begin
set @SqlSelect = 'select '
set @SqlCounts = 'Count(0)'
end
else
begin
set @SqlSelect = 'select distinct '
set @SqlCounts = 'Count(DISTINCT ' + @ID+ ')'
end
if @Sort= 0
begin
set @strFSortType= ' ASC '
set @strSortType= ' DESC '
end
else
begin
set @strFSortType= ' DESC '
set @strSortType= ' ASC '
end
-------- 生成查询语句 --------
-- 此处 @strTmp 为取得查询结果数量的语句
if @strCondition is null or @strCondition= '' -- 没有设置显示条件
begin
set @sqlTmp = @fldName + ' From ' + @tblName
set @strTmp = @SqlSelect+ ' @Counts=' + @SqlCounts+ ' FROM ' + @tblName
set @strID = ' From ' + @tblName
end
else
begin
set @sqlTmp = + @fldName + 'From ' + @tblName + ' where (1>0) ' + @strCondition
set @strTmp = @SqlSelect+ ' @Counts=' + @SqlCounts+ ' FROM ' + @tblName + ' where (1>0) ' + @strCondition
set @strID = ' From ' + @tblName + ' where (1>0) ' + @strCondition
end
---- 取得查询结果总数量 -----
exec sp_executesql @strTmp, N'@Counts int out ' , @Counts out
declare @tmpCounts int
if @Counts = 0
set @tmpCounts = 1
else
set @tmpCounts = @Counts
-- 取得分页总数
set @pageCount=( @tmpCounts+ @pageSize- 1)/ @pageSize
/**//** 当前页大于总页数取最后一页 **/
if @page> @pageCount
set @page= @pageCount
--/*----- 数据分页分处理 -------*/
declare @pageIndex int -- 总数 / 页大小
declare @lastcount int -- 总数 % 页大小
set @pageIndex = @tmpCounts/ @pageSize
set @lastcount = @tmpCounts% @pageSize
if @lastcount > 0
set @pageIndex = @pageIndex + 1
else
set @lastcount = @pagesize
-- 为配合显示
--set nocount off
--select @page curpage,@pageSize pagesize,@pageCount countpage,@tmpCounts [Rowcount]
--set nocount on
--//*** 显示分页
if @strCondition is null or @strCondition= '' -- 没有设置显示条件
begin
if @pageIndex< 2 or @page<= @pageIndex / 2 + @pageIndex % 2 -- 前半部分数据处理
begin
if @page= 1
set @strTmp= @SqlSelect+ ' top ' + CAST ( @pageSize as VARCHAR ( 40))+ ' ' + @fldName+ ' from ' + @tblName
+ ' order by ' + @fldSort + ' ' + @strFSortType
else
begin
set @strTmp= @SqlSelect+ ' top ' + CAST ( @pageSize as VARCHAR ( 40))+ ' ' + @fldName+ ' from ' + @tblName
+ ' where ' + @ID
if @Sort= 0
set @strTmp = @strTmp + '>(select max('
else
set @strTmp = @strTmp + '<(select min('
set @strTmp = @strTmp + right( @ID, len ( @ID)- charindex ( '.' , @ID)) + ') from (' + @SqlSelect+ ' top ' + CAST ( @pageSize*( @page- 1) as Varchar ( 20)) + ' ' + @ID + ' from ' + @tblName
+ ' order by ' + @fldSort + ' ' + @strFSortType+ ') AS TBMinID)'
+ ' order by ' + @fldSort + ' ' + @strFSortType
end
end
else
begin
set @page = @pageIndex- @page+ 1 -- 后半部分数据处理
if @page <= 1 -- 最后一页数据显示
set @strTmp= @SqlSelect+ ' * from (' + @SqlSelect+ ' top ' + CAST ( @lastcount as VARCHAR ( 40))+ ' ' + @fldName+ ' from ' + @tblName
+ ' order by ' + @fldSort + ' ' + @strSortType+ ') AS TempTB' + ' order by ' + @fldSort + ' ' + @strFSortType
else
begin
set @strTmp= @SqlSelect+ ' * from (' + @SqlSelect+ ' top ' + CAST ( @pageSize as VARCHAR ( 40))+ ' ' + @fldName+ ' from ' + @tblName
+ ' where ' + @ID
if @Sort= 0
set @strTmp= @strTmp+ ' <(select min('
else
set @strTmp= @strTmp+ ' >(select max('
set @strTmp= @strTmp+ right( @ID, len ( @ID)- charindex ( '.' , @ID)) + ') from(' + @SqlSelect+ ' top ' + CAST ( @pageSize*( @page- 2)+ @lastcount as Varchar ( 20)) + ' ' + @ID + ' from ' + @tblName
+ ' order by ' + @fldSort + ' ' + @strSortType+ ') AS TBMaxID)'
+ ' order by ' + @fldSort + ' ' + @strSortType+ ') AS TempTB' + ' order by ' + @fldSort + ' ' + @strFSortType
end
end
end
else -- 有查询条件
begin
if @pageIndex< 2 or @page<= @pageIndex / 2 + @pageIndex % 2 -- 前半部分数据处理
begin
if @page= 1
set @strTmp= @SqlSelect+ ' top ' + CAST ( @pageSize as VARCHAR ( 40))+ ' ' + @fldName+ ' from ' + @tblName
+ ' where 1=1 ' + @strCondition + ' order by ' + @fldSort + ' ' + @strFSortType
else
begin
set @strTmp= @SqlSelect+ ' top ' + CAST ( @pageSize as VARCHAR ( 40))+ ' ' + @fldName+ ' from ' + @tblName
+ ' where ' + @ID
if @Sort= 0
set @strTmp = @strTmp + '>(select max('
else
set @strTmp = @strTmp + '<(select min('
set @strTmp = @strTmp + right( @ID, len ( @ID)- charindex ( '.' , @ID)) + ') from (' + @SqlSelect+ ' top ' + CAST ( @pageSize*( @page- 1) as Varchar ( 20)) + ' ' + @ID + ' from ' + @tblName
+ ' where (1=1) ' + @strCondition + ' order by ' + @fldSort + ' ' + @strFSortType+ ') AS TBMinID)'
+ ' ' + @strCondition + ' order by ' + @fldSort + ' ' + @strFSortType
end
end
else
begin
set @page = @pageIndex- @page+ 1 -- 后半部分数据处理
if @page <= 1 -- 最后一页数据显示
set @strTmp= @SqlSelect+ ' * from (' + @SqlSelect+ ' top ' + CAST ( @lastcount as VARCHAR ( 40))+ ' ' + @fldName+ ' from ' + @tblName
+ ' where (1=1) ' + @strCondition + ' order by ' + @fldSort + ' ' + @strSortType+ ') AS TempTB' + ' order by ' + @fldSort + ' ' + @strFSortType
else
begin
set @strTmp= @SqlSelect+ ' * from (' + @SqlSelect+ ' top ' + CAST ( @pageSize as VARCHAR ( 40))+ ' ' + @fldName+ ' from ' + @tblName
+ ' where ' + @ID
if @Sort= 0
set @strTmp = @strTmp + '<(select min('
else
set @strTmp = @strTmp + '>(select max('
set @strTmp = @strTmp + right( @ID, len ( @ID)- charindex ( '.' , @ID)) + ') from(' + @SqlSelect+ ' top ' + CAST ( @pageSize*( @page- 2)+ @lastcount as Varchar ( 20)) + ' ' + @ID + ' from ' + @tblName
+ ' where (1=1) ' + @strCondition + ' order by ' + @fldSort + ' ' + @strSortType+ ') AS TBMaxID)'
+ ' ' + @strCondition+ ' order by ' + @fldSort + ' ' + @strSortType+ ') AS TempTB' + ' order by ' + @fldSort + ' ' + @strFSortType
end
end
end
------ 返回查询结果 -----
SET NOCOUNT off
print @strTmp
exec sp_executesql @strTmp
Go