[Oracle] - Manutenção de Triggers Duplicadas
Autor: Emerson S. Gaudencio | Publicado: 2016-07-13T10:41:00-04:00




Bom Pessoal,

&nbsp;Vou compartilhar um procedimento para fazer a exclusão de triggers duplicadas que são utilizadas para entregar id´s para cada registro inserido na tabela, segue abaixo:


begin
&nbsp; ----------------------------------------------------------
&nbsp; -- exclui triggers duplicadas -------
&nbsp; ----------------------------------------------------------
&nbsp; for rc1 in (select tta.*
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from (select t.TRIGGER_NAME,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;upper(t.TRIGGER_NAME) as nome_trg_upper,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count(1) over(partition by t.TABLE_NAME order by t.TRIGGER_TYPE) as num_trg,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;row_number() over(partition by t.TABLE_NAME order by t.TRIGGER_TYPE) as reg_trg,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.TRIGGER_TYPE,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.TRIGGERING_EVENT,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.TABLE_OWNER,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.base_object_type,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.TABLE_NAME,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.STATUS
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from user_triggers t
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where t.TRIGGER_TYPE = 'BEFORE EACH ROW' and t.TRIGGERING_EVENT = 'INSERT'
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) tta
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;order by 1 desc) loop
&nbsp; &nbsp; if rc1.num_trg &gt; 1 and rc1.reg_trg &gt; 1 then
&nbsp; &nbsp; &nbsp; execute immediate 'DROP TRIGGER ' || '"' || rc1.table_owner || '"."' ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rc1.trigger_name || '"';
&nbsp; &nbsp; end if;
&nbsp; end loop;
&nbsp; ----------------------------------------------------------
&nbsp; -- recompila triggers -------
&nbsp; ----------------------------------------------------------
&nbsp; for rc1 in (select tta.*
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from (select t.TRIGGER_NAME,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;upper(t.TRIGGER_NAME) as nome_trg_upper,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;count(1) over(partition by t.TABLE_NAME order by t.TRIGGER_TYPE) as num_trg,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;row_number() over(partition by t.TABLE_NAME order by t.TRIGGER_TYPE) as reg_trg,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.TRIGGER_TYPE,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.TRIGGERING_EVENT,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.TABLE_OWNER,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.base_object_type,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.TABLE_NAME,
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;t.STATUS
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; from user_triggers t
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; where t.TRIGGER_TYPE = 'BEFORE EACH ROW' and t.TRIGGERING_EVENT = 'INSERT'
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ) tta
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;order by 1 desc) loop
&nbsp; &nbsp; if rc1.num_trg = 1 and rc1.reg_trg = 1 then
&nbsp; &nbsp; &nbsp; execute immediate 'ALTER TRIGGER ' || '"' || rc1.table_owner || '"."' ||
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rc1.trigger_name || '" COMPILE';
&nbsp; &nbsp; end if;
&nbsp; end loop;
&nbsp; ----------------------------------------------------------
end;





Essa é uma dica simples, mais pode facilitar muito a vida de um DBA que encontre esse tipo de situação.



Qualquer dúvida sobre o post estou a disposição. Fiquem na Paz.