Mysql 游标不是 运行 嵌套循环

Mysql Cursor Not running nested loop

我创建了两个游标,用于循环遍历外循环中的每个值并遍历整个内循环。所以我创建了以下嵌套游标。但是当我的一个 table 有一行数据时它可以工作。然后我首先插入更多数据 table 但是游标没有正常工作

 BEGIN
DECLARE done int default false;
DECLARE a,b varchar(20);
DECLARE c,d date;
DECLARE f,j text;
DECLARE cur2 CURSOR FOR SELECT tran_id,tran_date FROM bank_tran where tcheck='NO'; 
DECLARE cur1 CURSOR FOR SELECT stran_id,stran_date,scheck FROM student_tran; 
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; 
 OPEN cur1;
  read_loop: LOOP
    FETCH cur1 INTO a,c,j;
    IF done THEN
         LEAVE read_loop;
      END IF;
    OPEN cur2;
    inside_loop:LOOP
      FETCH cur2 INTO b,d;
      IF done THEN
         LEAVE inside_loop;
      END IF;
      IF (a = b) and (c=d) THEN
        update student_tran set valid='YES',scheck='YES' where stran_id=a;
        update bank_tran set matched_sub_id=a ,tcheck='YES' where tran_id=a;
       CLOSE cur2;
      LEAVE inside_loop;
      ELSE
         ITERATE inside_loop;
      END IF;
    END LOOP inside_loop;

    END LOOP read_loop ;
  CLOSE cur1;
  END

我在游标中使用的两个 table 的结构是

CREATE TABLE IF NOT EXISTS `bank_tran` (
  `tran_id` varchar(20) NOT NULL,
  `tran_date` date NOT NULL,
  `tcheck` text NOT NULL,
  `matched_sub_id` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


CREATE TABLE IF NOT EXISTS `student_tran` (
  `student_id` varchar(20) NOT NULL,
  `sub_apply_id` varchar(20) NOT NULL,
  `stran_id` varchar(20) NOT NULL,
  `stran_date` date NOT NULL,
  `scheck` text NOT NULL,
  `valid` text NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

两个 table 的一些数据是

    INSERT INTO `bank_tran` (`tran_id`, `tran_date`, `tcheck`, `matched_sub_id`) VALUES

    ('4HZZGl', '2015-02-03', 'NO', ''),
    ('LkvUG5', '2015-02-03', 'NO', ''),
    ('fKbl8H', '2015-02-03', 'NO', ''),
    ('fIS7kg', '2015-02-03', 'NO', ''),
    ('GQqBic', '2015-02-03', 'NO', ''),
    ('ZPU3Yx', '2015-02-03', 'NO', ''),
    ('gKSNz7', '2015-02-03', 'NO', ''),
    ('DeyvKU', '2015-02-03', 'NO', ''),
    ('pKarTd', '2015-02-03', 'NO', ''),
    ('nVbJkW', '2015-02-03', 'NO', ''),
    ('1EuzDN', '2015-02-03', 'NO', ''),
    ('QrIxQD', '2015-02-03', 'NO', ''),
    ('vHtJID', '2015-02-03', 'NO', ''),
    ('cQzbYy', '2015-02-03', 'NO', ''),
    ('MGhWy0', '2015-02-03', 'NO', ''),
    ('6DDsSH', '2015-02-03', 'NO', '');
INSERT INTO `student_tran` (`student_id`, `sub_apply_id`, `stran_id`, `stran_date`, `scheck`, `valid`) VALUES
('', '', '4HZZGl', '2015-02-03', 'NO', ''),
('', '', 'LkvUG5', '2015-02-03', 'NO', ''),
('', '', 'fKbl8H', '2015-02-03', 'NO', ''),
('', '', 'fIS7kg', '2015-02-03', 'NO', ''),
('', '', 'GQqBic', '2015-02-03', 'NO', ''),
('', '', 'ZPU3Yx', '2015-02-03', 'NO', ''),
('', '', 'gKSNz7', '2015-02-03', 'NO', ''),
('', '', 'DeyvKU', '2015-02-03', 'NO', ''),
('', '', 'pKarTd', '2015-02-03', 'NO', ''),
('', '', 'nVbJkW', '2015-02-03', 'NO', ''),
('', '', '1EuzDN', '2015-02-03', 'NO', ''),
('', '', 'QrIxQD', '2015-02-03', 'NO', ''),
('', '', 'vHtJID', '2015-02-03', 'NO', ''),
('', '', 'cQzbYy', '2015-02-03', 'NO', ''),
('', '', 'MGhWy0', '2015-02-03', 'NO', ''),
('', '', '6DDsSH', '2015-02-03', 'NO', '');

我正在做的是,如果在学生交易 table 中,stran_id 是否有效,如果是,则将相应的 bank_trans 的 matched_sub_id 设置为 stran_id(现在只是为了看看它是否有效)。我是 pl/sql 编程的初学者

BEGIN
DECLARE done int default false;
DECLARE a,b varchar(20);
DECLARE c,d date;
DECLARE f,j text;
DECLARE cur2 CURSOR FOR SELECT tran_id,tran_date FROM bank_tran where tcheck='NO';
DECLARE cur1 CURSOR FOR SELECT stran_id,stran_date,scheck FROM student_tran where scheck='NO'; 
 DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; 
 OPEN cur1;
  read_loop: LOOP
    FETCH cur1 INTO a,c,j;
    IF done THEN
         LEAVE read_loop;
    END IF;
    OPEN cur2;
    inside_loop:LOOP
      FETCH cur2 INTO b,d;
       IF done THEN
         LEAVE inside_loop;
       END IF;
      IF (a = b) and (c=d) THEN
        update student_tran set valid='YES',scheck='YES' where stran_id=a;
        update bank_tran set matched_sub_id=a ,tcheck='YES' where tran_id=a;
        CLOSE cur2;
      LEAVE inside_loop;
      ELSE
         ITERATE inside_loop;
      END IF;
    END LOOP inside_loop;
   END LOOP read_loop ;
  CLOSE cur1;
END

只需在 cur1 中添加 where 子句即可。现在可以使用了,检查一下。