Oracle anydata資料型別

zhouwf0726發表於2019-05-21

Oracle anydata資料型別:

SQL> CONN TEST/TEST
已連線。
SQL> DESC ANYDATA

METHOD
------
 STATIC FUNCTION CONVERTNUMBER RETURNS ANYDATA
引數名稱                       型別                    輸入/輸出預設值?
------------------------------ ----------------------- ------ --------
 NUM                            NUMBER                  IN   

............

METHOD
------
 MEMBER FUNCTION ACCESSBDOUBLE RETURNS BINARY_DOUBLE

METHOD
------
 MEMBER FUNCTION ACCESSUROWID RETURNS ROWID


SQL> COL OBJECT_NAME FOR A30
SQL> SELECT object_name, object_type
  2  FROM dba_objects
  3  WHERE object_name like '%ANY%'
  4* AND wner = 'SYS';

OBJECT_NAME                    OBJECT_TYPE                                     
------------------------------ -------------------                             
/25dc84b4_SetDrawRectANY       JAVA CLASS                                      
/42700c8c_SetFillSpansANY      JAVA CLASS                                      
/69d73f42_SetDrawPolygonsANY   JAVA CLASS                                      
/acb51347_SetDrawLineANY       JAVA CLASS                                      
/c61bdc0a_SetFillRectANY       JAVA CLASS                                      
ANYDATA                        TYPE                                            
ANYDATA                        TYPE BODY                                       
ANYDATASET                     TYPE                                            
ANYDATASET                     TYPE BODY                                       
ANYTYPE                        TYPE                                            
ANYTYPE                        TYPE BODY                                       

OBJECT_NAME                    OBJECT_TYPE                                     
------------------------------ -------------------                             
DBMS_ANYDATASET_LIB            LIBRARY                                         
DBMS_ANYDATA_LIB               LIBRARY                                         
DBMS_ANYTYPE_LIB               LIBRARY                                         
STREAMS$_ANYDATA_ARRAY         TYPE                                            
SYS$RAWTOANY                   FUNCTION                                        

已選擇16行。

SQL> CREATE TABLE t (mycol sys.anyData);
CREATE TABLE t (mycol sys.anyData)
             *
第 1 行出現錯誤:
ORA-00955: 名稱已由現有物件使用

SQL> DROP TABLE T;

表已刪除。

SQL> CREATE TABLE t (mycol sys.anyData);

表已建立。

SQL>
SQL> desc t
 名稱                                      是否為空? 型別
 ----------------------------------------- -------- ----------------------------
 MYCOL                                              SYS.ANYDATA

SQL> INSERT INTO t VALUES (sys.anyData.convertNumber(5));

已建立 1 行。

SQL>
SQL> INSERT INTO t VALUES (sys.anyData.convertDate(SYSDATE));

已建立 1 行。

SQL>
SQL> INSERT INTO t VALUES (sys.anyData.convertVarchar2('hello world'));

已建立 1 行。

SQL>
SQL> COMMIT;

提交完成。

SQL> col typename format a20
SQL>
SQL> SELECT mytab.mycol.gettypeName() typeName
  2  FROM t mytab;

TYPENAME                                                                       
--------------------                                                           
SYS.NUMBER                                                                     
SYS.DATE                                                                       
SYS.VARCHAR2                                                                   

--不能直接顯示anydata型別資料:

SQL> SELECT * FROM T;

MYCOL()                                                                        
--------------------------------------------
ANYDATA()                                                                      
ANYDATA()                                                                      
ANYDATA()                                                                      

SQL> col getdata format a20

--建立函式來顯示:

SQL> CREATE OR REPLACE FUNCTION getData(p_x IN sys.anyData)
  2  RETURN VARCHAR2 IS
  3   l_num      NUMBER;
  4   l_date     DATE;
  5   l_varchar2 VARCHAR2(4000);
  6  BEGIN
  7    CASE p_x.gettypeName
  8    WHEN 'SYS.NUMBER' THEN
  9      IF (p_x.getNumber(l_num) = dbms_types.success) THEN
 10        l_varchar2 := l_num;
 11      END IF;
 12    WHEN 'SYS.DATE' THEN
 13      IF (p_x.getDate(l_date) = dbms_types.success) THEN
 14        l_varchar2 := l_date;
 15      END IF;
 16    WHEN 'SYS.VARCHAR2' THEN
 17      IF (p_x.getVarchar2(l_varchar2) = dbms_types.success) THEN
 18        NULL;
 19      END IF;
 20    ELSE
 21      l_varchar2 := '** unknown **';
 22    END CASE;
 23 
 24    RETURN l_varchar2;
 25  END getData;
 26  /

函式已建立。

SQL>
SQL> SELECT getData(mycol) GETDATA FROM t;

GETDATA                                                                        
--------------------                                                           
5                                                                              
10-7月 -08                                                                     
hello world                                                                    


 

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

相關文章