tags:

views:

393

answers:

3

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?

+1  A: 

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;
Matthew Watson
I tried it using `x := XMLType( '<Test></Test>' );` and your attempt seems to work (+1)
Peter Lang
Weirdly, the simple case works fine (defining the variable in code like the example above). But it fails when I try it against my database - I just get error ORA-06502: PL/SQL: numeric or value error. Selecting from DUAL (answer below) seems to work fine.
A: 

Depending on the length of your XML, and the limitations of dbms_output.put_line in your version of Oracle, you may need to wrap it with a little bit of intelligence like so:

PROCEDURE put_string
(
    p_text IN CLOB
)
IS
    v_index INTEGER;
BEGIN

    IF p_text IS NULL THEN
        RETURN;
    END IF;

    v_index := instr(p_text, chr(10));

    IF v_index > 0 AND v_index < 256 THEN
        dbms_output.put_line(substr(p_text, 1, v_index-1));
        put_string(substr(p_text, v_index+1));
    ELSE
        IF length(p_text) <= 255 THEN
            dbms_output.put_line(p_text);
        ELSE
            dbms_output.put_line(substr(p_text, 1, 255));
            put_string(substr(p_text, 256));
        END IF;
    END IF;

END;
kurosch
+1  A: 

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.

APC
Yes, this works great.