程序MySQL使用concat

时间:2016-10-18 12:31:01

标签: mysql procedure concat

你能帮助我吗?

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

有什么问题?

1 个答案:

答案 0 :(得分:1)

您正在尝试构建表名。这是不允许的。理论上,您可以使用动态SQL:

declare @sql;

set @sql = replace('create table [table] select * from account_class',
                   [table], 'CONCAT('account_class_', @formateddate));

prepare x from @sql;

execute x;

但是,当简单的视图可以做你想要的(或只是where子句)时,我会阻止创建这样的表扩散。