MYSQL中的动态游标

时间:2013-12-05 07:35:04

标签: mysql sql database cursor

 create procedure change_fid()

 begin
 declare t_flag integer default 0;
 declare t_fid varchar(5) default "";
 declare t_fname varchar(20) default "";
 declare t_source_code varchar(5) default "";
 declare t_destination_code varchar(5) default "";
 declare t_journey_hours decimal(2,0) default 0;

 declare change_fid_cursor cursor for
 select * from flight_temp;
 declare continue handler for not found set t_flag=1;

 begin
 drop table if exists flight_temp;
 create temporary table flight_temp (ta_fid varchar(5),ta_fname varchar(20),ta_source_code varchar(5),ta_destination_code varchar(5),ta_journey_hours decimal(2,0));
 select * into flight_temp from flight;
 update flight_temp set fid='FSA09' where fid='FSA08';
 open change_fid_cursor;
 looper : loop
 fetch change_fid_cursor into t_fid,t_fname,t_source_code,t_destination_code,t_journey_hours;
 if t_flag=1then leave looper;
 end if;
 select t_fid,t_fname,t_source_code,t_destination_code,t_journey_hours;
 end loop looper;
 close change_fid_cursor;
 end //

我得到未声明的变量flight_temp。我甚至创建了一个表flight_temp。我仍然得到同样的错误。 我在mysql中尝试动态游标。

  ERROR 1327 (42000): Undeclared variable: flight_temp
我在做错了什么。我是游标的新手,请指导我。感谢。

1 个答案:

答案 0 :(得分:0)

错误的原因是MySQL不支持SELECT INTO table语法。

但MySQL支持INTO variable子句。这就是为什么它将flight_temp视为变量名称。

您应该使用INSERT INTO ... SELECT FROM ...语法。

据说你看起来根本不需要光标。如果你能用简单的词语解释你想要实现的目标,那么你很可能也会得到帮助。