编写mysql函数但失败了

时间:2012-08-03 13:25:25

标签: mysql

尝试在mysql中编写此函数,但它给出的错误是:

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 'IF Per='hour' THEN SET t=Price ; END IF; IF Per='day' THEN SET t=Price/24; END ' at line 8

DELIMITER$$
CREATE  FUNCTION PricePerHour (Price REAL, Per VARCHAR(5))  
RETURNS REAL 
DETERMINISTIC

BEGIN 
DECLARE t REAL;
IF Price IS NOT NULL AND Per IS NOT NULL

IF Per='hour' THEN SET t=Price ; END IF;
IF Per='day' THEN SET t=Price/24; END IF;
IF Per='week' THEN SET t=Price/7/24; END IF;
IF Per='month' THEN SET t=Price/30/24; END IF;
IF Per='year' THEN SET t=Price/365/30/24; END IF;

RETURN t;
END IF;
END $$
DELIMITER;

任何帮助都应该受到赞赏。 Thanx提前。

1 个答案:

答案 0 :(得分:2)

缺少那个?

DELIMITER$$
CREATE  FUNCTION PricePerHour (Price REAL, Per VARCHAR(5))  
RETURNS REAL 
DETERMINISTIC

BEGIN 
DECLARE t REAL;
IF Price IS NOT NULL AND Per IS NOT NULL   **THEN**

IF Per='hour' THEN SET t=Price ; END IF;
IF Per='day' THEN SET t=Price/24; END IF;
IF Per='week' THEN SET t=Price/7/24; END IF;
IF Per='month' THEN SET t=Price/30/24; END IF;
IF Per='year' THEN SET t=Price/365/30/24; END IF;

RETURN t;
END IF;
END $$
DELIMITER;