********************************************** Cria a TRIGGER INSERT ********************************************** CREATE OR REPLACE TRIGGER atu_praca_cli_transp_ins AFTER INSERT ON vdp_prc_cli_trnp FOR EACH ROW DECLARE l_count NUMBER; BEGIN IF :NEW.empresa = ' ' THEN IF :NEW.estado IS NULL AND :NEW.praca IS NULL AND :NEW.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca IS NULL AND cod_cliente IS NULL AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NOT NULL AND :NEW.praca IS NOT NULL AND :NEW.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :NEW.estado AND cod_praca = :NEW.praca AND cod_cliente = :NEW.cliente AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NOT NULL AND :NEW.praca IS NULL AND :NEW.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :NEW.estado AND cod_praca IS NULL AND cod_cliente IS NULL AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NULL AND :NEW.praca IS NOT NULL AND :NEW.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca = :NEW.praca AND cod_cliente IS NULL AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NULL AND :NEW.praca IS NULL AND :NEW.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca IS NULL AND cod_cliente = :NEW.cliente AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NOT NULL AND :NEW.praca IS NOT NULL AND :NEW.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :NEW.estado AND cod_praca = :NEW.praca AND cod_cliente IS NULL AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NOT NULL AND :NEW.praca IS NULL AND :NEW.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :NEW.estado AND cod_praca IS NULL AND cod_cliente = :NEW.cliente AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NULL AND :NEW.praca IS NOT NULL AND :NEW.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca = :NEW.praca AND cod_cliente = :NEW.cliente AND cod_transpor = :NEW.transportadora; END IF; INSERT INTO praca_cli_transp (cod_uni_feder, cod_praca, cod_cliente, cod_transpor) VALUES (:NEW.estado, :NEW.praca, :NEW.cliente, :NEW.transportadora); END IF; END; / ********************************************** Cria a TRIGGER DELETE ********************************************** CREATE OR REPLACE TRIGGER atu_praca_cli_transp_del AFTER DELETE ON vdp_prc_cli_trnp FOR EACH ROW BEGIN IF :OLD.empresa = ' ' THEN IF :OLD.estado IS NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca IS NULL AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca = :OLD.praca AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca IS NULL AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca = :OLD.praca AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca IS NULL AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca = :OLD.praca AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca IS NULL AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca = :OLD.praca AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; END IF; END; / ********************************************** Cria a TRIGGER UPDATE. ********************************************** CREATE OR REPLACE TRIGGER atu_praca_cli_transp_upd AFTER UPDATE ON vdp_prc_cli_trnp FOR EACH ROW BEGIN IF :OLD.empresa = ' ' AND :NEW.empresa <> ' ' THEN IF :OLD.estado IS NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca IS NULL AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca = :OLD.praca AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca IS NULL AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca = :OLD.praca AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca IS NULL AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca = :OLD.praca AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca IS NULL AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca = :OLD.praca AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; END IF; IF :NEW.empresa = ' ' THEN --Excluir o registro antigo IF :OLD.estado IS NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca IS NULL AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca = :OLD.praca AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca IS NULL AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca = :OLD.praca AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca IS NULL AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca = :OLD.praca AND cod_cliente IS NULL AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NOT NULL AND :OLD.praca IS NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :OLD.estado AND cod_praca IS NULL AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; IF :OLD.estado IS NULL AND :OLD.praca IS NOT NULL AND :OLD.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca = :OLD.praca AND cod_cliente = :OLD.cliente AND cod_transpor = :OLD.transportadora; END IF; --Excluir o novo se existir IF :NEW.estado IS NULL AND :NEW.praca IS NULL AND :NEW.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca IS NULL AND cod_cliente IS NULL AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NOT NULL AND :NEW.praca IS NOT NULL AND :NEW.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :NEW.estado AND cod_praca = :NEW.praca AND cod_cliente = :NEW.cliente AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NOT NULL AND :NEW.praca IS NULL AND :NEW.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :NEW.estado AND cod_praca IS NULL AND cod_cliente IS NULL AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NULL AND :NEW.praca IS NOT NULL AND :NEW.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca = :NEW.praca AND cod_cliente IS NULL AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NULL AND :NEW.praca IS NULL AND :NEW.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca IS NULL AND cod_cliente = :NEW.cliente AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NOT NULL AND :NEW.praca IS NOT NULL AND :NEW.cliente IS NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :NEW.estado AND cod_praca = :NEW.praca AND cod_cliente IS NULL AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NOT NULL AND :NEW.praca IS NULL AND :NEW.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder = :NEW.estado AND cod_praca IS NULL AND cod_cliente = :NEW.cliente AND cod_transpor = :NEW.transportadora; END IF; IF :NEW.estado IS NULL AND :NEW.praca IS NOT NULL AND :NEW.cliente IS NOT NULL THEN DELETE FROM praca_cli_transp WHERE cod_uni_feder IS NULL AND cod_praca = :NEW.praca AND cod_cliente = :NEW.cliente AND cod_transpor = :NEW.transportadora; END IF; --Insere o registro novo INSERT INTO praca_cli_transp (cod_uni_feder, cod_praca, cod_cliente, cod_transpor) VALUES (:NEW.estado, :NEW.praca, :NEW.cliente, :NEW.transportadora); END IF; END; /