MySQL存储过程从秒查询返回变量值?

时间:2014-10-30 12:56:35

标签: mysql stored-procedures

SET @v1 = '';
SET @v2 = '';

SET @Query = CONCAT('SELECT sum(colName1), sum(colName2) INTO @v1, @v2 FROM tableName WHERE id=1 ');
PREPARE stmt FROM @Query;

EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SET @Query = CONCAT('SELECT id, name,',@v1,' as value1, ',@v2,' as value2 FROM tableName WHERE id=1 ');
PREPARE stmt FROM @Query;

EXECUTE stmt;
DEALLOCATE PREPARE stmt;

在读取值时抛出异常 - 列'value1'不属于表格。

如何获取@ v1和@ v2的值。

请帮帮我。

1 个答案:

答案 0 :(得分:0)

@ v2具有null 值,表示存储过程导致错误的原因。