  
--eseguire come utente DBCGO


--DIRECTORY
CREATE OR REPLACE DIRECTORY DBCGO_DBM_PATH AS '/mnpapp/dbcgo/flussi/dbm';
CREATE OR REPLACE DIRECTORY DBCGO_LOG_PATH AS '/mnpapp/dbcgo/log/script';

--SEQUENCE
CREATE SEQUENCE SEQ_LOV NOCACHE;
CREATE SEQUENCE SEQ_MNP_PRIORITY_REQUEST NOCACHE;


--TABLE
-------------------------------------------------------------------------
CREATE TABLE DBCGO_GESTIONE_RICHIESTA
(
  ID_RICHIESTA                VARCHAR2(23 BYTE) NOT NULL,
  STATO                       NUMBER(2)         NOT NULL,
  TIPO_CLIENTE                VARCHAR2(2 BYTE),
  DATA_CUT_OVER_CALC          DATE              NOT NULL,
  DATA_VALIDAZIONE_MAX        DATE              NOT NULL,
  DATARICEZIONERICHIESTA      DATE              NOT NULL,
  DATA_CUT_OVER               DATE,
  CODICERIFIUTOVALIDAZIONE    NUMBER(2),
  CODICE_OPERATORE_RECIPIENT  VARCHAR2(4 BYTE)  NOT NULL,
  MSISDN                      VARCHAR2(15 BYTE) NOT NULL,
  CODICE_FISCALE_PARTITA_IVA  VARCHAR2(16 BYTE),
  CODICE_PRE_POST_PAGATO      VARCHAR2(3 BYTE)  NOT NULL,
  NOME_CLIENTE                VARCHAR2(30 BYTE),
  COGNOME_CLIENTE             VARCHAR2(50 BYTE),
  RAGIONE_SOCIALE             VARCHAR2(80 BYTE),
  RICHIESTAADHOCAOM           VARCHAR2(1 BYTE)  DEFAULT '0',
  RICHIESTAADHOC              VARCHAR2(1 BYTE)  DEFAULT '0',
  MARCAGGIO                   VARCHAR2(2 BYTE),
  SOTTOMARCAGGIO              VARCHAR2(2 BYTE),
  ID_PRIORITA                 NUMBER(2)         DEFAULT 0,
  INSERT_DATE                 DATE,
  UPDATE_DATE                 DATE,
  CODICE_GRUPPO               VARCHAR2(12 BYTE),
  ICCD_SERIAL_NUMBER          VARCHAR2(30 BYTE),
  IMSI                        VARCHAR2(15 BYTE),
  IPOTESI_RIFIUTO             NUMBER(2),
  NUMERO_DOCUMENTO            VARCHAR2(30 BYTE) NOT NULL,
  TIPO_DOCUMENTO              VARCHAR2(3 BYTE)  NOT NULL,
  TIMESTAMP                   TIMESTAMP(6)      NOT NULL,
  DATA_TRANSIZIONE_STATO      DATE              NOT NULL,
  NOME_OT_GEO                 VARCHAR2(5 BYTE),
  NOME_OT_LEG                 VARCHAR2(5 BYTE),
  ID_PRIORITA_MNP             NUMBER(2)         DEFAULT 0
)
TABLESPACE DBCGO_DATA_BIG;

CREATE INDEX IDX00_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(STATO)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX01_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(TIPO_CLIENTE)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX02_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(DATA_CUT_OVER_CALC)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX03_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(DATA_VALIDAZIONE_MAX)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX04_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(DATARICEZIONERICHIESTA)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX05_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(CODICE_OPERATORE_RECIPIENT)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX06_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(CODICE_GRUPPO)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX07_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(MSISDN)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX08_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(CODICE_FISCALE_PARTITA_IVA)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX09_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(NOME_OT_LEG)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX10_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(NOME_OT_GEO)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX11_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(MARCAGGIO)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX12_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(SOTTOMARCAGGIO)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX13_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(ID_PRIORITA)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE UNIQUE INDEX PK_DBCGO_GESTIONE_RICHIESTA ON DBCGO_GESTIONE_RICHIESTA
(ID_RICHIESTA)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

