mysql游标循环选择

时间:2012-09-30 02:13:44

标签: mysql loops cursor handler fetch

我正在写一个程序,它将通过一个表并收集所有前10个结果。我正在使用游标然后获取结果。我有点不知道如何打印出结果。我使用select语句来获得这些结果,但我被告知存在错误。

任何帮助将不胜感激。谢谢。

create procedure plant_list()
begin
declare v_plant_id integer(5);
declare v_common_name varchar(30);
declare v_scientific_name varchar(20);


declare finished boolean default false;

declare cur_top_ten cursor for
    select P.plant_id, common_name, concat(genus, ' ', species)
    from plants P
        join plant_taxonomy PT on P.plant_id = PT.plant_id
    order by list_price desc
    limit 10
    ;

declare continue handler for not found set finished = true;

open cur_top_ten;
curloop: loop
    fetch cur_top_ten into v_plant_id, v_common_name, v_scientific_name;
    if finished then
        close cur_top_ten;
        set finished = false;
        leave curloop;
    end if

    select v_plant_id, v_common_name, v_scientific_name;


end loop curloop;

end;
#

1 个答案:

答案 0 :(得分:1)

结束后有一个缺少的分号!

  if finished then
    close cur_top_ten;
    set finished = false;
    leave curloop;
  end if;