IF OBJECT_ID('RC_DEB_ENVIO') IS NULL

BEGIN

    CREATE TABLE RC_DEB_ENVIO (
		CD_EMP INT NOT NULL,
		CD_FILIAL INT NOT NULL,
		CD_RC_DEB INT NOT NULL,
		CD_CNT INT NOT NULL

    CONSTRAINT PK_RC_DEB_ENVIO PRIMARY KEY CLUSTERED (CD_EMP ASC, CD_FILIAL ASC, CD_RC_DEB ASC));

    ALTER TABLE RC_DEB_ENVIO ADD CONSTRAINT FK_RC_DEB_ENVIO_RC_DEB FOREIGN KEY (CD_EMP, CD_FILIAL, CD_RC_DEB) 
                                                             REFERENCES RC_DEB (CD_EMP, CD_FILIAL, CD_RC_DEB)
    
    ALTER TABLE RC_DEB_ENVIO ADD CONSTRAINT FK_RC_DEB_ENVIO_BC_CONTAS FOREIGN KEY (CD_EMP, CD_CNT) 
                                                             REFERENCES BC_CONTAS (CD_EMP, CD_CNT)
END;