无法在我的sql中创建UDF

时间:2016-11-21 09:24:28

标签: mysql function

无法创建UDF。在创建时低于错误

Error: 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 ';
CREATE  FUNCTION `seat_count`(seatno varchar(250))
RETURNs varchar(250) 
BEG' at line 1

代码:http://pastebin.com/uf4DkXZh

代码:

CREATE  FUNCTION `seat_count`(seatno VARCHAR(250))
RETURNS VARCHAR(250) 
BEGIN
SET @str= REPLACE(seatno, ',', '');
SET @stcnt=CHAR_LENGTH(str); 
SET @x=1;
SET @result;
WHILE (@x <= @stcnt)
DO
    SET @st=SUBSTRING_INDEX(@str,',',X);
    IF ((LENGTH(@seatno) - LENGTH(REPLACE(@seatno, st, '')))/LENGTH(@st)=1)
        THEN SET @result=CONCAT(@st,@result);

     END IF;
     SET @x=@x+1;
END WHILE;
RETURN @result;

END $$

1 个答案:

答案 0 :(得分:1)

在MySQL中开发存储过程时,可以传递输入参数并声明局部变量,局部变量不会添加任何前缀,而不像@前面的用户定义变量。有关详细信息procedure vs user-defined变量

DELIMITER $$
    CREATE FUNCTION `SEAT_COUNT`(`seatno` VARCHAR(250)) RETURNS VARCHAR(250)
        DETERMINISTIC
    BEGIN

        DECLARE result VARCHAR(250);
        SET @str = REPLACE(seatno, ',', '');
        SET @stcnt = CHAR_LENGTH(@str); 
        SET @x = 1;

            WHILE (@x <= @stcnt)
            DO
                SET @st = SUBSTRING_INDEX(@str,',',X);
                IF ((LENGTH(@seatno) - LENGTH(REPLACE(@seatno, st, '')))/LENGTH(@st)=1)
                    THEN SET result = CONCAT(@st, @result);
                END IF;
                SET @x = @x+1;
            END WHILE;

        RETURN result;

    END$$
    DELIMITER ;

您已将结果变量定义为用户定义的SET @result而非本地变量,例如DECLARE result VARCHAR(250)

我希望这也会在你的最后工作。