-- ============================================================
--   TABLE: TIPORECE
-- ============================================================
CREATE GLOBAL TEMPORARY TABLE TMP_ITMAPURPRECIMPO
( 
   NRITMAPURPRECIMPO NUMBER 		NOT NULL,
   CDCENTITMAP 		 VARCHAR2(4) 	NOT NULL, 
   NRSEQITMAP 		 VARCHAR2(10) 	NOT NULL, 
   CDREGITMAP 	   	 VARCHAR2(3) 	NOT NULL,
   CDFORNITMAP 		 VARCHAR2(21) 	NOT NULL, 
   NRCOLEITMAP 		 VARCHAR2(10) 	NOT NULL, 
   DTENTRAPUT 		 DATE 		    NOT NULL, 
   CDFILIAP 		 VARCHAR2(4) 	NOT NULL, 
   CDPRODITMAP 		 VARCHAR2(10) 	NOT NULL, 
   CDMARCITMAP 		 VARCHAR2(10) 	NOT NULL, 
   CDIMPOSTO 		 VARCHAR2(2) 	NOT NULL, 
   IDPROCESSO        VARCHAR2(2),
   VRIMPOSTO 		 FLOAT(126), 
   NRREGRAFISCAL 	 VARCHAR2(10), 
   VRREDUBASECALC 	 FLOAT(126), 
   VRMAJOBASECALC 	 FLOAT(126), 
   VRPERCALIQUOTA 	 FLOAT(126), 
   VRBASECALC 		 FLOAT(126),  
   VRADIMPOSTOIT 	 FLOAT(126),  
   IDTRATAMENTO      VARCHAR2(1),
   CDGRUPOFISC       VARCHAR2(3),
   CDFISCAL          VARCHAR2(1),
   NRORG             NUMBER 	     DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG') 		NOT NULL,
   IDATIVO           VARCHAR2(1)     DEFAULT 'S'					NOT NULL,
   DTINCLUSAO        DATE            DEFAULT SYSDATE,
   DTULTATU          DATE            DEFAULT SYSDATE,
   NRORGINCLUSAO     NUMBER          DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG')		NULL,
   CDOPERINCLUSAO    VARCHAR2(12)    DEFAULT '1' 					NULL,
   NRORGULTATU       NUMBER          DEFAULT SYS_CONTEXT('NRORG_CTX','NRORG')		NULL,
   CDOPERULTATU      VARCHAR2(12)    DEFAULT '1',
		CONSTRAINT CKC_IDATIVO_TMPITMAPUPRECIPO CHECK (IDATIVO IN ('S','N')),
		CONSTRAINT PK_ITMAPURPRECIMPO_TMP PRIMARY KEY (NRITMAPURPRECIMPO, CDCENTITMAP, NRSEQITMAP, CDREGITMAP, CDFORNITMAP, NRCOLEITMAP, DTENTRAPUT, CDFILIAP, CDPRODITMAP, CDMARCITMAP, CDIMPOSTO, IDPROCESSO)
)
/
CREATE SEQUENCE SEQ_TMP_ITMAPURPRECIMPO INCREMENT BY 1 MINVALUE 1
/
DECLARE V_NRITMAPURPRECIMPO NUMERIC;
        V_TEXTO             VARCHAR2(1000);
BEGIN
   SELECT NVL(MAX(NRITMAPURPRECIMPO), 0)+1
     INTO V_NRITMAPURPRECIMPO
     FROM ITMAPURPRECIMPO;
   V_TEXTO := 'CREATE SEQUENCE SEQ_ITMAPURPRECIMPO INCREMENT BY 1 MINVALUE '||V_NRITMAPURPRECIMPO;
   BEGIN
      EXECUTE IMMEDIATE V_TEXTO;
   EXCEPTION
      WHEN OTHERS THEN NULL;
   END;   
END;