0

以下のクエリを書きました

  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

このエラーが発生するのはなぜですか?

4

1 に答える 1

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 ;
于 2015-10-12T00:11:47.463 に答える