今天做项目时遇到一个比较奇怪的情况,某个存储过程在数据表中取值,表中的该值(amount_second)的类型是double(13,2),存储的值为-3.22。
直接用语句读取时没有此问题,取出的值为-3.22,但是使用存储过程取出值为
存储过程如下:
CREATE DEFINER=`cashuser`@`%` PROCEDURE `select1`()
BEGIN
DECLARE selectsql VARCHAR(4001);
SET selectsql = "SELECT changedmode, amount, amount_second, occurtime INTO @changedmode_old, @amount_old, @amount_second_out, @occurtime_old FROM 表名 WHERE a= 1 AND b= 20 LIMIT 1;";
SELECT selectsql;
SET @runsql = selectsql;
PREPARE runstmt FROM @runsql;
EXECUTE runstmt;
DEALLOCATE PREPARE runstmt;
SELECT @changedmode_old, @amount_old, @amount_second_out;
END
这个程序是以前的同事开发的,已经上生产环境,直接改表字段的类型为DECIMAL可能会出现大的问题,想来想去,将存储过程改成下面的
CREATE DEFINER=`cashuser`@`%` PROCEDURE `select1`(OUT amount_second_out DECIMAL(13, 2))
BEGIN
DECLARE selectsql VARCHAR(4001);
SET selectsql = "SELECT changedmode, amount, amount_second, occurtime INTO @changedmode_old, @amount_old, @amount_second_out, @occurtime_old FROM 表名 WHERE a= 1 AND b= 20 LIMIT 1;";
SELECT selectsql;
SET @runsql = selectsql;
PREPARE runstmt FROM @runsql;
EXECUTE runstmt;
DEALLOCATE PREPARE runstmt;
SET amount_second_out = @amount_second_out;
SELECT @changedmode_old, @amount_old, @amount_second_out, amount_second_out;
END
终于正常