title: Nested Loops In Oracle Not Work Correctly (Exceeds the maximum)
body: <p>I wrote These Two queries For Sample Table to insert into &quot;z_exp14_resualt&quot; From &quot;z_exp14_main &quot;. The <strong>First Query Works Correctly</strong> (Null) But Second That  For rows have due_date on the main table (Not Null), not works correctly! . I Think Problem is The loop.<br/> <strong>It started before the due date and even goes beyond that</strong> . For NOT Nulls Insert must start from today(year and monts from sysdate and day from opening_date) and continue until due_date</p>
  <p><strong>Calculate For Null DUE_DATES</strong></p>
  <pre><code>declare
    i number := 1;
  BEGIN
  for i in 1..12 loop
    insert into z_exp14_resualt 
    select dep_id,ADD_MONTHS(ADD_MONTHS(opening_date,trunc( months_between (sysdate,opening_date))),i),rate*balance 
    from z_exp14_main
    WHERE due_date IS  null;
  end loop;
  END;
  /
  </code></pre>
  <p><strong>And For Not Null Due_dates</strong></p>
  <pre><code>  DECLARE
      diff number;
      x number :=1;
      BEGIN    
      for i in (select * FROM z_exp14_main WHERE due_date IS NOT null) loop    
          diff :=trunc( months_between (sysdate,i.opening_date));
          
              WHILE (ADD_MONTHS(ADD_MONTHS(i.opening_date,diff),x)&lt;i.due_date) LOOP
                  insert into z_exp14_resualt 
                  select dep_id,ADD_MONTHS(ADD_MONTHS(i.opening_date,diff),x),rate*balance 
                  from z_exp14_main WHERE due_date is not null  ;
                  x :=x+1;
              end loop;
        end loop;
      end;
      /
  </code></pre>
  <p>sample Date On Main (z_exp14_main)</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>--</th>
  <th>DEP_ID</th>
  <th>DUE_DATE</th>
  <th>BALANCE</th>
  <th>RATE</th>
  <th>OPENING_DATE</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>--</td>
  <td>20056634</td>
  <td>null</td>
  <td>283428</td>
  <td>10</td>
  <td>15-SEP-16</td>
  </tr>
  <tr>
  <td>--</td>
  <td>20056637</td>
  <td>null</td>
  <td>180222</td>
  <td>10</td>
  <td>07-NOV-14</td>
  </tr>
  <tr>
  <td>--</td>
  <td>20056639</td>
  <td>null</td>
  <td>58741</td>
  <td>10</td>
  <td>28-AUG-14</td>
  </tr>
  <tr>
  <td>--</td>
  <td>40000020</td>
  <td>27-NOV-21</td>
  <td>5000000</td>
  <td>22</td>
  <td>31-MAR-14</td>
  </tr>
  <tr>
  <td>--</td>
  <td>40000023</td>
  <td>23-APR-21</td>
  <td>63000000</td>
  <td>22</td>
  <td>25-AUG-18</td>
  </tr>
  </tbody>
  </table>
  </div>