INSERT INTO PRC_FILIAL_CONFIG (CD_EMP
								,CD_FILIAL
								,CD_CHAVE
								,VALOR
								,CD_USU
								,DT_ULT_ALT
								,GRAVA_MINUSCULO)
SELECT FIL.CD_EMP, 
	   FIL.CD_FILIAL, 
	   'DT_INI_EXCLUSAO_ICMS_PIS_COFINS_NF_ENTRADA' AS CD_CHAVE, 
	   '2023-05-01' as  VALOR, 
	   1 AS CD_USU, 
	   GETDATE() AS DT_ULT_ALT, 
	   0 AS GRAVA_MINUSCULO
FROM PRC_FILIAL FIL
WHERE NOT EXISTS (SELECT 1 FROM PRC_FILIAL_CONFIG CFG
				  WHERE CFG.CD_FILIAL = FIL.CD_FILIAL
				  AND CFG.CD_CHAVE = 'DT_INI_EXCLUSAO_ICMS_PIS_COFINS_NF_ENTRADA')


UPDATE PRC_FILIAL_CONFIG
SET VALOR = '2023-05-01'
WHERE CD_CHAVE = 'DT_INI_EXCLUSAO_ICMS_PIS_COFINS_NF_ENTRADA'
AND VALOR <> '2023-05-01'