使用内部函数创建视图

时间:2019-03-17 13:01:01

标签: mysql wamp

我正在尝试创建一个内部带有函数的视图。 当我想查看创建的视图时,会收到一条错误消息

  

1318-函数ret_stat的参数数目不正确;预期1,得到0

这是我的功能:

DELIMITER //
CREATE FUNCTION ret_stat (ret_stat CHAR (50))
RETURNS CHAR DETERMINISTIC
BEGIN
DECLARE result CHAR;
SET result = "";
SET result =
    CASE (result)
         WHEN (65-(YEAR(CURDATE()) - YEAR(bdate))) > 5 THEN '5 plus years'
         WHEN (65-(YEAR(CURDATE()) - YEAR(bdate))) > 4 THEN '5 years left'
         WHEN (65-(YEAR(CURDATE()) - YEAR(bdate))) > 3 THEN '4 years left'
         WHEN (65-(YEAR(CURDATE()) - YEAR(bdate))) > 2 THEN '3 years left'
         WHEN (65-(YEAR(CURDATE()) - YEAR(bdate))) > 1 THEN '2 years left'
         WHEN (65-(YEAR(CURDATE()) - YEAR(bdate))) = 1 THEN '1 year left'
         WHEN (65-(YEAR(CURDATE()) - YEAR(bdate))) = 0 THEN 'Soon retirement'
         WHEN (65-(YEAR(CURDATE()) - YEAR(bdate))) < 0 THEN 'Retired'
         END;
RETURN result;
END //
DELIMITER ;

...这是我对视图的查询:

 CREATE VIEW ret_stat_view AS
 SELECT fname, lname, faculty, ret_stat()
 FROM employees;

1 个答案:

答案 0 :(得分:0)

该函数的参数必须是bdate作为日期,并且返回值是TEXT,结果也是TEXT,CASE不需要任何参数。 ...并且可以正常工作!

KAPOW的支持者! ★★★★★

  

Veni,vidi,solvī!