我创建了两个游标来循环遍历外部循环中的每个值并遍历整个内部循环。所以我创建了以下嵌套游标。但是当我的一个表有一行数据时它就可以工作。然后我在第一个表中插入更多数据但光标不能正常工作
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编程
答案 0 :(得分: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 .it正在运行的地方添加where子句,检查它。