DELETE
	PG_CRED_HISTORICO_CPL
FROM
	PG_CRED_HISTORICO_CPL LEFT OUTER  JOIN PG_CRED ON
	PG_CRED_HISTORICO_CPL.CD_EMP = PG_CRED.CD_EMP AND
	PG_CRED_HISTORICO_CPL.CD_FILIAL = PG_CRED.CD_FILIAL AND
	PG_CRED_HISTORICO_CPL.CD_PG_CRED = PG_CRED.CD_PG_CRED 
WHERE
	PG_CRED.CD_PG_CRED  IS NULL
GO
IF  EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_PG_CRED_HISTORICO_CPL_PG_CRED]'))
begin
	ALTER TABLE [dbo].[PG_CRED_HISTORICO_CPL] DROP CONSTRAINT [FK_PG_CRED_HISTORICO_CPL_PG_CRED]
end
go
ALTER TABLE dbo.PG_CRED_HISTORICO_CPL ADD CONSTRAINT
	FK_PG_CRED_HISTORICO_CPL_PG_CRED FOREIGN KEY
	(
	CD_EMP,
	CD_FILIAL,
	CD_PG_CRED
	) REFERENCES dbo.PG_CRED
	(
	CD_EMP,
	CD_FILIAL,
	CD_PG_CRED
	) ON UPDATE  NO ACTION 
	 ON DELETE  NO ACTION 