storedproc_02.inc
来自「这个文件是windows mysql源码」· INC 代码 · 共 1,656 行 · 第 1/4 页
INC
1,656 行
DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1; DECLARE continue HANDLER FOR SQLSTATE '20000' SELECT '20000' AS 'SQLSTATE'; DECLARE continue HANDLER FOR SQLSTATE '21000' SELECT '21000' AS 'SQLSTATE'; DECLARE continue HANDLER FOR SQLSTATE '24000' SELECT '24000' AS 'SQLSTATE'; SET @x = 1; CASE cv WHEN 2 THEN SET @x = 2; WHEN 3 THEN SET @x = 3; END case; SET @x = 4; SELECT f1, f2 FROM t2 UNION SELECT f1, f2,3 FROM t2; SET @x = 5; FETCH cur1 INTO f1_value; SET @x = 6;END//# 2 - SQLEXCEPTION matches 2 of 3 conditions - CONTINUECREATE PROCEDURE sp2()BEGIN DECLARE f1_value CHAR(20); DECLARE cv INT DEFAULT 0; DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1; DECLARE continue HANDLER FOR SQLEXCEPTION SELECT 'SQLEXCEPTION' AS 'SQLSTATE'; DECLARE continue HANDLER FOR SQLSTATE '24000' SELECT '24000' AS 'SQLSTATE'; SET @x = 1; CASE cv WHEN 2 THEN SET @x = 2; WHEN 3 THEN SET @x = 3; END case; SET @x = 4; SELECT f1, f2 FROM t2 UNION SELECT f1, f2,3 FROM t2; SET @x = 5; FETCH cur1 INTO f1_value; SET @x = 6;END//# 3 - SQLSTATEs - EXITCREATE PROCEDURE sp3()BEGIN DECLARE f1_value CHAR(20); DECLARE cv INT DEFAULT 0; DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1; DECLARE EXIT HANDLER FOR SQLSTATE '20000' SELECT '20000' AS 'SQLSTATE'; DECLARE EXIT HANDLER FOR SQLSTATE '21000' SELECT '21000' AS 'SQLSTATE'; DECLARE EXIT HANDLER FOR SQLSTATE '24000' SELECT '24000' AS 'SQLSTATE'; SET @x = 1; CASE cv WHEN 2 THEN SET @x = 2; WHEN 3 THEN SET @x = 3; END case; SET @x = 4; SELECT f1, f2 FROM t2 UNION SELECT f1, f2,3 FROM t2; SET @x = 5; FETCH cur1 INTO f1_value; SET @x = 6;END//# 4 - SQLEXCEPTION matches 2 of 3 conditions - EXITCREATE PROCEDURE sp4()BEGIN DECLARE f1_value CHAR(20); DECLARE cv INT DEFAULT 0; DECLARE cur1 CURSOR FOR SELECT f1 FROM t2 LIMIT 1; DECLARE EXIT HANDLER FOR SQLEXCEPTION SELECT 'SQLEXCEPTION' AS 'SQLSTATE'; DECLARE EXIT HANDLER FOR SQLSTATE '24000' SELECT '24000' AS 'SQLSTATE'; SET @x = 1; CASE cv WHEN 2 THEN SET @x = 2; WHEN 3 THEN SET @x = 3; END case; SET @x = 4; SELECT f1, f2 FROM t2 UNION SELECT f1, f2,3 FROM t2; SET @x = 5; FETCH cur1 INTO f1_value; SET @x = 6; CLOSE cur1;END//delimiter ;//CALL sp0();SELECT '-0-', @x;CALL sp1();SELECT '-1-', @x;CALL sp2();SELECT '-2-', @x;CALL sp3();SELECT '-3-', @x;CALL sp4();SELECT '-4-', @x;# cleanup 3.1.2.58DROP PROCEDURE sp0;DROP PROCEDURE sp1;DROP PROCEDURE sp2;DROP PROCEDURE sp3;DROP PROCEDURE sp4;# ------------------------------------------------------------------------------let $message= Testcase 3.1.2.65:;--source include/show_msg.inclet $message=Ensure that FETCH <cursor name> returns the first row of the cursor_s result setthe first time FETCH is executed, that it returns each subsequent row of thecursor_s result set each of the subsequent times FETCH is executed, and that itreturns a NOT FOUND warning if it is executed after the last row of the cursor_sresult set has already been fetched.;--source include/show_msg80.inc--disable_warningsDROP PROCEDURE IF EXISTS sp1;DROP TABLE IF EXISTS temp;--enable_warningsCREATE TABLE temp( cnt INT, f1 CHAR(20), f2 CHAR(20), f3 INT, f4 CHAR(20), f5 INT);INSERT INTO temp VALUES(0, 'onip', 'abc', 8760, 'xyz', 10);# NOT used: declare continue handler for sqlstate '02000' set proceed=0;# --> warning is shown when procedure is executed.delimiter //;CREATE PROCEDURE sp1( )BEGIN declare proceed int default 1; declare count integer default 1; declare f1_value char(20); declare f2_value char(20); declare f5_value char(20); declare f4_value integer; declare f6_value integer; declare cur1 cursor for SELECT f1, f2, f4, f5, f6 from t2 where f4 >=-5000 order by f4 limit 3; open cur1; while proceed do SELECT count AS 'loop'; fetch cur1 into f1_value, f2_value, f4_value, f5_value, f6_value; insert into temp values (count, f1_value, f2_value, f4_value, f5_value, f6_value); set count = count + 1; END while;END//delimiter ;//--error ER_SP_FETCH_NO_DATACALL sp1();SELECT * FROM temp;# cleanup 3.1.2.65DROP TABLE temp;DROP PROCEDURE sp1;# ------------------------------------------------------------------------------let $message= Testcase 3.1.2.68:;--source include/show_msg.inclet $message=Ensure that FETCH <cursor name> fails with an appropriate error message if thenumber of columns to be fetched does not match the number of variables specifiedby the FETCH statement.;--source include/show_msg80.inc--disable_warningsDROP PROCEDURE IF EXISTS sp1;DROP PROCEDURE IF EXISTS sp2;--enable_warningsdelimiter //;--echo --> not enough columns in FETCH statementCREATE PROCEDURE sp1( )BEGIN declare newf1 char(20); declare cur1 cursor for SELECT f1, f2 from t2 limit 10; declare continue handler for sqlstate '02000' SELECT 'HANDLER executed.' AS ''; BEGIN open cur1; fetch cur1 into newf1; SELECT newf1; close cur1; END;END//--echo --> too many columns in FETCH statementCREATE PROCEDURE sp2( )BEGIN declare newf1 char(20); declare newf2 char(20); declare cur1 cursor for SELECT f1 from t2 limit 10; declare continue handler for sqlstate '02000' SELECT 'HANDLER executed.' AS ''; BEGIN open cur1; fetch cur1 into newf1, newf2; SELECT newf1, newf2; close cur1; END;END//delimiter ;//--echo --> not enough columns in FETCH statement--error ER_SP_WRONG_NO_OF_FETCH_ARGSCALL sp1();--echo --> too many columns in FETCH statement--error ER_SP_WRONG_NO_OF_FETCH_ARGSCALL sp2();# cleanup 3.1.2.68DROP PROCEDURE sp1;DROP PROCEDURE sp2;# ------------------------------------------------------------------------------let $message= Testcase 3.1.2.75:;--source include/show_msg.inclet $message=Ensure that, for nested compound statements, a cursor that was declared andopened during an outer level of the statement is not closed when an inner levelof a compound statement ends.;--source include/show_msg80.inc--disable_warningsDROP TABLE IF EXISTS temp1;DROP PROCEDURE IF EXISTS sp1;--enable_warningscreate table temp1( f0 char(20), f1 char(20), f2 char(20), f3 int, f4 char(20) );# Error: SQLSTATE: 02000 (ER_SP_FETCH_NO_DATA)# Message: No data to FETCHSELECT f1, f2, f4, f5 from t2 order by f4;delimiter //;CREATE PROCEDURE sp1( )BEGIN declare count integer; declare from0 char(20); declare newf1 char(20); declare newf2 char(20); declare newf5 char(20); declare newf4 integer; declare cur1 cursor for SELECT f1, f2, f4, f5 from t2 where f4 >= -5000 order by f4 limit 5; declare cur2 cursor for SELECT f1, f2, f4, f5 from t2 where f4 >= -5000 order by f4 limit 5; open cur1; open cur2; BEGIN declare continue handler for sqlstate '02000' set count = 1; fetch cur1 into newf1, newf2, newf4, newf5; SELECT '-1-', count, newf1, newf2, newf4, newf5; insert into temp1 values ('cur1_out', newf1, newf2, newf4, newf5); set count = 4; BEGIN while count > 0 do fetch cur1 into newf1, newf2, newf4, newf5; SELECT '-2-', count, newf1, newf2, newf4, newf5; set count = count - 1; END while; SELECT '-3-', count, newf1, newf2, newf4, newf4; END; BEGIN fetch cur1 into newf1, newf2, newf4, newf5; SELECT '-4-', newf1, newf2, newf4, newf5; insert into temp1 values ('cur1_in', newf1, newf2, newf4, newf5); END; fetch cur2 into newf1, newf2, newf4, newf5; SELECT '-5-', newf1, newf2, newf4, newf5; insert into temp1 values ('cur2', newf1, newf2, newf4, newf5); close cur1; END; fetch cur2 into newf1, newf2, newf4, newf5; SELECT '-6-', newf1, newf2, newf4, newf5; close cur2;END//delimiter ;//CALL sp1();SELECT * from temp1;# cleanup 3.1.2.75DROP PROCEDURE sp1;drop table temp1;# ------------------------------------------------------------------------------let $message= Testcase 3.1.2.76:;--source include/show_msg.inclet $message=Ensure that all cursors operate asensitively, so that there is no concurrencyconflict between cursors operating on the same, or similar, sets of resultsduring execution of one or more stored procedures.;--source include/show_msg80.inc--disable_warningsDROP PROCEDURE IF EXISTS sp1;drop table IF EXISTS temp1;drop table IF EXISTS temp2;--enable_warningscreate table temp1( f0 char(10), cnt int, f1 char(20), f2 char(20), f3 date, f4 integer );create table temp2( f0 char(10), cnt int, f1 char(20), f2 char(20), f3 date, f4 integer );delimiter //;CREATE PROCEDURE sp_inner( )BEGIN declare proceed int default 1; declare i_count integer default 20; declare i_newf1 char(20); declare i_newf2 char(20); declare i_newf3 date; declare i_newf4 integer; declare i_newf11 char(20); declare i_newf12 char(20); declare i_newf13 date; declare i_newf14 integer; declare cur1 cursor for SELECT f1, f2, f3, f4 from t2 where f4>=-5000 order by f4 limit 4; declare cur2 cursor for SELECT f1, f2, f3, f4 from t2 where f4>=-5000 order by f4 limit 3; declare continue handler for sqlstate '02000' set proceed=0; open cur1; open cur2; set i_count = 10; while proceed do fetch cur1 into i_newf1, i_newf2, i_newf3, i_newf4; IF proceed THEN insert into temp1 values ('sp_inner', i_count, i_newf1, i_newf2, i_newf3, i_newf4); fetch cur2 into i_newf11, i_newf12, i_newf13, i_newf14; IF proceed THEN insert into temp2 values ('sp_inner', i_count, i_newf11, i_newf12, i_newf13, i_newf14); END IF; END IF; set i_count = i_count - 1; END while; close cur1; close cur2;END//CREATE PROCEDURE sp_outer( )BEGIN DECLARE proceed INT DEFAULT 1; DECLARE o_count INTEGER DEFAULT 20; DECLARE o_newf1 CHAR(20); DECLARE o_newf2 CHAR(20); DECLARE o_newf3 DATE; DECLARE o_newf4 INTEGER; DECLARE o_newf11 CHAR(20); DECLARE o_newf12 CHAR(20); DECLARE o_newf13 DATE; DECLARE o_newf14 INTEGER; DECLARE cur1 CURSOR FOR SELECT f1, f2, f3, f4 FROM t2 WHERE f4>=-5000 ORDER BY f4 LIMIT 5; DECLARE cur2 CURSOR FOR SELECT f1, f2, f3, f4 FROM t2 WHERE f4>=-5000 ORDER BY f4 LIMIT 5; DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET proceed=0; OPEN cur1; OPEN cur2; SET o_count = 1; WHILE proceed DO FETCH cur1 INTO o_newf1, o_newf2, o_newf3, o_newf4; IF proceed THEN INSERT INTO temp1 VALUES ('_sp_out_', o_count, o_newf1, o_newf2, o_newf3, o_newf4); CALL sp_inner(); FETCH cur2 INTO o_newf11, o_newf12, o_newf13, o_newf14; IF proceed THEN INSERT INTO temp2 VALUES ('_sp_out_', o_count, o_newf11, o_newf12, o_newf13, o_newf14); END IF; END IF; SET o_count = o_count + 1; END WHILE; CLOSE cur1; CLOSE cur2;END//delimiter ;//CALL sp_outer();SELECT * FROM temp1;SELECT * FROM temp2;# cleanup 3.1.2.75DROP PROCEDURE sp_outer;DROP PROCEDURE sp_inner;DROP TABLE temp1;DROP TABLE temp2;# ==============================================================================# USE the same .inc to cleanup before and after the test--source suite/funcs_1/storedproc/cleanup_sp_tb.inc# ==============================================================================--echo--echo . +++ END OF SCRIPT +++--echo --------------------------------------------------------------------------------# ==============================================================================
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?