我有一个 MySQL 变量
@query = CONCAT('INSERT INTO history VALUES (',1,',',50,',UTC_TIMESTAMP()');
我想执行变量中存在的插入语句。
您必须首先准备语句 usingPREPARE
然后使用EXECUTE
见这里:http ://dev.mysql.com/doc/refman/5.0/en/sql-syntax-prepared-statements.html
就像是:
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()");
进而
PREPARE stmt_name FROM @query
最后
EXECUTE stmt_name
删除 CONCAT(),它除了生成错误之外什么都不做,因为它试图将一个字符串与其他任何内容连接起来。