CREATE FUNCTION salarystatus(employee_id integer) RETURNS VARCHAR(25)
DETERMINISTIC
BEGIN
DECLARE isalary DECIMAL(8,2);
DECLARE istatus varchar(25);
SELECT salary INTO isalary
FROM employees
WHERE employee_id = salarystatus.employee_id;
IF (isalary < 50000) then
SET istatus = 'low income';
ELSEIF (50000 <= isalary < 80000) then
SET istatus = 'medium income';
ELSE
SET istatus = 'high income';
ENDIF
RETURN istatus
END
返回此错误:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'RETURN istatus
在第20行结束
这个查询看起来正确,这里的语法错误是什么?
问题是我写了ENDIF而不是“END IF”
最后添加这个。
$$ DELIMITER;
如果不是endif结束并且每个语句都需要终止
P.Salmon已经评论了,而我正在做出答案。
DELIMITER $$
CREATE FUNCTION
# best to prefix with in_ so MySQL and you cant mix it up with existing columns
salarystatus (in_employee_id INTEGER)
RETURNS VARCHAR(25)
BEGIN
DECLARE isalary DECIMAL(8,2) DEFAULT NULL; # <- best to have a default value
DECLARE istatus varchar(25) DEFAULT NULL; # <- best to have a default value
SELECT salary
INTO isalary
FROM employees
WHERE employee_id = in_employee_id.employee_id; #<- use the function in param
IF (isalary < 50000) then
SET istatus = 'low income';
ELSEIF (50000 <= isalary < 80000) then
SET istatus = 'medium income';
ELSE
SET istatus = 'high income';
END IF; # <- semicon (;) missing in your query also ENDIF isnt valid syntax.
RETURN istatus;
END
DELIMITER ;
我也删除DETERMINISTIC
因为它不是真的需要。