我想编写一个过程,其中收集月份数据并将其存储在表中。所以我想要做的是将作为参数传递的月份名称附加到表名称中。
--这是我写的程序。
CREATE procedure month_data(IN month varchar(20))
BEGIN
declare monthName varchar(255);
set @monthName := concat('CREATE table IF NOT EXISTS monthName_"',month,'"
select * from dup_activity_log where monthname(cur_date)="',month,'";');
prepare stmt from @monthName;
execute stmt;
END //
/* 我在此遇到运行时语法错误,但我不确定应该如何删除它。我对 MySQL 相当陌生,所以提前感谢您的帮助。 */
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '"June" select * from dup_activity_log where monthname(cur_date)="June"' at line 1
最佳答案
表名的一部分周围有一些不必要的双引号。表名和选择开头之间应该有一个空格。试试这个(''
被视为嵌入字符串中的单引号):
set @sql := concat(
'CREATE table IF NOT EXISTS monthName_',
month,
' select * from dup_activity_log where monthname(cur_date) = ''',
month,
''';'
);
关于mysql - 将参数附加到mysql中的表名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51239270/