0

尝试在 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;

任何帮助都应该不胜感激。提前谢谢。

4

1 回答 1

2

缺少 THEN 吗?

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;
于 2012-08-03T13:29:03.580 回答