CREATE TABLE CRM_FUSAO_PARC
(
    NRSEQFUSAO      NUMBER                 NOT NULL,
    CDOPERDESFUSAO  VARCHAR2(12)           NULL    ,
    DTDESFUSAO      DATE                   NULL    ,
    NRPARCORI       NUMBER                 NOT NULL,
    NRPARCDEST      NUMBER                 NOT NULL,
    NRORG           NUMBER                 DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    DTINCLUSAO      DATE                   DEFAULT SYSDATE NOT NULL,
    DTULTATU        DATE                   NULL    ,
    NRORGINCLUSAO   NUMBER                 DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERINCLUSAO  VARCHAR2(12)           DEFAULT '1' NOT NULL,
    NRORGULTATU     NUMBER                 DEFAULT SYS_CONTEXT('nrorg_ctx','nrorg') NOT NULL,
    CDOPERULTATU    VARCHAR2(12)           DEFAULT '1' NOT NULL,
    IDATIVO         VARCHAR2(1)            DEFAULT 'S' NOT NULL
        CONSTRAINT CKC_IDATIVOCRMFUSPARC CHECK (IDATIVO IN ('S','N')),
    CONSTRAINT CRM_FUSAO_PARC_PK PRIMARY KEY (NRSEQFUSAO)
)
/

ALTER TABLE CONTATO ADD NRPARCORIGEM NUMBER
/

CREATE OR REPLACE TRIGGER TG_INSERE_PARC_ORI_CONTATO
BEFORE INSERT
ON CONTATO
REFERENCING NEW AS NEW 
FOR EACH ROW
BEGIN
  :NEW.NRPARCORIGEM := :NEW.NRPARCNEGOCIO;
END;
/

UPDATE CONTATO
SET NRPARCORIGEM = NRPARCNEGOCIO
WHERE NRPARCORIGEM IS NULL
/

ALTER TABLE CRM_CANDIDATO ADD NRPARCORIGEM NUMBER
/

CREATE OR REPLACE TRIGGER TG_INSERE_PARC_ORI_LEAD
BEFORE INSERT
ON CRM_CANDIDATO
REFERENCING NEW AS NEW 
FOR EACH ROW
BEGIN
  :NEW.NRPARCORIGEM := :NEW.NRPARCNEGOCIO;
END;
/

UPDATE CRM_CANDIDATO
SET NRPARCORIGEM = NRPARCNEGOCIO
WHERE NRPARCORIGEM IS NULL
/

ALTER TABLE CRM_OPORTUNI ADD NRPARCORIGEM NUMBER
/

CREATE OR REPLACE TRIGGER TG_INSERE_PARC_ORI_OPORTUNI
BEFORE INSERT
ON CRM_OPORTUNI
REFERENCING NEW AS NEW 
FOR EACH ROW
BEGIN
  :NEW.NRPARCORIGEM := :NEW.NRPARCOPORT;
END;
/

UPDATE CRM_OPORTUNI
SET NRPARCORIGEM = NRPARCOPORT
WHERE NRPARCORIGEM IS NULL
/

ALTER TABLE FILACONTATO ADD NRPARCORIGEM NUMBER
/

CREATE OR REPLACE TRIGGER TG_INSERE_PARC_ORI_FLCONTATO
BEFORE INSERT
ON FILACONTATO
REFERENCING NEW AS NEW 
FOR EACH ROW
BEGIN
  :NEW.NRPARCORIGEM := :NEW.NRPARCNEGOCIO;
END;
/

UPDATE FILACONTATO
SET NRPARCORIGEM = NRPARCNEGOCIO
WHERE NRPARCORIGEM IS NULL
/

ALTER TABLE INTEPROSP ADD CDPROSP_ORIGEM VARCHAR2(21)
/

CREATE OR REPLACE TRIGGER TG_INSERE_PARC_ORI_INTEPROSP
BEFORE INSERT
ON INTEPROSP
REFERENCING NEW AS NEW 
FOR EACH ROW
BEGIN
  :NEW.CDPROSP_ORIGEM := :NEW.CDPROSPECT;
END;
/

UPDATE INTEPROSP
SET CDPROSP_ORIGEM = CDPROSPECT
WHERE CDPROSP_ORIGEM IS NULL
/

ALTER TABLE PARCERIA ADD NRPARCORIGEM NUMBER
/

CREATE OR REPLACE TRIGGER TG_INSERE_PARC_ORI_PARCERIA
BEFORE INSERT
ON PARCERIA
REFERENCING NEW AS NEW 
FOR EACH ROW
BEGIN
  :NEW.NRPARCORIGEM := :NEW.NRPARCNEGOCIO;
END;
/

UPDATE PARCERIA
SET NRPARCORIGEM = NRPARCNEGOCIO
WHERE NRPARCORIGEM IS NULL
/

ALTER TABLE PLANOCUPACAO ADD NRPARCORIGEM NUMBER
/

CREATE OR REPLACE TRIGGER TG_INSERE_PARC_ORI_PLOCUPACAO
BEFORE INSERT
ON PLANOCUPACAO
REFERENCING NEW AS NEW 
FOR EACH ROW
BEGIN
  :NEW.NRPARCORIGEM := :NEW.NRPARCNEGOCIO;
END;
/

UPDATE PLANOCUPACAO
SET NRPARCORIGEM = NRPARCNEGOCIO
WHERE NRPARCORIGEM IS NULL
/

ALTER TABLE PROSPECT ADD NRPARCORIGEM NUMBER
/

CREATE OR REPLACE TRIGGER TG_INSERE_PARC_ORI_PROSPECT
BEFORE INSERT
ON PROSPECT
REFERENCING NEW AS NEW 
FOR EACH ROW
BEGIN
  :NEW.NRPARCORIGEM := :NEW.NRPARCNEGOCIO;
END;
/

UPDATE PROSPECT
SET NRPARCORIGEM = NRPARCNEGOCIO
WHERE NRPARCORIGEM IS NULL
/

ALTER TABLE RELACIONAPARC ADD NRPARCORIGEM NUMBER
/

CREATE OR REPLACE TRIGGER TG_INSERE_PARC_ORI_RELPARC
BEFORE INSERT
ON RELACIONAPARC
REFERENCING NEW AS NEW 
FOR EACH ROW
BEGIN
  :NEW.NRPARCORIGEM := :NEW.NRPARCNEGOCIO;
END;
/

UPDATE RELACIONAPARC
SET NRPARCORIGEM = NRPARCNEGOCIO
WHERE NRPARCORIGEM IS NULL