/****** Object:  Trigger [EST_CONTINGENCIA_NOTA_INICIAL_ATUALIZA]    Script Date: 08/24/2012 09:22:26 ******/
IF  EXISTS (SELECT * FROM sys.triggers WHERE object_id = OBJECT_ID(N'[dbo].[EST_CONTINGENCIA_NOTA_INICIAL_ATUALIZA]'))
DROP TRIGGER [dbo].[EST_CONTINGENCIA_NOTA_INICIAL_ATUALIZA]
GO

/****** Object:  Trigger [dbo].[EST_CONTINGENCIA_NOTA_INICIAL_ATUALIZA]    Script Date: 08/24/2012 09:22:26 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO



CREATE TRIGGER [dbo].[EST_CONTINGENCIA_NOTA_INICIAL_ATUALIZA] ON [dbo].[EST_CONTINGENCIA] 
FOR INSERT
AS
	UPDATE EST_CONTINGENCIA SET NOTA_INICIAL =(
	SELECT MAX(CD_NF) + 1 FROM EST_NF_SAI_ARQ_DANFE DANFE
							   INNER JOIN INSERTED ON DANFE.CD_EMP = INSERTED.CD_EMP AND DANFE.CD_FILIAL = INSERTED.CD_FILIAL)
	WHERE  EST_CONTINGENCIA.CD_CTR = (SELECT MAX(CD_CTR) FROM EST_CONTINGENCIA)
	



GO