
/*------ ATD 400583 ------*/
DECLARE v_count NUMBER(1) := 0;   
BEGIN   
SELECT 1 INTO v_count FROM USER_TAB_COLUMNS WHERE Upper(table_name) = 'COMPLEMENTOPRODUTO' AND Upper(column_name) = 'INSUMO';   
EXCEPTION   WHEN NO_DATA_FOUND THEN   
EXECUTE IMMEDIATE 'ALTER TABLE COMPLEMENTOPRODUTO ADD INSUMO NUMBER(1,0)';
END;
/
DECLARE v_count NUMBER(1) := 0; 
BEGIN 
SELECT 1 INTO v_count FROM USER_TAB_COLUMNS WHERE Upper(table_name) = 'ORDEMPRODUCAO' AND Upper(column_name) = 'TERCEIRIZADO'; 
EXCEPTION 
WHEN NO_DATA_FOUND THEN 
EXECUTE IMMEDIATE 'ALTER TABLE ORDEMPRODUCAO ADD TERCEIRIZADO NUMBER(1,0) DEFAULT 0 NOT NULL '; 
END;
/
DECLARE v_count NUMBER(1) := 0; 
BEGIN 
SELECT 1 INTO v_count FROM USER_TAB_COLUMNS WHERE Upper(table_name) = 'ORDEMPRODUCAO' AND Upper(column_name) = 'OIDFORNECEDOR'; 
EXCEPTION 
WHEN NO_DATA_FOUND THEN 
EXECUTE IMMEDIATE 'ALTER TABLE ORDEMPRODUCAO ADD OIDFORNECEDOR INT '; 
END;
/
DECLARE v_count NUMBER:=0;
BEGIN
SELECT Count(1) INTO v_count FROM USER_TABLES WHERE UPPER(table_name) = 'SPED_CORRECAOAPONTAMENTO';
IF v_count = 0 THEN
EXECUTE IMMEDIATE 'CREATE TABLE SPED_CORRECAOAPONTAMENTO(
        ID INTEGER NOT NULL,
		REGISTRO VARCHAR(4) NOT NULL,
        FILIAL VARCHAR(2) NOT NULL,
		DATAINICIAL DATE NOT NULL,
		DATAFINAL DATE NOT NULL,
        CODPRO VARCHAR(5) NOT NULL,
		ORDEMPRODUCAO INTEGER NULL,
        QTD_COR_POS NUMBER(15,3) NULL,
        QTD_COR_NEG NUMBER(15,3) NULL,
		DT_INI_AP DATE NULL,
		DT_FIN_AP DATE NULL,
		ORIGEM VARCHAR(1) NULL,
		DT_EST DATE NULL,
		IND_EST VARCHAR(1) NULL,
        ATUALIZADOEM DATE NULL,
        OIDUSUARIO INTEGER NULL)';
EXECUTE IMMEDIATE 'ALTER TABLE SPED_CORRECAOAPONTAMENTO ADD CONSTRAINT PK_SPED_CORRECAOAPONTAMENTO PRIMARY KEY (ID)';
EXECUTE IMMEDIATE 'CREATE SEQUENCE SPED_CORRECAOAPONTAMENTO_SEQ MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 NOCYCLE NOORDER' ;
EXECUTE IMMEDIATE 'CREATE OR REPLACE TRIGGER SPED_CORRECAOAPONTAMENTO_I
BEFORE INSERT ON SPED_CORRECAOAPONTAMENTO FOR EACH ROW
WHEN (new.ID IS NULL)
BEGIN
SELECT SPED_CORRECAOAPONTAMENTO_SEQ.NEXTVAL INTO :new.ID FROM dual;
END;';
END IF;
END;
/