Run mysql query present in variable

I have MySQLvariable

@query = CONCAT('INSERT INTO history VALUES (',1,',',50,',UTC_TIMESTAMP()'); 

I want to execute the insert statement present in a variable.

+6
source share
3 answers

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

Sort of:

 SET @query = CONCAT("INSERT INTO history VALUES (",1,",",50,",UTC_TIMESTAMP()"); PREPARE stmt1 FROM @query; EXECUTE stmt1; DEALLOCATE PREPARE stmt1; 
+12
source
 Set @query = CONCAT("INSERT INTO history VALUES (",1,",",50,",UTC_TIMESTAMP()"); 

and then

 PREPARE stmt_name FROM @query 

and finally

 EXECUTE stmt_name 
+2
source

delete CONCAT (), it does nothing but generate an error, as it tries to combine the string with nothing else.

-2
source

Source: https://habr.com/ru/post/913028/


All Articles