MySQL 5.6.21存储过程SQL错误1064

时间:2017-07-15 06:58:50

标签: mysql sql database stored-procedures

我有这个查询工作正常并给出预期的结果。

SELECT count(*) AS total_unsolved,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) = 0 and response_time is null) as today_to_solve,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 0 and datediff(deadline, CURTIME()) < 4 and response_time is null) as days_left_1_3,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 3 and datediff(deadline, CURTIME()) < 8 and response_time is null) as days_left_4_7,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 7 and response_time is null) as mt_week,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < 0 and datediff(deadline, CURTIME()) > -4 and response_time is null) as overdue_1_3_days,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -3 and datediff(deadline, CURTIME()) > -11 and response_time is null) as overdue_4_10_days,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -10 and datediff(deadline, CURTIME()) > -30 and response_time is null) as overdue_11_30_days,
    (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -30 and response_time is null) as overdue_mt_month,
    (select count(deadline) from my_table where datediff(deadline, response_time) < 0 and response_time is not null) as solved_with_delay  
 from my_table where response_time is null 

enter image description here

我想创建一个显示给定时间范围内结果的过程。 所以我输入了以下内容:

CREATE PROCEDURE status_in_timerange
     (
        IN   start_date                     TiMESTAMP, 
        IN   close_date                     TiMESTAMP,
        OUT  total_unsolved                 INT,  
        OUT  today_to_solve                 INT, 
        OUT  days_left_1_3                  INT, 
        OUT  days_left_4_7                  INT, 
        OUT  mt_week                        INT, 
        OUT  overdue_1_3_days               INT, 
        OUT  overdue_4_10_days              INT,      
        OUT  overdue_11_30_days             INT,    
        OUT  overdue_mt_month               INT,    
        OUT  solved_with_delay              INT    
     )
BEGIN 

    SELECT count(DATEDIFF(deadline, NOW())) AS total_unsolved,
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) = 0 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 0 and datediff(deadline, CURTIME()) < 4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 3 and datediff(deadline, CURTIME()) < 8 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 7 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < 0 and datediff(deadline, CURTIME()) > -4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -3 and datediff(deadline, CURTIME()) > -11 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -10 and datediff(deadline, CURTIME()) > -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
        (select count(deadline) from my_table where datediff(deadline, response_time) < 0 and response_time is not null and ktimestamp BETWEEN start_date and close_date) 

    INTO   total_unsolved , 
           today_to_solve, 
           days_left_1_3, 
           days_left_4_7, 
           mt_week, 
           overdue_1_3_days ,
           overdue_4_10_days,
           overdue_11_30_days,
           overdue_mt_month,
           solved_with_delay

  FROM my_table WHERE response_time is null and ktimestamp BETWEEN start_date and close_date
END ;

不幸的是,此查询显示如下错误:

  

SQL错误(1064):您的SQL语法中有错误;检查   手册,对应右边的MySQL服务器版本   要在&#39; END&#39;附近使用的语法在第41行

我已阅读相关问题(包括以下内容),这些问题无助于解决问题。

Mysql stored procedure error

Mysql stored procedure error 1064

P.S。 select version();返回5.6.21

1 个答案:

答案 0 :(得分:1)

尝试:

mysql> DELIMITER //

mysql> CREATE PROCEDURE status_in_timerange
    ->      (
    ->         IN   start_date                     TiMESTAMP, 
    ->         IN   close_date                     TiMESTAMP,
    ->         OUT  total_unsolved                 INT,  
    ->         OUT  today_to_solve                 INT, 
    ->         OUT  days_left_1_3                  INT, 
    ->         OUT  days_left_4_7                  INT, 
    ->         OUT  mt_week                        INT, 
    ->         OUT  overdue_1_3_days               INT, 
    ->         OUT  overdue_4_10_days              INT,      
    ->         OUT  overdue_11_30_days             INT,    
    ->         OUT  overdue_mt_month               INT,    
    ->         OUT  solved_with_delay              INT    
    ->      )
    -> BEGIN 
    ->     SELECT count(DATEDIFF(deadline, NOW())) AS total_unsolved,
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) = 0 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 0 and datediff(deadline, CURTIME()) < 4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 3 and datediff(deadline, CURTIME()) < 8 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) > 7 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < 0 and datediff(deadline, CURTIME()) > -4 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -3 and datediff(deadline, CURTIME()) > -11 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -10 and datediff(deadline, CURTIME()) > -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, CURTIME()) < -30 and response_time is null and ktimestamp BETWEEN start_date and close_date),
    ->         (select count(deadline) from my_table where datediff(deadline, response_time) < 0 and response_time is not null and ktimestamp BETWEEN start_date and close_date) 
    ->     INTO   total_unsolved , 
    ->            today_to_solve, 
    ->            days_left_1_3, 
    ->            days_left_4_7, 
    ->            mt_week, 
    ->            overdue_1_3_days ,
    ->            overdue_4_10_days,
    ->            overdue_11_30_days,
    ->            overdue_mt_month,
    ->            solved_with_delay
    ->   FROM my_table
    ->   WHERE response_time is null and
    ->         ktimestamp BETWEEN start_date and close_date;
    -> END//
Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER ;