Tengo un disparador en una base de datos Oracle armado de la siguiente forma:
CREATE OR REPLACE TRIGGER TRG_TELEPHONE_UD
BEFORE INSERT OR DELETE OR UPDATE
OF area_code, telephone_number, extention, telephone_type, address_type, line_state, country_code
ON TELEPHONE
FOR EACH ROW
DECLARE
.
.
ln_telephone_count NUMBER;
.
.
CURSOR cursor_telephone_count IS
SELECT COUNT(*)
INTO ln_telephone_count
FROM TELEPHONE t
WHERE t.telephone_number = :NEW.telephone_number
AND t.identity_code= :NEW.identity_code
AND t.customer_id = :NEW.customer_id;
.
.
BEGIN
IF DELETING THEN
.
.
.
ELSE
OPEN cursor_telephone_count;
FETCH cursor_telephone_count INTO ln_telephone_count;
.
.
.
.
IF updating AND ((:OLD.area_code <> :NEW.area_code) OR
(:OLD.telephone_number <> :NEW.telephone_number) OR
(:OLD.extention <> :NEW.extention) OR
(:OLD.telephone_type <> :NEW.telephone_type) OR
(:OLD.address_type <> :NEW.address_type) OR
(:OLD.line_state <> :NEW.line_state) OR
(:OLD.country_code <> :NEW.country_code)) THEN
.
.
.
IF ((:OLD.line_state <> :NEW.line_state) AND
(ln_telephone_count >= 1) AND
(:NEW.line_state='OK')) THEN
:NEW.line_state := 'INVALID';
END IF;
END IF;
IF inserting THEN
IF (lv_profile NOT IN ('DEFAULT', 'OPERATIVE')
OR lv_profile IS NULL) THEN
:NEW.update_from := 'O';
ELSE
:NEW.update_from := 'B';
END IF;
IF (ln_telephone_count >= 1) AND
(:NEW.line_state='OK') THEN
:NEW.line_state := 'INVALID';
END IF;
END IF;
END IF;
EXCEPTION
WHEN OTHERS THEN
.
.
.
END;
los puntos son mas líneas de código,Las obvie al no ser relevantes, aclaro que no hay error de ejecución.
Básicamente si existe uno o mas de un registro en teléfono con line_state='OK' el debería antes de insertar y actualizar igualarlo a line_state='invalid'. Me funciona antes de insertar, es decir actualiza el estado a invalido si pongo ok, pero al actualizar no lo hace, y en base me quedan dos teléfonos en estado ok. Estoy realizando la validación mal?.
He realizado pruebas y me doy cuenta que al hacer un update si tomo un cursor no me entra a las validaciones de abajo ponga lo que ponga.
IF updating AND ((:OLD.area_code <> :NEW.area_code) OR
(:OLD.telephone_number <> :NEW.telephone_number) OR
(:OLD.extention <> :NEW.extention) OR
(:OLD.telephone_type <> :NEW.telephone_type) OR
(:OLD.address_type <> :NEW.address_type) OR
(:OLD.line_state <> :NEW.line_state) OR
(:OLD.country_code <> :NEW.country_code)) THEN
IF (lv_profile NOT IN ('DEFAULT', 'OPERATIVE')
OR lv_profile IS NULL) THEN
:NEW.update_from := 'O';
ELSE
:NEW.update_from := 'B';
END IF;
--OPEN cursor_telephone_count_update;
--FETCH cursor_telephone_count_update INTO ln_telephone_count_update;
--CLOSE cursor_telephone_count_update;
IF --(ln_telephone_count_update >= 1) AND
(:NEW.line_state='OK') THEN
:NEW.line_state := 'INVALID';
END IF;
END IF;
IF inserting THEN
OPEN cursor_telephone_count;
FETCH cursor_telephone_count INTO ln_telephone_count;
CLOSE cursor_telephone_count;
IF (ln_telephone_count >= 1) AND
(:NEW.line_state='OK') THEN
:NEW.line_state := 'INVALID';
END IF;
IF (lv_profile NOT IN ('DEFAULT', 'OPERATIVE')
OR lv_profile IS NULL) THEN
:NEW.update_from := 'O';
ELSE
:NEW.update_from := 'B';
END IF;
END IF;
Si las lineas en if updating las desdocumento, nunca entra a la validacion de abajo, cosa que si entra si no hago el proceso de abrir el cursor. el query del cursor_telephone_count_update es el mismo de cursor_telephone_count.
CURSOR cursor_telephone_count IS
SELECT COUNT(*)
INTO ln_telephone_count
FROM TELEPHONE t
WHERE t.telephone_number = :NEW.telephone_number
AND t.identity_code= :NEW.identity_code
AND t.customer_id = :NEW.customer_id;
la razon de separarlos fue para validar si el query debia ser diferente en update, reemplaze los NEW por OLD pero no funciono.
WHEN others
que se trague un ORA-04091?