title: Execute SQL Statement stored in table column
body: <p>I have insert statement in DB2 database table column and i want to execute that in store procedure</p>
  
  <p>Table:</p>
  
  <pre><code>TB_SQL
  (
  DATA_SQL VARCHAR(12000)
  )
  </code></pre>
  
  <p>DATA_SQL column contains INSERT statements. for example :<br>
  SELECT * FROM TB_SQL</p>
  
  <pre><code>INSERT INTO ADDRESS (COL1, COL2) 
  SELECT COL1, COL2 FROM DEMO WHERE TYPE='ADDRESS'
  </code></pre>
  
  <p>How to PREPARE and EXECUTE the INSERT STATEMENT from DATA_SQL Column in DB2 Store Procedure ?</p>
  
  <p>Code I tried: </p>
  
  <pre><code>SET v_SQL=
          'SET ?=(
                      SELECT 
                      DATA_SQL
                      FROM TB_SQL
                      WHERE TBNAME='''||v_TBNAME||'''
                 )
          ';
          PREPARE SQL_QUERY FROM v_SQL;
          EXECUTE SQL_QUERY ;
  </code></pre>
  
  <p>In reality the above PREPARE AND EXECUTE will only prepare SELECT statement and execute SELECT statement. 
  What i want is, I want to execute the statement that comes as output from SELECT statement. </p>
  
  <p>I also have below option to store the INSERT statement again in another variable</p>
  
  <pre><code>SET v_SQL=
          'SET ?=(
                      SELECT 
                      DATA_SQL
                      FROM TB_SQL
                      WHERE TBNAME='''||v_TBNAME||'''
                 )
          ';
          PREPARE SQL_QUERY FROM v_SQL;
          EXECUTE SQL_QUERY INTO v_INSERT_STATEMENT;
          PREPARE SQL_INSERT FROM v_INSERT_STATEMENT;
          EXECUTE SQL_INSERT;
  
  </code></pre>
  
  <p>I believe now i have insert statement stored in <code>v_INSERT_STATEMENT</code>
  And again prepared SQL from variable and then executed. But this is not working. </p>