declare
    object_exists exception;
    pragma exception_init (object_exists , -00955);
begin
  execute immediate 'CREATE TABLE CONF_RESTRICAO_TRAMO (
       CONFRESTRICAOTRAMO_ID NUMBER(15,0) NOT NULL,   
       CONFRESTRICAOCANALVENTA_ID  NUMBER(15,0),
       TIPOPTOVTA_ID    NUMBER(2),
       ORIGEN_ID            NUMBER(7),       
       DESTINO_ID           NUMBER(7),
       TEMPOPERMITIR          DATE,
       SEGUNDA           NUMBER(1,0),
       TERCA           NUMBER(1,0),
       QUARTA           NUMBER(1,0),
       QUINTA           NUMBER(1,0),
       SEXTA           NUMBER(1,0),
       SABADO           NUMBER(1,0),
       DOMINGO           NUMBER(1,0),  
       USUARIO_ID           NUMBER(7),
       ACTIVO               NUMBER(1,0),
       FECMODIF DATE,
       PRIMARY KEY (CONFRESTRICAOTRAMO_ID),
       FOREIGN KEY (CONFRESTRICAOCANALVENTA_ID) REFERENCES CONF_RESTRICAO_CANALVENTA 
	)';
  exception when object_exists then null;
end;
/
declare
    object_exists exception;
    pragma exception_init (object_exists , -00955);
begin
  execute immediate
	'CREATE SEQUENCE "CONF_RESTRICAO_TRAMO_SEQ" MINVALUE 1 MAXVALUE 9999999999999999999999999999 INCREMENT BY 1 START WITH 1 CACHE 20 NOORDER NOCYCLE';
 exception when object_exists then null;
end;
/
declare
    column_exists exception;
    pragma exception_init (column_exists , -01430);
begin
    execute immediate 'ALTER TABLE conf_restricao_ptovta ADD CONFRESTRICAOTRAMO_ID NUMBER(15,0) constraint CONFRESTRICAOPTOVTA_TRAMO_FK references CONF_RESTRICAO_TRAMO(CONFRESTRICAOTRAMO_ID) ';
    exception when column_exists then null;
end;
/
declare
    column_exists exception;
    pragma exception_init (column_exists , -01430);
begin
    execute immediate 'ALTER TABLE conf_restricao_excecao ADD CONFRESTRICAOTRAMO_ID NUMBER(15,0) constraint CONFRESTRICAOEXCECAO_TRAMO_FK  references CONF_RESTRICAO_TRAMO(CONFRESTRICAOTRAMO_ID) ';
    exception when column_exists then null;
end;

