title: Mysql Triggers, declare a variable and use it as column name
body: <p>I have the table "mytable" that contains the "columnname" field wich is the name of a column in mytable2.</p>
  
  <p>I use this one for the selection:</p>
  
  <pre><code>SET @DptScn = (SELECT columnname FROM mytable WHERE tablename = 'CustomTableName' AND fieldlabel = 'CustomField');
  SET @identifiedid=144;
  </code></pre>
  
  <p>but, when I try:</p>
  
  <pre><code>SELECT @DptScn FROM mytable2 WHERE identifiedid = @identifiedid;
  </code></pre>
  
  <p>this give me NOT the content of the field but the name containted into variable @DptScn...</p>
  
  <p>Any advice?
  I can't use Prepared Statement because I'm in a Trigger...</p>
  
  <p>UPDATE:</p>
  
  <p>As suggested by spencer7593 I'm creating a procedure:</p>
  
  <pre><code>DROP PROCEDURE IF EXISTS p_t;
  DELIMITER $$
  CREATE PROCEDURE p_t (IN DptTcn VARCHAR(255), IN tid INT, OUT tT INT)
  BEGIN
      SET @DptTcn = DptTcn;
      SET @tid = tid;
      SET @sql = CONCAT('SELECT @DptTcn FROM mytable3 WHERE tid = @tid');
      PREPARE stmt FROM @sql;
      EXECUTE stmt;
  END$$
  DELIMITER ;
  </code></pre>
  
  <p>Then I try it:</p>
  
  <pre><code>SET @DptTcn = (SELECT columnname mytable WHERE tablename = 'CustomTableName' AND fieldlabel = 'CustomField');
  SET @identifiedid=145;
  CALL proc_ticket(@DptTcn, @identifiedid, @DptT);
  </code></pre>
  
  <p>But I receive a:</p>
  
  <pre><code>#2014 - Commands out of sync; you can't run this command now
  </code></pre>