MySQL:无法创建函数{functionName}

时间:2010-06-15 17:56:10

标签: mysql stored-procedures stored-functions

这适用于MySQL 5.0.41,但在5.1.31上它只是说“创建功能失败”。 我在控制台中输入:

delimiter |
<press enter>
CREATE DEFINER=`root`@`localhost` FUNCTION `ucwords`( str VARCHAR(128) ) RETURNS varchar(128) CHARSET utf8
BEGIN
  DECLARE c CHAR(1);
  DECLARE s VARCHAR(128);
  DECLARE i INT DEFAULT 1;
  DECLARE bool INT DEFAULT 1;
  DECLARE punct CHAR(17) DEFAULT ' ()[]{},.-_!@;:?/';
  SET s = LCASE( str );
  WHILE i < LENGTH( str ) DO 
    BEGIN
      SET c = SUBSTRING( s, i, 1 );
      IF LOCATE( c, punct ) > 0 THEN
        SET bool = 1;
      ELSEIF bool=1 THEN 
        BEGIN
          IF c >= 'a' AND c <= 'z' THEN 
            BEGIN
              SET s = CONCAT(LEFT(s,i-1),UCASE(c),SUBSTRING(s,i+1));
              SET bool = 0;
            END;
          ELSEIF c >= '0' AND c <= '9' THEN
            SET bool = 0;
          END IF;
        END;
      END IF;
      SET i = i+1;
    END;
  END WHILE;
  RETURN s;
END |
<press enter>

我甚至尝试将其最小化为:

delimiter |
<press enter>
CREATE DEFINER=`root`@`localhost` FUNCTION `ucwords`( str VARCHAR(128) ) RETURNS varchar(128) CHARSET utf8
BEGIN
  DECLARE s VARCHAR(128);
  RETURN s;
END |
<press enter>

我甚至在没有定义者的情况下尝试过它,只需使用:

delimiter |
<press enter>
CREATE FUNCTION `ucwords`( str VARCHAR(128) ) RETURNS varchar(128) CHARSET utf8
BEGIN
  DECLARE s VARCHAR(128);
  RETURN s;
END |
<press enter>

1 个答案:

答案 0 :(得分:0)

您最近是否从MySQL 5.0升级到5.1?如果是这样,您需要运行mysql_upgrade脚本来更新系统表。在这种情况下,mysql.proc表架构已更改,这可以解释您的问题: http://dev.mysql.com/doc/refman/5.1/en/mysql-upgrade.html

有关升级到5.1的更多信息: http://dev.mysql.com/doc/refman/5.1/en/upgrading-from-previous-series.html