【问题标题】:mysql cursor loop selectmysql游标循环选择
【发布时间】:2012-09-21 09:44:12
【问题描述】:

我正在编写一个程序,它将遍历一个表并收集所有前 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;
#

【问题讨论】:

    标签: mysql loops cursor handler fetch


    【解决方案1】:

    if! 后面缺少一个分号。

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

    【讨论】:

      猜你喜欢
      • 2013-07-25
      • 2013-07-12
      • 1970-01-01
      • 2014-01-02
      • 2018-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-20
      相关资源
      最近更新 更多