DB2通用分页存储过程
来源:网络 责任编辑:栏目编辑 发表时间:2013-07-02 01:46 点击:次
sql代码
CREATE PROCEDURE "P_GETPAGEDATANEW"
(
p_TableName varchar(500),--表名称
p_NickName varchar(200),--表昵称
p_IDName varchar(200),--表主键名称
p_PageIndex integer,--当前页数
p_PageSize integer,--每页大小
p_Strwhere varchar(8000),
OUT p_count integer --总记录
)
LANGUAGE SQL
BEGIN
DECLARE startNumber Integer;--起始记录
DECLARE endNumber Integer;--终止记录
DECLARE PageLowerBound integer;
--DECLARE StartID integer;
DECLARE strSql VARCHAR(4000);
DECLARE mySQL VARCHAR(4000);
DECLARE myCursor CURSOR WITH RETURN TO CLIENT FOR mySQL;
IF p_PageIndex<1 THEN
SET p_PageIndex=1;
END IF;
SET PageLowerBound = p_PageSize * (p_PageIndex-1);
IF PageLowerBound<1 THEN
SET PageLowerBound=1;
end if;
--查询总记录
SET strSql='SELECT count(*) FROM (Select '||p_NickName||', rownumber() over(ORDER BY '||p_IDName|| ' ASC) AS rn from '||p_TableName|| ' WHERE '||p_Strwhere||' ) AS au ';
prepare mySQL from strSql;
open myCursor;
fetch myCursor into p_count;-- 总记录数
close myCursor;
--查询结果
SET startNumber=(p_PageIndex-1)*p_PageSize+1;
SET endNumber=startNumber+p_PageSize;
--SET strSql='SELECT * FROM (Select '||p_NickName||', rownumber() over(ORDER BY '||p_IDName|| ' ASC) AS rn from '||p_TableName|| ' WHERE '||p_Strwhere||' ) AS au WHERE au.rn BETWEEN ' ||char(startNumber)|| ' AND '||char(endNumber-1);
SET strSql='select * from (Select '||p_NickName||', rownumber() over(ORDER BY '||p_IDName|| ' ) as rownum from '||p_TableName|| ' WHERE '||p_Strwhere||') as temp where rownum BETWEEN ' ||char(startNumber)|| ' AND '||char(endNumber-1);
prepare mySQL from strSql;
open myCursor;
--set nocount off;
END
调用例子
/// <summary>
/// 制度建设分页
/// 2011-12-19
/// </summary>
/// <param name="statue">制度状态</param>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="departId">部门ID</param>
/// <param name="m_disOperate">制度建设操作实体</param>
/// <param name="result"></param>
public void GetInsAllPage(string statue, int pageIndex, int pageSize, out int count, string unitId, CUEntityINSOPerate m_insOperate, CResult result)<
CREATE PROCEDURE "P_GETPAGEDATANEW"
(
p_TableName varchar(500),--表名称
p_NickName varchar(200),--表昵称
p_IDName varchar(200),--表主键名称
p_PageIndex integer,--当前页数
p_PageSize integer,--每页大小
p_Strwhere varchar(8000),
OUT p_count integer --总记录
)
LANGUAGE SQL
BEGIN
DECLARE startNumber Integer;--起始记录
DECLARE endNumber Integer;--终止记录
DECLARE PageLowerBound integer;
--DECLARE StartID integer;
DECLARE strSql VARCHAR(4000);
DECLARE mySQL VARCHAR(4000);
DECLARE myCursor CURSOR WITH RETURN TO CLIENT FOR mySQL;
IF p_PageIndex<1 THEN
SET p_PageIndex=1;
END IF;
SET PageLowerBound = p_PageSize * (p_PageIndex-1);
IF PageLowerBound<1 THEN
SET PageLowerBound=1;
end if;
--查询总记录
SET strSql='SELECT count(*) FROM (Select '||p_NickName||', rownumber() over(ORDER BY '||p_IDName|| ' ASC) AS rn from '||p_TableName|| ' WHERE '||p_Strwhere||' ) AS au ';
prepare mySQL from strSql;
open myCursor;
fetch myCursor into p_count;-- 总记录数
close myCursor;
--查询结果
SET startNumber=(p_PageIndex-1)*p_PageSize+1;
SET endNumber=startNumber+p_PageSize;
--SET strSql='SELECT * FROM (Select '||p_NickName||', rownumber() over(ORDER BY '||p_IDName|| ' ASC) AS rn from '||p_TableName|| ' WHERE '||p_Strwhere||' ) AS au WHERE au.rn BETWEEN ' ||char(startNumber)|| ' AND '||char(endNumber-1);
SET strSql='select * from (Select '||p_NickName||', rownumber() over(ORDER BY '||p_IDName|| ' ) as rownum from '||p_TableName|| ' WHERE '||p_Strwhere||') as temp where rownum BETWEEN ' ||char(startNumber)|| ' AND '||char(endNumber-1);
prepare mySQL from strSql;
open myCursor;
--set nocount off;
END
调用例子
/// <summary>
/// 制度建设分页
/// 2011-12-19
/// </summary>
/// <param name="statue">制度状态</param>
/// <param name="pageIndex">当前页</param>
/// <param name="pageSize">页大小</param>
/// <param name="departId">部门ID</param>
/// <param name="m_disOperate">制度建设操作实体</param>
/// <param name="result"></param>
public void GetInsAllPage(string statue, int pageIndex, int pageSize, out int count, string unitId, CUEntityINSOPerate m_insOperate, CResult result)<
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>