今天写一个对数据库做快照的存储过程,用到了动态表名,突然发现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