在同一块中声明的重复处理程序块

时间:2016-05-11 09:01:30

标签: mysql

您好我正在编写一个mysql程序,我一次使用2个游标,在实现这个时,我面临着在同一个块问题中定义的重复处理程序。我无法调试和纠正它。请帮帮我。

DELIMITER $$
CREATE PROCEDURE chainReaction 
(
    in p_qry_stmt int,OUT procstatus varchar(500)
) 
BEGIN 
declare v_Count int(11);
    declare final_id  int DEFAULT 0;
        declare final_id2  int DEFAULT 0;
     DECLARE cust_id12 INT ;
       DECLARE aff_cust_id12 INT  ;
     DECLARE done int DEFAULT 0;
   declare v_col1 int;

  DECLARE cur2 CURSOR FOR select cust_id from tmp_Cust_Algmnt_Cust_Aff_Child  ;

  DECLARE cur1 CURSOR FOR select cust_id,aff_cust_id
from    (select * from t_aff_load
         order by cust_id,aff_cust_id) products_sorted,
        (select @pv := v_col1) initialisation
where   find_in_set(cust_id, @pv) > 0
and     @pv := concat(@pv, ',', aff_cust_id);

   /* declare continue handler for sqlstate '02000' set done = 1;*/

    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; 



/*
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;*/


DECLARE CONTINUE HANDLER FOR NOT FOUND SET @isNOTFOUND := 0 ;



    DECLARE EXIT HANDLER FOR SQLEXCEPTION
   BEGIN
      GET DIAGNOSTICS CONDITION 1 @sqlstate = RETURNED_SQLSTATE, @errno = MYSQL_ERRNO, @text = MESSAGE_TEXT;
      SET @full_error = CONCAT("ERROR ", @errno, " (", @sqlstate, "): ", @text);
      set @isTrue = @full_error;
        insert into t_log(str_sstring) values(concat("chainReaction Execution Time: ", now() ," Exception :", @isTrue));
    SET procstatus:= @isTrue;
   END;




  DROP TABLE IF EXISTS tmp_Cust_Algmnt_Cust_Aff_Child;
  CREATE TEMPORARY TABLE tmp_Cust_Algmnt_Cust_Aff_Child (
   SeqNo bigint(20) NOT NULL PRIMARY KEY Auto_Increment ,
   cust_id int(11) NOT NULL,
   cust_algmnt_id bigint(20) NOT NULL
  );


 SET @isNOTFOUND := 1;

  SET @strQryStmt := CONCAT("INSERT INTO tmp_Cust_Algmnt_Cust_Aff_Child( cust_id, cust_algmnt_id )  ", p_qry_stmt);
  PREPARE QRYSTMT  FROM @strQryStmt;
  EXECUTE QRYSTMT;




    truncate table results_acc;
    truncate table results_prof;


OPEN cur2;

 repeat 
         fetch cur2           into  v_col1;
        /*   if no_more_rows1 then
               close cursor1;       
        leave LOOP1;
           end if;*/
  if not done then
  OPEN cur1;
   block2: BEGIN
               declare v_col2 int;
   DECLARE CONTINUE HANDLER FOR NOT FOUND SET doneLangLat = 1;

   repeat 
    FETCH cur1 INTO cust_id12,aff_cust_id12;
   if not doneLangLat then
    /* SET final_id = (select count(*) from t_cust where cust_id=cust_id12 and category_id=1);*/

  SET final_id2 = (select count(*) from t_cust where cust_id=aff_cust_id12 and category_id=1);
 if(final_id2>0) then
      insert into results_acc (cust_id1,aff_cust_id1)values(cust_id12,aff_cust_id12);  

      ELSE
       insert into results_prof (cust_id1,aff_cust_id1)values(cust_id12,aff_cust_id12);  

      end if;
      end if;
    until doneLangLat end repeat;
    END block2;
    close cur1;  
 end if;
    until done end repeat;
    close cur2;   

    DROP TABLE tmp_Cust_Algmnt_Cust_Aff_Child;

END$$
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

您正在复制处理程序块。要解决此问题,您需要删除一个游标处理程序声明。

请查看手册Mysql Cursor

  

游标声明必须出现在处理程序声明之前和之后   变量和条件声明。