DB2存储过程模版

1、日志表
 
create table joblog
(
jobfun varchar(50),      --存储过程的功能
procname varchar(100),   --存储过程
runtime varchar(50),     --运行时间
currentuser varchar(50), --当前用户
state varchar(4),        --状态
ChangeRecordsNum integer --受影响行数
)

state代表:
--Joblog stats =0  正在执行
--Joblog stats =1  存储过程正常结束
--Joblog stats =8  有其他过程正在执行,中断
--Joblog stats =9  存储过程异常终止
2、存储过程模板

create PROCEDURE YY_Proc01 (IN FromDate integer)
--Joblog stats =0  running
--Joblog stats =1  Finish right
--Joblog stats =8  Finish cause by another running
--Joblog stats =9  Stop with exception
LANGUAGE sql
YY_Proc:begin
    declare  JobFun varChar(50) default 'Delete W02 CINACC';--
    declare ChangeRecordsNum integer default 0 ;

    declare flag integer default 0;--考虑作为返回参数
    declare SQLSTATE char(5);
    declare at_end integer default 0;
    declare not_found condition for SQLSTATE '02000';

   
    declare MyCur cursor for < ..select..>;
    /**********异常处理************/
    declare exit HANDLER FOR SQLEXCEPTION
    begin
        rollback;
        insert into joblog values(JobFun,'YY_Proc01',(select current timestamp from sysibm.sysdummy1),
                         (select user from sysibm.sysdummy1),'9',0);--执行遇到异常
        set flag=1;
        return flag;
    end;

    declare CONTINUE HANDLER FOR  not_found
    begin
          set at_end=1;
    end;
    /***************************/
    
    
    set flag = 0;
    --开始工作 insert into logs
    insert into joblog values(JobFun,'YY_Proc01',(select current timestamp from sysibm.sysdummy1),
                         (select user from sysibm.sysdummy1),'0',0); 

    OPEN  MyCur;
    set at_end=0;
    FETCH MyCur INTO ans;
    WHILE at_end=0 DO
        --your work
        SET at_end = 0;
        --循环受影响行
        Set ChangeRecordsNum = ChangeRecordsNum+1;
        FETCH MyCur INTO ans;
    END WHILE;
    CLOSE MyCur;
    --顺利执行完毕
    insert into joblog values(JobFun,'YY_Proc01',(select current timestamp from sysibm.sysdummy1),
                         (select user from sysibm.sysdummy1),'1',ChangeRecordsNum);
        commit;
    set flag=0;
    return flag;
end YY_Proc

 

posted @ 2013-04-27 13:52  jackchain  阅读(350)  评论(0编辑  收藏  举报