DROP PROCEDURE IF EXISTS metro.cursorTest;
CREATE PROCEDURE cursorTest (in parameter int)
BEGIN
  DECLARE part_id int;
  DECLARE part_template_id int;
  
  DECLARE cursorMark int default 0;
  
  DECLARE cursorTest CURSOR FOR select t.part_id from part t;
  DECLARE cursorTestTemplate CURSOR FOR select part_template_id from part_template;
  
  -- DECLARE cursorTempTest CURSOR FOR select * from temp11;
  -- DECLARE cursorTempTest1 CURSOR FOR select * from temp12;
  
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET cursorMark = 1;
  
  drop TABLE IF EXISTS cursorTestTable;
  CREATE TABLE cursorTestTable(
      part_id int
  );
  drop TABLE IF EXISTS cursorTemplateTable;
  CREATE TABLE cursorTemplateTable(
      part_template_id int
  );
  CREATE TEMPORARY TABLE temp11
   SELECT * FROM part_template T;    OPEN cursorTest;   
        tt:LOOP   
            FETCH cursorTest INTO part_id;   
            IF cursorMark = 1 THEN  
                LEAVE tt;      
            ELSE
              INSERT INTO cursorTestTable VALUES(PART_ID);
            END IF;
        END LOOP tt;
    CLOSE cursorTest;
    
    
   OPEN cursorTestTemplate;   
      pp:LOOP   
            FETCH cursorTestTemplate INTO part_template_id;   
            IF cursorMark = 1 THEN  
                LEAVE pp;      
            ELSE
              INSERT INTO cursorTemplateTable VALUES(part_template_id);
            END IF;
      END LOOP pp;
    CLOSE cursorTestTemplate;
   
    
    COMMIT;
    drop table temp11;
END;
其实是很简单的一个存储过程,不过遇到了些比较奇怪的问题: 1: cursorMark 应该可以用在两个游标循环里面的吧。 2:  这个程序执行完毕后,表cursorTestTable  中 有记录插入,表cursorTemplateTable 中没有记录插入,其实是应该有记录插入的。 
  如果把两个cursor 打开的顺序颠倒一下就会变成cursorTemplateTable 有记录,而cursorTestTable 中则没有插入记录。
 不知道是程序的原因,还是因为这样写就会有这样的结果。 而且还有一个奇怪的现象就是: 
                        有时候第一个cursor里面插入的表有时候插入的全是空值
                        比如说应该插入
                           1
                           2
                           3
                      可是实际插入的记录是三条,不过全部为空值。
不知道大伙有没有遇到过,会是什么原因呢?

解决方案 »

  1.   

                 INSERT INTO cursorTestTable VALUES(PART_ID);
               END IF;
           END LOOP tt;
       CLOSE cursorTest;
      
    SET cursorMark = 0;
      
      OPEN cursorTestTemplate;  
         pp:LOOP  
               FETCH cursorTestTemplate INTO part_template_id;  
               IF cursorMark = 1 THEN 
                   LEAVE pp;     
               ELSE
                 INSERT INTO cu
      

  2.   


    可是这个问题会是什么原因呢? 有时候cursor里面插入的表有时候插入的全是空值
      比如说应该插入
      1
      2
      3
      可是实际插入的记录是三条,不过全部为空值。
    不知道大伙有没有遇到过,会是什么原因呢?