Use of UTL_FILE

Hi,
What is an advantage of UTL_FILE. I practiced following code but it didn't compiled. Could you please guide me use UTL_FILE. please check the following code.
warm Regards,
Venkat.
Practice code:
declare
v_filehandle UTL_FILE.File_type;
begin
v_filehandle:=UTL_FILE.fOPEN('/tmp/','utl_file.txt','w');
UTL_FILE.PUT_LINE(v_filehandle,'This is line 1');
for v_counter in 2..11 loop
UTL_FILE.PUTF(v_filehandle,'This is line %s!\n',v_counter);
end loop;
UTL_FILE.fCLOSE(v_filehandle);
end;
/

SQL> create directory dir1 as 'c:\temp';
Directory created.       
SQL> declare
  2   fp utl_file.file_type;
  3   str varchar2(100);
  4  begin
  5   fp := utl_file.fopen('DIR1','a.txt','r');
  6   loop
  7    utl_file.get_line(fp,str);
  8    dbms_output.put_line(str);
  9   end loop;
10  exception
11   when no_data_found then
12   dbms_output.put_line('--------'||chr(10)||'Reading Completed');
13   utl_file.fclose(fp);
14  end;
15  /
Hai Venkata..
Bye..
Reading Completed
PL/SQL procedure successfully completed.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

