Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert XMLTYPE in VARCHAR in ORACLE?

Tags:

oracle

I have two columns in my table(TRANSACTION) in ORACLE which are XMLTYPE(XML_IN and XML_OUT). My procedure is not working because I don't know how to convert them to VARCHAR or something(I just think that this is the error). My procedure is:

PROCEDURE SEARCH_XML
(
    P_ID_TRANSACTION IN TRANSACTION.ID_TRANSACTION%TYPE,

    P_CURSOR OUT T_CURSOR
)
IS
BEGIN

    OPEN P_CURSOR FOR

    SELECT T.XML_IN, T.XML_OUT
    FROM TRANSACTION T
    WHERE T.ID_TRANSACTION = P_ID_TRANSACTION;

END SEARCH_XML;

When I call this procedure error message in VisualStudio2008 is: "Unsupported oracle data type USERDEFINED encountered." Any idea how is this working?

like image 240
nemostyle Avatar asked Jan 27 '16 13:01

nemostyle


People also ask

How to select XMLType data in Oracle?

Selecting XML Data Using XMLType Methods. You can select XMLType data using PL/SQL, C, or Java. You can also use the XMLType methods getClobVal() , getStringVal() , getNumberVal(), and getBlobVal(csid) to retrieve XML data as a CLOB , VARCHAR , NUMBER , and BLOB value, respectively.

What is XMLType datatype?

The xml data type is a built-in data type in SQL Server and is somewhat similar to other built-in types such as int and varchar. As with other built-in types, you can use the xml data type as a column type when you create a table as a variable type, a parameter type, a function-return type, or in CAST and CONVERT.

What is Oracle XMLType?

XMLType is a system-defined opaque type for handling XML data. It as predefined member functions on it to extract XML nodes and fragments. You can create columns of XMLType and insert XML documents into it.

How do you check if a tag exists in XML in Oracle?

DECLARE myxml CLOB := ... BEGIN SELECT extractValue(XMLTYPE(myxml), '/order/suborders/item[1]') INTO firstSuborder FROM DUAL; IF (firstSuborder IS NULL) THEN dbms_output. put_line('suborder doesnt exist'); END IF; END; but I got ORA-19025: EXTRACTVALUE returns value of only one node .


1 Answers

XMLType has two methods: getStringVal() and getClobVal() which will convert the XML structure to their string representations (as a VARCHAR2 and CLOB respectively). Unless you know that your XML output is going to always be less than 4000 characters (bytes) then you will probably want to use getClobVal() like this:

PROCEDURE SEARCH_XML
(
    P_ID_TRANSACTION IN TRANSACTION.ID_TRANSACTION%TYPE,

    P_CURSOR OUT T_CURSOR
)
IS
BEGIN
  OPEN P_CURSOR FOR
    SELECT T.XML_IN.getClobVal() AS XML_IN,
           T.XML_OUT.getClobVal() AS XML_OUT
    FROM TRANSACTION T
    WHERE T.ID_TRANSACTION = P_ID_TRANSACTION;
END SEARCH_XML;
like image 101
MT0 Avatar answered Oct 10 '22 13:10

MT0