乐之殇 2022-12-22 16:08 采纳率: 100%
浏览 22
已结题

MySQL的游标使用出错,可以帮我看看吗

我在学习数据库 游标 的时候做了一个procedure:输入一个数值,然后输出以这个数为边界的两个表(比如输入60,就是查询及格和不及格的人)

/*
First, create a table containing the names, ages, genders, score and student numbers of students.
*/

create table student_info
    (ID varchar(12), name varchar(24), age tinyint, gender tinyint(1), score int);

/*
Next insert some data.
*/

insert student_info values(01, 'chenjiale', 22, 1, 98);
insert student_info values(02, 'zhangshan', 24, 0, 54);
insert student_info values(03, 'lishi', 18, 1, 100);
insert student_info values(04, 'wangwu', 34, 0, 69);
insert student_info values(05, 'zhaoliu', 26, 1, 12);
insert student_info values(06, 'chenqi', 15, 1, 96);
insert student_info values(07, 'liuba', 64, 0, 59);
insert student_info values(08, 'qiujiu', 74, 0, 49);

/*
create a procedure, the function is to input a score and display two parts of information 
with the score as the boundary.
*/ 
delimiter //
create procedure get_two_parts(in in_score int)
begin 
/*
建立游标,保存<in_score 和 >in_score 的ID 集合
*/
    declare save_id varchar(12);
    declare sub_cur cursor
        for select ID from student_info where score<in_score;
    declare meta_cur cursor
        for select ID from student_info where score>=in_score;
/*
创建两张表 用作显示结果
*/
    create table sub_table like student_info;
    create table meta_table like student_info;
/*
打开游标并进行表的插入操作
*/
    open sub_cur;
    while !done do
    fetch sub_cur  into save_id;
        insert into sub_table select * from student_info where ID=save_id;
    end while;

    open meta_cur;
    while !done do
    fetch meta_cur  into save_id;
        insert into meta_table select * from student_info where ID=save_id;
    end while;
/*
完事之后收拾手尾
*/
    close meta_cur;
    close sub_cur;
    select * from sub_table;
    select * from meta_table;
    drop table sub_table;
    drop table meta_table;
    end //

delimiter ;
call get_two_parts(60);

然后报了这个错误

ERROR 1054 (42S22): Unknown column 'done' in 'field list'

我也试过在insert 之前先判断

if save_id != done then
  insert into meta_table select * from student_info where ID=save_id;
end if;

还是不行

  • 写回答

1条回答 默认 最新

  • wux_labs 2022-12-22 16:15
    关注

    while 条件中的done未定义。
    你需要在存储过程开头定义declare done。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 12月30日
  • 已采纳回答 12月22日
  • 创建了问题 12月22日

悬赏问题

  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了