导入MySQL函数时出现语法错误

时间:2022-09-14 23:01:59

I get a #1064 syntax error on the beginning of line 5's if clause when trying to run the following code in PhpMyAdmin:

尝试在PhpMyAdmin中运行以下代码时,我在第5行的if子句的开头遇到#1064语法错误:

DELIMITER ;
CREATE DEFINER=`root`@`localhost` FUNCTION `ANREDE`(geschlecht enum('m','w'), vorname VARCHAR(255), nachname VARCHAR(255)) RETURNS varchar(1023) CHARSET latin1
    DETERMINISTIC
BEGIN
     IF geschlecht = 'm' THEN RETURN CONCAT_WS('',CONCAT_WS(' ','Sehr geehrter Herr',vorname,nachname),',');
     ELSE RETURN CONCAT_WS('',CONCAT_WS(' ','Sehr geehrte Frau',vorname,nachname),',');
END IF;
    END

It seems to me the if clause is correct - what could cause such an error?

在我看来,if子句是正确的 - 什么可能导致这样的错误?

2 个解决方案

#1


0  

You can either use DELIMITER or you can rewrite your function to make it one-statement function like so

您既可以使用DELIMITER,也可以重写函数以使其成为单语句函数

CREATE FUNCTION `ANREDE`
(
  geschlecht enum('m','w'), 
  vorname VARCHAR(255), 
  nachname VARCHAR(255)
) 
RETURNS VARCHAR(1023) CHARSET latin1
DETERMINISTIC
  RETURN CONCAT
          (CONCAT_WS(' ', 
                     IF(geschlecht = 'm', 
                        'Sehr geehrter Herr', 
                        'Sehr geehrte Frau'), 
                     vorname, 
                     nachname),
          ',');

Here is SQLFiddle demo.

这是SQLFiddle演示。

#2


0  

You need to change your delimiter. Currently, ; is your delimiter, and the parser interprets your ;, at the end of line 5, as the end of your function creation statement.

您需要更改分隔符。目前, ;是你的分隔符,解析器解释你的;在第5行的末尾,作为你的函数创建语句的结尾。

Try this:

DELIMITER // -- change the delimiter

CREATE ...
BEGIN
    IF ... ; -- this is not the end of the statement
    ...
END
// -- end of statement

DELIMITER ; -- useless if you issue this in PhpMyAdmin, but a good practice nevertheless

#1


0  

You can either use DELIMITER or you can rewrite your function to make it one-statement function like so

您既可以使用DELIMITER,也可以重写函数以使其成为单语句函数

CREATE FUNCTION `ANREDE`
(
  geschlecht enum('m','w'), 
  vorname VARCHAR(255), 
  nachname VARCHAR(255)
) 
RETURNS VARCHAR(1023) CHARSET latin1
DETERMINISTIC
  RETURN CONCAT
          (CONCAT_WS(' ', 
                     IF(geschlecht = 'm', 
                        'Sehr geehrter Herr', 
                        'Sehr geehrte Frau'), 
                     vorname, 
                     nachname),
          ',');

Here is SQLFiddle demo.

这是SQLFiddle演示。

#2


0  

You need to change your delimiter. Currently, ; is your delimiter, and the parser interprets your ;, at the end of line 5, as the end of your function creation statement.

您需要更改分隔符。目前, ;是你的分隔符,解析器解释你的;在第5行的末尾,作为你的函数创建语句的结尾。

Try this:

DELIMITER // -- change the delimiter

CREATE ...
BEGIN
    IF ... ; -- this is not the end of the statement
    ...
END
// -- end of statement

DELIMITER ; -- useless if you issue this in PhpMyAdmin, but a good practice nevertheless