This is an automated archive.

The original was posted on /r/mysql by /u/SufficientLet672 on 2023-08-10 15:45:01+00:00.


I want to upload data to a table but the table it uploads to has the current year in it’s name so I created a variable, but when I use the variable it says it’s invalid and if I put ` around the variables it just spits out the variable name instead of the tablename_year and gives me the error ‘schema.@variablename doesn’t exist’. I’ve tried googling how to insert a variable into a query but nothing comes up that shows me an example of how to properly insert a variable, all I get is instructions on how to create variables. if I insert a static tablename_2023, the query works fine but wont work next year when I need it to use the new tablename_2024, I’ll have to update my query then.

SET@var = CONCAT(“tablename_”,(SELECT DATE_FORMAT(CURDATE(),‘%Y’)));SET@Tablename_ = CONCAT_WS(" ", “SELECT count(*) FROM”,@var);PREPARE tablename_stmt FROM@tablename;EXECUTE tablename_stmt;SET GLOBAL local_infile=1;USE Schema;set time_zone=‘+00:00’;truncate @tablename;load data local infile “D:/ProgramData/MySQL/MySQL Server 5.7/Uploads/data.csv"IGNORE INTO TABLE @tablenameFIELDS TERMINATED BY ','OPTIONALLY ENCLOSED BY '”'LINES TERMINATED by ‘\r\n’