声明mysql存储过程中的变量

时间:2016-03-17 09:40:18

标签: mysql stored-procedures

我们试图在mysql存储过程中声明一个变量,该变量在其中实现了事务。但它似乎给出了语法错误:

以下是存储过程的语法:

CREATE PROCEDURE `sp_MarkAppointmentRefferal`(
  p_AppId bigint,
  p_NewLocation bigint,
  p_userId bigint,
  p_ReferralReason varchar(500),
  p_NewLocationName varchar(100)
)
begin


declare v_OldLocation int default 0;
set v_OldLocation = (select LocationId FROM appointments where iAppID = p_AppId limit 1 );

 DECLARE EXIT HANDLER FOR SQLEXCEPTION 
    BEGIN
        ROLLBACK;
        select -1;
    END;  


START TRANSACTION;




    update table
set is_referred = 1,
 referred_timestamp = now(),
  referral_reason = p_ReferralReason
 where iAppID = p_AppId
 limit 1;

  -- create a new appointment for the new referred location..

  insert into appointments
  (vAppName, vAppType, dAppDate, vCell, iPatID, iAppStatus, iuserid, iActive, 
  dInsertDate, iHSID, daily_ticket_no, LocationId, visit_id, encounter_id, ReferredFrom,ReferredOPDName, opd_name )

    select vAppName, vAppType, now(), vCell, iPatID, iAppStatus, p_userId, 
    1, now(), iHSID, fn_GenerateNextAppointmentTicket(now(),p_NewLocation) , p_NewLocation, visit_id, encounter_id+1, 
    (select LocationId FROM appointments where iAppID = p_AppId limit 1),
    (select OPD_Name FROM appointments where iAppID = p_AppId limit 1), p_NewLocationName
    FROM appointments
    where iAppID = p_AppId limit 1;

    select LAST_INSERT_ID();

COMMIT;



end;

语法检查器说声明命令在这里无效。 还尝试将此放在事务子句中,并显示类似的错误..

任何帮助表示赞赏..

1 个答案:

答案 0 :(得分:1)

所有声明语句都应位于存储过程体的顶部。在SET语句之前移动DECLARE EXIT HANDLER应解决问题。