这个是个很简单的存储过程,事实上我只是尝试使用一下游标。
可是事实上这个游标似乎并没有起作用BEGIN
declare myid int;
declare done int;
--定义游标
DECLARE rs_cursor CURSOR FOR select itemid from relation where userid=1;
--游标走到最后的标志
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
--打开游标
open rs_cursor; 
--开始循环
cursor_loop:loop
FETCH rs_cursor into myid;
--如果游标走到了最后,就跳出循环
if done=1 then
leave cursor_loop;
end if;
--打印出游标循环出来的每个itemid 
select myid;       
end loop cursor_loop;
close rs_cursor;
END
这里我遇到的问题是
我对应于userid=1的这个查询
select itemid from relation where userid=1;在数据库里面可以查出很多条itemid 
可是实际上我这个存储过程的select myid;只执行了一次,也就是只查询出一个itemid。
似乎这个游标只执行一次就退出循环了。
我的这个存储过程问题出在哪里呢?请高人指点一下
谢谢啦。

解决方案 »

  1.   

    declare myid int;
    declare done int;
    --定义游标
    DECLARE rs_cursor CURSOR FOR select itemid from relation where userid=1;
    --游标走到最后的标志
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
    --打开游标
    open rs_cursor;  
    --开始循环
    FETCH rs_cursor into myid;
    WHILE done=0 DO
    --打印出游标循环出来的每个itemid  
    select myid;   
    FETCH rs_cursor into myid;
    END WHILE;
    close rs_cursor;
      

  2.   

    楼上的做法不行
    我试了
    这次没有任何输出
    似乎那个while循环根本就没有执行过
      

  3.   

    BEGIN
    declare myid int;
    declare done int DEFAULT 0;
    --定义游标
    DECLARE rs_cursor CURSOR FOR select itemid from relation where userid=1;
    --游标走到最后的标志
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done=1;
    --打开游标
      

  4.   

    建议楼主多看看MYSQL的官方手册中的语法和例子。