mysql - 设置语句中的 MySQL 脚本
问题描述
我从 O.jones 那里得到了一个提示,然后去创建一个存储函数。
这就是我创建的:
DELIMITER $$ CREATE FUNCTION change_int(colres VARCHAR(500)) RETURNS INT(11)
BEGIN
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
Greate 现在它起作用了。但我无法唤起她:
运行测试:
SHOW FUNCTION STATUS;
它存在。
当我尝试像这样运行它时:
UPDATE `table` SET `col1` = function_name(`col1`);
也试过:
UPDATE `table` SET `col1` = db.function_name(`col1`);
没运气。
解决方案
您之前缺少列END WHILE
。肯定是END WHILE;
推荐阅读
- wordpress - 悬停在帖子链接(WordPress)上时如何显示帖子的特色图片?
- ios - iPad 上的 ActionSheet 未正确显示 SwiftUI
- sql - sql在年底获得余额
- excel - 使用基于单元格值的文件路径导入多个图像
- django - django 的导入名称模式问题
- ios - Xcode 12.3 Mac Catalyst 代码签名失败(代码对象根本没有签名)
- python - 如何摆脱 Python 中的 SSL 错误以确保网站安全?
- google-app-engine - 具有自动扩展 App Engine 的 Cloud Tasks,持续时间超过 10 分钟
- c# - 序列化到模型 - 序列化节点以选择两个对象
- flutter - Flutter web 从 API 下载 pdf 并保存在目录中