I have a MySQLvariable
@query = CONCAT('INSERT INTO history VALUES (',1,',',50,',UTC_TIMESTAMP()');
I want to execute the insert statement present in the variable.
@@ is used for system variables. Using different suffix with @@ , you can get either session or global value of the system variable.
Dynamic SQL is a programming technique you can use to build SQL statements as textual strings and execute them later. This technique could prove to be useful in some cases and therefore it's good to know we have it as an option. In today's article, we'll show how to create and execute dynamic SQL statements.
You must first prepare the statement using PREPARE
and then use EXECUTE
See here: http://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
Something like:
SET @query = CONCAT("INSERT INTO history VALUES (",1,",",50,",UTC_TIMESTAMP()");
PREPARE stmt1 FROM @query;
EXECUTE stmt1;
DEALLOCATE PREPARE stmt1;
Set @query = CONCAT("INSERT INTO history VALUES (",1,",",50,",UTC_TIMESTAMP()");
and then
PREPARE stmt_name FROM @query
and at last
EXECUTE stmt_name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With