Similar Messages

  • Using a utl_file.file_type as a procedure argument

    Hi,
    I'm trying to pass a utl_file.file_type variable defined in Proc A to ProcB as fllows:
    Proc A:
    output_file utl_file.file_type;
    output_file := utl_file.fopen('E:\tmp\', 'traspas_merma.log', 'W');
    call ProcB(output_file);
    Proc B:
    Procedure ProcB(filestr IN utl_file.file_type) IS
    BEGIN
    utl_file.put_line(filestr, str);
    END;
    When I compile I obtain the following error:
    PLS-00363 Expression 'FILE' can be used as a destination assignment
    So, whoy should I define the ProcB spec?
    Thanks

    Here is a code snippet that works:
    declare
       procedure procb (filestr in utl_file.file_type)
       is
       begin
          utl_file.put_line (filestr, 'Some Text');
       end procb;
       procedure a
       as
          output_file   utl_file.file_type;
       begin
          output_file := utl_file.fopen ('TEMP', 'traspas_merma.log', 'W');
          procb (output_file);
       end a;
    begin
       a ();
    end;
    /Note the usage of a (valid) directory object instead of a directory path!

  • Use of UTL_FILE package

    Hi,
    I am trying to use the following command:
    UTL_FILE.FOPEN('/HOME/SCMS/DUNNING/OUTPUT/','DUNNING_ACCT_YYYYMMDD.PSC','W');
    however i get an error invalid directory name.
    can anyone please advise how to assign the location as the name of a local directory eg....D:\xyz

    You need to use CREATE DIRECTORY statement to create a directory and use that name in fopen.
    SQL> CREATE DIRECTORY mydir AS '/appl/gl/log';
    SQL> GRANT READ ON DIRECTORY mydir TO DBA;
    DECLARE
      V1 VARCHAR2(32767);
      F1 UTL_FILE.FILE_TYPE;
    BEGIN
      -- In this example MAX_LINESIZE is less than GET_LINE's length request
      -- so the number of bytes returned will be 256 or less if a line terminator is seen.
      F1 := UTL_FILE.FOPEN('MYDIR','MYFILE','R',256);
      UTL_FILE.GET_LINE(F1,V1,32767);
      UTL_FILE.FCLOSE(F1);
      -- In this example, FOPEN's MAX_LINESIZE is NULL and defaults to 1024,
      -- so the number of bytes returned will be 1024 or less if a line terminator is seen.
      F1 := UTL_FILE.FOPEN('MYDIR','MYFILE','R');
      UTL_FILE.GET_LINE(F1,V1,32767);
      UTL_FILE.FCLOSE(F1);
      -- In this example, GET_LINE doesn't specify a number of bytes, so it defaults to
      -- the same value as FOPEN's MAX_LINESIZE which is NULL in this case and defaults to 1024.
      -- So the number of bytes returned will be 1024 or less if a line terminator is seen.
      F1 := UTL_FILE.FOPEN('MYDIR','MYFILE','R');
      UTL_FILE.GET_LINE(F1,V1);
      UTL_FILE.FCLOSE(F1);
    END; This is an example from Oracle docs. You better go through that first.
    Cheers
    Sarma.

  • Setting a loop to write a file using the UTL_FILE

    I never done this using the UTL file, so I am going to ask, I need to create a file that is looks like this
    H xxxxxxxxxxxx (the header same for the all file)
    I Invoice Data (Information on Each account Number)
    D (invoice Dtail) Detail information in each account
    D
    D
    D
    I  Invoice Data (Information on Each account Number (record 2 ) diferrent Account #n )
    D (invoice Dtail) Detail information on account2
    D
    D
    D
    I  Invoice Data (Information on Each account Number (record 3 ) diferrent Account #n )
    D (invoice Dtail) Detail information on account3
    D
    D
    D
    File Footer
    Number on invoices
    And totals I have the H the header the I (invoice Data) and D (detail) in cursors
    I wonder how I can set up the loop, so the information will ended
    like
    The heade on top of the file, not problem here
    but each
    I Invoice Data
    needs to have the respectively
    D detail lines
    I hope this is clear!!

    If cursors x, y, and z map to H, I, and D in your file spec, you would need to nest the D cursor within the I cursor
    FOR   x in  Invoice_data_cur
       LOOP
             utl_file.put_line(outfile,
               RPAD ('I', 2, ' ')
             ||RPAD (x.spriden_id, 9)
             ||TO_CHAR (SYSDATE, 'MM/DD/RRRR')
             ||RPAD (x.balance1, 13, '0')
               ---more stuff
       FOR  y in invoice_detail_charges_cur(x.pidm) 
             LOOP
                 utl_file.put_line( outfile,
                  RPAD ('D', 2, ' ')
                ||RPAD (y.spriden_id, 9)
                ||RPAD ('Previous Balance', 16, ' ')
                ||RPAD (y.stvterm_desc, 9, ' ')
                ||RPAD (TO_CHAR (SYSDATE, 'MM/DD/RRRR'), 10)
                ||RPAD (y.tbbdetc_desc, 50, ' ')
                ||LPAD(NVL (y.tbraccd_amount, '0000000000000'), 13, '0')
           for z in invoice_detail_payments_cur(x.pidm)
              LOOP
                   utl_file.put_line( outfile,
                    RPAD ('D', 2, ' ')
                  ||RPAD (z.spriden_id, 9)
                  ||RPAD ('Pending Aid ', 16, ' ')
                  ||RPAD (z.STVTERM_DESC, 9, ' ')
                  ||RPAD (TO_CHAR (SYSDATE, 'MM/DD/RRRR'), 10)
                  ||RPAD (z.tbbdetc_desc, 50, ' ')
                  ||LPAD (NVL (z.tbraccd_amount, '0000000000000'), 13, '0')
              END LOOP;      
            END LOOP;
    END LOOP;From a code clarity standpoint, if your file spec uses H, I, and D as a sort of row-type identifier, I would strongly suggest that your loop variable follow that convention or spell out the row type. That is
    FOR h IN Invoice_data_cur
    LOOP
      <<write header>>
      FOR i IN invoice_detail_charges_cur(h.pidm)
      LOOP
        <<write invoice data>>
        FOR d IN invoice_detail_payments_cur( h.pidm )
        LOOP
          <<write invoice detail>>
        END LOOP;
      END LOOP;
    END LOOP;Justin

  • Write error while using the UTL_FILE.FILE_TYPE

    i get an exception 'ORA-06125 - write error' when trying to write to a flat file using utl_file.put_line function. The hint which i got out of trial and error was this 'write error' exception came out when the total character size exceeded 725-730 characters. I don't know if that is the real problem.. but when the character count is less than this.. it writes without any problem..
    Thanks in advance!!
    PK

    Try setting max_linesize in FOPEN function, for example
    p_file := utl_file.fopen( p_dir, p_filename, 'w', 4000 );
    maximum value is 32767. The default is approximately 1000 bytes.

  • Where to define directory used by UTL_FILE?

    Hello,
    I run Oracle 9.2 on Windows 2003 Server. I would like to create a trigger to write to an ascii file. So I would like to do something like:
    UTL_FILE.fopen ('\tmp', 'myfile', 'a');But where (file location) do I define the directory (in this case "\tmp") so I don't get this error:
    ORA-20000: ERROR: Invalid path for file or path not in INIT.ORA.I looked for all the INIT.ORA on disk and they seem to be binary.
    Thank you.

    Note the security risks:
    http://download-east.oracle.com/docs/cd/B19306_01/server.102/b14237/initparams225.htm#REFRN10230
    All users can read or write to all files specified by this parameter. Therefore all PL/SQL users must be trusted with the information in the directories specified by this parameter.
    Note the recommendation:
    http://download-east.oracle.com/docs/cd/B19306_01/em.102/b16231/db.htm#sthref893
    <quote>
    Action
    For Oracle 9i Release 1 and later, remove the UTL_FILE_DIR initialization parameter. Instead, use the CREATE DIRECTORY feature.
    </quote>

  • Using SQLLOader / UTL_File within Transaction?

    Hi all,
    We need to bulk load large amounts of data into tables within a transaction.
    The reason we want to do it within a transaction is so we don't have an extra connection, and to avoid the extra COMMIT.
    So, is UTL_File better for this, or SQLLoader, and can we make flexible enough to be able to use the same function or package to load datasets into different tables.
    thanks for any help
    Philip

    Hi,
    Data could be read from the external table and loaded on to any oracle table(which could be done as single transaction), and this oracle table you could do/have any DML, index, triggers etc. What the other person wants to do, is to bring in the data from an external source into to the database.
    I feel that SQLLDR would be a better option than UTL_FILE to load the data into oracle.
    Vijay

  • How to read a specific value or a portion of text using utl_file.

    hi,
    I have a small requirement which goes as follows. I have a text file which is a resultof the sql query and it contains 16 columns as PIPE delimited text . I am using the UTL_FILE package concept to read the data. In general when we use the UTL_FILE.GET_LINE we read the entire line of text. But i need to read the 10th column of the query or the PIPE delimited text .
    Please advice.
    My query goes something like this:
    declare
    f utl_file.file_type;
    s long;
    c number := 0;
    begin
    f := utl_file.fopen('ABC_EXTRACTS','sample1.txt','R');
    loop
    utl_file.get_line(f,s);
    insert into s values (s);
    c := c + 1;
    end loop;
    exception
    when NO_DATA_FOUND then
    utl_file.fclose(f);
    dbms_output.put_line('No. of rows inserted : ' || c);
    end;

    Why don't you use varchar2 instead of long data type. I doubt you can read a portion from a file using utl_file but after doing the fetch you can do substring over the varchar2 variable and retrieve just the 10th column.
    sample sql:
    If your DB is 10g or higher, you can use the below regular expression to retrieve value based on your need.
    PRAZY@11gR1> select regexp_substr('111|222|333|444|555|666|777|888|999|000|aaa|bbb|','[^|]+',1,10) from dual;
    REG
    000
    Elapsed: 00:00:00.00Btwn, if the text file has fixed number of columns at all time, why don't you use a external table instead?
    Regards,
    Prazy
    Edited by: Prazy on Mar 22, 2010 4:11 PM

  • Error writing data in a flat file using UTL_FILE feature

    Hi All,
    I have written a package which fetches data from four different cursors and inserts into a temporary table.
    Now this temporary table is used to write data in a file using the UTL_FILE feature.
    fhandler :=
    UTL_FILE.fopen (l_path,'Demand_Transactions_'
    || TO_CHAR (SYSDATE, 'YYYYMMDDHH24MI')
    || '.txt',
    'w',
    max_linesize => 32767
    This table has 62593 records and when it starts writing data into the file from the table it errors out after writing 30045 records with the error - ORA-01722: invalid number.
    Can anyome please advise me is it because of the max_linesize => 32767. If not then what can be the possible reason.
    Any help in this would be highly appreciated.
    Regards,
    Shruti

    891330 wrote:
    Hi All,
    I have written a package which fetches data from four different cursors and inserts into a temporary table.
    Now this temporary table is used to write data in a file using the UTL_FILE feature.
    fhandler :=
    UTL_FILE.fopen (l_path,'Demand_Transactions_'
    || TO_CHAR (SYSDATE, 'YYYYMMDDHH24MI')
    || '.txt',
    'w',
    max_linesize => 32767
    This table has 62593 records and when it starts writing data into the file from the table it errors out after writing 30045 records with the error - ORA-01722: invalid number.
    Can anyome please advise me is it because of the max_linesize => 32767. If not then what can be the possible reason.Max linesize is the number of characters in a line before a newline has to be issued, not the number of rows written.
    The error would indicate that you have a character to number conversion going on somewhere but the characters are not numeric so it's failing that conversion.
    This doesn't sound like a UTL_FILE issue, but more to do with how you are building up your strings to output to the file.
    You would need to show us your code that builds up the lines you are writing out, with details of the datatypes of any of the columns/variables included in that.

  • Invalid_path exception while using UTL_FILE.FOPEN

    Hi
    I am getting invalid_path exception while using the UTL_FILE.fopen subprogram. I tried finding out the reason but could not solve it. Please help.
    Below is my piece of code.
    create directory utldr as 'e:\utldir';
    declare
    f utl_file.file_type;
    s varchar2(200);
    begin
    dbms_output.put_line('1');
    f := utl_file.fopen('UTLDR','utlfil.txt','r');
    dbms_output.put_line('2');
    utl_file.get_line(f,s);
    dbms_output.put_line('3');
    utl_file.fclose(f);
    dbms_output.put_line('4');
    dbms_output.put_line(s);
    exception
    when utl_file.invalid_path then
    dbms_output.put_line('invalid_path');
    end;
    the result is:
    1
    invalid_path

    I am executing it from sys. The same user who created the directory.
    The output is as below:
    SELECT * FROM dba_directories
    OWNER     DIRECTORY_NAME     DIRECTORY_PATH
    SYS     MEDIA_DIR      d:\avale\rel4\demo\schema\product_media\
    SYS     LOG_FILE_DIR     d:\avale\rel4\assistants\dbca\logs\
    SYS     DATA_FILE_DIR     d:\avale\rel4\demo\schema\sales_history\
    SYS     EMP_DIR     E:\Oracle Directory
    SYS     REMOTED     \\10.1.1.12\oracle directory
    SYS     UTLDR     e:\utldir
    SELECT * FROM dba_tab_privs WHERE table_name='UTLDR'
    GRANTEE     OWNER     TABLE_NAME     GRANTOR     PRIVILEGE     GRANTABLE     HIERARCHY
    PUBLIC     SYS     UTLDR     SYS     READ     NO     NO

  • Samples using UTL_FILE package

    hello;
    I would like to use the utl_file package: fopen, put_line ...
    can you send me some samples using this package.
    thanks to your help
    mam
    null

    declare
    T1 UTL_FILE.FILE_TYPE;
    begin
    begin
    T1:= UTL_FILE.FOPEN
    ('/u02/applmgr/10.7p161/eye/3.3.1/out','filename.dat','w');
    EXCEPTION
    WHEN UTL_FILE.INVALID_PATH THEN
    DBMS_OUTPUT.PUT_LINE('Invalid Path');
    WHEN UTL_FILE.INVALID_MODE THEN
    DBMS_OUTPUT.PUT_LINE('Invalid Mode');
    WHEN UTL_FILE.INVALID_OPERATION THEN
    DBMS_OUTPUT.PUT_LINE('Invalid Operation');
    end;
    UTL_FILE.PUT(T1,'PROD DATE ');
    UTL_FILE.PUT(T1,'PRODUCT ');
    UTL_FILE.PUT_LINE(T1,'INTEREST TYPE ');
    UTL_FILE.PUT(T1,'LEASE NUMBER ');
    begin
    UTL_FILE.FCLOSE(T1);
    EXCEPTION
    WHEN UTL_FILE.WRITE_ERROR THEN
    DBMS_OUTPUT.PUT_LINE('write error');
    WHEN UTL_FILE.INVALID_FILEHANDLE THEN
    DBMS_OUTPUT.PUT_LINE('Invald File Handle');
    end;
    END;
    mamoudou (guest) wrote:
    : hello;
    : I would like to use the utl_file package: fopen, put_line ...
    : can you send me some samples using this package.
    : thanks to your help
    : mam
    null

  • Problem in using UTL_FILE Package

    Dear Mates,
    I am trying to make use of UTL_FILE for inseting data in to the tables by reading the contents of a text file. I have set the UTL_FILE_DIR parameter by giving the path. Still i am not able to insert the records to the table.
    The error I am getting is
    ERROR at line 1:
    ORA-06510: PL/SQL: unhandled user-defined exception
    ORA-06512: at "PRASANNA.LOADSTUDENTS", line 61
    ORA-06512: at line 1
    I have set the parameter UTL_FILE_DIR in INIT<SID>.ORA as
    UTL_FILE_DIR=C:\developer
    please help me out
    It will be helpful if you can mail me at [email protected]

    ORA-06512: at "PRASANNA.LOADSTUDENTS", line 61how looks like line 61 in your PRASANNA.LOADSTUDENTS function?
    what's happening there?
    From "Oracle Product Document Library":
    ORA-06512 at string line string
    Cause: Backtrace message as the stack is unwound by unhandled exceptions.
    Action: Fix the problem causing the exception or write an exception handler for this condition. Or you may need to contact your application administrator or database administrator.

  • Writing files using UTL_FILE package

    Greetings:
    I'm attempting to write simple text files on the server side using the UTL_FILE package.I can get it to write the file to a local drive on the Oracle server with no problems, but not a network drive. When trying to write to a network drive, the FOPEN function raises the UTL_FILE.INVALID_OPERATION exception. This is even with my UTL_FILE_DIR parameter set the * for all directories and I have "Full Control" permission on the directory. I am running in a NT Server/Wkstn environment. Anyone have any ideas why I can't write a file to a network drive?
    Thanks a lot,
    Chris Scopp

    Thanks for your response...
    I have set the UTL_FILE_DIR parameter... I've tried setting it to the * for all directories and also mapping a drive letter from the server to where I want to write the file and then explicitly naming this path in the UTL_FILE_DIR parameter. Neither works, I still get the INVALID_OPERATION exception raised on the FOPEN function. I'm convinced now that it does have something to do with NT because I have been able to do the same operation writing to a Win95/98 box and it works fine. I have "Full Control" to all places I'm trying to write to, any other ideas?
    Thanks a lot,
    Chris Scopp

  • How to do an inbound and outbound interfacing using UTL_FILE ?

    dear members,
    How can we do interfacing from a legacy system to oracle and vice versa using the UTL_FILE package. I mean how to do an INBOUND and OUTBOUND interfacing using utl_file.
    regards
    sandeep

    in/
    file/
    bad/
    done/
    out/
    file/
    bad/
    done/
    I would start by ftping / putting the file in in/file/ folder, once ftp has completed move it to complete/ folder and process the file. If errorrs are generated move the file over to bad folder. If completed successfully move over to done folder.
    Having said that you would use UTL_FILE_DIR init parameter to set your directory.
    ALTER SYSTEM SET UTL_FILE_DIR='directory1','directory2' scope=spfile;
    Then you would use the regular utl_file packages to read / write to the files.
    UTL_FILE.FOPEN and so on.

  • How to do inbound and outbound interfacing using UTL_FILE?

    dear members,
    How can we do interfacing from a legacy system to oracle and vice versa using the UTL_FILE package. I mean how to do an INBOUND and OUTBOUND interfacing using utl_file.
    regards
    sandeep

    Hi Sandeep,
    I do not know if I got your question correct.
    Here is my understanding of it, pls correct me if I am wrong:
    You want to read from and write to the OS from inside the DB? Right?
    There are differnt waysa to do so, depanding on your Oracle version:
    1. you can use the initialization parameter UTL_FILE_DIR to specify a directory on OS where you can read from and write to through the package UTL_FILE.
    2. You can use a directory object in the DB to manage read write access to the OS. This is also usable with the package UTL_FILE as location where to read from and write to.
    => CREATE DIRECTORY my_dir AS '/home/oracle/my_directory';
    GRANT read , write ON DIRECTORY my_dir TO scott;
    SELECT * FROM dba_directories;
    You need CREATE ANY DIRECTORY system privilege for this.
    You can use directory objects also for external tables.
    With these you can read from flat files in teh directory on OS with a select statement as if it were a table inside the DB.
    Does this go into the direction you were thinking of?
    Hope it helps for the first.
    Regards,
    Lutz

Maybe you are looking for

  • Facevook sharing in iWeb

    I'm definitely a layperson in dealing with web design. I hardly ever use iWeb but for the most part I get what I need. I've been pulling my hair out over this. I've googled it, found answers that were supposed to help, but in the end its not. I'm usi

  • IBA - Supporting Languages in Video file

    Hello Can any one adivse me about supporting languages in iBooks Author's video and audio file. In the book, it is written with English but Video and Audio are in different language. Language in audio and video - Myanmar (Burmese) The language was no

  • How Camera Raw and Various Profiles Handle Transition Into Overexposure

    With all the attention I've been paying to profiles lately, I have been opening a lot of images with bright blue colors (hence my recent post on trying to get better blue sky color).  One of the things I've noticed is that the transition into overexp

  • Create work order using BAPI_ALM_ORDER_MAINTAIN

    Does anybody know how to use BAPI_ALM_ORDER_MAINTAIN to create a maintenance order?  I did a where used and it isn't used by SAP.  The documentation makes it look like I can use it for create but I am not sure how to do that. Regards, Davis

  • When I'm trying to post something, profanity message is shown

    I'm trying to post a message about my laptop but I keep getting a message about profanity ? Any ideas.