1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
DROP TABLE IF EXISTS t1,t2,t3,t4;
DROP FUNCTION IF EXISTS sf1;
CREATE TABLE t1 (id INTEGER NULL , data INTEGER NULL, KEY(id));
CREATE TABLE t2 (i INTEGER NULL);
CREATE TABLE t3 (id INTEGER NULL, data INTEGER NULL, KEY(id));
CREATE TABLE t4 (i INTEGER NULL);
INSERT INTO t1 VALUES(1,1),(2,1),(3,4),(4,5);
INSERT INTO t2 VALUES(1),(2),(3);
CREATE FUNCTION sf1() RETURNS INTEGER
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE a CHAR(16);
DECLARE b,c INT;
DECLARE cur1 CURSOR FOR SELECT id,data FROM t1;
DECLARE cur2 CURSOR FOR SELECT i FROM t2;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET done = 1;
OPEN cur1;
OPEN cur2;
REPEAT
FETCH cur1 INTO a, b;
FETCH cur2 INTO c;
IF NOT done THEN
IF b < c THEN
INSERT INTO t3 VALUES (a,b);
ELSE
INSERT INTO t3 VALUES (a,c);
END IF;
END IF;
UNTIL done END REPEAT;
CLOSE cur1;
CLOSE cur2;
RETURN 0;
END//
SELECT sf1();
sf1()
0
Warnings:
Error 1329 No data - zero rows fetched, selected, or processed
SELECT * FROM t1 ORDER BY id;
id data
1 1
2 1
3 4
4 5
SELECT * FROM t2 ORDER BY i;
i
1
2
3
SELECT * FROM t3 ORDER BY id;
id data
1 1
2 1
3 3
INSERT INTO t4 VALUES(sf1());
Warnings:
Error 1329 No data - zero rows fetched, selected, or processed
SELECT * FROM t1 ORDER BY id;
id data
1 1
2 1
3 4
4 5
SELECT * FROM t2 ORDER BY i;
i
1
2
3
SELECT * FROM t3 ORDER BY id;
id data
1 1
1 1
2 1
2 1
3 3
3 3
UPDATE t4 SET i = sf1() + 1 WHERE i = sf1();
Warnings:
Error 1329 No data - zero rows fetched, selected, or processed
Error 1329 No data - zero rows fetched, selected, or processed
SELECT * FROM t1 ORDER BY id;
id data
1 1
2 1
3 4
4 5
SELECT * FROM t2 ORDER BY i;
i
1
2
3
SELECT * FROM t3 ORDER BY id;
id data
1 1
1 1
1 1
1 1
2 1
2 1
2 1
2 1
3 3
3 3
3 3
3 3
DELETE FROM t4 WHERE i = sf1() + 1;
Warnings:
Error 1329 No data - zero rows fetched, selected, or processed
SELECT * FROM t1 ORDER BY id;
id data
1 1
2 1
3 4
4 5
SELECT * FROM t2 ORDER BY i;
i
1
2
3
SELECT * FROM t3 ORDER BY id;
id data
1 1
1 1
1 1
1 1
1 1
2 1
2 1
2 1
2 1
2 1
3 3
3 3
3 3
3 3
3 3
DROP FUNCTION sf1;
DROP TABLE t1,t2,t3,t4;
|