MySQL遊標多迴圈一次的解決方法
在MySQL中使用遊標的時候,發現遊標多迴圈一次
想在表中插入一條語句,但是實際上卻插入了兩條
語句如下:
mysql> create table test(id int(5));
Query OK, 0 rows affected (0.13 sec)
mysql> delimiter $$
mysql> create procedure proc_drop_table()
-> BEGIN
-> /* Declare Variables */
-> DECLARE done_1 INT DEFAULT FALSE;
-> DECLARE v_history_table_name varchar(64);
-> declare the_query VARCHAR(500);
->
-> /* Declare Conditions */
-> DECLARE not_found CONDITION FOR 1741;
->
-> /* Declare Cursors */
-> DECLARE cur1 CURSOR FOR select CONCAT(TABLE_SCHEMA,'.',TABLE_NAME) from information_schema.tables where TABLE_SCHEMA='test' and TABLE_NAME like '%test%';
->
-> /* Declare Exception Handlers, usually with set actions */
-> /* usually with set actions, the following handler has two forms,
/*> one with begin .. end statements, and the other without */
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET done_1 = TRUE;
->
-> OPEN cur1;
-> read_loop_1: LOOP
-> FETCH cur1 INTO v_history_table_name;
-> /*IF done_1 IS FALSE THEN*/
-> set @v_history_table_name=v_history_table_name;
-> select concat('insert into ', @v_history_table_name, ' values (10)') into the_query;
-> SET @stmt=the_query;
-> PREPARE STMT FROM @stmt;
-> EXECUTE STMT;
-> DEALLOCATE PREPARE STMT;
-> /*END IF;*/
-> IF done_1 THEN
-> LEAVE read_loop_1;
-> END IF;
-> END LOOP read_loop_1;
-> CLOSE cur1;
-> end$$
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> select * from test;
Empty set (0.00 sec)
mysql> call proc_drop_table();
Query OK, 0 rows affected (0.01 sec)
mysql> select * from test;
+------+
| id |
+------+
| 10 |
| 10 |
+------+
2 rows in set (0.00 sec)
解決方法:
在實際執行的語句兩邊增加IF判斷
mysql> truncate table test;
Query OK, 0 rows affected (0.07 sec)
mysql> drop procedure proc_drop_table;
Query OK, 0 rows affected (0.09 sec)
mysql> delimiter $$
mysql> create procedure proc_drop_table()
-> BEGIN
-> /* Declare Variables */
-> DECLARE done_1 INT DEFAULT FALSE;
-> DECLARE v_history_table_name varchar(64);
-> declare the_query VARCHAR(500);
->
-> /* Declare Conditions */
-> DECLARE not_found CONDITION FOR 1741;
->
-> /* Declare Cursors */
-> DECLARE cur1 CURSOR FOR select CONCAT(TABLE_SCHEMA,'.',TABLE_NAME) from information_schema.tables where TABLE_SCHEMA='test' and TABLE_NAME like '%test%';
->
-> /* Declare Exception Handlers, usually with set actions */
-> /* usually with set actions, the following handler has two forms,
/*> one with begin .. end statements, and the other without */
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET done_1 = TRUE;
->
-> OPEN cur1;
-> read_loop_1: LOOP
-> FETCH cur1 INTO v_history_table_name;
-> IF done_1 IS FALSE THEN
-> set @v_history_table_name=v_history_table_name;
-> select concat('insert into ', @v_history_table_name, ' values (10)') into the_query;
-> SET @stmt=the_query;
-> PREPARE STMT FROM @stmt;
-> EXECUTE STMT;
-> DEALLOCATE PREPARE STMT;
-> END IF;
-> IF done_1 THEN
-> LEAVE read_loop_1;
-> END IF;
-> END LOOP read_loop_1;
-> CLOSE cur1;
-> end$$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call proc_drop_table();
Query OK, 0 rows affected (0.01 sec)
mysql> select * from test;
+------+
| id |
+------+
| 10 |
+------+
1 row in set (0.00 sec)
想在表中插入一條語句,但是實際上卻插入了兩條
語句如下:
mysql> create table test(id int(5));
Query OK, 0 rows affected (0.13 sec)
mysql> delimiter $$
mysql> create procedure proc_drop_table()
-> BEGIN
-> /* Declare Variables */
-> DECLARE done_1 INT DEFAULT FALSE;
-> DECLARE v_history_table_name varchar(64);
-> declare the_query VARCHAR(500);
->
-> /* Declare Conditions */
-> DECLARE not_found CONDITION FOR 1741;
->
-> /* Declare Cursors */
-> DECLARE cur1 CURSOR FOR select CONCAT(TABLE_SCHEMA,'.',TABLE_NAME) from information_schema.tables where TABLE_SCHEMA='test' and TABLE_NAME like '%test%';
->
-> /* Declare Exception Handlers, usually with set actions */
-> /* usually with set actions, the following handler has two forms,
/*> one with begin .. end statements, and the other without */
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET done_1 = TRUE;
->
-> OPEN cur1;
-> read_loop_1: LOOP
-> FETCH cur1 INTO v_history_table_name;
-> /*IF done_1 IS FALSE THEN*/
-> set @v_history_table_name=v_history_table_name;
-> select concat('insert into ', @v_history_table_name, ' values (10)') into the_query;
-> SET @stmt=the_query;
-> PREPARE STMT FROM @stmt;
-> EXECUTE STMT;
-> DEALLOCATE PREPARE STMT;
-> /*END IF;*/
-> IF done_1 THEN
-> LEAVE read_loop_1;
-> END IF;
-> END LOOP read_loop_1;
-> CLOSE cur1;
-> end$$
Query OK, 0 rows affected (0.01 sec)
mysql> delimiter ;
mysql> select * from test;
Empty set (0.00 sec)
mysql> call proc_drop_table();
Query OK, 0 rows affected (0.01 sec)
mysql> select * from test;
+------+
| id |
+------+
| 10 |
| 10 |
+------+
2 rows in set (0.00 sec)
解決方法:
在實際執行的語句兩邊增加IF判斷
mysql> truncate table test;
Query OK, 0 rows affected (0.07 sec)
mysql> drop procedure proc_drop_table;
Query OK, 0 rows affected (0.09 sec)
mysql> delimiter $$
mysql> create procedure proc_drop_table()
-> BEGIN
-> /* Declare Variables */
-> DECLARE done_1 INT DEFAULT FALSE;
-> DECLARE v_history_table_name varchar(64);
-> declare the_query VARCHAR(500);
->
-> /* Declare Conditions */
-> DECLARE not_found CONDITION FOR 1741;
->
-> /* Declare Cursors */
-> DECLARE cur1 CURSOR FOR select CONCAT(TABLE_SCHEMA,'.',TABLE_NAME) from information_schema.tables where TABLE_SCHEMA='test' and TABLE_NAME like '%test%';
->
-> /* Declare Exception Handlers, usually with set actions */
-> /* usually with set actions, the following handler has two forms,
/*> one with begin .. end statements, and the other without */
-> DECLARE CONTINUE HANDLER FOR NOT FOUND SET done_1 = TRUE;
->
-> OPEN cur1;
-> read_loop_1: LOOP
-> FETCH cur1 INTO v_history_table_name;
-> IF done_1 IS FALSE THEN
-> set @v_history_table_name=v_history_table_name;
-> select concat('insert into ', @v_history_table_name, ' values (10)') into the_query;
-> SET @stmt=the_query;
-> PREPARE STMT FROM @stmt;
-> EXECUTE STMT;
-> DEALLOCATE PREPARE STMT;
-> END IF;
-> IF done_1 THEN
-> LEAVE read_loop_1;
-> END IF;
-> END LOOP read_loop_1;
-> CLOSE cur1;
-> end$$
Query OK, 0 rows affected (0.00 sec)
mysql> delimiter ;
mysql> call proc_drop_table();
Query OK, 0 rows affected (0.01 sec)
mysql> select * from test;
+------+
| id |
+------+
| 10 |
+------+
1 row in set (0.00 sec)
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2134241/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 【MySql】MySql儲存,遊標,迴圈的簡單使用MySql
- 【PL/SQL】遊標提取迴圈SQL
- oracle cursor遊標迴圈比較遊標元素是否相同Oracle
- Oracle顯示遊標的使用及遊標for迴圈Oracle
- for 迴圈境實現遊標LOOP提取OOP
- 解決迴圈引用
- Josephus問題解決方法三(單向迴圈連結串列標識法)
- Josephus問題解決方法四(迴圈陣列)陣列
- pl/sql中三種遊標迴圈效率對比SQL
- 使用遊標迴圈進行SQL更新插入的SQL語句SQL
- Python 判斷for迴圈最後一次的方法Python
- spring解決迴圈依賴Spring
- Block迴圈引用的三種解決方式BloC
- 一次線上死迴圈的排查
- Mysql儲存過程中使用多遊標MySql儲存過程
- MySQL迴圈語句MySql
- mysql 中 while 迴圈的用法。MySqlWhile
- 解決rpm包迴圈依賴
- Swift解決【閉包引起的迴圈強引用】Swift
- 重置win10系統後卡在引導迴圈中的解決方法Win10
- 探討兩種迴圈表示方法的區別,while迴圈與for迴圈的小總結While
- JavaScript的map迴圈、forEach迴圈、filter迴圈、reduce迴圈、reduceRight迴圈JavaScriptFilter
- NSTimer迴圈引用的幾種解決方案
- 如何在 iOS 中解決迴圈引用的問題iOS
- 多執行緒迴圈控制欄位失效造成死迴圈的坑執行緒
- SpringBoot 迴圈引用解決辦法Spring Boot
- Sqlserver使用遊標迴圈查詢所有sqlserver error日誌帶有Exception的報錯SQLServerErrorException
- 一步步學MongoDB之使用迴圈來遍歷遊標MongoDB
- 解決ora-20005的遊標
- mysql 的procedure 中 loop迴圈的用法。MySqlOOP
- Mysql系列第十八講 遊標詳解MySql
- 解決NSTimer迴圈引用導致記憶體洩漏的六種方法記憶體
- 【Spring】Spring中的迴圈依賴及解決Spring
- iOS | 用於解決迴圈引用的block timeriOSBloC
- spring 迴圈注入:is there an unresolvable circular reference的解決方案Spring
- Swift中的迴圈強引用 【使用無主引用解決】Swift
- sybase中cursor的使用中死迴圈問題解決
- 事件迴圈詳解事件