ALTER TABLE DBCGO_GESTIONE_RICHIESTA ADD (
  CONSTRAINT PK_DBCGO_GESTIONE_RICHIESTA PRIMARY KEY (ID_RICHIESTA)
    USING INDEX 
    TABLESPACE DBCGO_IDX_BIG);
    
-------------------------------------------------------------------------
CREATE TABLE DBCGO_GESTIONE_RICHIESTA_REC
(
  ID_RICHIESTA                VARCHAR2(23 BYTE) NOT NULL,
  STATO                       NUMBER(2)         NOT NULL,
  TIPO_CLIENTE                VARCHAR2(2 BYTE)  NOT NULL,
  DATA_CUT_OVER_CALC          DATE              NOT NULL,
  DATA_VALIDAZIONE_MAX        DATE,
  DATARICEZIONERICHIESTA      DATE              NOT NULL,
  DATA_CUT_OVER               DATE,
  CODICE_OPERATORE_DONATING   VARCHAR2(4 BYTE)  NOT NULL,
  MSISDN                      VARCHAR2(15 BYTE) NOT NULL,
  CODICE_FISCALE_PARTITA_IVA  VARCHAR2(16 BYTE),
  CODICE_PRE_POST_PAGATO      VARCHAR2(3 BYTE)  NOT NULL,
  NOME_CLIENTE                VARCHAR2(30 BYTE),
  COGNOME_CLIENTE             VARCHAR2(50 BYTE),
  RAGIONE_SOCIALE             VARCHAR2(80 BYTE),
  CAUSALE_ACCODAMENTO         VARCHAR2(100 BYTE),
  RICHIESTAADHOC              NUMBER(1)         DEFAULT 0                     NOT NULL,
  RICHIESTAADHOCAOM           NUMBER(1)         DEFAULT 0                     NOT NULL,
  ID_PRIORITA                 NUMBER(2)         DEFAULT 0,
  INSERT_DATE                 DATE,
  UPDATE_DATE                 DATE,
  ICCD_SERIAL_NUMBER          VARCHAR2(30 BYTE),
  IMSI                        VARCHAR2(15 BYTE),
  NOME_OT                     VARCHAR2(5 BYTE)  DEFAULT NULL,
  NUMERO_DOCUMENTO            VARCHAR2(30 BYTE) NOT NULL,
  TIPO_DOCUMENTO              VARCHAR2(3 BYTE)  NOT NULL,
  NUMERO_INVII                NUMBER(3)         DEFAULT 0                     NOT NULL,
  TIMESTAMP                   TIMESTAMP(6)      NOT NULL,
  DATA_TRANSIZIONE_STATO      DATE              NOT NULL,
  ID_PRIORITA_MNP             NUMBER(2)         DEFAULT 0
)
TABLESPACE DBCGO_DATA_BIG;

CREATE INDEX IDX00_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(STATO)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX01_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(TIPO_CLIENTE)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX02_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(DATA_CUT_OVER_CALC)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX03_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(DATA_VALIDAZIONE_MAX)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX04_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(DATARICEZIONERICHIESTA)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX05_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(CODICE_OPERATORE_DONATING)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX06_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(MSISDN)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX07_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(CODICE_FISCALE_PARTITA_IVA)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX08_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(NOME_OT)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX09_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(ID_PRIORITA)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX10_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(DATA_CUT_OVER)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE INDEX IDX11_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(CAUSALE_ACCODAMENTO)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

CREATE UNIQUE INDEX PK_DBCGO_GESTIONE_RIC_REC ON DBCGO_GESTIONE_RICHIESTA_REC
(ID_RICHIESTA)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

ALTER TABLE DBCGO_GESTIONE_RICHIESTA_REC ADD (
  CONSTRAINT PK_DBCGO_GESTIONE_RIC_REC PRIMARY KEY (ID_RICHIESTA)
    USING INDEX 
    TABLESPACE DBCGO_IDX_BIG);

