我检查了一些关于此错误的问题,并且大多数问题在使用“SELECT”函数时遇到此错误但在我的情况下我使用CREATE TABLE如果不存在但遇到此错误,您是否可以检查我的代码以查看导致该错误的原因错误或者如果你们知道其他方式做同样的事情。
我想创建一个年度付款表,实际上应该是每月付款,但我现在正在尝试。
/* INSERT HERE THE LIST OF ALL EXISTING PAYMENT TABLES */
DROP TEMPORARY TABLE IF EXISTS T_distinctTable;
CREATE TEMPORARY TABLE T_distinctTable (
ctr int PRIMARY KEY AUTO_INCREMENT NOT NULL,
DisTable varchar (18)
);
INSERT INTO T_distinctTable (DisTable)
SELECT TABLE_NAME from information_schema.tables WHERE TABLE_SCHEMA = 'cams' and TABLE_NAME like concat ('%','Payment_20','%');
SET @endYearTemp = (select count(*) from T_distinctTable);
set @YearNow = (SELECT YEAR(CURDATE()));
set @TableName = concat('Payment_',@YearNow);
set @SQLTable = ('CREATE TABLE IF NOT EXISTS `', @TableName ,'` id int NOT NULL AUTO_INCREMENT, name CHAR(30),PRIMARY KEY (id)');
SELECT @SQLTable;
PREPARE stmt FROM @SQLTable;
EXECUTE stmt;
答案 0 :(得分:1)
使用concat()
连接字符串。将它们放在括号中并用逗号分开它们就不会这样做。你忘记了CREATE TABLE
。
...
set @SQLTable = concat('CREATE TABLE IF NOT EXISTS `', @TableName ,'` (id int NOT NULL AUTO_INCREMENT, name CHAR(30),PRIMARY KEY (id))');
SELECT @SQLTable;
PREPARE stmt FROM @SQLTable;
EXECUTE stmt;
...