Home » SQL & PL/SQL » SQL & PL/SQL » Reading a csv file (Oracle 10g)
Reading a csv file [message #604508] Tue, 31 December 2013 09:39 Go to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

HI All,

How it is possible to read comma separated file in Pl/Sql.

Regards,
Nathan
Re: Reading a csv file [message #604509 is a reply to message #604508] Tue, 31 December 2013 09:50 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
use UTL_FILE or EXTERNAL TABLE for file that resides on DB Server system
Re: Reading a csv file [message #604511 is a reply to message #604509] Tue, 31 December 2013 10:02 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Sir which one is better for reading the file and updating a table. Can you provide some link for doing that if possible.

Re: Reading a csv file [message #604512 is a reply to message #604511] Tue, 31 December 2013 10:06 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
Please read and follow the forum guidelines, to enable us to help you:

http://www.orafaq.com/forum/t/88153/0/

>Sir which one is better for reading the file and updating a table.
which metric measures better?
What table? First post only says that CSV file exists.

>Can you provide some link for doing that if possible.
unwilling or incapable to use GOOGLE or SEARCH yourself?

Re: Reading a csv file [message #604517 is a reply to message #604508] Tue, 31 December 2013 10:32 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
sss111ind wrote on Tue, 31 December 2013 21:09

How it is possible to read comma separated file in Pl/Sql.


It's a FAQ in any PL/SQL forum. Just doing a google search got millions of helpful links. Check this link
Re: Reading a csv file [message #604518 is a reply to message #604517] Tue, 31 December 2013 10:42 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thanks for Security reason that site you suggested I can not access. I got one linke but I have doubt like many file will be there one folder I have to read one by one and update a table. How can external table help here because each time we have to drop a recreated while reading. As follows

1.create the directory
2.store all the files in that directory
3.loop over all the files one by one and store a reference like filename in a file_table
4.loop over all file names in the file_table
4.create external table
5.update the base table
6.drop the external table
7.go to step 4 until the process is completed.

Please suggest what to do in the case.

http://asktom.oracle.com/pls/asktom/f?p=100:11:::::p11_question_id:464420312302
Re: Reading a csv file [message #604522 is a reply to message #604518] Tue, 31 December 2013 11:40 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3273
Registered: January 2010
Location: Connecticut, USA
Senior Member
First of all, you need to tell us if file resides on database server or on your client box. PL/SQL is database server side tool - it can't see files residing on client side.

SY.
Re: Reading a csv file [message #604560 is a reply to message #604522] Wed, 01 January 2014 05:16 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member


THE FILE IS FIRST residing AT remote server. THEN we are moving that INTO LOCAL DATABASE server.
So from there we have to start reading it.
Re: Reading a csv file [message #604564 is a reply to message #604560] Wed, 01 January 2014 05:40 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

From what has been told to you.

Re: Reading a csv file [message #604565 is a reply to message #604564] Wed, 01 January 2014 05:47 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Yes Michel, The file is residing in the database server only.So which is the good option whether external table or utl_file.
Re: Reading a csv file [message #604566 is a reply to message #604565] Wed, 01 January 2014 05:51 Go to previous messageGo to next message
Lalit Kumar B
Messages: 3174
Registered: May 2013
Location: World Wide on the Web
Senior Member
sss111ind wrote on Wed, 01 January 2014 17:17
So which is the good option whether external table or utl_file.


It depends on your requirement. The way you want to process the data.

Check this link

[edit : fixed link]

[Updated on: Wed, 01 January 2014 05:53]

Report message to a moderator

Re: Reading a csv file [message #604567 is a reply to message #604566] Wed, 01 January 2014 06:02 Go to previous messageGo to next message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

Thaks Lalit, That IS FOR THE best way TO writing A csv FILE. I want TO READ it.
Re: Reading a csv file [message #604571 is a reply to message #604567] Wed, 01 January 2014 07:41 Go to previous messageGo to next message
Michel Cadot
Messages: 68641
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

http://www.orafaq.com/forum/mv/msg/190911/604509/#msg_604509

Re: Reading a csv file [message #604672 is a reply to message #604571] Thu, 02 January 2014 10:17 Go to previous message
sss111ind
Messages: 634
Registered: April 2012
Location: India
Senior Member

--Loading the files into the directory and processing all using external table

--1.table for storing all file info present in the directory
create table file_list(file_name varchar2(100),file_status varchar2(1));

--Actual table which hold the final data along with the file name
 CREATE TABLE emp_temp_file
  (
    EMPNO     VARCHAR2(50) ,
    ENAME     VARCHAR2(50) ,
    JOB       VARCHAR2(50) ,
    MGR       VARCHAR2(50) ,
    HIREDATE  VARCHAR2(50) ,
    SAL       VARCHAR2(50) ,
    COMM      VARCHAR2(50) ,
    DEPTNO    VARCHAR2(50) ,
    FILE_NAME VARCHAR2(50)
  );


--2.create a directory for the actual location where files will be stored
CREATE OR REPLACE DIRECTORY  temp_dir AS '/home/oracle/files/';

--3.external table for loading the emp csv file(make sure atleast one file like emp structure must be there while creation  )
CREATE TABLE ext_emp_table
  (
    c1  VARCHAR2(100),
    c2  VARCHAR2(100),
    c3  VARCHAR2(100),
    c4  VARCHAR2(100),
    c5  VARCHAR2(100),
    c6  VARCHAR2(100),
    c7  VARCHAR2(100),
    c8  VARCHAR2(100)
  )
  ORGANIZATION EXTERNAL
  (
    TYPE oracle_loader
    DEFAULT DIRECTORY TEMP_DIR
    ACCESS PARAMETERS ( fields terminated BY ',' missing field VALUES are NULL )
    LOCATION ('emp_deptno_10.txt'));
	
--checking whether it got created successfully or not 
	select * from ext_emp_table; 

--4.Searching the specified directory and getting all the files and storing the filenames into a table
--java class to load the file name into the table

SET define OFF;
create or replace and compile java source named FileDirList as
       import java.io.*;
       import java.sql.*;
       import oracle.sql.*;
       import oracle.jdbc.*;
       public class FileDirList
		{
		   public static void getList(String directory) throws SQLException
		  {
			  File path = new File( directory );
			  String[] list = path.list();
			  String element;
			  for(int i = 0;i < list.length; i++)
			  {
				element = list[i];
				File temp=new File(path,element);
				if(temp.isFile()){
				   #sql{ Insert into file_LIST(file_name,file_status) values (:element,'N') };
				}
			  }
		 }
	   };
	   
--Procedure for loading the table with the file name present in the directory
create or replace procedure get_dir_list( p_directory in varchar2 )
  as language java
  NAME 'FileDirList.getList( java.lang.String )';
    
--4.Place the sample file into the directory(actual location) 
--For testing generation of sample csv file and put in the location /home/oracle/files/

select empno||','||ename||','||job||','||mgr||','||hiredate||','||sal||','||comm||','||deptno  file_name 
from emp where deptno=10;--emp_deptno_10.txt
select empno||','||ename||','||job||','||mgr||','||hiredate||','||sal||','||comm||','||deptno  file_name 
from emp where deptno=20;--emp_deptno_20.txt
select empno||','||ename||','||job||','||mgr||','||hiredate||','||sal||','||comm||','||deptno  file_name
 from emp where deptno=30;--emp_deptno_30.txt

 --5.Getting the actual content of the file to the actual table via external table
create or replace procedure load_file_info(p_file_path in varchar2) is 
BEGIN
DELETE FROM file_list;
COMMIT;
dbms_java.grant_permission( 'SCOTT', 'SYS:java.io.FilePermission', ''||p_file_path||''||'*', 'read' );
get_dir_list(''||p_file_path||'');
  FOR i IN (SELECT * FROM file_list where file_name like '%.txt%' )
  LOOP
    EXECUTE IMMEDIATE 'alter table ext_emp_table location ('''||i.file_name||''')';
    
    INSERT INTO emp_temp_file(EMPNO, ENAME, JOB, MGR, HIREDATE, SAL, COMM, DEPTNO,file_name) 
    SELECT t.*,i.file_name FROM ext_emp_table t;
    COMMIT;
    END loop;
end load_file_info;


--6.Final execution of the procedure for getting the data
--execute the procedure to load the data
exec load_file_info('/home/oracle/files/');

--Checking of actual table data
select * from emp_temp_file;


Previous Topic: Please help me to rewrite this
Next Topic: ORA-2391: exceeded simultaneous SESSIONS_PER_USER limit
Goto Forum:
  


Current Time: Fri Apr 19 09:35:26 CDT 2024