Oracle PL/SQL: How to print a table type

Oracle has objects but it's ... different. Not exactly sure with your question if you want to see the values of the properties or if you want to actually see the type.


CREATE OR REPLACE TYPE MY_TYPE IS OBJECT ( 
  MyString Varchar(20)
  , counter Number(9) 
);

Now run some code for it.


DECLARE
    myType  MY_TYPE;
BEGIN
  myType := MY_TYPE('ABC123',0);
  -- To see the values reference the properties
  DBMS_OUTPUT.PUT_LINE(myType.mystring);
  -- To see they TYPE of the OBJECT
  DBMS_OUTPUT.PUT_LINE(SYS.ANYDATA.CONVERTOBJECT(myType).getTypeName());
END;

Of course you can create methods on the object to return information for you a bit easier.


CREATE OR REPLACE TYPE MY_TYPE IS OBJECT ( 
  MyString Varchar(20)
  , counter Number(9)
 , MEMBER FUNCTION getType RETURN VARCHAR2
 , MEMBER FUNCTION toString RETURN VARCHAR2
)
/

CREATE OR REPLACE TYPE BODY MY_TYPE 
AS
  MEMBER FUNCTION getTYPE RETURN VARCHAR2 IS
    BEGIN
      RETURN SYS.ANYDATA.CONVERTOBJECT(SELF).getTypeName();
    END;
  MEMBER FUNCTION toString RETURN VARCHAR2 IS
    BEGIN
      RETURN 'MY_TYPE('||self.mystring||','||self.counter||')';
    END;
END;
/

You can call the functions on the object now makes it easier to read imo.


DECLARE
  mytype    MY_TYPE;
BEGIN
  mytype := MY_TYPE('AGAIN','0');
  DBMS_OUTPUT.PUT_LINE(mytype.toString);
  DBMS_OUTPUT.PUT_LINE(mytype.getType);
END;

dbms_output.put_line(v_temp_tabtype(i).myString);

Tags:

Oracle

Plsql