在使用游标的过程中使用select score into sco from home_answer where user_id=10 and home_id=hid;获取某个字段的值,如果表中没有满足条件的记录,sco的值是NULL吗?如何判断?对游标有影响吗?
我自己试的时候发现如果查不到记录,循环就不做了。我希望如果查不到记录,给sco赋值0,不知如何实现,请各位高手帮忙!
具体代码:
declare done int default 0;
declare get_homes cursor for select home_id from homework where course_no=cno order by home_id;
declare continue handler for not found set done=1;open get_homes;
home_loop:loop
fetch get_homes into hid;
if done=1 then
leave home_loop;
end if;
select score into sco from home_answer where user_id=10 and home_id=hid;
select hid,sco;
end loop;
close get_homes;存储过程mysqlselect into游标

解决方案 »

  1.   

    加上这一句。因为select score into sco fro.... 会影响到 declare continue handler for not found set done=1;declare done int default 0;
    declare get_homes cursor for select home_id from homework where course_no=cno order by home_id;
    declare continue handler for not found set done=1;open get_homes;
    home_loop:loop
    fetch get_homes into hid;
    if done=1 then
    leave home_loop;
    end if;
    select score into sco from home_answer where user_id=10 and home_id=hid;
    set done=0;
    select hid,sco;
    end loop;
    close get_homes;
      

  2.   

    在使用游标的过程中使用select score into sco from home_answer where user_id=10 and home_id=hid;获取某个字段的值,如果表中没有满足条件的记录,sco的值是NULL吗?
    如果没有
    对sco赋初值,为NULL如何判断?
    set sco=ifnull(sco,0)
    or
    if ifnull(sco,0)=0 then 
    ....
    end if
    对游标有影响吗?
    有,在执行后,set done=0;