我想在数据库中查询有无给定条件的记录,如果有就返回FALSE如果无就返回TRUE,然后再用IF语句控制执行下面的程序。
create procedure st1(in xuehao varchar(10),in xingming varchar(50),in academyno smallint(2),in banji varchar(20),in email varchar(20),in passwd varchar(20),out flag bool)
begin
declare a varchar(10);
declare getstudentno cursor for select studentno from user where studentno=xuehao;
set flag=false;
open getstudentno;
fetch getstudentno into a;
if a!=null || a!=""  then
insert into user(studentno,name,academyno,usertype,class,email,passwd) values
(xuehao,xingming,academyno,banji,email,passwd);
set flag=true;
end if;
close getstudentno;
end我用这个试了一下 错误提示是说FETCH是空值
很痛苦
我想我问一下大家MYSQL存储过程中是否有判断游标内是否为空的函数啊
最好能给个例子谢谢

解决方案 »

  1.   

    不用使用游标的
    参考:
    DECLARE _item_in_used INT;
    SELECT COUNT(*) INTO _item_in_used FROM order_detail WHERE item_id = _item_id;/* do not update name,
    UI should protected changing name
    but we protected it again in the proc ...
    */
    IF _item_in_used >0 THEN
    UPDATE items SET cat_id = _cat_id, code = _code, price = _price WHERE item_id = _item_id;
    ELSE
    UPDATE items SET cat_id = _cat_id, code = _code, name = _name, price = _price WHERE item_id = _item_id;
    END IF;