Mysql函数从查询中返回一个值
作者:互联网
我想创建一个使用查询计算值的函数,我在返回值时遇到问题:
缩短了,我的查询是:
CREATE FUNCTION func01(value1 INT , monto DECIMAL (10,2)) RETURNS DECIMAL(10,2)
BEGIN
SET @var_name = 0;
select @var_name=if(value1 = 1,monto * table.divisa_dolar,table.monto *divisa_euro) from table where data_init = 1;
return @var_nam;
END
我收到SQL语法错误.
SQL Error (1064): You have an error in your SQL syntax;
解决方法:
假设这些都是通用名称(表不是一个好的表名),问题是你不能使用==进行比较.您还缺少一些关键语法(DECLARE,SELECT INTO等).
改为:
CREATE FUNCTION func01(value1 INT , monto DECIMAL (10,2))
RETURNS DECIMAL(10,2)
DETERMINISTIC
BEGIN
DECLARE var_name DECIMAL(10,2);
SET var_name = 0;
SELECT if(value1 = 1,monto *divisa_dolar,monto *divisa_euro) INTO var_name
FROM table
WHERE data_init = 1;
RETURN var_name;
END
MySQL Comparison Functions and Operators
功能帮助:http://www.databasejournal.com/features/mysql/article.php/3569846/MySQL-Stored-Functions.htm
标签:mysql-error-1064,function,mysql 来源: https://codeday.me/bug/20191001/1837441.html