title: MySQL loop through tables
body: <p>I want to output data from every table that contains a <code>first_name</code> column. I put together the following procedure, but in my loop, mysql interprets the table name literally instead of evaluating the variable <code>table_name</code>. What's the fix?</p>
  
  <pre><code>delimiter //
  
  drop procedure if exists hunt //
  create procedure hunt()
  begin
      DECLARE done int default false;
      DECLARE table_name CHAR(255);
  
      DECLARE cur1 cursor for SELECT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS 
          WHERE TABLE_SCHEMA = "wholesale_production" and COLUMN_NAME LIKE "%first%" ;
      DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
      open cur1;
  
      myloop: loop
          fetch cur1 into table_name;
          if done then
              leave myloop;
          end if;
          select * from `wholesale_production`.table_name where created_at &gt;= '2012-10-01';
      end loop;
  
      close cur1;
  end //
  
  delimiter ;
  
  call hunt();
  </code></pre>