-------------------------------------------------------------------------
CREATE TABLE DBCGO_LOV
(
  LOV_ID    NUMBER,
  LOV_NAME  VARCHAR2(40 BYTE),
  NAME      VARCHAR2(40 BYTE),
  DESCR     VARCHAR2(200 BYTE)
)
TABLESPACE DBCGO_DATA_SMALL;

CREATE UNIQUE INDEX PK_DBCGO_LOV ON DBCGO_LOV
(LOV_ID)
NOLOGGING
TABLESPACE DBCGO_IDX_SMALL;

CREATE UNIQUE INDEX AK0_DBCGO ON DBCGO_LOV
(LOV_NAME, NAME)
NOLOGGING
TABLESPACE DBCGO_IDX_SMALL;

ALTER TABLE DBCGO_LOV ADD (
  CONSTRAINT PK_DBCGO_LOV PRIMARY KEY (LOV_ID)
    USING INDEX 
    TABLESPACE DBCGO_IDX_SMALL,
  CONSTRAINT AK_DBCGO_0 UNIQUE (LOV_NAME, NAME));

-------------------------------------------------------------------------
CREATE TABLE DBCGO_PRIORITA
(
  ID_PRIORITA  NUMBER(2)                        NOT NULL,
  DESCR        VARCHAR2(40 BYTE)                NOT NULL
)
TABLESPACE DBCGO_DATA_SMALL;
CREATE UNIQUE INDEX PK_DBCGO_PRIORITA ON DBCGO_PRIORITA
(ID_PRIORITA)
NOLOGGING
TABLESPACE DBCGO_IDX_SMALL;

ALTER TABLE DBCGO_PRIORITA ADD (
  CONSTRAINT PK_DBCGO_PRIORITA PRIMARY KEY (ID_PRIORITA)
    USING INDEX 
    TABLESPACE DBCGO_IDX_SMALL);
    
-------------------------------------------------------------------------
CREATE TABLE DBCGO_PRIORITA_REC
(
  ID_PRIORITA  NUMBER(2)                        NOT NULL,
  DESCR        VARCHAR2(40 BYTE)                NOT NULL
)
TABLESPACE DBCGO_DATA_SMALL;

CREATE UNIQUE INDEX PK_DBCGO_PRIORITA_REC ON DBCGO_PRIORITA_REC
(ID_PRIORITA)
NOLOGGING
TABLESPACE DBCGO_IDX_SMALL;

ALTER TABLE DBCGO_PRIORITA_REC ADD (
  CONSTRAINT PK_DBCGO_PRIORITA_REC PRIMARY KEY (ID_PRIORITA)
    USING INDEX 
    TABLESPACE DBCGO_IDX_SMALL);
    
-------------------------------------------------------------------------
CREATE TABLE DBCGO_STATO
(
  ID_STATO       NUMBER(5)                      NOT NULL,
  DESCR_STATO    VARCHAR2(20 BYTE)              NOT NULL,
  FINALE         NUMBER(1)                      DEFAULT 0,
  PRIORITY_TYPE  NUMBER(1)                      DEFAULT 0,
  ID_STATO_GRP   NUMBER(5),
  DESCR          VARCHAR2(40 BYTE)
)
TABLESPACE DBCGO_DATA_SMALL;

CREATE UNIQUE INDEX PK_STATO ON DBCGO_STATO
(ID_STATO)
NOLOGGING
TABLESPACE DBCGO_IDX_SMALL;

ALTER TABLE DBCGO_STATO ADD (
  CONSTRAINT PK_STATO PRIMARY KEY (ID_STATO)
    USING INDEX 
    TABLESPACE DBCGO_IDX_SMALL);
    
