批改一个分页的存储过程
修改一个分页的存储过程
因为虚拟空间使用的sql 2000 不支持row_number函数,在此麻烦大牛帮我修改一下。多谢多谢!!
------解决方案--------------------
try this,
-- ----------------------------
-- Procedure structure for [dbo].[PF_Sys_PageControl]
-- ----------------------------
DROP PROCEDURE [dbo].[PF_Sys_PageControl]
GO
create PROCEDURE [dbo].[PF_Sys_PageControl]
@tableName varchar(500), -- 表名
@tableFields varchar(5000) = '*', -- 字段名(全部字段为*)
@sqlWhere varchar(5000) = NULL, -- 条件语句(不加 where,可带 group by 分组条件,分组条件需要带 group by 关键字)
@orderFields varchar(5000), -- 排序字段(必须,支持多字段,不加 order by)
@pageSize int, -- 页大小(每页多少条记录)
@pageIndex int = 1 , -- 指定当前为第几页
@totalPage int output -- 返回总页数
AS
BEGIN
-- Begin Tran -- 开始事务
Declare @sql nvarchar(4000);
Declare @totalRecord int;
-- 计算总记录数
if (@sqlWhere = '' or @sqlWhere = NULL)
set @sql = 'select @totalRecord = count(0) from ' + @tableName
else
BEGIN
if(CHARINDEX('group by', LOWER(@sqlWhere)) > 0)
set @sql = 'select @totalRecord = count(0) from (select ' + @tableFields + ' from ' + @tableName + ' where ' + @sqlWhere + ') as Tab_GroupTable'
else
set @sql = 'select @totalRecord = count(0) from ' + @tableName + ' where ' + @sqlWhere
END
--print @Sql
EXEC sp_executesql @sql,N'@totalRecord int OUTPUT',@totalRecord OUTPUT
-- 计算总页数
select @totalPage=CEILING((@totalRecord+0.0)/@pageSize)
-- 处理页数超出范围情况
if @pageIndex<=0
Set @pageIndex = 1
if @pageIndex>@totalPage
Set @pageIndex = @totalPage
select @totalPage=@totalRecord
-- 处理开始点和结束点
Declare @startRecord int
Declare @endRecord int
set @startRecord = (@pageIndex-1)*@pageSize + 1
set @endRecord = @startRecord + @pageSize - 1
-- 合成sql语句
if (@sqlWhere = '' or @sqlWhere = NULL)
set @sql = 'Select * FROM (select ' + @tableFields + ',ROW_NUMBER() Over(order by ' + @orderFields + ') as rowId from ' + @tableName
else
set @sql = 'Select * FROM (select ' + @tableFields + ',ROW_NUMBER() Over(order by ' + @orderFields + ') as rowId from ' + @tableName + ' where ' + @sqlWhere
set @Sql = @Sql + ') as Tab_TotalTable where rowId between ' + Convert(varchar(50),@startRecord) + ' and ' + Convert(varchar(50),@endRecord)
Exec(@Sql)
-- if @@Error <> 0
-- BEGIN
-- RollBack Tran
-- Return -1
-- END
-- else
-- BEGIN
-- Commit Tran
-- Return @totalRecord --- 返回记录总数
-- END
END
因为虚拟空间使用的sql 2000 不支持row_number函数,在此麻烦大牛帮我修改一下。多谢多谢!!
------解决方案--------------------
try this,
alter procedure dbo.PF_Sys_PageControl
@tableName varchar(500), -- 表名
@tableFields varchar(5000) = '*', -- 字段名(全部字段为*)
@sqlWhere varchar(5000) = NULL, -- 条件语句(不加 where,可带 group by 分组条件,分组条件需要带 group by 关键字)
@orderFields varchar(5000), -- 排序字段(必须,支持多字段,不加 order by)
@pageSize int, -- 页大小(每页多少条记录)
@pageIndex int = 1 , -- 指定当前为第几页
@totalPage int output -- 返回总页数
AS
BEGIN
Declare @sql nvarchar(4000);
Declare @totalRecord int;
-- 计算总记录数
if (@sqlWhere = '' or @sqlWhere = NULL)
set @sql = 'select @totalRecord = count(0) from ' + @tableName