
/*------ ATD 237679 ------*/
DECLARE V_COUNT NUMBER:=0;

BEGIN SELECT 1 INTO V_COUNT FROM USER_TABLES WHERE UPPER(TABLE_NAME) = 'DESPESAS'; 
EXCEPTION WHEN NO_DATA_FOUND THEN BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE DESPESAS(
	CODDESPESA	INT NOT NULL,
	DESCRICAO	VARCHAR(50),	
	TIPO		NUMERIC(2);'

EXECUTE IMMEDIATE 'ALTER TABLE DESPESAS ADD CONSTRAINT PK_DESPESAS PRIMARY KEY (CODDESPESA)'; 

EXECUTE IMMEDIATE 'CREATE SEQUENCE DESPESAS_SEQ MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 NOCYCLE NOORDER' ;

EXECUTE IMMEDIATE 'CREATE OR REPLACE TRIGGER DESPESAS_I

	BEFORE INSERT ON DESPESAS FOR EACH ROW

	WHEN (NEW.CODDESPESA IS NULL)

	BEGIN

	SELECT DESPESAS_SEQ.NEXTVAL INTO :NEW.CODDESPESA FROM DUAL;

	END;';

END;
END;
/


DECLARE V_COUNT NUMBER:=0;

BEGIN SELECT 1 INTO V_COUNT FROM USER_TABLES WHERE UPPER(TABLE_NAME) = 'DESPESAVEICULOS'; 
EXCEPTION WHEN NO_DATA_FOUND THEN BEGIN
EXECUTE IMMEDIATE 'CREATE TABLE DESPESAVEICULOS(
	ID				INT NOT NULL,
	FILIAL			VARCHAR(2) DEFAULT '',
	PLACA			VARCHAR(7) DEFAULT '',	
	CODDESPESA		VARCHAR(5),
	DATAEMISSAO		DATETIME ,
	QUANTIDADE		NUMERIC(15,3) DEFAULT 0,
	VALORUNITARIO	NUMERIC(15,3) DEFAULT 0,
	VALORTOTAL		NUMERIC(15,2) DEFAULT 0,
	MK				NUMERIC(9,0) DEFAULT 0,
	KMLITRO			NUMERIC(15,3) DEFAULT 0,
	NUMNOTA			VARCHAR(13) DEFAULT '',
	SERIE			VARCHAR(3) DEFAULT '';'

EXECUTE IMMEDIATE 'ALTER TABLE DESPESAVEICULOS ADD CONSTRAINT PK_DESPESAVEICULOS PRIMARY KEY (ID)'; 

EXECUTE IMMEDIATE 'CREATE SEQUENCE DESPESAVEICULOS_SEQ MINVALUE 1 MAXVALUE 999999999999999999999999999 INCREMENT BY 1 NOCYCLE NOORDER' ;

EXECUTE IMMEDIATE 'CREATE OR REPLACE TRIGGER DESPESAVEICULOS_I

	BEFORE INSERT ON DESPESAVEICULOS FOR EACH ROW

	WHEN (NEW.ID IS NULL)

	BEGIN

	SELECT DESPESAVEICULOS_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL;

	END;';

END;
END;
/