Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

View a SYS.XMLTYPE returned from an Oracle function, using PL/SQL

Tags:

xml

oracle

plsql

I have an Oracle function that dynamically creates an XML document, and returns it in a SYS.XMLTYPE value.

I want to run a query from SQL Developer that calls that function and prints the document (either via a select, or dbms_output - I don't care).

But all the examples/documentation seem to refer to querying XML columns in tables, and I can't seem to get the syntax right for my particular usage. I'd like something like this:

declare
   x SYS.XMLTYPE;
begin
   x := my_package.my_function();
   select x.getclobval() from x;  -- doesn't work!
end;

How can I print out the value of the XML type variable 'x' in the above code?

like image 718
David Avatar asked Apr 01 '10 11:04

David


People also ask

How do I view SYS Xmltype in SQL Developer?

To access the SYS. XMLTYPE 'type', you will need to access the 'Complex' datatype list and navigate to the 'SYS' schema. You can then set any of the XML specific options. You can read the nitty-gritty details on XMLTYPE here.

What is Oracle Sys 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 parse XML in PL SQL?

First approach: Load the XML file into an XML table and then parse it. First, create a table in Oracle that includes a column with data type XMLTYPE. For example, use the following code to create the table: CREATE TABLE xml_tab ( File_name varchar2(100), xml_data XMLTYPE );

How do you write a recursive function in PL SQL?

A recursive function is simply one that calls itself. SQL> create or replace 2 function factorial ( 3 n positive 4 ) return positive 5 is 6 begin 7 if n = 1 then 8 return n; 9 else 10 return n * factorial(n-1); 11 end if; 12 end; 13 / Function created. SQL> SQL> set serverout on SQL> begin 2 for i in 1..


2 Answers

Try this ( No guarantee, I haven't really used XML stuff )

declare
   x SYS.XMLTYPE;
begin
   x := my_package.my_function();
   dbms_output.put_line ( x.getCLOBVal() );
end;
like image 51
Matthew Watson Avatar answered Oct 19 '22 23:10

Matthew Watson


Here is a function which returns an XMLType...

SQL> create or replace function get_emp_xml
  2      (p_eno in emp.empno%type)
  3      return xmltype
  4  is
  5      return_value xmltype;
  6  begin
  7      select value(emprec) as "EMP_REC"
  8      into return_value
  9      from table (xmlsequence
 10                  (cursor
 11                      ( select * from emp e
 12                        where e.empno = p_eno
 13                       )
 14                   )
 15                  ) emprec
 16      ;
 17
 18      return return_value;
 19  end;
 20  /

Function created.

SQL>

Querying it from a SELECT statement is just as easy as you might hope it would be:

SQL> set long 5000
SQL>
SQL> select get_emp_xml(8060) from dual
  2  /

GET_EMP_XML(8060)
--------------------------------------------------------------------------
 <ROW>
  <EMPNO>8060</EMPNO>
  <ENAME>VERREYNNE</ENAME>
  <JOB>PLUMBER</JOB>
  <MGR>7839</MGR>
  <HIREDATE>08-APR-08</HIREDATE>
  <SAL>4500</SAL>
  <DEPTNO>50</DEPTNO>
 </ROW>


SQL>

This also works in SQL Developer.

like image 23
APC Avatar answered Oct 20 '22 00:10

APC