ORACLE utl_raw函式與dbms_stats.convert_raw_value函式使用

startay發表於2016-01-28
ORACLE utl_raw函式與dbms_stats.convert_raw_value函式使用

ORACLE VERSION 11.2.0.4

下面是utl_raw包下的所有函式
  1. SQL>desc utl_raw

  2. FUNCTION BIT_AND RETURNS RAW
  3.  Argument Name Type In/Out Default?
  4.  ------------------------------ ----------------------- ------ --------
  5.  R1 RAW IN
  6.  R2 RAW IN

  7. FUNCTION BIT_COMPLEMENT RETURNS RAW
  8.  Argument Name Type In/Out Default?
  9.  ------------------------------ ----------------------- ------ --------
  10.  R RAW IN

  11. FUNCTION BIT_OR RETURNS RAW
  12.  Argument Name Type In/Out Default?
  13.  ------------------------------ ----------------------- ------ --------
  14.  R1 RAW IN
  15.  R2 RAW IN

  16. FUNCTION BIT_XOR RETURNS RAW
  17.  Argument Name Type In/Out Default?
  18.  ------------------------------ ----------------------- ------ --------
  19.  R1 RAW IN
  20.  R2 RAW IN

  21. FUNCTION CAST_FROM_BINARY_DOUBLE RETURNS RAW
  22.  Argument Name Type In/Out Default?
  23.  ------------------------------ ----------------------- ------ --------
  24.  N BINARY_DOUBLE IN
  25.  ENDIANESS BINARY_INTEGER IN DEFAULT

  26. FUNCTION CAST_FROM_BINARY_FLOAT RETURNS RAW
  27.  Argument Name Type In/Out Default?
  28.  ------------------------------ ----------------------- ------ --------
  29.  N BINARY_FLOAT IN
  30.  ENDIANESS BINARY_INTEGER IN DEFAULT

  31. FUNCTION CAST_FROM_BINARY_INTEGER RETURNS RAW
  32.  Argument Name Type In/Out Default?
  33.  ------------------------------ ----------------------- ------ --------
  34.  N BINARY_INTEGER IN
  35.  ENDIANESS BINARY_INTEGER IN DEFAULT

  36. FUNCTION CAST_FROM_NUMBER RETURNS RAW
  37.  Argument Name Type In/Out Default?
  38.  ------------------------------ ----------------------- ------ --------
  39.  N NUMBER IN

  40. FUNCTION CAST_TO_BINARY_DOUBLE RETURNS BINARY_DOUBLE
  41.  Argument Name Type In/Out Default?
  42.  ------------------------------ ----------------------- ------ --------
  43.  R RAW IN
  44.  ENDIANESS BINARY_INTEGER IN DEFAULT

  45. FUNCTION CAST_TO_BINARY_FLOAT RETURNS BINARY_FLOAT
  46.  Argument Name Type In/Out Default?
  47.  ------------------------------ ----------------------- ------ --------
  48.  R RAW IN
  49.  ENDIANESS BINARY_INTEGER IN DEFAULT

  50. FUNCTION CAST_TO_BINARY_INTEGER RETURNS BINARY_INTEGER
  51.  Argument Name Type In/Out Default?
  52.  ------------------------------ ----------------------- ------ --------
  53.  R RAW IN
  54.  ENDIANESS BINARY_INTEGER IN DEFAULT

  55. FUNCTION CAST_TO_NUMBER RETURNS NUMBER
  56.  Argument Name Type In/Out Default?
  57.  ------------------------------ ----------------------- ------ --------
  58.  R RAW IN
  59. FUNCTION CAST_TO_NVARCHAR2 RETURNS NVARCHAR2
  60.  Argument Name Type In/Out Default?
  61.  ------------------------------ ----------------------- ------ --------
  62.  R RAW IN

  63. FUNCTION CAST_TO_RAW RETURNS RAW
  64.  Argument Name Type In/Out Default?
  65.  ------------------------------ ----------------------- ------ --------
  66.  C VARCHAR2 IN

  67. FUNCTION CAST_TO_VARCHAR2 RETURNS VARCHAR2
  68.  Argument Name Type In/Out Default?
  69.  ------------------------------ ----------------------- ------ --------
  70.  R RAW IN
  71. FUNCTION COMPARE RETURNS NUMBER
  72.  Argument Name Type In/Out Default?
  73.  ------------------------------ ----------------------- ------ --------
  74.  R1 RAW IN
  75.  R2 RAW IN
  76.  PAD RAW IN DEFAULT

  77. FUNCTION CONCAT RETURNS RAW
  78.  Argument Name Type In/Out Default?
  79.  ------------------------------ ----------------------- ------ --------
  80.  R1 RAW IN DEFAULT
  81.  R2 RAW IN DEFAULT
  82.  R3 RAW IN DEFAULT
  83.  R4 RAW IN DEFAULT
  84.  R5 RAW IN DEFAULT
  85.  R6 RAW IN DEFAULT
  86.  R7 RAW IN DEFAULT
  87.  R8 RAW IN DEFAULT
  88.  R9 RAW IN DEFAULT
  89.  R10 RAW IN DEFAULT
  90.  R11 RAW IN DEFAULT
  91.  R12 RAW IN DEFAULT

  92. FUNCTION CONVERT RETURNS RAW
  93.  Argument Name Type In/Out Default?
  94.  ------------------------------ ----------------------- ------ --------
  95.  R RAW IN
  96.  TO_CHARSET VARCHAR2 IN
  97.  FROM_CHARSET VARCHAR2 IN

  98. FUNCTION COPIES RETURNS RAW
  99.  Argument Name Type In/Out Default?
  100.  ------------------------------ ----------------------- ------ --------
  101.  R RAW IN
  102.  N NUMBER IN
  103. FUNCTION LENGTH RETURNS NUMBER
  104.  Argument Name Type In/Out Default?
  105.  ------------------------------ ----------------------- ------ --------
  106.  R RAW IN

  107. FUNCTION OVERLAY RETURNS RAW
  108.  Argument Name Type In/Out Default?
  109.  ------------------------------ ----------------------- ------ --------
  110.  OVERLAY_STR RAW IN
  111.  TARGET RAW IN
  112.  POS BINARY_INTEGER IN DEFAULT
  113.  LEN BINARY_INTEGER IN DEFAULT
  114.  PAD RAW IN DEFAULT
  115. FUNCTION REVERSE RETURNS RAW
  116.  Argument Name Type In/Out Default?
  117.  ------------------------------ ----------------------- ------ --------
  118.  R RAW IN

  119. FUNCTION SUBSTR RETURNS RAW
  120.  Argument Name Type In/Out Default?
  121.  ------------------------------ ----------------------- ------ --------
  122.  R RAW IN
  123.  POS BINARY_INTEGER IN
  124.  LEN BINARY_INTEGER IN DEFAULT
  125. FUNCTION TRANSLATE RETURNS RAW
  126.  Argument Name Type In/Out Default?
  127.  ------------------------------ ----------------------- ------ --------
  128.  R RAW IN
  129.  FROM_SET RAW IN
  130.  TO_SET RAW IN

  131. FUNCTION TRANSLITERATE RETURNS RAW
  132.  Argument Name Type In/Out Default?
  133.  ------------------------------ ----------------------- ------ --------
  134.  R RAW IN
  135.  TO_SET RAW IN DEFAULT
  136.  FROM_SET RAW IN DEFAULT
  137.  PAD RAW IN DEFAULT

  138. FUNCTION XRANGE RETURNS RAW
  139.  Argument Name Type In/Out Default?
  140.  ------------------------------ ----------------------- ------ --------
  141.  START_BYTE RAW IN DEFAULT
  142.  END_BYTE RAW IN DEFAULT

