Using Text_IO To Read Files in Oracle D2k

來源:互聯網
上載者:User

標籤:varchar   into   one   jpg   box   inline   ora   traffic   app   

Suppose you want to read a file from D2k client and want to store its content in Oracle database. But if you will insert row by row from client to server it will take more time and increase lot of network traffic / round trips.

The solution of this problem is to store the content of the text file into an array and then pass it to database procedure and insert record through that procedure. Here is the example step by step:

1)  Create a package in Oracle database.
Create or Replace Package DB_insert
as
Type textrow is table of Varchar2(1000)
    index by binary_integer;

Procedure Insert_into_table (iarray in textrow);
End;
/

Create or Replace Package Body DB_insert 
as
Procedure Insert_into_table(iarray in textrow)
is
Begin
   For i in 1..iarray.count loop
       Insert into Dummytbl values (iarray(i));
       -- you can extract the content from iarray(i) to insert values into multiple fields
       -- e.g. iarray(i).fieldname
       --
   End Loop;
Commit;
End;
/2)   Now in D2k write a procedure to read text file and store it into array and pass it to that package you crated above.
Procedure Read_File (ifilename in Varchar2)
is
infile Text_IO.File_type;
irow DB_insert.textrow; 
nelm number := 1;
Begin
   infile := text_io.fopen(ifilename, ‘r‘);
Loop
     text_io.get_line(infile, irow(nelm));
     nelm := nelm + 1;
End Loop;
Exception
   when no_data_found then
     -- end of file reached
    text_io.fclose(infile);
   message(‘Read Completed.‘); 
   -- pass it to database
   DB_insert.insert_into_table(irow);
  message(‘Data saved.‘);
  when others then
    if text_io.is_open(infile) then
       text_io.fclose(infile);
    end if;
   message(sqlerrm);   
End;See also: Reading csv files with Text_io
http://foxinfotech.blogspot.com/2013/02/reading-and-importing-comma-delimited.html
   

Using Text_IO To Read Files in Oracle D2k

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.