4

我有一个这样的存储过程:

SET @query = CONCAT('insert into tblcommodity (id , idname , count)values (',p1, p2,p3,')');
PREPARE stmt FROM @query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

但是当我想运行它时,它有这个错误:

> Procedure execution failed 1136 - Column count doesn't match value
> count at row 1

另一件事是,当我只运行插入代码时,它就会运行!

请帮助我。

4

4 回答 4

11

恕我直言,你这样做的方式有点违背了准备好的陈述的全部目的。我会改用这种形式:

SET @query = 'INSERT INTO tblcommodity (id, idname, count) VALUES (?, ?, ?)';
PREPARE stmt FROM @query;
EXECUTE stmt USING @p1, @p2, @p3;
于 2012-12-22T13:30:00.520 回答
3

它应该是

SET @query = CONCAT('insert into tblcommodity (id , idname , count)values (',',',p1,',', p2,',',p3,')');
于 2012-12-22T13:27:48.993 回答
1

试试这个:

SET @query = CONCAT("insert into tblcommodity (id , idname , count) values (", p1, ", '", p2,"',",p3,")"); 
PREPARE stmt FROM @query; 
EXECUTE stmt; 
DEALLOCATE PREPARE stmt;
于 2012-12-22T13:31:02.183 回答
0

顺便说一句,您的代码:

SET @query = CONCAT('insert into tblcommodity (id , idname , count)values (',p1, p2,p3,')');

有一个逗号开头:

    ,p1,p2,p3  

这意味着语句中的四个字段。这就是为什么您会收到“列数与值不匹配”的原因

于 2017-06-19T14:59:06.487 回答