Skip to content

Instantly share code, notes, and snippets.

@jahentao
Created June 6, 2017 04:29
Show Gist options
  • Save jahentao/391cdba2814ed38d46d1773fe01d4b95 to your computer and use it in GitHub Desktop.
Save jahentao/391cdba2814ed38d46d1773fe01d4b95 to your computer and use it in GitHub Desktop.
存储过程Demo
delimiter //
CREATE PROCEDURE curdemo()
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE a CHAR(16);
DECLARE b, c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM test.t1;
DECLARE cur2 CURSOR FOR SELECT i FROM test.t2;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN cur1;
OPEN cur2;
read_loop: LOOP
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF done THEN
LEAVE read_loop;
END IF;
IF b < c THEN
INSERT INTO test.t3 VALUES (a,b);
ELSE
INSERT INTO test.t3 VALUES (a,c);
END IF;
END LOOP;
CLOSE cur1;
CLOSE cur2;
END//
delimiter ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment