koolbinu
asked on
read data fro a flat file into oracle table using utl file package
I wanted to read the data from the flat file into an oracle table I want to use Utl file package because each time I will be getting a new input file ...............I have devised a way to capture the new file name so my safest bet is to use plsql procedure to fetch the flat file insert the data into a table using utl file package can somone help me
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
procedure YOUR_PROC is
sasha SYS.UTL_FILE.FILE_TYPE;
sanjeev VARCHAR2(1800);
begin
sasha := UTL_File.Fopen('D:\staff\c v','56789. txt', 'r');
LOOP
UTL_File.get_line(sasha,sa njeev);
-- FORMAT THE DATA in/from buffer sanjeev and insert in Oracle DB using INSERT statement
END LOOP
EXCEPTION
WHEN no_data_found THEN
UTL_File.Fclose(sasha);
WHEN UTL_File.invalid_filehandl e THEN ... UTL_File.Fclose(sasha);
-- invalid_filehandle - not a valid file handle
WHEN UTL_File.read_error THEN ... UTL_File.Fclose(sasha);
-- OS error occurred during read
WHEN UTL_File.invalid_operation THEN ... UTL_File.Fclose(sasha);
end YOUR_PROC ;
REMARK
Server security for PL/SQL file I/O consists of a restriction on the directories that can be accessed.
Accessible directories must be specified in the
instance parameter initialization file (INIT.ORA).
Specify the accessible directories for the UTL_FILE
functions in the initialization file
using the UTL_FILE_DIR parameter.
For example:
UTL_FILE_DIR = <directory name>
like in
UTL_FILE_DIR = C:\DATA\ORACLE\DATAFILES
Starting from 9i you can use Oracle directories instead:
create or replace directory XMLDIR as 'E:\ICW_TASKS\XML\SOURCE\' ;
grant read on directory xmldir to my_user with grant option;
sasha SYS.UTL_FILE.FILE_TYPE;
sanjeev VARCHAR2(1800);
begin
sasha := UTL_File.Fopen('D:\staff\c
LOOP
UTL_File.get_line(sasha,sa
-- FORMAT THE DATA in/from buffer sanjeev and insert in Oracle DB using INSERT statement
END LOOP
EXCEPTION
WHEN no_data_found THEN
UTL_File.Fclose(sasha);
WHEN UTL_File.invalid_filehandl
-- invalid_filehandle - not a valid file handle
WHEN UTL_File.read_error THEN ... UTL_File.Fclose(sasha);
-- OS error occurred during read
WHEN UTL_File.invalid_operation
end YOUR_PROC ;
REMARK
Server security for PL/SQL file I/O consists of a restriction on the directories that can be accessed.
Accessible directories must be specified in the
instance parameter initialization file (INIT.ORA).
Specify the accessible directories for the UTL_FILE
functions in the initialization file
using the UTL_FILE_DIR parameter.
For example:
UTL_FILE_DIR = <directory name>
like in
UTL_FILE_DIR = C:\DATA\ORACLE\DATAFILES
Starting from 9i you can use Oracle directories instead:
create or replace directory XMLDIR as 'E:\ICW_TASKS\XML\SOURCE\'
grant read on directory xmldir to my_user with grant option;
external tables approach:
reate directory med as 'c:\med' ; -- on main
GRANT READ ON DIRECTORY med TO lsmedical;
GRANT WRITE ON DIRECTORY med TO lsmedical;
drop table lsmedical.USERMEDBASIC_EXT ;
create table lsmedical.USERMEDBASIC_EXT (
PZN VARCHAR2(33),
NAME VARCHAR2(111) )
organization external
(type oracle_loader
default directory med
access parameters (records delimited by newline
fields terminated by '|')
location ('med-de-formatted.txt','m ed-de-insu lin-format ted.txt',
'med-us-formatted.txt','me d-us-insul in-formatt ed.txt'));
Now you can select from theExternal table.
reate directory med as 'c:\med' ; -- on main
GRANT READ ON DIRECTORY med TO lsmedical;
GRANT WRITE ON DIRECTORY med TO lsmedical;
drop table lsmedical.USERMEDBASIC_EXT
create table lsmedical.USERMEDBASIC_EXT
PZN VARCHAR2(33),
NAME VARCHAR2(111) )
organization external
(type oracle_loader
default directory med
access parameters (records delimited by newline
fields terminated by '|')
location ('med-de-formatted.txt','m
'med-us-formatted.txt','me
Now you can select from theExternal table.
ASKER
Thanks a lot for all your help the reason for using utl_file package is to automate the whole system ifI use orcle loader(external table) i have to specifty the name of the file in location
(location ('med-de-formatted.txt','m ed-de-insu lin-format ted.txt',
'med-us-formatted.txt','me d-us-insul in-formatt ed.txt')); )
or the infile :name_of_data_file over here the name of the file is dynamic so I am going for utl file.I devised a way to capture the name of the file from the front end asp.net and store it into an oracle table use this as a reference to
call ult_file package .........................P lease do suggest me by any chance can I use sql loader or external file ie changing the name of the input data file ...........based on the name of the file stored in the new table (captured from front end asp,net and stored in oracle table using stored proc)
(location ('med-de-formatted.txt','m
'med-us-formatted.txt','me
or the infile :name_of_data_file over here the name of the file is dynamic so I am going for utl file.I devised a way to capture the name of the file from the front end asp.net and store it into an oracle table use this as a reference to
call ult_file package .........................P
Just to add some complexity... How to read a remote location file. Lets assume that the file is not present in the database system. It is avialble in some other server.
Sql loader:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14215/part_ldr.htm#i436326
External Tables:
http://download.oracle.com/docs/cd/B19306_01/server.102/b14215/et_concepts.htm#g1017623