Examples using UTL_FILE

Hi
Where can I to find examples using UTL_FILE, wirh Read an Write, Read a file, searcha data in tables, and Write in other file

this link about UTL_FILE has some examples.

Similar Messages

  • Error while using UTL_FILE

    I am getting the following error while using UTL_FILE procedure while using UTL_FILE.FOPEN procedure
    from system account. I am using Windows XP with NTFS.
    SQL> create or replace directory FILE_DIR as 'C:\'
    2 /
    Directory created.
    SQL> grant read on directory FILE_DIR to public;
    Grant succeeded.
    1 create or replace PROCEDURE file_upload IS
    2 v_file_name VARCHAR2(200);
    3 v_file_type UTL_FILE.FILE_TYPE;
    4 v_line VARCHAR2(1000);
    5 BEGIN
    6 v_file_name := 'customers_'||TO_CHAR(SYSDATE,'dd')||TO_CHAR(SYSDATE,'MON')||TO_CHAR(SYSDATE,'YYYY')||'.txt';
    7 v_file_type := UTL_FILE.FOPEN('FILE_DIR','V_FILE_NAME','r',1000);
    8 UTL_FILE.GET_LINE(v_file_type,V_LINE,1000);
    9 UTL_FILE.FCLOSE(v_file_type);
    10 DBMS_OUTPUT.PUT_LINE(V_LINE);
    11* END;
    SQL> exec file_upload;
    BEGIN file_upload; END;
    ERROR at line 1:
    ORA-29283: invalid file operation
    ORA-06512: at "SYS.UTL_FILE", line 475
    ORA-29283: invalid file operation
    ORA-06512: at "SYSTEM.FILE_UPLOAD", line 7
    ORA-06512: at line 1
    I appreciate if someone can help me in this regard.
    Thanks in Advance.
    Regards
    Muhammad ALi

    Hello,
    What exactly are you trying to do? Here I got 2 examples for you , replace with your directory name and pick right option for 'a' (append) or 'w' (write) or 'R' (read).
    To read from a directory_ . This will read an existing file 'customer.txt" from the directory
    {code}
    CREATE OR REPLACE PROCEDURE .file_upload
    IS
    v_file_name VARCHAR2 (200);
    v_file_type UTL_FILE.file_type;
    v_line VARCHAR2 (1000);
    BEGIN
    v_file_name := 'customers.txt';
    v_file_type := UTL_FILE.fopen ('EXT_TABLES', v_file_name, 'R');
    UTL_FILE.get_line (v_file_type, v_line); -- Error was here
    DBMS_OUTPUT.put_line (v_line);
    UTL_FILE.fclose (v_file_type);
    END;
    {code}
    _*To open a file*_ This will create customer_XXXXX file under the directory
    {code}
    CREATE OR REPLACE PROCEDURE testme.file_upload
    IS
    v_file_name VARCHAR2 (200);
    v_file_type UTL_FILE.file_type;
    v_line VARCHAR2 (1000);
    BEGIN
    v_file_name := 'customers_'
    || TO_CHAR (SYSDATE, 'dd')
    || TO_CHAR (SYSDATE, 'MON')
    || TO_CHAR (SYSDATE, 'YYYY')
    || '.txt';
    v_file_type := UTL_FILE.fopen ('EXT_TABLES', v_file_name, 'a');
    UTL_FILE.put_line (v_file_type, v_line);
    DBMS_OUTPUT.put_line (v_line);
    UTL_FILE.fclose (v_file_type);
    END;
    {code}
    Regards
    Edited by: OrionNet on Jan 29, 2009 12:30 AM

  • Reading all files on directory using "utl_file" package...

    I need to read all files in directory via PL/SQL. I don't know
    name files (are data dynamics create for automation system),
    only I know your extensions.
    Can I do this using the package "utl_file" or I need to create
    program in another language (C, C++, for example)?
    Any ideas...
    Thanks.

    Hi,
    you can't do that with the UTL_FILE package (it can't retrieve
    file names).
    A very simple solution would be, if you created on OS-level a
    file which contains the filenames of directory and then read this
    file using UTL_FILE. With the information on all file names you
    can enter a loop which opens and reads all files again using
    UTL_FILE.
    A more mundane solution could be to use the features on the iFS.
    Cheers
    Gerald

  • Using utl_file and unix pipes

    Hi,
    I'm trying to use utl_file and unix pipes to communicate with a unix process.
    Basically I want the unix process to read off one pipe and give me back the result on a different pipe.
    In the example below the unix process is a dummy one just copying the input to the output.
    I cant get this to work for a single plsql block writing and reading to/from the pipes - it hangs on the first read of the return pipe.
    Any ideas?
    ======== TEST CASE 1 ===============
    create directory tmp as '/tmp';
    on unix:
    cd /tmp
    mknod outpip p
    mknod inpip p
    cat < inpip > outpip
    drop table res;
    create table res (m varchar2(200));
    declare
    l_filehandle_rec UTL_FILE.file_type;
    l_filehandle_send UTL_FILE.file_type;
    l_char VARCHAR2(200);
    begin
    insert into res values ('starting');commit;
    l_filehandle_send := UTL_FILE.fopen ('TMP', 'inpip', 'A', 32000);
    insert into res values ('opened inpip ');commit;
    l_filehandle_rec := UTL_FILE.fopen ('TMP', 'outpip', 'R', 32000);
    insert into res values ('opened outpip ');commit;
    FOR i in 1..10 LOOP
    utl_file.put_line(l_filehandle_send,'line '||i);
    insert into res values ('written line '||i); commit;
    utl_file.get_line(l_filehandle_rec,l_char);
    insert into res values ('Read '||l_char);commit;
    END LOOP;
    utl_file.fclose(l_filehandle_send);
    utl_file.fclose(l_filehandle_rec);
    END;
    in a different sql session:
    select * from res:
    starting
    opened inpip
    opened outpip
    written line 1
    ============ TEST CASE 2 =================
    However If I use 2 different sql session (not what I want to do...), it works fine:
    1. unix start cat < inpip > outpip
    2. SQL session 1:
    set serveroutput on size 100000
    declare
    l_filehandle UTL_FILE.file_type;
    l_char VARCHAR2(200);
    begin
    l_filehandle := UTL_FILE.fopen ('TMP', 'outpip', 'R', 32000);
    FOR i in 1..10 LOOP
    utl_file.get_line(l_filehandle,l_char);
    dbms_output.put_line('Read '||l_char);
    END LOOP;
    utl_file.fclose(l_filehandle);
    END;
    3. SQL session 2:
    set serveroutput on size 100000
    declare
    l_filehandle UTL_FILE.file_type;
    begin
    l_filehandle := UTL_FILE.fopen ('TMP', 'inpip', 'A', 32000);
    FOR i in 1..10 LOOP
    utl_file.put_line(l_filehandle,'line '||i);
    --utl_lock.sleep(1);
    dbms_output.put_line('written line '||i);
    END LOOP;
    utl_file.fclose(l_filehandle);
    END;
    /

    > it hangs on the first read of the return pipe.
    Correct.
    A pipe is serialised I/O device. One process writes to the pipe. The write is blocked until a read (from another process or thread) is made on that pipe. Only when there is a reader for that data, the writer is unblocked and the actual write I/O occurs.
    The reverse is also true. A read on the pipe is blocked until another process/thread writes data into the pipe.
    Why? A pipe is a memory structure - not a file system file. If the write was not blocked the writer process can writes GBs of data into the pipe before a reader process starts to read that data. This will drastically knock memory consumption and performance.
    Thus the purpose of a pipe is to serve as a serialised blocking mechanism between a reader and a writer - allowing one to write data that is read by the other. With minimal memory overheads as the read must be serviced by a write and a write serviced by a read.
    If you're looking for something different, then you can open a standard file in share mode and write and read from it using two different file handles within the same process. However, the file will obviously have a file system footprint ito space (growing until the writer stops and the reader terminates and trashes the file) .
    OTOH a pipe's footprint is minimal.

  • How do I use UTL_FILE to insert a large number of fields to a file?

    Hi
    I am trying to use UTL_FILE for the first time in a Stored Procedure. I need to run a complex query to select 50 fields from various tables. I need these to be inserted into one line in the output file for all rows. Is this possible? My procedure so far is like the following
    CREATE OR REPLACE PROCEDURE PROC_TEST IS
    output_file UTL_FILE.FILE_TYPE;
    BEGIN
    FOR query in (SELECT FIELD1, FIELD2, ..........FIELD50)
    FROM TABLE A, TABLE B
    WHERE A.ID = B.ID
    ETC
    LOOP
    UTL_FILE.PUT_LINE(output_file, <put all 50 fields for all records into file> );
    END LOOP;               
    UTL_FILE.FCLOSE (output_file);
    EXCEPTION
    WHEN NO_DATA_FOUND THEN
    NULL;
    WHEN OTHERS THEN
         UTL_FILE.FCLOSE_ALL;
    RAISE;
    END PROC_TEST;
    Do I need to define 'query' (after the FOR) anywhere, also please advise with how I put all of the fields into the file.
    Thanks
    GB

    Thanks Steve,
    I have the UTL_FILE working fine now.
    I have other queries to run and conditions to apply in the same procedure, and I need to schedule via Enterprise Manager, therefore using UTL_FILE in a procedure seemed the best option. I looked up Data-pump but this seems to be an 11g feature, and we are still on 10g therefore I will not be able to use it.
    Thanks for your help.
    GB

  • 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

  • How to read a tab seperated data from a text file using utl_file

    Hi,
    How to read a tab seperated data from a text file using utl_file...
    I know if we use UTL_FILE.get_line we can read the whole line...but i need to read the tab separated value separately.....
    Thanks in advance...
    Naveen

    Naveen Nishad wrote:
    How to read a tab seperated data from a text file using utl_file...
    I know if we use UTL_FILE.get_line we can read the whole line...but i need to read the tab separated value separately.....If it's a text file then UTL_FILE will only allow you to read it a line at a time. It is then up to you to split that string up (search for split string on this forum for methods) into it's individual components.
    If the text file contains a standard structure on each line, i.e. it is a fixed delimited structure, then you could use external tables to read the data instead.

  • HOW TO READ DATA FROM A FILE AND INSERT INTO A TABLE USING UTL_FILE

    Hi..
    I have a file.I want to read the data from file and load it into a table using utl_file.
    how can I do it?
    Any reply apreciated...

    Hi,
    This is not your requirment but u can try this :
    CREATE OR REPLACE DIRECTORY text_file AS 'D:\TEXT_FILE\';
    GRANT READ ON DIRECTORY text_file TO fah;
    GRANT WRITE ON DIRECTORY text_file TO fah;
    DROP TABLE load_a;
    CREATE TABLE load_a
    (a1 varchar2(20),
    a2 varchar2(200))
    ORGANIZATION EXTERNAL
    (TYPE ORACLE_LOADER
    DEFAULT DIRECTORY text_file
    ACCESS PARAMETERS
    (FIELDS TERMINATED BY ','
    LOCATION ('data.txt')
    select * from load_a;
    CREATE TABLE A AS select * from load_a;
    SELECT * FROM A
    Regards
    Faheem Latif

  • Issue while accessing a file using UTL_FILE

    Hi,
    My requirement is to check for the existence of a file in the file system(apps tier) and then launch a conc program to load the data.
    There is a separate DB tier.
    I am trying to use UTL_FILE.fopen to indirectly see if the file exists
    and then proceed and I am trying to use UTL_FILE.fopen for that.
    The file is placed in the apps tier. I have created a directory in the database with the path and using that in the UTL_FILE.FOPEN call.
    However I am getting the following error. ORA-29283 - An attempt was made to read from a file or directory that does not exist, or file or directory access was denied by the operating system.
    What could be the issue here? Is there anything that I need to do after creating the directory?.
    Thanks,
    Balaji

    /Pl post details of OS, database and EBS versions.
    Are the Apps and DB servers physically separate ? If so, the directory on the Apps server where the file resides will have to be visible/updateable to the DB server in order for UTL_FILE to be able to read/write to it.
    HTH
    Srini/
    The apps and db servers are separate. EBS version is 11.5.10.2 and the OS is unix.
    How do I make the directory in apps server visible/updateable to the DB server?. I have created a directory in the database with the location of the file in apps tier.
    Thanks for your quick reply
    Regards
    Balaji

  • Concatenating file using utl_file pkg.

    Hi ,
    I have files generated every 10 mins. I have to concatenate all these files for every 5 hours and distribute that file to client locations.
    can u plz. tell me is it possible to concatenate small but huge number of files
    using utl_file package.
    Help needed !!
    Thanks in advance

    Hi Guido
    Thanks for the reply ..
    But can i call this shell script from oracle procedure ...
    As i have this files in DB server and i don't have my java there ...
    I have to invoke this shell script from oracle ...

  • Unable to read the file using UTL_FILE Dir

    I need to read/write a file from/to the operating system through PLSQL Package. I used UTL_FILE package to do the same. In the application server the utl_file_dir(/usr/tmp in my system) is created as a softlink to database server.
    oI want to run this package through the application server. However I am unable to read and write the file to the operating system.
    I just wondering it is not reading/opening the file from the server. Please suggest me how to modify my code.

    Hi,
    How to check if it is end of file or not? I have the following function which is working fine in my win2003 server std 32bit and Oracle10g Database R2 Standard Edition one 32bit. HOwever, when I deploy to production server which is 64bit OS and database, it gives error at UTL_FILE.GET_LINE(fptr, tmp);
    Any idea why it behaves like that?
         FUNCTION readFile (
              inHTML OUT CLOB,
              path IN VARCHAR2,
              htmlFile IN VARCHAR2
         ) RETURN BOOLEAN IS
         fptr utl_file.file_type;
         tmp VARCHAR2(5023);
         bufferlen BINARY_INTEGER;
         BEGIN
              fptr := UTL_FILE.FOPEN(path, htmlFile, 'r');
              DBMS_LOB.CREATETEMPORARY(inHTML, TRUE);
              DBMS_LOB.OPEN(inHTML, DBMS_LOB.LOB_READWRITE);
              LOOP
                   UTL_FILE.GET_LINE(fptr, tmp);
                   if tmp is not null then
                        tmp := tmp || CHR(10);
                        bufferlen := LENGTH(tmp);
                        DBMS_LOB.WRITEAPPEND(inHTML, bufferlen, tmp);
                   end if;
              END LOOP;
              WHEN NO_DATA_FOUND THEN
              DBMS_LOB.CLOSE(inHTML);
                   UTL_FILE.FCLOSE(fptr);
                   RETURN TRUE;
              WHEN OTHERS THEN
                   DBMS_LOB.CLOSE(inHTML);
                   DBMS_LOB.FREETEMPORARY(inHTML);
                   RETURN FALSE;
         END readFile;

  • Error while using UTL_FILE package

    I am getting error while using UTL_FILE package in apex 3.0 version
    Pls help me out.

    ok, how are you using UTL_FILE and what is the error?

  • Using UTL_FILE in NT with network drive

    I want to use utl_file for file handling and try to use util_file.fopen for opening file from a mapping network drive (say F with error "Invalid Operation".
    I've got no error when I handling file in local drive.
    Can anyone help ?
    Rgds,
    Edward
    (ps I've already set the parameter utl_file_dir=*)
    null

    Hello,
    In december an Edward TSE had a same kind of problem. I replied to him the following solution after which I was helped.
    Instead of using:
    declare
    fh_lis utl_file.file_type;
    begin
    fh_lis := utl_file.fopen(
    location => '\\caghk8\vtfp2\transfer\',
    filename => 'tpx.txt',
    open_mode => 'r');
    utl_file.fclose(fh_lis);
    end;
    Use:
    declare
    fh_lis utl_file.file_type;
    begin
    fh_lis := utl_file.fopen('\\caghk8\vtfp2\transfer\', 'tpx.txt', 'r');
    utl_file.fclose(fh_lis);
    end;
    He said it worked!
    null

  • Exporting data from text file to a table using utl_file

    Dear all,
    I have a text file as below and i have a table having 12 columns. Now i need to insert this text file into the table story_books.
    CREATE TABLE story_books
    book_id NUMBER,
    Category VARCHAR2(100 BYTE),
    Book_type VARCHAR2(100 BYTE),
    Name VARCHAR2(700 BYTE),
    Location VARCHAR2(700 BYTE),
    Ownership_code VARCHAR2(700 BYTE),
    Author VARCHAR2(700 BYTE),
    Less_Sel_fact VARCHAR2(700 BYTE),
    Reason VARCHAR2(700 BYTE),
    Buying VARCHAR2(700 BYTE),
    Suspected Book VARCHAR2(700 BYTE),
    Conditions VARCHAR2(700 BYTE)
    -------------------------text file---------------
    Books Out Table: Books
    Book. Type          Name          Location               Ownership Code
    Story               SL          hyd               SS-HYD
    Known Author:     Unknown               
    Less Selling Factors: Thunderstorms     
    Reason:     Unknown               
    Buying (if applicable):
    Not Applicable
    Suspected Book:
    Unknown
    Conditions to increace sales:
    Advertisement in all areas
    i was able to read the data and storing if it is in the same line.But i dont know how to read below data
    Book. Type          Name          Location               Ownership Code
    Story               SL          hyd               SS-HYD
    In this data i have to search for 'Book. type' and then i need to save the word 'Story' to the column 'Book_type'
    Then i need to search for 'Name' and i need to save 'SL' into the column into 'Name'
    Then i need to search for 'Location' and i need to save 'hyd' into the column into 'Location'
    I was able to extract the data if it is in below format using utl_file.get_line
    Known Author:     Unknown               
    Less Selling Factors: Thunderstorms     
    Reason:     Unknown     
    Any one can explain me how to solve the above criteria.
    Thanks in advance.

    Dear all,
    I have a text file as below and i have a table having 12 columns. Now i need to insert this text file into the table story_books.
    CREATE TABLE story_books
    book_id NUMBER,
    Category VARCHAR2(100 BYTE),
    Book_type VARCHAR2(100 BYTE),
    Name VARCHAR2(700 BYTE),
    Location VARCHAR2(700 BYTE),
    Ownership_code VARCHAR2(700 BYTE),
    Author VARCHAR2(700 BYTE),
    Less_Sel_fact VARCHAR2(700 BYTE),
    Reason VARCHAR2(700 BYTE),
    Buying VARCHAR2(700 BYTE),
    Suspected Book VARCHAR2(700 BYTE),
    Conditions VARCHAR2(700 BYTE)
    -------------------------text file---------------
    Books Out Table: Books
    Book. Type          Name          Location               Ownership Code
    Story               SL          hyd               SS-HYD
    Known Author:     Unknown               
    Less Selling Factors: Thunderstorms     
    Reason:     Unknown               
    Buying (if applicable):
    Not Applicable
    Suspected Book:
    Unknown
    Conditions to increace sales:
    Advertisement in all areas
    i was able to read the data and storing if it is in the same line.But i dont know how to read below data
    Book. Type          Name          Location               Ownership Code
    Story               SL          hyd               SS-HYD
    In this data i have to search for 'Book. type' and then i need to save the word 'Story' to the column 'Book_type'
    Then i need to search for 'Name' and i need to save 'SL' into the column into 'Name'
    Then i need to search for 'Location' and i need to save 'hyd' into the column into 'Location'
    I was able to extract the data if it is in below format using utl_file.get_line
    Known Author:     Unknown               
    Less Selling Factors: Thunderstorms     
    Reason:     Unknown     
    Any one can explain me how to solve the above criteria.
    Thanks in advance.

  • Need to generate the excel file with diffrent sheets using utl_file package

    Hi,
    Sorry for previous message in which I had missed the usage of " UTL_FILE " package
    I need to generate the excel file with diffrent sheets . Currently I am generating the data in three diffrent excel files using
    " UTL_File " package and my requirement is to generate this in a single excel file with diffrent sheets.
    Please help on this
    Thanks & Regards,
    Krishna Vyavahare

    Hello 10866107,
    at Re: How to save a query result and export it to, say excell? you can find links to different solutions. At least the packages behind second and fourth link support more than one worksheet.
    Regards
    Marcus

Maybe you are looking for

  • Need help getting my phoen to work again. It wont get out of reboot mode just stays on its reboot red eye screen.

    My MotorolaDroid Razr M was acting weird it wouldnt open apps. They would shut down automatically so I tried to reboot the phone and it stays in the red eye reboot mode and never goes to a usable mode. Help!

  • Error deploying engine ear

    Hi, I just stopped the engine ( to synchronize with directory services) but was unable to restart it. I tried creating engine ear and deploy it to cluster but am getting an error. not sure what has gone wrong. Kindly help Details of error: Unable to

  • Bridge not loading raw images from new camera

    I just got a new mark iii camera and bridge is not recognizing the preview. Other folders from my old camera (mark II) work fine. Any ideas? I tried a Google search and someone suggested purging the cache. That didn't work. I am running CS5 on a Mac.

  • "Automating" applications that really weren't designed to be automated?

    Hello folks. I recently purchased the American Heritage fourth edition dictionary and thesaurus, which comes with a CD-Rom with both products. It's a great dictionary and the one I want to use but it's an older application (I think it's called eRefer

  • Apps not loading after restore

    I recently had to restore my iPhone after disabling it by typing the wrong password too many times. I managed to get my apps, pictures etc back because of back-up, however, now my apps won't load. When I click on an app it starts up then just closes.