-------------------------------------------------------------------------
CREATE TABLE DBCGO_STATO_REC
(
  ID_STATO       NUMBER(5)                      NOT NULL,
  DESCR_STATO    VARCHAR2(40 BYTE)              NOT NULL,
  FINALE         NUMBER(1)                      DEFAULT 0,
  PRIORITY_TYPE  NUMBER(1)                      DEFAULT 0,
  ID_STATO_GRP   NUMBER(5),
  DESCR          VARCHAR2(40 BYTE)
)
TABLESPACE DBCGO_DATA_SMALL;

CREATE UNIQUE INDEX PK_STATO_REC ON DBCGO_STATO_REC
(ID_STATO)
NOLOGGING
TABLESPACE DBCGO_IDX_SMALL;

ALTER TABLE DBCGO_STATO_REC ADD (
  CONSTRAINT PK_STATO_REC PRIMARY KEY (ID_STATO)
    USING INDEX 
    TABLESPACE DBCGO_IDX_SMALL);
    
-------------------------------------------------------------------------
CREATE TABLE DBM_ANAG_CLU0
(
  COD_CLU   VARCHAR2(2 BYTE),
  DESC_CLU  VARCHAR2(40 BYTE),
  COD_MAR   VARCHAR2(2 BYTE)
)
TABLESPACE DBCGO_DATA_SMALL;

CREATE UNIQUE INDEX PK_DBM_ANAG_CLU0 ON DBM_ANAG_CLU0
(COD_CLU)
NOLOGGING
TABLESPACE DBCGO_IDX_SMALL;

ALTER TABLE DBM_ANAG_CLU0 ADD (
  CONSTRAINT PK_DBM_ANAG_CLU0 PRIMARY KEY (COD_CLU)
    USING INDEX 
    TABLESPACE DBCGO_IDX_SMALL); 
    
-------------------------------------------------------------------------
CREATE TABLE DBM_ANAG_CLU1
(
  COD_CLU   VARCHAR2(2 BYTE),
  DESC_CLU  VARCHAR2(40 BYTE),
  COD_MAR   VARCHAR2(2 BYTE)
)
TABLESPACE DBCGO_DATA_SMALL;

CREATE UNIQUE INDEX PK_DBM_ANAG_CLU1 ON DBM_ANAG_CLU1
(COD_CLU)
NOLOGGING
TABLESPACE DBCGO_IDX_SMALL;

ALTER TABLE DBM_ANAG_CLU1 ADD (
  CONSTRAINT PK_DBM_ANAG_CLU1 PRIMARY KEY (COD_CLU)
    USING INDEX 
    TABLESPACE DBCGO_IDX_SMALL); 
    
-------------------------------------------------------------------------
CREATE TABLE DBM_ANAG_MAR0
(
  COD_MAR   VARCHAR2(2 BYTE),
  DESC_MAR  VARCHAR2(40 BYTE)
)
TABLESPACE DBCGO_DATA_SMALL;

CREATE UNIQUE INDEX PK_DBM_ANAG_MAR0 ON DBM_ANAG_MAR0
(COD_MAR)
NOLOGGING
TABLESPACE DBCGO_IDX_SMALL;

ALTER TABLE DBM_ANAG_MAR0 ADD (
  CONSTRAINT PK_DBM_ANAG_MAR0 PRIMARY KEY (COD_MAR)
    USING INDEX 
    TABLESPACE DBCGO_IDX_SMALL); 
    
-------------------------------------------------------------------------
CREATE TABLE DBM_ANAG_MAR1
(
  COD_MAR   VARCHAR2(2 BYTE),
  DESC_MAR  VARCHAR2(40 BYTE)
)
TABLESPACE DBCGO_DATA_SMALL;

CREATE UNIQUE INDEX PK_DBM_ANAG_MAR1 ON DBM_ANAG_MAR1
(COD_MAR)
NOLOGGING
TABLESPACE DBCGO_IDX_SMALL;

ALTER TABLE DBM_ANAG_MAR1 ADD (
  CONSTRAINT PK_DBM_ANAG_MAR1 PRIMARY KEY (COD_MAR)
    USING INDEX 
    TABLESPACE DBCGO_IDX_SMALL); 
    