DBMS_STATS.CONVERT_RAW_VALUE函式
  1. SQL> desc dbms_stats
  2. PROCEDURE CONVERT_RAW_VALUE
  3.  Argument Name Type In/Out Default?
  4.  ------------------------------ ----------------------- ------ --------
  5.  RAWVAL RAW IN
  6.  RESVAL VARCHAR2 OUT

  7. PROCEDURE CONVERT_RAW_VALUE
  8.  Argument Name Type In/Out Default?
  9.  ------------------------------ ----------------------- ------ --------
  10.  RAWVAL RAW IN
  11.  RESVAL DATE OUT

  12. PROCEDURE CONVERT_RAW_VALUE
  13.  Argument Name Type In/Out Default?
  14.  ------------------------------ ----------------------- ------ --------
  15.  RAWVAL RAW IN

  16.  RESVAL NUMBER OUT
  17. PROCEDURE CONVERT_RAW_VALUE
  18.  Argument Name Type In/Out Default?
  19.  ------------------------------ ----------------------- ------ --------
  20.  RAWVAL RAW IN
  21.  RESVAL BINARY_FLOAT OUT

  22. PROCEDURE CONVERT_RAW_VALUE
  23.  Argument Name Type In/Out Default?
  24.  ------------------------------ ----------------------- ------ --------
  25.  RAWVAL RAW IN
  26.  RESVAL BINARY_DOUBLE OUT

