I am trying to define the following function in mysql but I get an error:
DELIMITER //
CREATE FUNCTION salario_maximo(dep INT)
RETURNS FLOAT
BEGIN
DECLARE msalario FLOAT;
DECLARE dpto INT;
SELECT COUNT(*) INTO dpto FROM departamentos WHERE DEPT_NO=dep;
IF(SELECT COUNT(*) FROM departamentos WHERE DEPT_NO=dep>0)
BEGIN
IF(SELECT COUNT(*) FROM empleados WHERE DEPT_NO=dep>0)
BEGIN
SELECT max(salario) INTO msalario from empleados where dept_no=dep;
END
ELSE
BEGIN
SET msalario = 0;
END
END
ELSE
BEGIN
SET msalario = -1;
END
RETURN msalario;
END; //
DELIMITER ;
My error is:
#1064 - Algo está equivocado en su sintax cerca 'BEGIN
IF(SELECT COUNT(*) FROM empleados WHERE DEPT_NO=dep>0)
' en la linea 8
I need some help! Thanks!
There are couple of things here.
First, you need a "THEN" after a if condition.
Second, you need to close your IF ELSE block with a END IF statement.
Also, put a semicolon after the END statements.
Try this:
DELIMITER //
CREATE FUNCTION salario_maximo(dep INT)
RETURNS FLOAT
BEGIN
DECLARE msalario FLOAT;
DECLARE dpto INT;
SELECT COUNT(*) INTO dpto FROM departamentos WHERE DEPT_NO=dep;
IF(SELECT COUNT(*) FROM departamentos WHERE DEPT_NO=dep>0)
THEN
BEGIN
IF(SELECT COUNT(*) FROM empleados WHERE DEPT_NO=dep>0)
THEN
BEGIN
SELECT max(salario) INTO msalario from empleados where dept_no=dep;
END;
ELSE
BEGIN
SET msalario = 0;
END;
END IF;
END;
ELSE
BEGIN
SET msalario = -1;
END;
END IF;
RETURN msalario;
END; //
DELIMITER ;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.