title: MySQL set database name dynamically and select db.schematable
body: <p>I am creating a stored procedure, which takes a number of input parameters. I would like to be able to select which database to use, whereafter depending on the conditions as shown below, I would like to use the correct where conditions.</p>
  
  <p>I know you cannot use <strong>"USE"</strong> keyword in a stored proc and I have also tried to set @ds in the query but with not much success.</p>
  
  <p>I'm using Navicat and have defined my parameters in the IDE, my input params are :</p>
  
  <ul>
  <li>icustomername varchar(255)</li>
  <li>ipostcode varchar(255)</li>
  <li>iaccountnumber varchar(255)</li>
  <li>imemberdbname varchar(255)</li>
  </ul>
  
  <p><strong>SQL</strong></p>
  
  <pre><code>BEGIN
  declare dbName varchar(255); --attempt at using db name passed in
  select imemberdbname into dbName;
  set @ds = concat(dbName,'.customers'); -- I have also tried this
  
  if LENGTH(icustomername) &gt; 0 THEN
      (Select customerid, customername, postcode, accountnumber from dbName.customers
          WHERE customername = icustomername);
  ELSEIF len(ipostcode) &gt; 0 THEN
      (Select customerid, customername, postcode, accountnumber from dbName.customers
          WHERE postcode = ipostcode);
  ELSE 
      (Select customerid, customername, postcode, accountnumber from dbName.customers
          WHERE accountnumber = iaccountnumber);
  end if;
  
  END
  </code></pre>
  
  <p>I am unable to select the db.table in order to carry out the selects. Is there a way this is possible? or is there a better solution?</p>