utl_raw、CONVERT_RAW_VALUE使用在 字元數值比對、統計資訊等指標數值轉換上
 

下面是簡單實驗。

  1. --TABLE存在4個不同型別的欄位
  2. SQL> DESC TABLE
  3.  Name                      Null?     Type
  4.  ------------------------- -------- ----------------------------
  5.  DIS_NUMBER                NOT NULL NUMBER(12)
  6.  RSVDC3                             VARCHAR2(16)
  7.  ORDER_PV                           NUMBER(12,2)
  8.  SALE_DATE                          DATE

  9. --統計資訊收集後,列的統計資訊如下。SQL執行計劃與索引的使用,會參考統計資訊獲得的值。
  10. --現在我們主要關注LOW_VALUE,HIGH_VALUE欄位的值。
  11. COLUMN_NAME    LOW_VALUE          HIGH_VALUE
  12. -------------- ------------------ ------------------------
  13. DIS_NUMBER     C102               C60A6464646464
  14. RSVDC3         3C6241395166       C40A4D4323
  15. ORDER_PV       3D582C5166         C4400707450B
  16. SALE_DATE      786D0305010101     78C70C04010101


使用utl_raw.CAST_TO_NUMBER函式獲取DIS_NUMBER欄位,LOW_VALUE與HIGH_VALUE的值。

  1. SQLselect utl_raw.CAST_TO_NUMBER('C102') low_num,utl_raw.CAST_TO_NUMBER('C60A6464646464') high_num from dual

  2.    LOW_NUM HIGH_NUM
  3. ---------- ----------------------
  4.          1 99999999999
  5.   
  6. --同樣,可以使用utl_raw.CAST_FROM_NUMBER函式轉換成資料庫的raw格式。
  7.   
  8. SQL>select utl_raw.CAST_FROM_NUMBER(1) LOW_VALUE,utl_raw.CAST_FROM_NUMBER(99999999999) HIGH_VALUE from dual
  9.   
  10.          LOW_VALUE  HIGH_VALUE
  11. ------------------ ------------------------
  12.              C102   C60A6464646464

同理,VARCHAR型別的值,也可使用utl_raw.CAST_TO_VARCHAR2 

