DB2通用分页存储过程

页面导航:首页 > 数据库 > DB2 > DB2通用分页存储过程

DB2通用分页存储过程

来源: 作者: 时间:2016-01-23 11:15 【

sql代码CREATE PROCEDURE P_GETPAGEDATANEW(p_TableName varchar(500),--表名称p_NickName varchar(200),--表昵称p_IDName varchar(200),--表主键名称p_PageIndex integer,--当前页数p_PageSize intege...
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(m_insOperate.INS_OPUSERID))
                {
                    sql.AppendFormat(" AND IO.INS_OPUSERID IN  ( select USERID from USERINFO  where USERNAME  like '%{0}%' )", m_insOperate.INS_OPUSERID);
                }
                //是否审核
                //if (!string.IsNullOrEmpty(m_insOperate.INS_ISAUDIT.ToString()))
                //{
                //    sql.AppendFormat(" AND IO.INS_ISAUDIT = '{0}'", m_insOperate.INS_ISAUDIT);
                /
Tags:

文章评论

最 近 更 新
热 点 排 行
Js与CSS工具
代码转换工具

<