-------------------------------------------------------------------------
CREATE TABLE DBM_LT_INF0
(
  NUMTEL           NUMBER(13),
  COD_MAR          VARCHAR2(2 BYTE),
  COD_MAR_OLD      VARCHAR2(2 BYTE),
  DT_LAST_AGG_MAR  VARCHAR2(6 BYTE),
  COD_CLU          VARCHAR2(2 BYTE),
  COD_CLU_OLD      VARCHAR2(2 BYTE),
  DT_LAST_AGG_CLU  VARCHAR2(6 BYTE),
  OT_LEGALE        VARCHAR2(2 BYTE),
  OT_GEO           VARCHAR2(2 BYTE),
  FAM_LINEA        NUMBER(1),
  FAM_LINEA_OLD    NUMBER(1),
  DT_LAST_AGG_FAM  VARCHAR2(6 BYTE),
  COD_FISCALE      VARCHAR2(16 BYTE),
  COD_AGENT_TEAM   VARCHAR2(2 BYTE)
)
TABLESPACE DBCGO_DATA_BIG;

CREATE UNIQUE INDEX PK_DBM_LT_INF0 ON DBM_LT_INF0
(NUMTEL)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

ALTER TABLE DBM_LT_INF0 ADD (
  CONSTRAINT PK_DBM_LT_INF0 PRIMARY KEY (NUMTEL)
    USING INDEX 
    TABLESPACE DBCGO_IDX_BIG); 
    
-------------------------------------------------------------------------
CREATE TABLE DBM_LT_INF1
(
  NUMTEL           NUMBER(13),
  COD_MAR          VARCHAR2(2 BYTE),
  COD_MAR_OLD      VARCHAR2(2 BYTE),
  DT_LAST_AGG_MAR  VARCHAR2(6 BYTE),
  COD_CLU          VARCHAR2(2 BYTE),
  COD_CLU_OLD      VARCHAR2(2 BYTE),
  DT_LAST_AGG_CLU  VARCHAR2(6 BYTE),
  OT_LEGALE        VARCHAR2(2 BYTE),
  OT_GEO           VARCHAR2(2 BYTE),
  FAM_LINEA        NUMBER(1),
  FAM_LINEA_OLD    NUMBER(1),
  DT_LAST_AGG_FAM  VARCHAR2(6 BYTE),
  COD_FISCALE      VARCHAR2(16 BYTE),
  COD_AGENT_TEAM   VARCHAR2(2 BYTE)
)
TABLESPACE DBCGO_DATA_BIG;

CREATE UNIQUE INDEX PK_DBM_LT_INF1 ON DBM_LT_INF1
(NUMTEL)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

ALTER TABLE DBM_LT_INF1 ADD (
  CONSTRAINT PK_DBM_LT_INF1 PRIMARY KEY (NUMTEL)
    USING INDEX 
    TABLESPACE DBCGO_IDX_BIG); 
    
-------------------------------------------------------------------------
CREATE TABLE MNP_PRIORITY_REQUEST
(
  PROGRESSIVE_ID   NUMBER(18)                   NOT NULL,
  MNP_REQUEST_ID   VARCHAR2(23 BYTE)            NOT NULL,
  MNP_SERVICE_ID   NUMBER(2)                    NOT NULL,
  MNP_PRIORITY_ID  NUMBER(2)                    NOT NULL,
  MNP_STATE        NUMBER(2),
  CREATE_DATE      TIMESTAMP(6),
  UPDATE_DATE      TIMESTAMP(6),
  REQUEST_STATE    NUMBER(2)                    DEFAULT (0)
)
TABLESPACE DBCGO_DATA_BIG;

CREATE UNIQUE INDEX PK_MNP_PRIORITY_REQUEST ON MNP_PRIORITY_REQUEST
(PROGRESSIVE_ID)
NOLOGGING
TABLESPACE DBCGO_IDX_BIG;

