[英]procedure MySQL using concat
Can you help me here?你能帮我吗?
BEGIN
set @currentdate=now();
set @formateddate=convert(date_format(@currentdate,'%Y%m%d'),char);
select @t;
drop table if exists CONCAT('account_class_',@formateddate)
,CONCAT('class_courses_',@formateddate),CONCAT('student_',@formateddate),CONCAT(
'messages_',@formateddate),CONCAT('announcement_',@formateddate),CONCAT('student
_activity_',@formateddate),CONCAT('activity_',@formateddate),CONCAT('account_cla
ss_students_',@formateddate);
create table CONCAT('account_class_',@formateddate) select * from account_class;
create table CONCAT('class_courses_',@formateddate) select * from class_courses;
create table CONCAT('student_',@formateddate) select * from student;
create table CONCAT('messages_',@formateddate) select * from messages;
create table CONCAT('announcement_',@formateddate) select * from announcement;
create table CONCAT('student_activity_',@formateddate) select * from
student_activity;
create table CONCAT('activity_',@formateddate) select * from activity;
create table CONCAT('account_class_students_',@formateddate) select * from
account_class_students;
END
What is the problem?问题是什么?
You are trying to construct a table name.您正在尝试构造一个表名。 That is not allowed.这是不允许的。 In theory, you could use dynamic SQL:理论上,您可以使用动态 SQL:
declare @sql;
set @sql = replace('create table [table] select * from account_class',
[table], 'CONCAT('account_class_', @formateddate));
prepare x from @sql;
execute x;
However, I would discourage creating such a proliferation of tables, when simple views can do what you want (or just a where
clause).但是,我不鼓励创建如此大量的表,因为简单的视图可以执行您想要的操作(或只是一个where
子句)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.