Mysql光标未运行嵌套循环
Posted
技术标签:
【中文标题】Mysql光标未运行嵌套循环【英文标题】:Mysql Cursor Not running nested loop 【发布时间】:2015-04-19 19:31:30 【问题描述】:我创建了两个游标循环遍历外循环中的每个值并遍历整个内循环。所以我创建了以下嵌套游标。但是当我的一个表有一行数据时它可以工作。然后我在第一个表中插入更多数据但游标没有正常工作
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
我在游标中使用的两个表的结构是
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;
两个表的一些数据是
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', '');
我正在做的是,如果在学生事务表中 stran_id 是否有效,如果是,则将相应的 bank_trans 的matched_sub_id 设置为 stran_id(现在只看它是否工作)。我是 pl 的初学者/sql 编程
【问题讨论】:
【参考方案1】: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 子句。它现在正在工作,检查它。
【讨论】:
以上是关于Mysql光标未运行嵌套循环的主要内容,如果未能解决你的问题,请参考以下文章