ALTER TABLE MNP_PRIORITY_REQUEST ADD (
  CONSTRAINT PK_MNP_PRIORITY_REQUEST PRIMARY KEY (PROGRESSIVE_ID)
    USING INDEX 
    TABLESPACE DBCGO_IDX_BIG);    
-------------------------------------------------------------------------

--EXTERNAL TABLE
-------------------------------------------------------------------------
CREATE TABLE EXT_DBM_ANAG_MAR
(
  tipo_record VARCHAR(2),
  cod_mar   VARCHAR(2),
  desc_mar VARCHAR(40)
)
ORGANIZATION EXTERNAL
  (  TYPE ORACLE_LOADER
     DEFAULT DIRECTORY DBCGO_DBM_PATH
     ACCESS PARAMETERS 
      (RECORDS DELIMITED BY NEWLINE
       LOAD WHEN tipo_record!='01' AND tipo_record!='03'
       BADFILE 'ext_dbm_anag_mar.bad'
       LOGFILE 'ext_dbm_anag_mar.log' 
       FIELDS ( tipo_record CHAR(2),cod_mar CHAR(2),desc_mar CHAR(40))
      )
     LOCATION (DBCGO_DBM_PATH:'DBM_ANAG_MAR.DAT')
  )
REJECT LIMIT UNLIMITED
NOLOGGING 
NOCACHE
NOPARALLEL;

-------------------------------------------------------------------------
CREATE TABLE EXT_DBM_ANAG_CLU
(
  tipo_record VARCHAR(2),
  cod_clu     VARCHAR(2),
  desc_clu    VARCHAR(40),
  cod_mar     VARCHAR(2)
)
ORGANIZATION EXTERNAL
  (  TYPE ORACLE_LOADER
     DEFAULT DIRECTORY DBCGO_DBM_PATH
     ACCESS PARAMETERS 
      ( RECORDS DELIMITED BY NEWLINE
        LOAD WHEN tipo_record!='01' AND tipo_record!='03'
        BADFILE 'ext_dbm_anag_clu.bad'
        LOGFILE 'ext_dbm_anag_clu.log'
        FIELDS (tipo_record CHAR(2) ,cod_clu CHAR(2),desc_clu CHAR(40),cod_mar CHAR(2))
      )
     LOCATION (DBCGO_DBM_PATH:'DBM_ANAG_CLU.DAT')
  )
REJECT LIMIT UNLIMITED
NOLOGGING 
NOCACHE
NOPARALLEL;

-------------------------------------------------------------------------
CREATE TABLE ext_dbm_lt_inf
(
  tipo_record VARCHAR(2),
  numtel  VARCHAR(13),
  cod_mar VARCHAR(2),
  cod_mar_old VARCHAR(2),
  dt_last_agg_mar VARCHAR(6),
  cod_clu VARCHAR(2),
  cod_clu_old VARCHAR(2),
  dt_last_agg_clu VARCHAR(6),
  ot_legale VARCHAR(2),
  ot_geo VARCHAR(2),
  fam_linea VARCHAR(1),
  fam_linea_old VARCHAR(1),
  dt_last_agg_fam VARCHAR(6),
  cod_fiscale VARCHAR(16),
  cod_agent_team  VARCHAR(2)
)
ORGANIZATION EXTERNAL
  (  TYPE ORACLE_LOADER
     DEFAULT DIRECTORY DBCGO_DBM_PATH
     ACCESS PARAMETERS 
      ( RECORDS DELIMITED BY NEWLINE
        LOAD WHEN tipo_record!='01' AND tipo_record!='03'
        BADFILE 'ext_dbm_lt_inf.bad'
        LOGFILE 'ext_dbm_lt_inf.log'
        FIELDS (tipo_record CHAR(2),numtel CHAR(13),cod_mar CHAR(2),cod_mar_old CHAR(2),dt_last_agg_mar char(6) date_format date mask "YYYYMM",
         cod_clu CHAR(2),cod_clu_old CHAR(2),dt_last_agg_clu char(6) date_format date mask "YYYYMM",
         ot_legale CHAR(2),ot_geo CHAR(2),fam_linea CHAR(1),fam_linea_old CHAR(1),dt_last_agg_fam char(6) date_format date mask "YYYYMM",
         cod_fiscale CHAR(16),cod_agent_team CHAR(2))
      )
     LOCATION (DBCGO_DBM_PATH:'DBM_LT_INF.DAT')
  )
