我在 mysql create function 语句中遇到错误
I am getting error in mysql create function statement
DROP FUNCTION IF EXISTS PortName;
CREATE FUNCTION PortName(port INT) RETURNS VARCHAR(3) CHARSET latin1
BEGIN
DECLARE x VARCHAR(3);
CASE port;
WHEN 996 THEN SET x = 'RP';
WHEN 995 THEN SET x = 'IA';
WHEN 990 THEN SET x = 'K1';
WHEN 989 THEN SET x = 'K2';
WHEN 988 THEN SET x = 'K3';
WHEN 986 THEN SET x = 'NG';
WHEN 985 THEN SET x = 'NW';
WHEN 984 THEN SET x = 'N2';
WHEN 983 THEN SET x = 'N3';
ELSE SET x = 'NA';
RETURN x;
END;
CASE port
和 WHEN
语句之间不应有 ;
。最后你需要END CASE;
。
DROP FUNCTION IF EXISTS PortName;
CREATE FUNCTION PortName(port INT) RETURNS VARCHAR(3) CHARSET latin1
BEGIN
DECLARE x VARCHAR(3);
CASE port
WHEN 996 THEN SET x = 'RP';
WHEN 995 THEN SET x = 'IA';
WHEN 990 THEN SET x = 'K1';
WHEN 989 THEN SET x = 'K2';
WHEN 988 THEN SET x = 'K3';
WHEN 986 THEN SET x = 'NG';
WHEN 985 THEN SET x = 'NW';
WHEN 984 THEN SET x = 'N2';
WHEN 983 THEN SET x = 'N3';
ELSE SET x = 'NA';
END CASE;
RETURN x;
END;
有关 CASE
的语法,请参见 https://dev.mysql.com/doc/refman/8.0/en/case.html。
DROP FUNCTION IF EXISTS PortName;
CREATE FUNCTION PortName(port INT) RETURNS VARCHAR(3) CHARSET latin1
BEGIN
DECLARE x VARCHAR(3);
CASE port;
WHEN 996 THEN SET x = 'RP';
WHEN 995 THEN SET x = 'IA';
WHEN 990 THEN SET x = 'K1';
WHEN 989 THEN SET x = 'K2';
WHEN 988 THEN SET x = 'K3';
WHEN 986 THEN SET x = 'NG';
WHEN 985 THEN SET x = 'NW';
WHEN 984 THEN SET x = 'N2';
WHEN 983 THEN SET x = 'N3';
ELSE SET x = 'NA';
RETURN x;
END;
CASE port
和 WHEN
语句之间不应有 ;
。最后你需要END CASE;
。
DROP FUNCTION IF EXISTS PortName;
CREATE FUNCTION PortName(port INT) RETURNS VARCHAR(3) CHARSET latin1
BEGIN
DECLARE x VARCHAR(3);
CASE port
WHEN 996 THEN SET x = 'RP';
WHEN 995 THEN SET x = 'IA';
WHEN 990 THEN SET x = 'K1';
WHEN 989 THEN SET x = 'K2';
WHEN 988 THEN SET x = 'K3';
WHEN 986 THEN SET x = 'NG';
WHEN 985 THEN SET x = 'NW';
WHEN 984 THEN SET x = 'N2';
WHEN 983 THEN SET x = 'N3';
ELSE SET x = 'NA';
END CASE;
RETURN x;
END;
有关 CASE
的语法,请参见 https://dev.mysql.com/doc/refman/8.0/en/case.html。