2017-07-25 66 views
0

我嘗試創建以下過程:MySQL的存儲過程分隔符

DROP PROCEDURE IF EXISTS my_super_procedure; 
DELIMITER $$ 
CREATE PROCEDURE my_super_procedure(IN in_id VARCHAR(255), IN in_from_date timestamp, IN in_to_date timestamp) 
BEGIN 

    DROP TEMPORARY TABLE IF EXISTS tblResults; 
    CREATE TEMPORARY TABLE IF NOT EXISTS tblResults(
     selected_date timestamp, 
     total_value int 
    ); -- !!!!! Syntax error: missing END 

    DECLARE tmp_date timestamp; 
    SET tmp_date = in_from_date; 

    WHILE tmp_date < in_to_date DO 

     SELECT count(*) as total_events FROM event e where e.scheduled between tmp_date and tmp_date + interval 1 day; 
     set tmp_date = DATE_ADD(tmp_date, INTERVAL 1 DAY); 
    END WHILE; 
END$$ 

DELIMITER ; 

call my_super_procedure("3dccd75a-4c8e-11e7-bf68-5ce0c56861d1", "2017-06-24 04:16:43", "2017-06-28 04:16:43"); 

我在存儲過程中新。 我該如何解決這個錯誤?

回答

2

在開頭聲明所有變量,而不是在代碼中間。

DROP PROCEDURE IF EXISTS my_super_procedure; 
DELIMITER $$ 
CREATE PROCEDURE my_super_procedure(IN in_id VARCHAR(255), IN in_from_date timestamp, IN in_to_date timestamp) 
BEGIN 
    DECLARE tmp_date timestamp; 

    DROP TEMPORARY TABLE IF EXISTS tblResults; 
    CREATE TEMPORARY TABLE IF NOT EXISTS tblResults(
     selected_date timestamp, 
     total_value int 
    ); 

    SET tmp_date = in_from_date; 

    WHILE tmp_date < in_to_date DO 

     SELECT count(*) as total_events FROM event e where e.scheduled between tmp_date and tmp_date + interval 1 day; 
     set tmp_date = DATE_ADD(tmp_date, INTERVAL 1 DAY); 
    END WHILE; 
END$$ 

DELIMITER ; 
+0

是的!謝謝! –