用System用户创建了一个目录
create directory test_dir as 'e:/develop/';
grant read,write on directory test_dir to scott;
用Scott用户登录后:
declare
src clob;
xmlfile UTL_FILE.FILE_TYPE;
length integer;
buffer varchar2(16384);
begin
src :=dbms_xmlquery.getXml('select * from emp');
length :=dbms_lob.getlength(src);
dbms_lob.read(src,length,1,buffer);
xmlfile :=utl_file.fopen('TEST_DIR','employees.xml','w');
utl_file.put(xmlfile,buffer);
utl_file.fclose(xmlfile);
EXCEPTION
WHEN utl_file.invalid_mode THEN
RAISE_APPLICATION_ERROR (-20051, 'Invalid Mode Parameter');
WHEN utl_file.invalid_path THEN
RAISE_APPLICATION_ERROR (-20052, 'Invalid File Location');
WHEN utl_file.invalid_filehandle THEN
RAISE_APPLICATION_ERROR (-20053, 'Invalid Filehandle');
WHEN utl_file.invalid_operation THEN
RAISE_APPLICATION_ERROR (-20054, 'Invalid Operation');
WHEN utl_file.read_error THEN
RAISE_APPLICATION_ERROR (-20055, 'Read Error');
WHEN utl_file.internal_error THEN
RAISE_APPLICATION_ERROR (-20057, 'Internal Error');
WHEN utl_file.charsetmismatch THEN
RAISE_APPLICATION_ERROR (-20058, 'Opened With FOPEN_NCHAR
But Later I/O Inconsistent');
-- WHEN utl_file.file_open THEN
-- RAISE_APPLICATION_ERROR (-20059, 'File Already Opened');
WHEN utl_file.invalid_maxlinesize THEN
RAISE_APPLICATION_ERROR(-20060,'Line Size Exceeds 32K');
-- WHEN utl_file.invalid_filename THEN
-- RAISE_APPLICATION_ERROR (-20061, 'Invalid File Name');
-- WHEN utl_file.access_denied THEN
-- RAISE_APPLICATION_ERROR (-20062, 'File Access Denied By');
-- WHEN utl_file.invalid_offset THEN
-- RAISE_APPLICATION_ERROR (-20063,'FSEEK Param Less Than 0');
WHEN others THEN
RAISE_APPLICATION_ERROR (-20099, 'Unknown UTL_FILE Error');
end;