Insert query using prepared-statement in mysql? - mysql

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.

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;

It should be
SET #query = CONCAT('insert into tblcommodity (id , idname , count)values (',',',p1,',', p2,',',p3,')');

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

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"

Related

Mysql server and dynamic table name

I want to know if Mysql server support dynamic table name like this :
SET #tbl = 'Dawin';
INSERT INTO #tbl (col1,col2) VALUES (val1,val2);
because I tried all the possible syntax that I know on my query, but it failed every time.
if it is not supporting such a query, then how to solve this issue?
Thank you
You can try this;)
SET #tbl = 'test';
SET #sql = CONCAT('INSERT INTO ', #tbl, ' (col1,col2) VALUES (val1,val2)');
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

MySQL stored procedure return variable values from seconds query?

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;
While reading values the throw an Exception - Column 'value1' does not belong to table.
How can I get the value of #v1 and #v2.
Please help me.
The #v2 has null value that why the stored procedure gives error.

Paramater for name of a table without using prepare statement in MySQL

can i somehow set parameter for name of table in query without using a prepare statement?
This is example:
SET #tableName = 'Customer';
SELECT * FROM #tableName;
Thanks
Depending on the version of MySQL you are using, you may be able to use something like:
SET #tableName = 'Customer';
SET #s = CONCAT('SELECT * FROM ', #tableName);
PREPARE stmt FROM #s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

MYSQL get row count of update from prepared statement in a stored procedure

OK I have this code in a stored procedure:
SET #qry = CONCAT('UPDATE ', usermeta_table, ' SET meta_value = ', #token_count, ' WHERE user_id = ', #temp_id, ' AND meta_key = "token_count"');
PREPARE stmt FROM #qry;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET #did_update = (SELECT ROW_COUNT());
IF #did_update = 0 THEN
SET #qry = CONCAT('INSERT INTO ', usermeta_table, '(user_id, meta_key, meta_value) VALUES (', #temp_id, ', "token_count", ', #token_count, ')');
PREPARE stmt FROM #qry;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END IF;
What I'm after is if the update doesn't update any rows then make a row in the table.
For some reason when I run the above code did_update is always coming back as 0 so the insert aways happens even if the update has updated some rows. How do I fix this to work?
UPDATE: I've just realised that if the values don't change in the update despite the rows existing it returns zero rows. This is what is causing my logic to break...
According to this site,
"you can use ROW_COUNT() with prepared statements, but you need to call it after EXECUTE, not after DEALLOCATE PREPARE, because the row count for allocate prepare is always 0."

Insert query on procedure

I have a procedure like this
set #dt=now();
SET #query = CONCAT('insert into tbl_Name (Name , Created_Date , Created_By) values (?,?,?)');
PREPARE stmt FROM #sql;
EXECUTE stmt using #tbl_column_value, #dt, #tbl_user_id;
END IF;
DEALLOCATE PREPARE stmt;
im getting this error
java.sql.SQLException: Incorrect arguments to EXECUTE