DB2通用分页存储过程
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)
{
StringBuilder sql = new StringBuilder();//
sql.AppendFormat(" II.INS_ID =IO.INS_OPID AND IO.INS_OPUNITID = '{0}'", unitId);// AND IO.INS_OPRANGE = 0
if (!string.IsNullOrEmpty(statue))
{
sql.AppendFormat(" AND II.INS_STATE in ({0}) ", statue);
}
if (m_insOperate != null)
{
//制度名称
if (!string.IsNullOrEmpty(m_insOperate.INS_NAME))
{
sql.AppendFormat(" AND II.INS_NAME like '%{0}%'", m_insOperate.INS_NAME);
}
//制度类型
if (!string.IsNullOrEmpty(m_insOperate.INS_TYPEID))
{
sql.AppendFormat(" AND II.INS_TYPEID = '{0}'", m_insOperate.INS_TYPEID);
}
//制度状态
if (m_insOperate.INS_STATE != CommonStatus.GetProStatus(CommonStatus.ProjectStatus.Select))
{
sql.AppendFormat(" AND II.INS_STATE = {0}", m_insOperate.INS_STATE);
}
//编辑部门
if (!string.IsNullOrEmpty(m_insOperate.INS_OPDEPID.ToString()))
{
sql.AppendFormat(" AND IO.INS_OPDEPID = {0}", m_insOperate.INS_OPDEPID);
}
//编辑人
if (!string.IsNullOrEmpty(