set语句中的mysql脚本

mbzjlibv  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(298)

我从o.jones那里得到一个提示,然后去创建一个存储函数。
这就是我创造的:
分隔符$$create function change\ u int(colres varchar(500))返回int(11)
开始

DECLARE res int(11);
    DECLARE leng int(11);
    DECLARE newres int(11);
    DECLARE mult int(11);
    DECLARE temp1 int(11);
    DECLARE temp2 int(11);  

SET res = CAST(colres AS UNSIGNED);
SET leng = CHAR_LENGTH(CAST( colres AS CHAR)); 
SET newres = 0;
SET mult = 1;
SET temp1 = 0;
SET temp2 = 0;

WHILE (res > 0) DO

    SET temp1 = MOD(res , 10 );
    SET res = (res DIV 10);
    SET temp2 = MOD(res , 10 );
    SET newres = (newres +((temp1 + temp2 ) * mult));       
    SET mult = mult*10;
END WHILE;
SET newres = SUBSTRING (newres, 1, leng );
RETURN newres;  
END $$
DELIMITER ;

但是当我在第6行运行它时,我得到了一个错误:


# 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 ''res' = CAST(colres AS CHAR)' at line 6

添加了分隔符,新错误:


# 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 'SET newres = SUBSTRING (newres, 1, leng );

    RETURN newres;    
    END' at line 27

好极了,现在成功了。但我无法唤起她:
运行测试时使用:

SHOW FUNCTION STATUS;

它是存在的。
当我试着这样运行它时:

UPDATE `table` SET `col1` = function_name(`col1`);

也尝试过:

UPDATE `table` SET `col1` = db.function_name(`col1`);

运气不好。

envsm3lx

envsm3lx1#

以前缺少列 END WHILE . 一定是的 END WHILE; 工作示例

相关问题