Question : Why does this Oracle procedure can not update target table?

I have a test table called test_number like this:

select *
from test_numbers;

NUMBER1                NUMBER2                OPERATION            
---------------------- ---------------------- --------------------
3                      5                      Add                  
6                      3                      Divide              
6                      5                      Multiply            
5                      3                      Subtract            
6                      0                      Divide              
5                      1                      Add  

The task is to write a procedure that will check the test_numbers table for any rows that have Divide as the operation, and 0 as the second number. If it finds a row formatted this way, the procedure will change the operation (for that row only) to Divide  Invalid.

So I wrote the following procedure, but it did not update the 2nd from last record in test_numbers table when it is executed. Could any expert help me the cause? Thanks!

 
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
create or replace
PROCEDURE check_invalid_division As
 
tn_number2  test_numbers.number2%type;
tn_operation test_numbers.operation%type;
 
CURSOR crsr_test_numbers Is
select tn_number2 , tn_operation
from test_numbers
where tn_number2 = 0 AND upper( tn_operation ) = 'DIVIDE'
FOR UPDATE;
 
BEGIN
  for test_num_row in crsr_test_numbers loop
    update test_numbers
    set operation = 'Divide - Invalid'
    where current of crsr_test_numbers;
  end loop;
END;
Open in New Window Select All

Answer : Why does this Oracle procedure can not update target table?

dont use the variables in the your cursor :
you check on variables that have a current null -value in stead of checking the values in
the table: so your cursor will never select a record.

create or replace
PROCEDURE check_invalid_division As
CURSOR crsr_test_numbers Is
select number2 , operation
from test_numbers
where number2 = 0 AND upper( operation ) = 'DIVIDE'
FOR UPDATE;
 
BEGIN
  for test_num_row in crsr_test_numbers loop
    update test_numbers
    set operation = 'Divide - Invalid'
    where current of crsr_test_numbers;
  end loop;
END;
Random Solutions  
 
programming4us programming4us