Mysql游标详解

版权声明:如若转载,请联系作者。 https://blog.csdn.net/liu16659/article/details/83311337

Mysql游标详解

1.简单示例

  • 建表
CREATE TABLE `student` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) DEFAULT NULL,
  `address` varchar(20) DEFAULT NULL,
  `score` varchar(10) DEFAULT NULL,
  `rank` varchar(10) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8
  • 导数
INSERT INTO `insidemysql`.`student` (`id`, `name`, `address`, `score`, `rank`) VALUES ('1', 'lawson', 'anhui_jinzhai', '98.2', '1%'),
('2', 'ting', 'anhui_suzhou', NULL, NULL),
('3', 'lee', 'anhui_hefei', NULL, NULL),
('4', 'tao', 'anhui_sanyuan', NULL, NULL);
  • 写cursor
drop procedure if exists getResultFromTest;
delimiter //
 
create procedure getResultFromTest(out result varchar(128))  
  begin
     -- 存储过程所使用变量需申明
     declare id int(11);
     declare name varchar(20);
     -- 遍历数据结束标志 注意位置顺序
     DECLARE done INT DEFAULT FALSE;
     -- 注意用别名 因为id在上面已经有定义所以需要使用表的别名区别
     declare cur_test CURSOR for select t.id, t.name from student t;
     -- 将结束标志绑定到游标 
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
     
	 -- 打开游标
	 open cur_test; 
          repeat
             fetch cur_test into id, name;	   
			 select id,name;
	     -- concat_ws函数用逗号后面的参数隔开 concat_ws(',','12','33','332') --> 12,33,332
             select concat_ws(',',id,name) into result ;			 
          until done
	  end repeat;
-- 注意关闭游标
      close cur_test;         
 end;
 //
 delimiter ;
call getResultFromTest(@out);

执行结果如下:

mysql> call getResultFromTest(@out);
+------+--------+
| id   | name   |
+------+--------+
|    1 | lawson |
+------+--------+
1 row in set (0.00 sec)

+------+------+
| id   | name |
+------+------+
|    2 | ting |
+------+------+
1 row in set (0.00 sec)

+------+------+
| id   | name |
+------+------+
|    3 | lee  |
+------+------+
1 row in set (0.00 sec)

+------+------+
| id   | name |
+------+------+
|    4 | tao  |
+------+------+
1 row in set (0.00 sec)

+------+------+
| id   | name |
+------+------+
|    4 | tao  |
+------+------+
1 row in set (0.00 sec)

稍有不解的是,这里为啥会多出一条记录出来?
同时,我将上述的sql修改成如下样子时【修改declare cur_test CURSOR for select t.id, t.name from student t;declare cur_test CURSOR for select id, name from student ;】,就出现了执行失败的情况:

drop procedure if exists getResultFromTest;
-- delimiter表示以//结束编译
delimiter //
 
create procedure getResultFromTest(out result varchar(128))  
  begin
     -- 存储过程所使用变量需申明
     declare id int(11);
     declare name varchar(20);
     -- 遍历数据结束标志 注意位置顺序
     DECLARE done INT DEFAULT FALSE;
     -- 注意用别名 因为id在上面已经有定义所以需要使用表的别名区别
     declare cur_test CURSOR for select id, name from student ;
     -- 将结束标志绑定到游标 
     DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
     
	 -- 打开游标
	 open cur_test; 
          repeat
             fetch cur_test into id, name;	   
			 select id,name;
	     -- concat_ws函数用逗号后面的参数隔开 concat_ws(',','12','33','332') --> 12,33,332
             select concat_ws(',',id,name) into result ;			 
          until done
	  end repeat;
-- 注意关闭游标
      close cur_test;        
 end;
 //
 delimiter ;
call getResultFromTest(@out);

为什么执行失败,我更不理解了。

猜你喜欢

转载自blog.csdn.net/liu16659/article/details/83311337
今日推荐