title: How to create or execute a Stored Procedure within another Stored Procedure in IBM DB2?
body: <p>How to create a Dynamic Stored Procedure using a Stored Procedure in IBM DB2 9.7 for Windows ?</p>
  
  <p>Example below is my dynamic generated stored procedure:</p>
  
  <pre><code>CREATE PROCEDURE DB2ADMIN.INSERT_A (inputVar1 Integer, inputVar2 Integer) 
  LANGUAGE SQL
  BEGIN
  INSERT INTO DB2ADMIN.TABLE_A (var1, var2) VALUES (inputVar1, inputVar2);-- 
  END;
  </code></pre>
  
  <p>Then I have the execute statement stored procedure:</p>
  
  <pre><code>CREATE PROCEDURE ExecuteScript
  (
      inputStmt VARCHAR(4000)
  )
  LANGUAGE SQL
  BEGIN   
      DECLARE stmt VARCHAR(4000);--
      SET stmt = inputStmt;--
  
      PREPARE rs_stmt FROM stmt; --
      EXECUTE rs_stmt;--
  END;
  </code></pre>
  
  <p>Finally I call the stored procedure like this:</p>
  
  <pre><code>CALL ExecuteScript('CREATE PROCEDURE DB2ADMIN.INSERT_A (inputVar1 Integer, inputVar2 Integer) 
  LANGUAGE SQL
  BEGIN
  INSERT INTO DB2ADMIN.TABLE_A (var1, var2) VALUES (inputVar1, inputVar2);-- 
  END;')
  </code></pre>
  
  <p>I got the following error when I call it in DB2 Control Center and also JAVA JDBC:</p>
  
  <pre><code>com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-104, SQLSTATE=42601, SQLERRMC=LANGUAGE;LANGUAGE SQL;IS, AS, DRIVER=4.14.122
  </code></pre>
  
  <p>Anyway is it possible to create a stored procedure within a stored procedure? Or just create it using JDBC for IBM DB2? Thanks.</p>