title: Cursor for loop in procedure with called function in Oracle
body: <p>The table is:</p>
  
  <pre><code>F TIME1                                                                       END_TIME                          
  - --------------------------------------------------------------------------- ----------------------
  C 16-NOV-16 09.45.32.000000 AM                                                17-NOV-16 09.45.32.000000 AM      
  A 16-NOV-16 10.14.54.000000 AM                                                16-NOV-16 11.14.54.000000 AM      
  A 16-NOV-16 10.14.56.000000 AM                                                16-NOV-16 11.14.56.000000 AM      
  </code></pre>
  
  <p>I have created a function..</p>
  
  <pre><code>CREATE OR REPLACE FUNCTION datediff
  (
  time1 TIMESTAMP
  , time2 TIMESTAMP
  )
  RETURN number
  as
  tot number;
  BEGIN
  SELECT(extract(DAY FROM time2-time1)*24*60*60)+
  (extract(HOUR FROM time2-time1)*60*60)
   into tot from tt ;
  
  RETURN tot;
  END;
  </code></pre>
  
  <p>I am then calling the function in procedure...</p>
  
  <pre><code>CREATE OR REPLACE PROCEDURE P1
  IS 
    CURSOR c1
     IS    
     select count(*) as cnt,time1,end_time
     from tt group by time1,end_time ;
  
    a number;
  BEGIN
    FOR i IN c1
    LOOP
      declare
        a number;
      BEGIN
        insert into y1 values(i.cnt,datediff(i.time1,i.end_time)) ;
  
        --display(i.cnt||'        '||a);
  
      /* EXCEPTION
           WHEN OTHERS THEN
              dbms_output.put_line('Error updating record ' || SUBSTR (SQLERRM, 1, 250));*/
      END;
  
    END LOOP;
  
  END P1;
  </code></pre>
  
  <p>The error I am getting is ...</p>
  
  <pre><code>SQL&gt; exec p1
  BEGIN p1; END;
  *
  ERROR at line 1:
  ORA-01422: exact fetch returns more than requested number of rows
  ORA-06512: at "ANU.DATEDIFF", line 11
  ORA-06512: at "ANU.P1", line 14
  ORA-06512: at line 1
  </code></pre>
  
  <p>This is working for single record in the table ,but not for multiple records..?  please guide..</p>