Mysql server and dynamic table name - mysql

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;

Related

Using dynamic SQL in MySql function

I have created a function in phpMyAdmin as shown in this screenshot.
When I try to use it like this:
Select DMax ("id","customers")
I get error #1305 saying that uTable does not exist. This is probably some basic syntax issue, as uTable in the sql statement is taken literally and not seen as a parameter. So how do I make it work?
You can't use parameters to a procedure for column or table names. Instead, you need to prepare a statement using those values and execute that. For example:
BEGIN
DECLARE uValue INT(11);
SET #sql = CONCAT('SELECT MAX(', uField, ') INTO uValue FROM ', uTable);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
RETURN uValue;
END
Note that you cannot use dynamic SQL in a function, so you would need to convert this into a stored procedure with uValue an OUT parameter i.e.
CREATE PROCEDURE DMax(
IN uField VARCHAR(100),
IN uTable VARCHAR(100),
OUT uValue <appropriate type>
)
BEGIN
DECLARE uValue INT(11);
SET #sql = CONCAT('SELECT MAX(', uField, ') INTO uValue FROM ', uTable);
PREPARE stmt FROM #sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END
Then you would need to call the procedure, something like
CALL DMax('table1', 'column1', #DMax)
and you can then
SELECT #DMax
(yes, this is a monumental pain)

Table in FROM clause defined by variable in MySQL

I want to create a SQL script for MySQL 5.7 that inserts data from a table of a database origin into a table of another target database.
I want to have this source-database defined by a variable.
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = concat(#origin_db,'.','tablename');
INSERT INTO target_table SELECT * FROM #origin_table;
Variables are used in various example to define column names but I never seen a way to define a table with it.
Is anyone has a trick for this ?
Variables won't use in table name in MySQL. You only can use a prepared statement for dynamic build query. For example:
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = CONCAT(#origin_db,'.','tablename');
SET #query = CONCAT('INSERT INTO target_table SELECT * FROM ', #origin_table);
PREPARE stmt FROM #query;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
You can read more detail about it in official documentation
You can use Prepared Statement like this:
USE my_target_db;
SET #origin_db='my_origin_db';
SET #origin_table = concat(#origin_db,'.','tablename');
SET #qry1 = concat('INSERT INTO target_table SELECT * FROM ', #origin_table);
PREPARE stmt1 from #qry1;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;

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 using prepared-statement in 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"