4

I have an stored procedure like this:

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

But when I want to run it, it has this error:

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

and another thing is that when I just run the insert code it runs!

plz help me.

2
  • you prepare a statement to deallocate it just after ? and take care : CONCAT() returns NULL if any argument is NULL. Commented Dec 22, 2012 at 13:27
  • actually i had a long sp that this part is not executed, so i seperate this part ans test in another sp, but it doesnt run too. Commented Dec 22, 2012 at 13:30

4 Answers 4

11

With all due respect, the way you do it kindof defeats the whole purpose of prepared statements. I'd use this form instead:

SET @query = 'INSERT INTO tblcommodity (id, idname, count) VALUES (?, ?, ?)';
PREPARE stmt FROM @query;
EXECUTE stmt USING @p1, @p2, @p3;
Sign up to request clarification or add additional context in comments.

Comments

3

It should be

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

Comments

1

Try this:

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

Comments

0

BTW, Your code:

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

has an starting comma:

    ,p1,p2,p3  

it means FOUR fields in the statement. Thats why you get an "Column count doesn't match value"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.