
/*
*
*  Author     : Vishal Gupta
*  Purpose    : Oracle Data Type test case
*  Descrption : Test case to test the size of various data types.
*  Parameters : None
*               
*
*  Revision History:
*  ===================
*  Date       Author        Description
*  ---------  ------------  -----------------------------------------
*  01-Sep-15  Vishal Gupta  Created
*
*
*/


------------------------------------------------------------------------------------------------------------
-- Number data types
-- NUMBER [ (p [, s]) ]  - Number having precision p and scale s. 
--         The precision p can range from 1 to 38. The scale s can range from -84 to 127. 
--         Both precision and scale are in decimal digits. A NUMBER value requires from 1 to 22 bytes.
------------------------------------------------------------------------------------------------------------

drop table vg_number_types purge;

----------------------------------------------
-- Create table
----------------------------------------------
DECLARE
  lv_sqltext VARCHAR2(32565);
BEGIN
  lv_sqltext := 'create table vg_number_types (col_number number';
  for p in 1 .. 38
  LOOP
     FOR s in 0 .. LEAST(p,25)
	    LOOP
        lv_sqltext := lv_sqltext || ',col_number_' || p || '_' || s || ' number(' || p || ',' || s || ')';  
     END LOOP;	 
  END LOOP;	 
  lv_sqltext := lv_sqltext || ')';
  execute immediate lv_sqltext;
END;
/

desc vg_number_types;

----------------------------------------------
-- Populate table
----------------------------------------------
set serveroutput on 
DECLARE
  lv_sqltext VARCHAR2(32565);
BEGIN
  execute immediate 'truncate table vg_number_types';  
  insert into vg_number_types (col_number ) values(1/3);  
  commit;
  for p in 1 .. 38
  LOOP
     FOR s in 0 .. LEAST(p,25)
	    LOOP
        lv_sqltext := 'update vg_number_types set col_number_' || p || '_' || s || ' = power(10,' || TO_CHAR((p-s)-1) || ') + TRUNC(1/3,' || LEAST(s,p) || ') ';
		  dbms_output.put_line(lv_sqltext);
        execute immediate lv_sqltext;
        commit;
     END LOOP;	 
  END LOOP;	 
END;
/


----------------------------------------------
-- Fetch Column Length
----------------------------------------------
set serveroutput on 
DECLARE
  lv_column_name   VARCHAR2(30);
  lv_column_length VARCHAR2(30);
  lv_sqltext     VARCHAR2(32565);
BEGIN
  for p in 1 .. 38
  LOOP
     FOR s in 0 .. LEAST(p,25)
	    LOOP
		  lv_column_name := 'col_number_' || p || '_' || s ;
        lv_sqltext := 'SELECT VSIZE(' || lv_column_name || ') FROM vg_number_types ' ;
        execute immediate lv_sqltext INTO lv_column_length ;
        dbms_output.put_line('NUMBER(' || p || ',' || s || ')' || ' : Length = ' ||  lv_column_length || ' bytes');
     END LOOP;	 
  END LOOP;	 
END;
/


----------------------------------------------
-- Date and Timestamp data types
----------------------------------------------
drop table vg_date_timestamp_types purge;

