2015-10-14 81 views
-1
DELIMITER // 
CREATE FUNCTION NodeState (current_time DATETIME, lastcheck DATETIME, polling_time INT) 
RETURNS varchar(20) 

BEGIN 

    DECLARE node_state VARCHAR(20) DEFAULT grey; 
    DECLARE time_gap DATETIME; 
    SET time_gap = TIMESTAMPDIFF(SECOND, current_time, lastcheck); 
    SET perc = polling_time*0.05; 

    if time_gap > perc then 
     SET node_state = 'red'; 
    ELSEif time_gap = perc then 
     SET node_state = 'orange'; 
    ELSEif time_gap – lastcheck < perc then 
     SET node_state = 'green'; 
    END IF; 

    RETURN node_state; 

END; // 

我試圖創建這個函數,我一行一行地去,但找不到錯誤。創建函數時出現SQL語法錯誤

我得到的錯誤是1064(42000)

+0

如果出現語法錯誤,錯誤信息是什麼? – Jens

+3

默認灰色 - >'灰色'? – Pieter21

回答

0

我會改變CURRENT_TIME參數的名稱,因爲有CURRENT_TIME()函數和MySQL可能無法在你的代碼,如果在某些位置來區分你的代碼引用參數或函數。

+0

我已經改變了,我仍然得到這個錯誤: 你的SQL語法有錯誤;檢查與您的MySQL服務器版本相對應的手冊,在'DELIMITER // CREATE FUNCTION NodeState(current_time2 DATETIME,lastcheck DATE')第1行 –

+0

附近使用正確的語法。好吧,我沒有聲明perc ...但我仍然得到錯誤 –