0

我正在把一个函数放在 MySql 中。没什么大不了的。但它不会保存。我在 HeidiSql 中使用。

CREATE DEFINER=`root`@`localhost` FUNCTION `mainLinks`(`nid` INT)
    RETURNS varchar(1500)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    DECLARE aid INT;
    DECLARE atype INT;
    DECLARE aparent INT;
    DECLARE ausername VARCHAR(32);
    DECLARE auserid INT;
    DECLARE aCOUNTRY VARCHAR(2);
    DECLARE aOutput VARCHAR(1500); 

    SELECT id, type, parent, userName, userId, country
    INTO aid, atype, aparent, auserName, auserId, acountry
    FROM arraytest
    WHERE id = nid;

    CASE atype
    WHEN 1 THEN SET aOutput = 'One';
    WHEN 2 THEN SET aOutput = 'Two';
    WHEN 3 THEN SET aOutput = 'Three';
    ELSE 'Neither';
    END;

  RETURN (aOutput);
END

这是case导致所有问题的原因,我已经尝试了一切。移动和移除;。但没有什么能让它发挥作用。

这是错误:

`/* SQL Error (1064): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''Neither';
    END;

  RETURN (aOutput);
END' at line 26 */`

此函数中没有第 26 行。你能帮忙吗?

带有数据和其他东西的表 Fiddle

4

2 回答 2

2

你的 CASE 子句全错了

它应该是

SET aOutput = CASE atype
              WHEN 1 THEN 'One'
              WHEN 2 THEN 'Two'
              WHEN 3 THEN 'Three'
              ELSE 'Neither'
END;

编辑

CREATE DEFINER=`root`@`localhost` FUNCTION `mainLinks`(`nid` INT)
    RETURNS varchar(1500)
    LANGUAGE SQL
    NOT DETERMINISTIC
    CONTAINS SQL
    SQL SECURITY DEFINER
    COMMENT ''
BEGIN
    DECLARE aid INT;
    DECLARE atype INT;
    DECLARE aparent INT;
    DECLARE ausername VARCHAR(32);
    DECLARE auserid INT;
    DECLARE aCOUNTRY VARCHAR(2);
    DECLARE aOutput VARCHAR(1500); 

    SELECT id, type, parent, userName, userId, country
    INTO aid, atype, aparent, auserName, auserId, acountry
    FROM arraytest
    WHERE id = nid;

    SET aOutput = CASE atype
        WHEN 1 THEN CONCAT (auserName, ' other stuff')
        WHEN 2 THEN auserName
        WHEN 3 THEN 'other stuff'
        ELSE 'default stuff'
    END;
    RETURN (aOutput);
END

在sqlfiddle上可以进行非常相似的测试

于 2013-06-06T13:17:30.753 回答
0

这是导致您出错的案例陈述

CASE atype
WHEN 1 THEN SET aOutput = 'One';
WHEN 2 THEN SET aOutput = 'Two';
WHEN 3 THEN SET aOutput = 'Three';
ELSE 'Neither';
END;

应该

CASE atype
    WHEN 1 THEN SET aOutput = 'One'
    WHEN 2 THEN SET aOutput = 'Two'
    WHEN 3 THEN SET aOutput = 'Three'
    ELSE 'Neither'
END CASE;
于 2013-06-06T13:18:05.410 回答