MySQL Variables storing database name

后端 未结 1 1950
Happy的楠姐
Happy的楠姐 2021-01-15 03:39

I have a long script that I need to run on several different databases (all witht he same tables and field names).

What I would like to do is something like this:

相关标签:
1条回答
  • 2021-01-15 03:50

    It will work if you concat the entire query. Try:

    set @db = 'mydb';
    set @tble = 'table';
    
    set @query = concat('INSERT INTO tablefoo SELECT * FROM ', @db, '.', @tble);
    PREPARE stmt FROM @query;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
    
    0 讨论(0)
提交回复
热议问题