title: MySQL Variables storing database name
body: <p>I have a long script that I need to run on several different databases (all witht he same tables and field names).</p>
  
  <p>What I would like to do is something like this:</p>
  
  <pre><code>1  SET @TARGET_DATABASE = 'beta'
  2  SET @SOURCE_DATABASE = 'sandbox';
  3  
  4  CREATE DATABASE IF NOT EXISTS @TARGET_DATABASE;
  5  USE @TARGET_DATABASE;
  
  ...
  
  10 INSERT INTO `tableFoo` SELECT * FROM @SOURCE_DATABASE.`tableFoo`;
  </code></pre>
  
  <p>On line 10 I get an error (I'm not surprised): "Script line: 10 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 '@SOURCE_DATABASE.<code>tableFoo</code>' at line..."</p>
  
  <p>So I tried this insted for line 10:</p>
  
  <pre><code>10 SET @TABLE=CONCAT('`',@SOURCE_DATABASE,'`','.`tableFoo`');
  11 INSERT INTO `tableFoo` SELECT * FROM @TABLE;
  </code></pre>
  
  <p>...and again error "Script line: 11 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 '@TABLE' at line..." As you can see, line 10 works fine...</p>
  
  <p>Is there any way to refer to a table in a database with a variable?</p>
  
  <p>// Thank you.</p>