Monday, February 23, 2015

CREATING A FILE IN UNIX SERVER USING PLSQL CODE

--check for the accessable unix paths for utl_file using query..

select * From v$parameter where name = 'utl_file_dir';


--here utl_file_dir is a parameter created by dba in INIT.ora
--if you want to create your own path then contact your dba for permissions
--now try executing following query.

 /
DECLARE
  fileHandler UTL_FILE.FILE_TYPE;
  today varchar2(20):=to_char(sysdate);
  result_file varchar2(30);
BEGIN
   result_file:=today||'test_file.txt';
  fileHandler := UTL_FILE.FOPEN('/u04/trn2r12/db/tech_st/11.1.0/appsutil/outbound/trng2_training2', result_file, 'W');
  UTL_FILE.PUTF(fileHandler, 'Writing TO a file\n');
  UTL_FILE.FCLOSE(fileHandler);
EXCEPTION
  WHEN utl_file.invalid_path THEN
     raise_application_error(-20000, 'ERROR: Invalid PATH FOR file.');
END;
/


--here my file name is result_file variables value
--path is '/u04/trn2r12/db/tech_st/11.1.0/appsutil/outbound/trng2_training2'
--it is opened in write mode 'W'

No comments:

Post a Comment