Question

I am currently working on an application using Oracle Apex which is being used as a database to keep a record of cabling.

When components in the database are deleted, a PL/SQL trigger is executed which deletes any associations to the components from another table (PORT) and sets the port references to 0.

The trigger seems to work perfectly for the majority of components I attempt to delete, but presents me with a 'numeric or value error' for others and I am struggling to find out why (as shown below)

error

The trigger being executed looks like this:

create or replace trigger "COMPONENT_TRIGPORT" 
AFTER delete 
    on "COMPONENT"
    for each row
declare    
    TYPE portType IS TABLE OF PORT%ROWTYPE; 
    port_arr portType;
begin
    DELETE FROM PORT
    where PORT.COMPONENT_ID = :old.COMPONENT_ID
    RETURNING PORT_ID, PORT_NUMBER, COMPONENT_ID, CONNECTOR_ID, PORT_TYPE
    BULK COLLECT INTO port_arr;
    begin
        FOR i IN port_arr.FIRST .. port_arr.LAST
        LOOP
            update PORT
            set PORT.CONNECTOR_ID = 0
            where PORT.CONNECTOR_ID = port_arr(i).CONNECTOR_ID;
        END LOOP;
    end;
end;​

I know that the line giving me the error is

where PORT.COMPONENT_ID = :old.COMPONENT_ID

But I cannot work out why it works fine in some cases but not in others!

Was it helpful?

Solution

Replace

FOR i IN port_arr.FIRST .. port_arr.LAST

with:

FOR i IN 1 .. port_arr.COUNT

If no records are deleted the array will not be initialized and port_arr.FIRST and port_arr.LAST will return null, which will raise the exception ORA-06502: PL/SQL: numeric or value error. COUNT is always safe to use, even on a collection that has not been initialized.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top