本文将为您介绍一个DB2存储过程使用动态游标的例子,如果您对动态游标的使用感兴趣的话,不妨一看,对您学习DB2的使用会有所帮助。
CREATE PROCEDURE data_wtptest( IN in_taskid_timestamp varchar(30),
OUT o_err_no int,
OUT o_err_msg varchar(1024))
LANGUAGE SQL
P1: BEGIN ATOMIC
--声明开始
--临时变量出错变量
DECLARE SQLCODE integer default 0;
DECLARE SQLStmt varchar(1024) default '';
DECLARE r_code integer default 0;
DECLARE state varchar(1024) default 'AAA';--记录程序当前所作工作
DECLARE at_end int DEFAULT 0;
DECLARE t_destnetid int default 0;
DECLARE t_recvid varchar(30) default '';
DECLARE SP_Name varchar(50) default 'data_wtptest';
--声明放游标的值
--声明动态游标存储变量
DECLARE stmt1 STATEMENT;
DECLARE c1 CURSOR FOR stmt1;
--声明出错处理
DECLARE EXIT HANDLER FOR SQLEXCEPTION #p#
begin
set r_code=SQLCODE;
set o_err_no=1;
set o_err_msg='处理['||state||']出错 ,'||'错误代码SQLCODE:['||CHAR(r_code) || '].';
insert into fcc_sp_log(object,name,value) values(SP_Name,in_taskid_timestamp,o_err_msg);
end;
DECLARE continue HANDLER for not found
begin
set at_end = 1;
set state='找到0行记录或已经到记录结尾.';
end;
--声明结束
SET state='[add]单独测试中,统计条数';
SET SQLStmt='SELECT count(*) FROM wtp_pre_download where task_timestamp = ?';
PREPARE stmt1 FROM SQLStmt;
OPEN c1 USING in_taskid_timestamp;
FETCH c1 INTO t_destnetid;
CLOSE c1;
SET state='[add]查具体信息';
SET at_end = 0;
SET SQLStmt='SELECT recv_userid FROM wtp_pre_download where task_timestamp = ?';
PREPARE c1 FROM SQLStmt;
OPEN c1 USING in_taskid_timestamp;
SET state='[add]************';
FETCH c1 INTO t_recvid;
SET state='[add]============';
insert into fcc_sp_log(object,name,value) values(SP_Name,in_taskid_timestamp,t_recvid);
END p1