原文地址:http://blog.csdn.net/business122/article/details/7528859

今天写一个对数据库做快照的存储过程,用到了动态表名,突然发现MYSQL不支持直接使用变量做表名,GOOGLE了下,采用以下方法:
DECLARE dbName VARCHAR(30);
SET dbName = 'aaa'; SET @STMT :=CONCAT("CREATE TABLE ",dbName," SELECT id from gamedb;"); PREPARE STMT FROM @STMT;
EXECUTE STMT; END
使用预处理sql
 
 
 
05-11 22:55