
IF NOT OBJECT_ID('EST_NF_ENT_DAR_PEND') IS NULL
	BEGIN
		IF OBJECT_ID('FK_EST_NF_ENT_DAR_PEND_EST_NF_ENT') IS NULL 
			BEGIN 
				ALTER TABLE [dbo].[EST_NF_ENT_DAR_PEND]  WITH CHECK ADD  CONSTRAINT [FK_EST_NF_ENT_DAR_PEND_EST_NF_ENT] FOREIGN KEY(CD_EMP, CD_FILIAL, CD_NF)
				REFERENCES [dbo].[EST_NF_ENT] (CD_EMP, CD_FILIAL, CD_NF)				
				ON DELETE CASCADE
				
				ALTER TABLE [dbo].[EST_NF_ENT_DAR_PEND] CHECK CONSTRAINT [FK_EST_NF_ENT_DAR_PEND_EST_NF_ENT]
			END 
	END
ELSE
	BEGIN
		RAISERROR ('Tabela EST_NF_ENT_DAR_PEND Nao existe nesta base de dados. Crie a tabela primeiro para depois executar este script',16, 1);  
		RETURN		
	END 
