
/*------ ATD 326496 ------*/
DECLARE v_count NUMBER:=0;

BEGIN

SELECT Count(1) INTO v_count FROM USER_TABLES WHERE UPPER(table_name) = 'PAGAMENTONFE';

IF v_count = 0 THEN

EXECUTE IMMEDIATE 'create table PagamentoVenda(
				ID INT NOT NULL,
				NumOrd INT NOT NULL, 
				Valor NUMERIC(15,2) NOT NULL,
				rTipoDoc INT NOT NULL, 
				rCliente INT NOT NULL DEFAULT 0)';
EXECUTE IMMEDIATE 'ALTER TABLE PAGAMENTOVENDA ADD CONSTRAINT PK_PAGAMENTOVENDA PRIMARY KEY (ID)';
EXECUTE IMMEDIATE 'CREATE SEQUENCE PAGAMENTOVENDA_SEQ MINVALUE 1 MAXVALUE 
		999999999999999999999999999 INCREMENT BY 1 NOCYCLE NOORDER';
EXECUTE IMMEDIATE 'CREATE OR REPLACE TRIGGER PAGAMENTOVENDA_I
		BEFORE INSERT ON PAGAMENTOVENDA for each ROW
		WHEN (new.ID IS NULL)
		BEGIN
		SELECT PAGAMENTOVENDA_SEQ.NEXTVAL INTO :new.ID FROM dual;
		END;';
EXECUTE IMMEDIATE 'create index PAGAMENTOVENDA_IDX1 on PAGAMENTOVENDA(NumOrd)';

END IF;

END;