Using Text_io to Read Files in Oracle d2k

Source: Internet
Author: User

Suppose want to read a file from d2k client and want to store it content in Oracle database. But if you would insert row by row from the client to server it would take the more time and increase lot of network Traffic/round Trips.

the solution of this problem are to store the content of the text file into an array and th En pass it to database procedure and insert record through that procedure. Here are 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));
--Can extract the content from IArray (i) to insert values into multiple fields
--e.g. IArray (i). fieldname
--
End Loop;
Commit;
End;
/2) Now on d2k write a procedure to read the text file and store it into an array and pass it to this package you crated abo ve.
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.