db2 通用分页存储过程CREATE PROCEDURE DB2ADMIN.PROC_GETPAGE
db2 通用分页存储过程 (INOUT CURRENTPAGE 
INTEGER
db2 通用分页存储过程  
IN STRSQL VARCHAR(500), 
db2 通用分页存储过程  
IN PAGESIZE INTEGER
db2 通用分页存储过程  OUT TOTALPAGE 
INTEGER
db2 通用分页存储过程  OUT TOTALRECORD 
INTEGER
db2 通用分页存储过程 ) 
db2 通用分页存储过程  SPECIFIC DB2ADMIN.SQL080201103307110
db2 通用分页存储过程  DYNAMIC RESULT SETS 
1
db2 通用分页存储过程  LANGUAGE SQL
db2 通用分页存储过程  
NOT DETERMINISTIC
db2 通用分页存储过程  CALLED 
ON NULL INPUT
db2 通用分页存储过程  READS SQL DATA
db2 通用分页存储过程  INHERIT SPECIAL REGISTERS
db2 通用分页存储过程  
BEGIN
db2 通用分页存储过程    
DECLARE tmpsql   VARCHAR(1000);   -- 主语句
db2 通用分页存储过程
    DECLARE orderFiled VARCHAR(50);
db2 通用分页存储过程    
DECLARE s  VARCHAR(1000);
db2 通用分页存储过程    
DECLARE v_Start  INTEGER;
db2 通用分页存储过程    
DECLARE v_End INTEGER;
db2 通用分页存储过程    
DECLARE result CURSOR WITH RETURN TO CALLER FOR S2;
db2 通用分页存储过程    
db2 通用分页存储过程    
SET orderFiled =getOrderField(strsql);
db2 通用分页存储过程   
db2 通用分页存储过程    
if(length(orderFiled )>0then
db2 通用分页存储过程         
set orderFiled =' order by ' || orderFiled ;
db2 通用分页存储过程    
end if;
db2 通用分页存储过程    
db2 通用分页存储过程
db2 通用分页存储过程
db2 通用分页存储过程    
SET s  =getSQL(strsql);
db2 通用分页存储过程    
set tmpsql = 'select count(*) from (' || strsql || ') as a';
db2 通用分页存储过程    
prepare s2 from tmpsql;
db2 通用分页存储过程    
open result;
db2 通用分页存储过程        
fetch result into totalrecord;-- 总记录数
db2 通用分页存储过程
    close result; 
db2 通用分页存储过程    
db2 通用分页存储过程    
if(pagesize = 0then
db2 通用分页存储过程        
set pagesize = 20;-- 每页显示数
db2 通用分页存储过程
    end if
db2 通用分页存储过程
db2 通用分页存储过程    
set totalPage = (totalrecord - 1/ pagesize + 1;-- 总页数 
db2 通用分页存储过程

db2 通用分页存储过程
db2 通用分页存储过程
db2 通用分页存储过程    
if(currentPage < 1then
db2 通用分页存储过程        
set currentPage = 1;-- 当前页
db2 通用分页存储过程
    else
db2 通用分页存储过程        
if(currentPage > totalPage) then
db2 通用分页存储过程            
set currentPage = totalPage;
db2 通用分页存储过程        
end if;
db2 通用分页存储过程    
end if
db2 通用分页存储过程
db2 通用分页存储过程    
set v_Start = (currentPage-1* pagesize ;
db2 通用分页存储过程    
set v_End = currentPage * pagesize;
db2 通用分页存储过程
db2 通用分页存储过程
db2 通用分页存储过程    
set  tmpsql ='select   *   from   ' ||
db2 通用分页存储过程           
'(select   rownumber()   over()   as   row, ' ||
db2 通用分页存储过程    
'w.*   from   ( select   *   from   (  ' || s ||    ') n  ' || orderFiled  || ') w)   w1   where   row between '  || char(v_Start ) ||  ' and  '  || char(v_End );
db2 通用分页存储过程    
db2 通用分页存储过程    
prepare s2 from tmpsql ;
db2 通用分页存储过程    
open result;
db2 通用分页存储过程    
db2 通用分页存储过程
END;

使用了两个java函数

import java.util.regex.*;

import COM.ibm.db2.app.UDF;

public class FunctionDB2 extends UDF
{
 public static String getOrderField(String sql)
 {
  Pattern Regex = Pattern.compile("select(.+)order\\s*by\\s*(.+)",
    Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
   Matcher RegexMatcher = Regex.matcher(sql);
   if (RegexMatcher.find()) {
    return RegexMatcher.group(2);
   }
   return "";
 }
 
 public static String getSQL(String sql)
 {
  Pattern Regex = Pattern.compile("(.+)order\\s*by\\s*(.+)",
    Pattern.CANON_EQ | Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
   Matcher RegexMatcher = Regex.matcher(sql);
   if (RegexMatcher.find()) {
    return RegexMatcher.group(1);
   }
   return sql;
 }
 
 
 
}


相关文章: