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 回答