title: How to write a mysql function with dynamic table name?
body: <p>I'm trying to write a my sql function doing the following things:
  1- get the table name used in join as a parameter.</p>
  
  <p>but I get mysql syntax error</p>
  
  <pre><code>1064 - 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 DETERMINISTIC 
    BEGIN select `r`.`id` AS `id`, (case ' at line 2
  </code></pre>
  
  <p>This is my query</p>
  
  <pre><code>DELIMITER $$
  CREATE FUNCTION getTranslation (tablename varchar(50),entity varchar(20),itemid int,lang char(3)) 
  RETURNS table
  DETERMINISTIC
  BEGIN 
  select 
      `r`.`id` AS `id`,
      (case
          when isnull(`t`.`descr`) then `r`.`descr_ml`
          else `t`.`descr`
      end) AS `descr`
  from
      (tablename `r`
      left join `g001_translation` `t` ON ((`t`.`item_id` = `r`.`id`)))
  END$$
  DELIMITER ;
  </code></pre>
  
  <p>I the select part works fine with static table name by the way.</p>