Este insert pega informações de uma tabela e verifica se o COD_TRAT_CC existe nas tabelas TAB_CC e TABELA_TRATAMENTO, se o codigo existir em uma das duas tabelas e não deve inserir. Porém ele continua a inserir o codigo que já existe na tabela TAB_CC na TABELA_TRATAMENTO, o que eu estou fazendo de errado?
insert into TABELA_TRATAMENTO
select distinct '13',
COD_TRAT_CC,
W_DATA,
'I'
from CONTA_CC
where COD_TRAT_CC not in
(select COD_CC from TAB_CC) and
COD_TRAT_CC not in
(select COD_PROD from TABELA_TRATAMENTO where TAB = '13')
select
distinct '13'
, x.COD_TRAT_CC
, x.W_DATA
, 'I'
from CONTA_CC x
where NOT EXISTS (select 1
from TAB_CC
where COD_CC = x.COD_TRAT_CC)
and NOT EXISTS (select 1
from TABELA_TRATAMENTO
where COD_PROD = x.COD_TRAT_CC
AND TAB = '13')