存储过程出错(错误代码:1064)

时间:2013-05-07 07:21:08

标签: mysql sql stored-procedures cursor

   My stored procedure is like this ...



 DELIMITER $$

DROP PROCEDURE IF EXISTS `tds_dev`.`BlockTokenSheduler`$$

CREATE PROCEDURE `BlockTokenSheduler`(cdate date,shift varchar(20))
BEGIN
        declare lo_SERIALNO int;
        declare lo_TOKENNUMBER int;
        declare lo_ARRIVALTIME time;
        declare lo_ADJUSTMENTTIME time;
        declare lo_APPOINTMENTTIME time;
        declare bt_ADJUSTMENTTIME time;
        declare bt_NEXTAPPOINTMENTTIME time;
        declare lo_CONSULTATIONTYPE varchar(20);
        declare lo_NEXTAPPOINTMENTTIME time;
        declare lo_CONSULTATIONSTATUS varchar(20);
        declare lo_ACTUALFINISHEDTIME time;
        declare lo_SMSSTATUS varchar(20);
        declare temp_appTime time;
        declare time_diff time;
        declare done int;
         declare btdone int;
        declare btcount int;
        declare co int;
          Declare btcountcur Cursor for
           select ADJUSTMENTTIME,NEXTAPPOINTMENTTIME from tds_tokengeneration where TOKENDATE =cdate and SHIFTID = shift and blockstatus='BT'  ORDER BY APPOINTMENTTIME ;  

            declare continue handler for not found set btdone=1;


          open btcountcur;


          bt_loop :LOOP 

               if btdone=1 then 
                     leave bt_loop;
                  end if;

              FETCH btcountcur into bt_ADJUSTMENTTIME,bt_NEXTAPPOINTMENTTIME;

                  Declare mycur cursor for
                 select TOKENNUMBER,APPOINTMENTTIME,ADJUSTMENTTIME,CONSULTATIONTYPE,NEXTAPPOINTMENTTIME,CONSULTATIONSTATUS,SMSSTATUS from tds_tokengeneration
                 where TOKENDATE=cdate and SHIFTID=shift and blockstatus='MT';
                           declare continue handler for not found set done=1; 
                  open mycur;
                  time_loop :LOOP 
                  FETCH mycur into lo_TOKENNUMBER,lo_APPOINTMENTTIME,lo_ADJUSTMENTTIME,
                               lo_CONSULTATIONTYPE,lo_NEXTAPPOINTMENTTIME,lo_CONSULTATIONSTATUS,lo_SMSSTATUS;
                  if done=1 then 
                     leave time_loop;
                  end if;

              if (lo_ADJUSTMENTTIME >= bt_ADJUSTMENTTIME and lo_APPOINTMENTTIME <= bt_NEXTAPPOINTMENTTIME) or  (lo_NEXTAPPOINTMENTTIME >= bt_ADJUSTMENTTIME and lo_NEXTAPPOINTMENTTIME <= bt_NEXTAPPOINTMENTTIME)then

                 set lo_ADJUSTMENTTIME=bt_NEXTAPPOINTMENTTIME;
                 if lo_CONSULTATIONTYPE='C' then
                         set lo_NEXTAPPOINTMENTTIME = ADDTIME(lo_ADJUSTMENTTIME,'00:12:00');
                     else
                         set lo_NEXTAPPOINTMENTTIME = ADDTIME(lo_ADJUSTMENTTIME,'00:20:00'); 
                     end if;
                 update tds_tokengeneration set  ADJUSTMENTTIME=lo_ADJUSTMENTTIME,
                            NEXTAPPOINTMENTTIME=lo_NEXTAPPOINTMENTTIME,
                            SMSSTATUS=lo_SMSSTATUS
                          where TOKENNUMBER=lo_TOKENNUMBER and TOKENDATE=cdate and SERIALNO=lo_SERIALNO;
               end if;
             end loop time_loop;
           close mycur; 

      end loop bt_loop;
    close btcountcur; 

    END$$

DELIMITER ;

。但是当我执行这个程序时,我遇到了错误

    (0 row(s)affected)
(0 ms taken)

Error Code : 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Declare mycur cursor for
                 select TOKENNUMBER,APPOINTMENTTIME,ADJ' at line 33
(0 ms taken)

1 个答案:

答案 0 :(得分:2)

Declarations must follow a certain order。 不允许在过程中声明游标或事件处理程序。是的,错误信息至少可以说是误导。您必须在mycur块的开头声明BEGIN ... END光标。

您可以将第二个游标声明移动到过程的开头,或将nest a BEGIN ... END block移动到适当的位置。