【问题标题】:scheduler events in mysql using if conditionsmysql中使用if条件的调度程序事件
【发布时间】:2015-10-10 03:56:00
【问题描述】:

我已经写了下面的查询

  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

为什么会出现这个错误?

【问题讨论】:

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


    【解决方案1】:

    问题是您只能在 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 ;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-01-20
      • 2014-12-21
      • 1970-01-01
      • 2011-12-05
      • 2018-09-10
      • 2018-09-18
      • 2014-12-14
      • 2022-10-13
      相关资源
      最近更新 更多