title: How to insert into a table using stored procedures in DB2 using SQL?
body: <p>I wanted to know how to insert into a table using stored procedures in DB2 using SQL.
  I have created a table as follows:</p>
  
  <pre><code>create table ADCLIBT.Itest
  (ITNBR CHAR(15) CCSID 65535 NOT NULL DEFAULT '');
  </code></pre>
  
  <p>This table contains a list of items. Using this list I want to insert various other fields to another table. But, just for example sake, let's say I just want to insert these values one by one using cursors.
  I have written the stored procedure as follows:</p>
  
  <pre><code>create procedure ADCLIBT.itest1()
      LANGUAGE SQL
  BEGIN
      DECLARE itemno char(15);
      DECLARE END_TABLE INT DEFAULT 0;
      DECLARE not_found CONDITION FOR SQLSTATE '20000';
      DECLARE c CURSOR FOR 
          select ITNBR from ADCLIBT.ITEMAT;
      DECLARE CONTINUE HANDLER FOR not_found
      SET END_TABLE = 1;
  
      open c;
      fetch from c into itemno;
      WHILE END_TABLE = 0 DO
          insert into ADCLIBT.ITEST
                  (ITNBR)
                  values
          (select a.ITNBR from ADCLIBT.ITEMAT a where ITNBR=itemno GROUP BY a.ITNBR);
      END WHILE;
      Close c;
  END;
  </code></pre>
  
  <p>This is giving me an infinite loop. Can anyone please tell me how do I stop the infinite loop and insert these records. I want to use the cursor because I want to further use <code>itemno</code> to compare and get single results.</p>