IF OBJECT_ID ('TRG_EST_PROD_NCM_CADASTRO_REPLICA_EST_PROD', 'TR') IS NOT NULL
BEGIN
	DROP TRIGGER TRG_EST_PROD_NCM_CADASTRO_REPLICA_EST_PROD
END
GO

CREATE TRIGGER [dbo].[TRG_EST_PROD_NCM_CADASTRO_REPLICA_EST_PROD] ON [dbo].[EST_PROD_NCM_CADASTRO] AFTER INSERT,UPDATE AS
BEGIN	

	SET NOCOUNT ON;

	BEGIN TRY

		 SELECT DISTINCT NR_NCM
		 INTO #NCM_PROCESSAR
		 FROM INSERTED NCM

		 DECLARE @NR_NCM_AUX VARCHAR(100)  -- Ajuste o tipo de dados conforme necessário
		 
		 DECLARE cursor_ncm CURSOR FOR
		 SELECT NR_NCM
		 FROM #NCM_PROCESSAR

		 OPEN cursor_ncm
		 
		 FETCH NEXT FROM cursor_ncm INTO @NR_NCM_AUX
		 
			 WHILE @@FETCH_STATUS = 0
			 BEGIN
		 		EXEC P_EST_PROD_REPLICA_NCM_CADASTRO @NR_NCM_UNICO = @NR_NCM_AUX
		 
		 		FETCH NEXT FROM cursor_ncm INTO @NR_NCM_AUX
			 END
		 
		 CLOSE cursor_ncm
		 DEALLOCATE cursor_ncm


	END TRY
	BEGIN CATCH
		PRINT 1
	END CATCH
END


