How to read/write .CSV file into CLOB column in a table of Oracle 10g

I have a requirement which is nothing but a table has two column
create table emp_data (empid number, report clob)
Here REPORT column is CLOB data type which used to load the data from the .csv file.
The requirement here is
1) How to load data from .CSV file into CLOB column along with empid using DBMS_lob utility
2) How to read report columns which should return all the columns present in the .CSV file (dynamically because every csv file may have different number of columns) along with the primariy key empid).
eg: empid report_field1 report_field2
1 x y
Any help would be appreciated.

If I understand you right, you want each row in your table to contain an emp_id and the complete text of a multi-record .csv file.
It's not clear how you relate emp_id to the appropriate file to be read. Is the emp_id stored in the csv file?
To read the file, you can use functions from [UTL_FILE|] (as long as the file is in a directory accessible to the Oracle server):
    lt_report_clob CLOB;
    l_max_line_length integer := 1024;   -- set as high as the longest line in your file
    l_infile UTL_FILE.file_type;
    l_buffer varchar2(1024);
    l_emp_id report_table.emp_id%type := 123; -- not clear where emp_id comes from
    l_filename varchar2(200) := 'my_file_name.csv';   -- get this from somewhere
   -- open the file; we assume an Oracle directory has already been created
    l_infile := utl_file.fopen('CSV_DIRECTORY', l_filename, 'r', l_max_line_length);
    -- initialise the empty clob
    dbms_lob.createtemporary(lt_report_clob, TRUE, DBMS_LOB.session);
         utl_file.get_line(l_infile, l_buffer);
         dbms_lob.append(lt_report_clob, l_buffer);
         when no_data_found then
    end loop;
    insert into report_table (emp_id, report)
    values (l_emp_id, lt_report_clob);
    -- free the temporary lob
   -- close the file
end;This simple line-by-line approach is easy to understand, and gives you an opportunity (if you want) to take each line in the file and transform it (for example, you could transform it into a nested table, or into XML). However it can be rather slow if there are many records in the csv file - the lob_append operation is not particularly efficient. I was able to improve the efficiency by caching the lines in a VARCHAR2 up to a maximum cache size, and only then appending to the LOB - see [three posts on my blog|].
There is at least one other possibility:
- you could use [DBMS_LOB.loadclobfromfile|]. I've not tried this before myself, but I think the procedure is described [here in the 9i docs|]. This is likely to be faster than UTL_FILE (because it is all happening in the underlying DBMS_LOB package, possibly in a native way).
That's all for now. I haven't yet answered your question on how to report data back out of the CLOB. I would like to know how you associate employees with files; what happens if there is > 1 file per employee, etc.
Regards Nigel
Edited by: nthomas on Mar 2, 2009 11:22 AM - don't forget to fclose the file...