DATE型別使用dbms_stats.convert_raw_value()函式。
  1. DECLARE
     rv RAW(32) := '786D0305010101';
     dt DATE := NULL;
    BEGIN
      dbms_stats.convert_raw_value(rv, dt);
      dbms_output.put_line('LOW_DATE: '||TO_CHAR(dt, 'YYYY-MM-DD hh24:mi:ss'));
    END;
    /

  2. LOW_DATE: 2009-03-05 00:00:00

下面自定義函式,利用dbms_stats.convert_raw_value函式,可快速獲得不同型別的真實值。
  1. create or replace function display_raw (rawval raw, type varchar2)
    return varchar2
    is
       cn     number;
       cv     varchar2(32);
       cd     date;
       cnv    nvarchar2(32);
       cr     rowid;
       cc     char(32);
    begin
      if (type = 'NUMBER') then
         dbms_stats.convert_raw_value(rawval, cn);
         return to_char(cn);
      elsif (type = 'VARCHAR2') then
         dbms_stats.convert_raw_value(rawval, cv);
         return to_char(cv);
      elsif (type = 'DATE') then
         dbms_stats.convert_raw_value(rawval, cd);
         return to_char(cd);
      elsif (type = 'NVARCHAR2') then
         dbms_stats.convert_raw_value(rawval, cnv);
         return to_char(cnv);
      elsif (type = 'ROWID') then
         dbms_stats.convert_raw_value(rawval, cr);
         return to_char(cnv);
      elsif (type = 'CHAR') then
         dbms_stats.convert_raw_value(rawval, cc);
         return to_char(cc);
      else
         return 'UNKNOWN DATATYPE';
      end if;
    end;
    /


    select
        a.column_name, a.partition_name,
        a.num_distinct,
        display_raw(a.low_value,b.data_type) as low_val,
        display_raw(a.high_value,b.data_type) as high_val,
        b.data_type
     from
        dba_part_col_statistics a, dba_tab_cols b
     where
       a.owner='&OWNER' and
       a.table_name='&TABLE_NAME' and
       a.table_name=b.table_name and
       a.column_name=b.column_name and
       a.low_value is not null
    order by 1, 2;

COLUMN_NAME                    PARTITION_NAME     NUM_DISTINCT LOW_VALUE          HIGH_VALUE               DATA_TYPE
------------------------------ ------------------ ------------ ------------------ ------------------------ ----------------
ALLOCATION_DIST_NUMBER         P_MOH_2012_10              4964 6737               9999520                  NUMBER
ALLOCATION_DIST_NUMBER         P_MOH_2012_11              4989 6737               9999956                  NUMBER
ALLOCATION_DIST_NUMBER         P_MOH_2012_12              5351 6737               9999936                  NUMBER
ALLOCATION_DIST_NUMBER         P_MOH_2012_6                 12 7007633            9961247                  NUMBER
ALLOCATION_DIST_NUMBER         P_MOH_2012_7                380 7000028            9996912                  NUMBER
CURRENCY_CODE                  P_MOH_2014_4                  3 HKD                RMB                      VARCHAR2
CURRENCY_CODE                  P_MOH_2014_5                  3 HKD                RMB                      VARCHAR2
CURRENCY_CODE                  P_MOH_2014_6                  3 HKD                RMB                      VARCHAR2
CURRENCY_CODE                  P_MOH_2014_7                  3 HKD                RMB                      VARCHAR2
INVOICE_SALES_DATE             P_MOH_2014_10                78 06-SEP-14          23-JAN-15                DATE
INVOICE_SALES_DATE             P_MOH_2014_11                77 14-OCT-14          26-JAN-15                DATE
INVOICE_SALES_DATE             P_MOH_2014_12                75 10-NOV-14          01-FEB-15                DATE
INVOICE_SALES_DATE             P_MOH_2014_2                 40 12-FEB-14          29-MAR-14                DATE
INVOICE_SALES_DATE             P_MOH_2014_3                 81 01-MAR-14          30-APR-14                DATE

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17086096/viewspace-1983619/,如需轉載,請註明出處,否則將追究法律責任。

相關文章