mysql (5.1) > create table with name from a variable

前端 未结 2 1028
庸人自扰
庸人自扰 2021-01-01 23:28

I\'m trying to create a table with a name based on the current year and month(2011-09), but MySQL doesn\'t seem to like this.

SET @yyyy_mm=Year(         


        
相关标签:
2条回答
  • 2021-01-01 23:39

    You should be able to do something like this:

    SET @yyyy_mm=DATE_FORMAT(now(),'%Y-%m');
    SET @c = CONCAT('CREATE TABLE `survey`.`',@yyyy_mm, '` LIKE `survey`.`interim`');
    PREPARE stmt from @c;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    0 讨论(0)
  • 2021-01-01 23:42
    set @yyyy_mm=concat(year(now()),'-',month(now()));
    set @str = concat('create table survery.`', @yyyy_mm,'` like survey.interim;');
    prepare stmt from @str;
    execute stmt;
    deallocate prepare stmt;
    
    0 讨论(0)
提交回复
热议问题