CREATE TABLE FCT_TIPOMOTIVOAGEND(
    NRTIPOMOTIVOAGEND NUMBER,
    NMTIPOMOTIVOAGEND VARCHAR2(500),
    NRORG             NUMBER 	      DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG') 		NOT NULL,
    IDATIVO           VARCHAR2(1)     DEFAULT 'S'					NOT NULL,
    DTINCLUSAO        DATE            DEFAULT SYSDATE,
    DTULTATU          DATE            DEFAULT SYSDATE,
    NRORGINCLUSAO     NUMBER          DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG')		NULL,
    CDOPERINCLUSAO    VARCHAR2(12)    DEFAULT '1' 					NULL,
    NRORGULTATU       NUMBER          DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG')		NULL,
    CDOPERULTATU      VARCHAR2(12)    DEFAULT '1',
        CONSTRAINT CKC_IDATIVO_FCT_TIPOMOTIVOGEND CHECK (IDATIVO IN ('S','N')),
        CONSTRAINT PK_FCT_TIPOMOTIVOAGEND PRIMARY KEY (NRTIPOMOTIVOAGEND)
)
/
INSERT INTO FCT_TIPOMOTIVOAGEND (NRTIPOMOTIVOAGEND, NMTIPOMOTIVOAGEND, CDOPERINCLUSAO) 
    VALUES (1, 'EFETIVO', '1')
/
INSERT INTO FCT_TIPOMOTIVOAGEND (NRTIPOMOTIVOAGEND, NMTIPOMOTIVOAGEND, CDOPERINCLUSAO) 
    VALUES (2, 'COBERTURA', '1')
/
INSERT INTO FCT_TIPOMOTIVOAGEND (NRTIPOMOTIVOAGEND, NMTIPOMOTIVOAGEND, CDOPERINCLUSAO) 
    VALUES (3, 'EXCEDENTE', '1')
/
INSERT INTO FCT_TIPOMOTIVOAGEND (NRTIPOMOTIVOAGEND, NMTIPOMOTIVOAGEND, CDOPERINCLUSAO) 
    VALUES (4, 'TREINAMENTO', '1')
/
INSERT INTO FCT_TIPOMOTIVOAGEND (NRTIPOMOTIVOAGEND, NMTIPOMOTIVOAGEND, CDOPERINCLUSAO) 
    VALUES (5, 'CORTESIA', '1')
/
CREATE TABLE OM_TPMOTIVOAGENDCOMISS(
    NRTPMOTIVOAGENDCOMISS    NUMBER,
    NRTIPOMOTIVOAGEND         NUMBER,
    NRORG             NUMBER 	      DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG') 		NOT NULL,
    IDATIVO           VARCHAR2(1)     DEFAULT 'S'					NOT NULL,
    DTINCLUSAO        DATE            DEFAULT SYSDATE,
    DTULTATU          DATE            DEFAULT SYSDATE,
    NRORGINCLUSAO     NUMBER          DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG')		NULL,
    CDOPERINCLUSAO    VARCHAR2(12)    DEFAULT '1' 					NULL,
    NRORGULTATU       NUMBER          DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG')		NULL,
    CDOPERULTATU      VARCHAR2(12)    DEFAULT '1',
        CONSTRAINT CKC_IDATIVO_OM_TPMTIVAGNDCOMIS CHECK (IDATIVO IN ('S','N')),
        CONSTRAINT PK_OM_TPMOTIVOAGENDCOMISS PRIMARY KEY (NRTPMOTIVOAGENDCOMISS)
)
/
ALTER TABLE OM_TPMOTIVOAGENDCOMISS 
  ADD CONSTRAINT FKCRM00097TPMTAGND_TPMTGNDOMIS FOREIGN KEY (NRTIPOMOTIVOAGEND)
      REFERENCES FCT_TIPOMOTIVOAGEND (NRTIPOMOTIVOAGEND)
/
CREATE INDEX CRM00097_FK ON DIVISAO(CDSEGMER)
/
CREATE TABLE DIVISAO (
    CDDIVISAO VARCHAR2(5) NOT NULL,
    CDSEGMER VARCHAR2(5) NOT NULL,
    DSDIVISAO VARCHAR2(40) NOT NULL,
    DTULTATUDIV DATE DEFAULT SYSDATE,
    NRORG             NUMBER 	      DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG') 		NOT NULL,
    IDATIVO           VARCHAR2(1)     DEFAULT 'S'					NOT NULL,
    DTINCLUSAO        DATE            DEFAULT SYSDATE,
    DTULTATU          DATE            DEFAULT SYSDATE,
    NRORGINCLUSAO     NUMBER          DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG')		NULL,
    CDOPERINCLUSAO    VARCHAR2(12)    DEFAULT '1' 					NULL,
    NRORGULTATU       NUMBER          DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG')		NULL,
    CDOPERULTATU      VARCHAR2(12)    DEFAULT '1',
        CONSTRAINT CKC_IDATIVO_DIVISAO CHECK (IDATIVO IN ('S','N')),
        CONSTRAINT PK_DIVISAO PRIMARY KEY (CDDIVISAO),
        CONSTRAINT FKCRM00098DIVISAO_SEGMERCADO FOREIGN KEY (CDSEGMER) 
            REFERENCES SEGMERCADO(CDSEGMER)
)
/
CREATE INDEX CRM00098_FK ON DIVISAO(CDSEGMER)
/
ALTER TABLE CRM_CANDIDATO ADD CDDIVISAO VARCHAR2(5)
/
ALTER TABLE CRM_CANDIDATO
  ADD CONSTRAINT FKCRM00099CANDIDATO_DIVISAO FOREIGN KEY (CDDIVISAO) 
        REFERENCES DIVISAO(CDDIVISAO)
/
CREATE INDEX CRM00099_FK ON CRM_CANDIDATO (CDDIVISAO)
/
ALTER TABLE PROSPECT ADD CDDIVISAO VARCHAR2(5)
/
ALTER TABLE PROSPECT
  ADD CONSTRAINT FKCRM000100PROSPECT_DIVISAO FOREIGN KEY (CDDIVISAO) 
        REFERENCES DIVISAO(CDDIVISAO)
/
CREATE INDEX CRM000100_FK ON PROSPECT (CDDIVISAO)
/
ALTER TABLE CLIENTE ADD CDDIVISAO VARCHAR2(5)
/
ALTER TABLE CLIENTE
  ADD CONSTRAINT FKCRM0001016CLIENTE_DIVISAO FOREIGN KEY (CDDIVISAO) 
        REFERENCES DIVISAO(CDDIVISAO)
/
CREATE INDEX CRM000101_FK ON CLIENTE (CDDIVISAO)