在MySQL存储过程中INSERT INTO .. SELECT的任何已知问题?

时间:2011-11-10 11:41:25

标签: mysql stored-procedures insert

我正在尝试从MySQL存储过程调用INSERT INTO ... SELECT语句。

我总是在MySQL中遇到这个错误:ERROR 1242 (21000): Subquery returns more than 1 row

我的 SELECT 语句肯定会返回多行,但 INSERT 语句不会返回。

我错过了什么?

代码:

PREPARE query FROM 'INSERT INTO tmpTable (client_program, ref_client, ref_household) SELECT ref_client_program, ref_member, \"84k3\" FROM client_program_members WHERE ref_client_program IN (74)';
EXECUTE query ;
DEALLOCATE PREPARE query;

2 个答案:

答案 0 :(得分:0)

不确定,但请尝试这一行 -

PREPARE query FROM 'INSERT INTO `tmpTable`(client_program, ref_client, ref_household) SELECT ref_client_program, ref_member, ''84k3'' FROM client_program_members WHERE ref_client_program IN(74)';

答案 1 :(得分:-1)

确实可以做insert ... select 并且您可以从该选择中拥有多行。

以下代码应该有效。

DECLARE sql varchar(1000);
SET sql = CONCAT("INSERT INTO tmpTable (client_program, ref_client, ref_household) "
               , " SELECT ref_client_program, ref_member, \'84k3\' "
               , " FROM client_program_members WHERE ref_client_program = ? ");

PREPARE query FROM sql;
SET @client_prog = 74; 
EXECUTE query USING @client_prog;
DEALLOCATE PREPARE query;