mysql 游标fetch 不到数据问题,求助

xiexie1357 2017-04-28 09:58:43
这是 建表语句、插入两行数据
drop table if exists testinfo;
create table testinfo
(
name varchar(10) not null,
age int
);
truncate table testinfo;
insert into testinfo() values('you',15);
insert into testinfo() values('me',20);

查询的表数据
mysql> select name,age from testinfo;
+------+------+
| name | age |
+------+------+
| you | 15 |
| me | 20 |
+------+------+
这是使用游标的存储过程

drop procedure if exists test_proc;
delimiter //
create procedure test_proc()
begin
DECLARE done int default false;
DECLARE a char(10);
DECLARE b int;
DECLARE cur1 cursor for select name,age from testinfo ;
DECLARE continue handler for SQLSTATE '02000' set done = true;

open cur1;

read_loop: loop
FETCh cur1 into a,b;
if done then
leave read_loop;
end if;
select @a,@b;
insert into testinfo values(@a,@b);
end loop;

close cur1;
end
//
delimiter ;
call test_proc();


执行结果如下
mysql> \. /opt/mysql/test_proc.sql
Query OK, 0 rows affected, 1 warning (0.00 sec)

Query OK, 0 rows affected (0.02 sec)

+------+------+
| @a | @b |
+------+------+
| NULL | NULL |
+------+------+
1 row in set (0.00 sec)

ERROR 1048 (23000): Column 'name' cannot be null

不知道错在哪里,为什么取不到数据?
堵了两天了
...全文
548 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiexie1357 2017-04-28
  • 打赏
  • 举报
回复
谢谢,确实是这个问题,我疏忽了
c_sdn_shang_bu_qi 2017-04-28
  • 打赏
  • 举报
回复
insert into testinfo values(@a,@b);改成insert into testinfo values(a,b);就好了 你声明了变量a,b,然后通过游标给他赋值,但是并没有给@a,@b赋值。。

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