Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Return cumulative from operation inside loop

Carl CFeb 6 2019 — edited Feb 7 2019

Hi guys,

I've this issue with this code.

  1. declare 
  2.  
  3. ret_val   int default 0; 
  4. x         int default 0; 
  5. y         int default 0; 
  6. cumul     int default 0; 
  7.  
  8. internal_counter int default 0; 
  9.  
  10. begin 
  11.  
  12.    <<counter_loop>> 
  13.    for counter in 1..5 loop 
  14.  
  15.       x := 10;
  16.       y := 15; 
  17.  
  18.       internal_counter := internal_counter + x; 
  19.  
  20.       ret_val := x + y; 
  21.       cumul := ret_val + x; 
  22.  
  23.       dbms_output.put_line('value ret_val X + Y :' 
  24.                            || to_char(ret_val) 
  25.                            || ' Value Cumul  ' 
  26.                            || to_char(cumul) 
  27.                            || ' Internal_Counter  : ' 
  28.                            || to_char(internal_counter) 
  29.                           ); 
  30.  
  31.    end loop counter_loop; 
  32.     
  33. end

In fact when I run thue code, I get the following results for each variable from the loop:

  1. value ret_val X + Y : 25 Value Cumul 35 Internal_Counter : 10 
  2. value ret_val X + Y : 25 Value Cumul 35 Internal_Counter : 20 
  3. value ret_val X + Y : 25 Value Cumul 35 Internal_Counter : 30 
  4. value ret_val X + Y : 25 Value Cumul 35 Internal_Counter : 40 
  5. value ret_val X + Y : 25 Value Cumul 35 Internal_Counter : 50 

and

What I need, is increment the result returned from the first loop into the second variable and get the cumulative, (e.g.)

(value return X + Y :'25' Value cumul ' 35 ' Counter  : ' 10)

(value return X + Y :'25' Value cumul ' 45 ' Counter  : ' 20)

(value return X + Y :'25' Value cumul ' 55 ' Counter  : ' 30)

(value return X + Y :'25' Value cumul ' 65 ' Counter  : ' 40)

(value return X + Y :'25' Value cumul ' 75 ' Counter  : ' 50)

Thank you in advance

Rgds

Message was edited by: Carl C Code updated with variables names.

Comments
Post Details
Added on Feb 6 2019
16 comments
501 views