Search code examples
sqloracleplsqlutl-file

Error when trying to write to file


I have the following procedure which is supposed to write some data to a file.

CREATE OR REPLACE PROCEDURE export_to_xml
IS
  F UTL_FILE.FILE_TYPE;
  CURSOR c_cursor IS
    SELECT x FROM (select xmlelement( "user", xmlforest( uname, action, time ) ) as x from table_log);
  action c_cursor%ROWTYPE;
BEGIN
  F := UTL_FILE.FOPEN('XML', 'log.xml', 'w');
  OPEN c_cursor;
  LOOP
    EXIT WHEN c_cursor%NOTFOUND;
    FETCH c_cursor INTO action;
    UTL_FILE.PUT(F, action.x);
    UTL_FILE.NEW_LINE(F);
  END LOOP;
  CLOSE c_cursor;
  UTL_FILE.FCLOSE(F);
END;
/

But I get the following error when I try to compile the procedure

PLS-00306: wrong number or types of arguments in call to 'PUT'

Anyone knows what the cause of the problem might be? I know it's from UTL_FILE.PUT(F, action.x); but I don't see any other way to refer to the cursor.


Solution

  • xmlelement returns an XMLType. You can use getStirngVal() to convert it to a varchar so you can use it with put:

    CURSOR c_cursor IS
        SELECT x FROM (select xmlelement( "user", xmlforest( uname, action, time ) ).getStringVal() as x from table_log);
      action c_cursor%ROWTYPE;