UTL_FILE - read and write file or Random Access of Files

automatically generate the loader control file:

--create the directory
CREATE OR REPLACE DIRECTORY UTL_DIR AS 'c:/oraload';

--write the loader control file
CREATE OR REPLACE PROCEDURE Write_File(text_context VARCHAR2,filename VARCHAR2)
IS
file_handle utl_file.file_type;
Write_content VARCHAR2(32767);
Write_file_name VARCHAR2(1000);
BEGIN
--open file
write_file_name := filename;
file_handle := utl_file.fopen('UTL_DIR',write_file_name,'w');
write_content := text_context;

--write file
IF utl_file.is_open(file_handle) THEN
utl_file.put_line(file_handle,write_content);
dbms_output.put_line(write_content);
END IF;

--close file
utl_file.fclose(file_handle);
EXCEPTION
WHEN OTHERS THEN
BEGIN
IF utl_file.is_open(file_handle) THEN
utl_file.fclose(file_handle);
END IF;
END;
END Write_File;
/


--generate the control file of table specified
CREATE OR REPLACE PROCEDURE generate_ldrFile(tablename IN VARCHAR2) AS
   l_curr_line       LONG;
   l_table_name      user_tables.table_name%TYPE;
BEGIN
   select   table_name
   into     l_table_name
   from     user_tables
   where    table_name =upper(tablename);
   l_curr_line := '
LOAD DATA
INFILE '''||lower(l_table_name)||'.txt'||''' '||'
Truncate INTO TABLE ';
  l_curr_line := l_curr_line||l_table_name||'
  FIELDS TERMINATED BY '||''''||','||''''||'
  TRAILING NULLCOLS
(';
   for rec in  ( select  table_name,column_name,column_id,nullable
   from    user_tab_columns
   where   table_name =upper(tablename)
   order by  column_id) loop
   if rec.column_id = 1 THEN
         IF rec.nullable = 'Y' THEN
      l_curr_line := l_curr_line||'
      '||rpad(rec.column_name||' NULLIF '||rec.column_name||'='||'''''',80);
         ELSE
      l_curr_line := l_curr_line||'
      '||rpad(rec.column_name,80);
         END IF;
    else
       IF rec.nullable = 'Y' THEN
      l_curr_line := l_curr_line||'
      ,'||rpad(rec.column_name||' NULLIF '||rec.column_name||'='||'''''',80);
       ELSE
      l_curr_line := l_curr_line||'
      ,'||rpad(rec.column_name,80);
       END IF;
  end if;
   end loop;
  l_curr_line := l_curr_line||')';
  Write_File(l_curr_line,l_table_name||'.ctl');
END generate_ldrFile;
/

--generate all control files
CREATE OR REPLACE PROCEDURE generate_All_ldrFile AS
BEGIN
  FOR x IN (SELECT table_name FROM user_tables) LOOP
      generate_ldrFile(x.table_name);
  END LOOP;
END generate_All_ldrFile;
/

--exec PROCEDURE
execute generate_All_ldrFile
exit;
//////////////////////////////////////////////////////////////////////////////////////////////

UTL_FILE - Random Access of Files

I was recently asked if I could read the first and last line of a file using PL/SQL. Until recently this was not possible without reading the whole file or using a Java stored procedure, but Oracle9i Release 2 supports random access of files through the UTL_FILE package. This article shows a simple mechanism to solve this problem using these UTL_FILE enhancements.

First we create a directory object pointing to the location of the file of interest:

CREATE OR REPLACE DIRECTORY my_docs AS '/usr/users/oracle/';

Prior to Oracle9i Release 2 I would solve this problem by reading the whole file as follows:

SET SERVEROUTPUT ON SIZE 1000000
DECLARE
  l_file         UTL_FILE.file_type;
  l_location     VARCHAR2(100) := 'MY_DOCS';
  l_filename     VARCHAR2(100) := 'temp';
  l_text         VARCHAR2(32767);
BEGIN
  -- Open file.
  l_file := UTL_FILE.fopen(l_location, l_filename, 'r', 32767);
  
  -- Read and output first line.
  UTL_FILE.get_line(l_file, l_text, 32767);
  DBMS_OUTPUT.put_line('First Line: |' || l_text || '|');

  -- Read through the file until we reach the last line.
  BEGIN
    LOOP
      UTL_FILE.get_line(l_file, l_text, 32767);
    END LOOP;
  EXCEPTION
    WHEN NO_DATA_FOUND THEN
      NULL;
  END;
  
  -- Output the last line.
  DBMS_OUTPUT.put_line('Last Line : |' || l_text || '|');

  -- Close the file.
  UTL_FILE.fclose(l_file);
END;
/

Using the UTL_FILE enhancements we can now do the following:

SET SERVEROUTPUT ON SIZE 1000000
DECLARE
  l_file         UTL_FILE.file_type;
  l_location     VARCHAR2(100) := 'MY_DOCS';
  l_filename     VARCHAR2(100) := 'temp';
  l_exists       BOOLEAN;
  l_file_length  NUMBER;
  l_blocksize    NUMBER;
  l_text         VARCHAR2(32767);
BEGIN
  UTL_FILE.fgetattr(l_location, l_filename, l_exists, l_file_length, l_blocksize);

  IF l_exists THEN
    -- Open file.
    l_file := UTL_FILE.fopen(l_location, l_filename, 'r', 32767);
    
    -- Read and output first line.
    UTL_FILE.get_line(l_file, l_text, 32767);
    DBMS_OUTPUT.put_line('First Line: |' || l_text || '|');
    UTL_FILE.FSEEK (l_file, l_file_length-1);
  
    -- Step backwards through the file until we reach the start of the last line.
    FOR i IN REVERSE 0 .. l_file_length-2 LOOP
      UTL_FILE.FSEEK (l_file, NULL, -2);
      UTL_FILE.get_line(l_file, l_text, 1);
      EXIT WHEN l_text IS NULL;
    END LOOP;
    
    -- Read and output the last line.
    UTL_FILE.get_line(l_file, l_text, 32767);
    DBMS_OUTPUT.put_line('Last Line : |' || l_text || '|');
  
    -- Close the file.
    UTL_FILE.fclose(l_file);
  END IF;
END;
/

The fgetattr procedure allows us to check that the file exists and return the file length. We then read the first line using the get_line procedure as normal. To get the last line we need to skip to the end of the file using the fseek procedure and work backwards until we hit a line terminator. The get_line procedure does not return line terminators so we detect it's presence by checking for the return of an empty line. We can then display the last line.

I'm not too sure about the performance of the fseek procedure. For large files it's often quicker to read the whole file which is a bit disappointing. Even so, if you need to move both backwards and forwards in the file, these enhancements may still be useful.

reference link:

http://www.psoug.org/reference/utl_file.html

你可能感兴趣的:(UTL_FILE - read and write file or Random Access of Files)