create table vg_date_timestamp_types
(
  col_date date
 ,col_timestamp_0 timestamp(0) 
 ,col_timestamp_1 timestamp(1) 
 ,col_timestamp_2 timestamp(2) 
 ,col_timestamp_3 timestamp(3) 
 ,col_timestamp_4 timestamp(4) 
 ,col_timestamp_5 timestamp(5) 
 ,col_timestamp_6 timestamp(6)  -- default for timestamp
 ,col_timestamp_7 timestamp(7) 
 ,col_timestamp_8 timestamp(8) 
 ,col_timestamp_9 timestamp(9) 
 ,col_timestamp_0_with_tz timestamp(0) WITH TIME ZONE
 ,col_timestamp_1_with_tz timestamp(1) WITH TIME ZONE
 ,col_timestamp_2_with_tz timestamp(2) WITH TIME ZONE
 ,col_timestamp_3_with_tz timestamp(3) WITH TIME ZONE
 ,col_timestamp_4_with_tz timestamp(4) WITH TIME ZONE
 ,col_timestamp_5_with_tz timestamp(5) WITH TIME ZONE
 ,col_timestamp_6_with_tz timestamp(6) WITH TIME ZONE -- default for timestamp
 ,col_timestamp_7_with_tz timestamp(7) WITH TIME ZONE
 ,col_timestamp_8_with_tz timestamp(8) WITH TIME ZONE
 ,col_timestamp_9_with_tz timestamp(9) WITH TIME ZONE
 ,col_timestamp_0_with_ltz timestamp(0) WITH LOCAL TIME ZONE
 ,col_timestamp_1_with_ltz timestamp(1) WITH LOCAL TIME ZONE
 ,col_timestamp_2_with_ltz timestamp(2) WITH LOCAL TIME ZONE
 ,col_timestamp_3_with_ltz timestamp(3) WITH LOCAL TIME ZONE
 ,col_timestamp_4_with_ltz timestamp(4) WITH LOCAL TIME ZONE
 ,col_timestamp_5_with_ltz timestamp(5) WITH LOCAL TIME ZONE
 ,col_timestamp_6_with_ltz timestamp(6) WITH LOCAL TIME ZONE -- default for timestamp
 ,col_timestamp_7_with_ltz timestamp(7) WITH LOCAL TIME ZONE
 ,col_timestamp_8_with_ltz timestamp(8) WITH LOCAL TIME ZONE
 ,col_timestamp_9_with_ltz timestamp(9) WITH LOCAL TIME ZONE
)
;


insert into vg_date_timestamp_types
(
   col_date 
 , col_timestamp_0 ,col_timestamp_1 ,col_timestamp_2 ,col_timestamp_3 ,col_timestamp_4 ,col_timestamp_5 ,col_timestamp_6 ,col_timestamp_7 ,col_timestamp_8 ,col_timestamp_9
 , col_timestamp_0_with_tz ,col_timestamp_1_with_tz ,col_timestamp_2_with_tz ,col_timestamp_3_with_tz 
 , col_timestamp_4_with_tz ,col_timestamp_5_with_tz ,col_timestamp_6_with_tz 
 , col_timestamp_7_with_tz ,col_timestamp_8_with_tz ,col_timestamp_9_with_tz
 , col_timestamp_0_with_ltz ,col_timestamp_1_with_ltz ,col_timestamp_2_with_ltz ,col_timestamp_3_with_ltz 
 , col_timestamp_4_with_ltz ,col_timestamp_5_with_ltz ,col_timestamp_6_with_ltz ,col_timestamp_7_with_ltz 
 , col_timestamp_8_with_ltz ,col_timestamp_9_with_ltz
)
values
(sysdate
,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp
,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp
,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp,systimestamp
);

commit;

SELECT VSIZE(col_date)
     , VSIZE(col_timestamp_0) 
     , VSIZE(col_timestamp_1) 
     , VSIZE(col_timestamp_2) 
     , VSIZE(col_timestamp_3) 
     , VSIZE(col_timestamp_4) 
     , VSIZE(col_timestamp_5) 
     , VSIZE(col_timestamp_6) 
     , VSIZE(col_timestamp_7) 
     , VSIZE(col_timestamp_8) 
     , VSIZE(col_timestamp_9) 
from vg_date_timestamp_types
;

SELECT VSIZE(col_timestamp_0_with_tz) 
     , VSIZE(col_timestamp_1_with_tz) 
     , VSIZE(col_timestamp_2_with_tz) 
     , VSIZE(col_timestamp_3_with_tz) 
     , VSIZE(col_timestamp_4_with_tz) 
     , VSIZE(col_timestamp_5_with_tz) 
     , VSIZE(col_timestamp_6_with_tz) 
     , VSIZE(col_timestamp_7_with_tz) 
     , VSIZE(col_timestamp_8_with_tz) 
     , VSIZE(col_timestamp_9_with_tz) 
from vg_date_timestamp_types
;

SELECT VSIZE(col_timestamp_0_with_ltz) 
     , VSIZE(col_timestamp_1_with_ltz) 
     , VSIZE(col_timestamp_2_with_ltz) 
     , VSIZE(col_timestamp_3_with_ltz) 
     , VSIZE(col_timestamp_4_with_ltz) 
     , VSIZE(col_timestamp_5_with_ltz) 
     , VSIZE(col_timestamp_6_with_ltz) 
     , VSIZE(col_timestamp_7_with_ltz) 
     , VSIZE(col_timestamp_8_with_ltz) 
     , VSIZE(col_timestamp_9_with_ltz) 
from vg_date_timestamp_types
;