REJECT LIMIT UNLIMITED
NOLOGGING 
NOCACHE
NOPARALLEL;
-------------------------------------------------------------------------

--FOREIGN KEY
-------------------------------------------------------------------------
ALTER TABLE DBCGO_GESTIONE_RICHIESTA ADD 
CONSTRAINT FK0_DBCGO_GESTIONE_RICHIESTA
 FOREIGN KEY (ID_PRIORITA)
 REFERENCES DBCGO_PRIORITA (ID_PRIORITA) ENABLE
 VALIDATE;
 
ALTER TABLE DBCGO_GESTIONE_RICHIESTA ADD 
CONSTRAINT FK1_DBCGO_GESTIONE_RICHIESTA
 FOREIGN KEY (ID_PRIORITA_MNP)
 REFERENCES DBCGO_PRIORITA (ID_PRIORITA) ENABLE
 VALIDATE;
 
ALTER TABLE DBCGO_GESTIONE_RICHIESTA ADD 
CONSTRAINT FK2_DBCGO_GESTIONE_RICHIESTA
 FOREIGN KEY (STATO)
 REFERENCES DBCGO_STATO (ID_STATO) ENABLE
 VALIDATE;
 
ALTER TABLE DBCGO_GESTIONE_RICHIESTA_REC ADD 
CONSTRAINT FK0_DBCGO_GESTIONE_RIC_REC
 FOREIGN KEY (ID_PRIORITA)
 REFERENCES DBCGO_PRIORITA_REC (ID_PRIORITA) ENABLE
 VALIDATE;
 
ALTER TABLE DBCGO_GESTIONE_RICHIESTA_REC ADD 
CONSTRAINT FK1_DBCGO_GESTIONE_RIC_REC
 FOREIGN KEY (ID_PRIORITA_MNP)
 REFERENCES DBCGO_PRIORITA_REC (ID_PRIORITA) ENABLE
 VALIDATE; 

ALTER TABLE DBCGO_GESTIONE_RICHIESTA_REC ADD 
CONSTRAINT FK2_DBCGO_GESTIONE_RIC_REC
 FOREIGN KEY (STATO)
 REFERENCES DBCGO_STATO_REC (ID_STATO) ENABLE
 VALIDATE; 

--VIEW
-------------------------------------------------------------------------
CREATE OR REPLACE VIEW DBCGO_STATO_GRP
(ID_STATO, DESCR_STATO, FINALE, PRIORITY_TYPE, ID_STATO_GRP, 
 DESCR)
AS 
select distinct a.ID_STATO, a.DESCR_STATO, a.FINALE, a.PRIORITY_TYPE, a.ID_STATO_GRP, a.DESCR
from dbcgo_stato a, dbcgo_stato b
where b.id_stato_grp=a.id_stato;

CREATE OR REPLACE VIEW DBCGO_STATO_REC_GRP
(ID_STATO, DESCR_STATO, FINALE, PRIORITY_TYPE, ID_STATO_GRP, 
 DESCR)
AS 
select distinct a.ID_STATO, a.DESCR_STATO, a.FINALE, a.PRIORITY_TYPE, a.ID_STATO_GRP, a.DESCR
from dbcgo_stato_rec a, dbcgo_stato_rec b
where b.id_stato_grp=a.id_stato;

--SYNONYM
-------------------------------------------------------------------------
CREATE SYNONYM DBM_ANAG_CLU FOR DBM_ANAG_CLU0;

CREATE SYNONYM DBM_ANAG_MAR FOR DBM_ANAG_MAR0;

CREATE SYNONYM DBM_LT_INF FOR DBM_LT_INF0;

