CREATE TABLE CRM_ORGANIZACAO (
  CRM_NRORGTRAB     NUMBER 			NOT NULL,
  NMORGTRAB         VARCHAR2(100),
  DSORG             VARCHAR2(450),
  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_CRM_ORGANIZACAO CHECK (IDATIVO IN ('S','N')),
	CONSTRAINT PK_CRM_ORGANIZACAO PRIMARY KEY (NRORG, CRM_NRORGTRAB)
)
/

CREATE TABLE CRM_STATUSPARC (
  NRPARCNEGOCIO     NUMBER 			NOT NULL,
  NRPARCRESP        NUMBER,
  CDOPERRESP        VARCHAR2(12),
  STATUSPARC        VARCHAR2(30) 	NOT NULL,
  CRM_NRORGTRAB     NUMBER 			NOT NULL,
  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_CRM_STATUSPARC CHECK (IDATIVO IN ('S','N')),
	CONSTRAINT PK_STATUSPARC PRIMARY KEY (NRPARCNEGOCIO,NRORG,CRM_NRORGTRAB)
)
/

ALTER TABLE CRM_STATUSPARC ADD CONSTRAINT FKCRM00093STATUSPARC_ORG FOREIGN KEY (NRORG, CRM_NRORGTRAB) REFERENCES CRM_ORGANIZACAO(NRORG, CRM_NRORGTRAB)
/

CREATE INDEX CRM00093_FK ON CRM_STATUSPARC (NRORG ASC, CRM_NRORGTRAB ASC)
/

CREATE TABLE CRM_ORGREL (
  CDOPERADOR        VARCHAR2(12)	NOT NULL,
  CRM_NRORGTRAB     NUMBER(4) 		NOT NULL,
  NMOPERADOR        VARCHAR2(40),
  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_CRM_ORGREL CHECK (IDATIVO IN ('S','N')),
	CONSTRAINT PK_ORGREL PRIMARY KEY (CDOPERADOR,NRORG,CRM_NRORGTRAB)
)
/

ALTER TABLE CRM_ORGREL ADD CONSTRAINT FKCRM00094ORGREL_ORG FOREIGN KEY (NRORG, CRM_NRORGTRAB) REFERENCES CRM_ORGANIZACAO(NRORG, CRM_NRORGTRAB)
/

CREATE INDEX CRM00094_FK ON CRM_ORGREL (NRORG ASC, CRM_NRORGTRAB ASC)
/

ALTER TABLE OPERADOR ADD CRM_NRORGTRAB NUMBER(4)
/

ALTER TABLE EVOLCONT ADD CRM_NRORGTRAB NUMBER(4)
/

ALTER TABLE CRM_OPORTUNI ADD CRM_NRORGTRAB NUMBER(4)
/

ALTER TABLE CRM_ITENSOPORTUNI ADD CRM_NRORGTRAB NUMBER(4)
/

ALTER TABLE FILACONTATO ADD CRM_NRORGTRAB NUMBER(4)
/