mysql中的调度程序事件使用if条件

时间:2015-10-10 03:56:01

标签: mysql mysql-error-1064 mysql-event

我写了以下查询

  CREATE EVENT test_event_03
  ON SCHEDULE EVERY 1 MINUTE
  STARTS CURRENT_TIMESTAMP
  ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
  DO
  DECLARE cnt1 AS BIGINT
  SELECT COUNT(*) AS cnt1, d.Invoice_Date AS Invoice_Date1 FROM Depot_Sales__c AS d,  Advance_Payment_Request__c A, Supplier_Payment__c S WHERE d.Supplier_Code=A.Supplier AND d.Supplier_Code=S.Supplier AND S.Supplier=80
   IF cnt1=0 THEN
   SELECT COUNT(*) FROM Depot_Sales__c AS d 
   END IF;

我收到以下错误

 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 cnt1 as bigint
  SELECT COUNT(*) as cnt1, d.Invoice_Date as Invoice_Date1 ' at line 8

  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  'IF cnt1=0 THEN' at line 10

为什么我收到此错误?

1 个答案:

答案 0 :(得分:0)

问题是你只能在BEGIN..END块中使用DECLARE语句,并且只能在该块的开头使用。请参阅http://dev.mysql.com/doc/refman/5.0/en/declare.html上的MySQL文档。 您需要将do语句包装在BEGIN ... END块中。而且我认为您还需要更改分隔符,以便您可以执行多个语句。 所以,它最终将成为:

  delimiter |
  CREATE EVENT test_event_03
  ON SCHEDULE EVERY 1 MINUTE
  STARTS CURRENT_TIMESTAMP
  ENDS CURRENT_TIMESTAMP + INTERVAL 1 HOUR
  DO
  BEGIN
  DECLARE cnt1 AS BIGINT;
  SELECT COUNT(*) AS cnt1, d.Invoice_Date AS Invoice_Date1 FROM Depot_Sales__c AS d,  Advance_Payment_Request__c A, Supplier_Payment__c S WHERE d.Supplier_Code=A.Supplier AND d.Supplier_Code=S.Supplier AND S.Supplier=80;
   IF cnt1=0 THEN
   SELECT COUNT(*) FROM Depot_Sales__c AS d; 
   END IF;
 END |
 delimiter ;