Similar Messages

  • How to read the CSV Files into Database Table

    Friends i have a Table called tblstudent this has the following fields Student ID, StudentName ,Class,Father_Name, Mother_Name.
    Now i have a CSV File with 1500 records with all These Fields. Now in my Program there is a need for me to read all these Records into this Table tblstudent.
    Note: I have got already 2000 records in the Table tblstudent now i would like to read all these CSV File records into the Table tblstudent.
    Please give me some examples to do this
    Thank your for your service

    1) Read the CSV file line by line using BufferedReader.
    2) Convert each line (record) to a List and add it to a parent List. If you know the columns before, you might use a List of DTO's.
    3) Finally save the two-dimensional List or the List of DTO's into the datatable using plain JDBC or any kind of ORM (Hibernate and so on).
    This article contains some useful code snippets to parse a CSV:

  • How to Upload a PDF file into BLOB column in a table using Forms 9i

    Can anyone tell me how to upload a PDF file from client system using File dialog window and store its content in BLOB column in a table. The file to be uploaded will be in client side.

    please, search a bit on the forum before do a question:
    Just searching by "upload blob pdf" ...
    How to batch upload PDF files into database BLOB

  • How to read a CSV file into the portal

    hi all,
    I want to read the content of CSV file that is avaliable in my desktop.
    plz help me with suitable code
    Edited by: Savitha S R on Jun 1, 2009 8:25 AM

    Please use this code for that
    REPORT  znkp_upload_csv line-size 400.
    DATA : v_filename TYPE string.
    PARAMETER : p_file LIKE rlgrap-filename DEFAULT 'C:\Documents and Settings\Administrator\Desktop\JV.csv' .
    *Types for Reading CSV file
    TYPES : BEGIN OF x_csv,
              text(400) TYPE c,
            END OF x_csv.
    *Global internal table for reading CSV file
    DATA : lt_csv TYPE TABLE OF x_csv.
    *Global work area for reading CSV file
    DATA : wa_csv LIKE LINE OF lt_csv.
    v_filename = p_file.
        filename                      = v_filename
      FILETYPE                      = 'ASC'
      HAS_FIELD_SEPARATOR           = ' '
      HEADER_LENGTH                 = 0
      READ_BY_LINE                  = 'X'
      DAT_MODE                      = ' '
      CODEPAGE                      = ' '
      IGNORE_CERR                   = ABAP_TRUE
      REPLACEMENT                   = '#'
      CHECK_BOM                     = ' '
      VIRUS_SCAN_PROFILE            =
      NO_AUTH_CHECK                 = ' '
      FILELENGTH                    =
      HEADER                        =
        data_tab                      = lt_csv
       file_open_error               = 1
       file_read_error               = 2
       no_batch                      = 3
       gui_refuse_filetransfer       = 4
       invalid_type                  = 5
       no_authority                  = 6
       unknown_error                 = 7
       bad_data_format               = 8
       header_not_allowed            = 9
       separator_not_allowed         = 10
       header_too_long               = 11
       unknown_dp_error              = 12
       access_denied                 = 13
       dp_out_of_memory              = 14
       disk_full                     = 15
       dp_timeout                    = 16
       OTHERS                        = 17
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    DATA : BEGIN OF item OCCURS 0 ,
            t1(20) TYPE c,
            t2(20) TYPE c,
            t3(20) TYPE c,
            t4(20) TYPE c,
            t5(20) TYPE c,
            t6(20) TYPE c,
            t7(20) TYPE c,
            t8(20) TYPE c,
           END OF item.
    DATA : txt(1) TYPE c. " 1-Header 2-Item
    LOOP AT lt_csv into wa_csv.
    split wa_csv-text at ',' into item-t1
    append item.
    clear item.
    Check ITEM TABLE

  • How to read\write text file into oracle database.

    I am having text file called getInfo in c:\temp folder. I would like to populate data from text file to data base. How to proceed. Could anybody help me out.

    Here is a web page with easy to follow instructions regarding external files:
    Now I understand what all the excitement is over external tables.
    "External tables can read flat files (that follow some rules) as though they were ordinary (although read-only) Oracle tables. Therefore, it is convenient to use external tables to load flat files into the DB." -from the above link.

  • How to Upload a PDF file into BLOB column in a table using Forms 6i

    Can anyone tell me how to upload a PDF file from client and store its content in BLOB column in a table. The file will be genered using reports and win be stored in db.The file to be uploaded will be in client side.

    If you are using version 9 or 10 use webutil.... Look in webutil.pll.
    Use either one these two fuctions...
    FUNCTION Client_To_DB( clientFile       in VARCHAR2,
                             tableName        in VARCHAR2,
                             columnName       in VARCHAR2,
                             whereClause      in VARCHAR2,
                             asynchronous     in BOOLEAN default FALSE,
                             callbackTrigger  in VARCHAR2 default NULL) return BOOLEAN;
      FUNCTION Client_To_DB_With_Progress
                         (   clientFile       in VARCHAR2,
                             tableName        in VARCHAR2,
                             columnName       in VARCHAR2,
                             whereClause      in VARCHAR2,
                             progressTitle    in VARCHAR2,
                             progressSubTitle in VARCHAR2,
                             asynchronous     in BOOLEAN default FALSE,
                             callbackTrigger  in VARCHAR2 default NULL) return BOOLEAN;

  • Read text file into clob column

    Dear Oracle users and Oracle support,
    I have a text file that includes hundreds of data entries. The format is below. What I need to do is read each entry (
    <DATALOAD ................</DATALOAD>) into the CLOB column as a table record. Then create a loop in the table to convert each record into XML data type. I have general idea on how to convert CLOB to XML. The difficult part to me is read the each entry in the text file to CLOB table. Please let me know what technique I should use. Any recommendation and sample code are welcome! I appreciate!
    <DATALOAD ................</DATALOAD>
    <DATALOAD ................</DATALOAD>
    <DATALOAD ................</DATALOAD>
    <DATALOAD ................</DATALOAD>

    RBYL wrote:
    Thank you for your response. What I want to achieve is read the text file into ClOB column. There are hundreds reocords in the text file. The format is below. Each entry is '<DATALOAD (sensitive data here, use....... instead)</DATALOAD>' that needs to be read into clob table as a record. That is basically what I need to achieve.
    <DATALOAD ................</DATALOAD>
    <DATALOAD ................</DATALOAD>
    hundreds of them here........
    <DATALOAD ................</DATALOAD>
    <DATALOAD ................</DATALOAD>So, is it really a text file or is it a well structured XML file?
    Just reading it into a CLOB to process is not likely to be the best way.
    If each line of the file is a record, then you're likely to be better using something like External Tables.
    If it's a structured XML file, then it can be read using CLOB functionality into an XMLTYPE datatype and then shredded down into relational table structures.
    Be more clear in what your requirements are and we can help you better.

  • Can we strore .CSV file into CLOB datatype

    can we strore .CSV file into CLOB datatype
    its giving me error ot hexa coonversion?
    can anyone provide sample code...
    when i m sending mail from oracle database when i send as blob object then nope but when i send attachment stored in table with clob column it gives me hex to raw conversion error
    folllowing is my code--
    CREATE OR REPLACE PROCEDURE com_maildata_prc1( pi_sender IN com_batch_contact_dtl.strreceivername%TYPE,
    pi_recipients IN com_batch_contact_dtl.stremailaddr%TYPE,
    pi_subject IN VARCHAR2 ,
    pi_text IN VARCHAR2 ,
    pi_filename IN VARCHAR2 ,
    pi_blob IN cLOB
    conn utl_smtp.connection;
    i NUMBER;
    len NUMBER;
    conn := demo_mail.begin_mail( sender => pi_sender,
    recipients => pi_recipients,
    subject => pi_subject,
    mime_type => demo_mail.MULTIPART_MIME_TYPE
    demo_mail.begin_attachment(conn => conn,
    mime_type => 'application/csv',
    inline => TRUE,
    filename => pi_filename,
    transfer_enc => 'base64'
    -- split the Base64 encoded attachment into multiple lines
    i := 1;
    len := DBMS_LOB.getLength(pi_blob);
    WHILE (i < len) LOOP
    IF(i + demo_mail.MAX_BASE64_LINE_WIDTH < len)THEN
    UTL_SMTP.Write_raw_Data (conn
    , UTL_ENCODE.Base64_Encode(
    DBMS_LOB.Substr(pi_blob, demo_mail.MAX_BASE64_LINE_WIDTH, i)));
    UTL_SMTP.Write_raw_Data (conn
    , UTL_ENCODE.Base64_Encode(
    DBMS_LOB.Substr(pi_blob, (len - i)+1, i)));
    END IF;
    UTL_SMTP.Write_Data(conn, UTL_TCP.CRLF);
    i := i + demo_mail.MAX_BASE64_LINE_WIDTH;
    demo_mail.end_attachment(conn => conn);
    conn => conn,
    data => pi_text,
    mime_type => 'text/csv');
    demo_mail.end_mail( conn => conn );
    Thanx in advance...
    Message was edited by:
    Message was edited by:

    Ignore "SQL*Loader-292: ROWS parameter ignored when an XML, LOB or VARRAY column is loaded" error
    after importing your csv file just change length CHAR(100000).
    ex: your column col1 CHAR(1000) to change CHAR(100000).
    and deploy your mapping and execute

  • How to covert a CSV file into a file of spreadsheet format(staroffice)?

    Hello everybody,
    I want to create a java that can convert CSV file into spreadsheet. But i dont have any idea how to create a Spreadsheet (i just know it have a Binary File format).
    So anyone can give me some reference or program sample, some advises ????
    Pls help

    set the content type as given below and PrintWriter class to write into excel sheet
    response.setHeader("Content-Disposition", "attachment; filename=" + "abc Download" + ".xls");

  • How to import an .csv file into the database?

    and can we code the program in JSP to import the.csv file into the database.

    It is better to use Java class to read the CSV file and store the contents in the database.
    You can use JSP to upload the CSV file to the server if you want, but don't use it to perform database operations.
    JSPs are good for displaying information on the front-end, and for displaying HTML forms, there are other technologies more suitable for the middle layer, back end and the database layer.
    So break you application into
    1) Front end - JSPs to display input html forms and to display data retrieved from the database.
    2) Middle layer - Servlets and JavaBeans to interact with JSPs. The code that reads the CSV file to parse it's contents should be a Java Class in the middle layer. It makes use of Java File I/O
    3) Database layer - Connects to the database using JDBC (Java Database Connectivity), and then writes to the database with SQL insert statements.
    Keeping the above concepts in mind, first build a simple JSP and get it to work,
    then research on Google , for Java File I/O , discover how to read a file,
    Then search on how to readh a CSV file using Java.
    After researching you should be able to read the CSV file line by line and store each line inside a Collection.
    Then research on Google, on how to write to the database using JDBC
    Write a simple program that inserts something to a dummy table in the database.
    Then, read the data stored in the Collection, and write insert statements for each records in the collection.

  • How to read a .csv file(excel format) using Java.

    Hi Everybody,
    I need to read a .csv file(excel) and store all the columns and rows in 2d arrays. Then I can do the rest of the coding myself. I would like it if somebody could post their code to read .csv files over here. The .csv file can have different number of columns and different number of rows every time it is ran. The .csv file is in excel format, so I don't know if that affects the code or not. I would also appreciate it if the classes imported are posted too. I would also like to know if there is a way I can recognize how many rows and columns the .csv file has. I need this urgently so I would be very grateful to anybody who has the solution. Thanks.
    Sincerely Taufiq.

    I used this
    BufferedReader in = new BufferedReader (new FileReader ("test.csv"));
    // and                
    StringTokenizer parser = new StringTokenizer (str, ", ");
                    while (parser.hasMoreTokens () == true)
                    { //crap }works like a charm!

  • Read a CSV file into Flex

    Hi all,
    I'm new to flex and have been asked to provide a small widget
    that will display the contents of a csv in a list. Can I use the
    HTTPService to open a csv file and then create an actionscript 3
    function to parse it? If so can anyone point me to a tutorial on
    how to parse a csv file please.
    Thanks in advance

    Hello Sir,
      I am new to flex and want to read a csv file and using below code but does not seems to be working. Can you please help?
      i use below code but did not work
    var file:File = evt.currentTarget as File;
             file = file.resolvePath(file.nativePath);
             var fileStream:FileStream = new FileStream();
   , FileMode.READ);     
             var fileData:String =  fileStream.readUTFBytes(fileStream.bytesAvailable);       
             var endings:Array = [File.lineEnding, "\n", "\r"];
    but for some reason it return "ÐÏ ࡱá" funny value. Any idea why don't i get the correct data from file.
    belwo is the csv file i am trying to open.
    Retention Marketing
    Monday, April 13th, 2009
    Retention Marketing
    Monday, April 13th, 2009
    Monday, April 13th, 2009
    Monday, April 13th, 2009
    Genesis Vizeum
    Monday, April 13th, 2009
    Genesis Vizeum
    Monday, April 13th, 2009
    Monday, April 13th, 2009
    Central Reproductions
    Monday, April 13th, 2009
    Central Reproductions
    Monday, April 13th, 2009
    Des Rosiers
    Monday, April 13th, 2009
    McMillan LLP
    Monday, April 13th, 2009

  • How to read a .CSV file using UTL_FILE

    How do i read a .csv file line by line using UTL_FILE?
    Thanks in advance

    ----do open the file logic
    ----Let's say this file is delimited by ','
    ---declare variables
    v_startPos number; -- starting position of field
    v_Pos number; -- position of string
    v_lenString number; -- length
    v_first_field varchar2(30);
    v_second_field varchar2(30);
    v_third_field varchar2(30);
    v_fourth_field varchar2(30);
    input_String varchar2(1000); -- buffer for each line of file
    ----Say you have a 4 column file delimited by ','
    delimitChar varchar2(1) := ','
    utl_file.get_line(input_file, input_String); -- get each line
    ---- this will get the first field as specified by the last number
    v_Pos := instr(input_String,delChar,1,1);
    v_lenString := v_Pos - 1;
    v_first_field := substr(input_String,1,v_lenString);
    v_startPos := v_Pos + 1;
    -- this will get the second field
    v_Pos := instr(inString,delChar,1,2);
    v_lenString := v_Pos - v_startPos;
    v_second_field := substr(input_String,v_startPos,v_lenString);
    v_startPos := v_Pos + 1;
    -- 3rd field
    v_Pos := instr(inString,delChar,1,3);
    v_lenString := v_Pos - v_startPos;
    v_third_field := substr(input_String,v_startPos,v_lenString);
    v_startPos := v_Pos + 1;
    -- last field -- there is no delimiter for last field
    v_Pos := length(input_String) + 1;
    v_lenString := v_Pos - v_startPos;
    v_fourth_field := substr(input_String,v_StartPos,v_lenString);
    WHEN no_data_found then
              fnd_file.put_line(FND_FILE.LOG, 'Last line so exit');
    end loop;

  • How do i import my local csv files into HANA database as temporary tables  using java program?

    I want to import my local csv file into database for further apply Join with other tables programmatic in JAVA

    Hi Vivek,
    Please go through the following blogs and video to resolve your issue.
    Loading large CSV files to SAP HANA
    HANA Academy - Importing Data using CTL Method - YouTube
    Hope it helps.

  • How to convert a csv file into excel report?

    I have a csv file, can any body help me how to convert it into an excel report.
    thanks in advance

    Search this board for "read CVS" and "write Excel". both are very common questions, you should be able to find answers very quickly.
    Hints: String.split(), Apache POI

Maybe you are looking for

  • Bluetooth connection with audi MMI iphone 4 drops after software update 5.0.1

    Hello, After uptdating my iPhone 4 with software 5.0.1, I have problems with my bluetooth connection in my car (2006 Audi MMI). When I make a call or someone calls me, after a minute or so, the connection is lost for 10- 15 seconds, and then reconnec

  • Does globals.jsa exist in JDeveloper

    Hi Guys, I've built some old jsps using JDeveloper (Build 1913) and want to bring them across using the JDeveloper Because 'globals.jsa' was automatically generated by the JDeveloper and when I tried to migrate the p

  • How to create a PreparedStatement/ResultSet with a blank record?

    I want to query my database and if no records are found I want to have a valid result set filled with blanks (or zeros) for the proper columns. For example, given this code: <pre> PreparedStatement statement = connection.prepareStatement( "SELECT * F

  • IR video tutorial for business users

    Hi There, I was wondering if anybody came across a good tutorial (preferably video) on IR for business/end users? I mean a tutorial that explains, in simple layman's terms, the low hanging features such as 1. How to user filters 2. How to select and

  • HT201363 How do I set a rescue email ??

    Anyone know how to set a rescue email !! :)