External table creation in oracle using csv from ftp location - sql

I am trying to load the csv file data into external_tables for this i have tried
create table ext_table_csv (
i Number,
n Varchar2(20),
m Varchar2(20)
)
organization external (
type oracle_loader
default directory ext_dir
access parameters (
records delimited by newline
fields terminated by ','
missing field values are null
)
location ('f
ile.csv')
)
reject limit unlimited;
but i got error as
Error starting at line 8 in command:
create table ext_table_csv (
i Number,
n Varchar2(20),
m Varchar2(20)
)
organization external (
type oracle_loader
default directory ext_dir
access parameters (
records delimited by newline
fields terminated by ','
missing field values are null
)
location ('f
ile.csv')
)
reject limit unlimited
Error at Command Line:15 Column:23
Error report:
SQL Error: ORA-06564: object DATA_DIR does not exist
06564. 00000 - "object %s does not exist"
*Cause: The named object could not be found. Either it does not exist
or you do not have permission to access it.
*Action: Create the object or get permission to access it.
what i have to do is i want to read the csv file from ftp location. Is there any way to acheive this?

You probably need to specify the location of the file.
location (FTP_DIRECTORY:'emp.dat')
Where FTP_DIRECTORY is an Oracle Directory object pointing to the location where you placed
the file.

Related

Extract data from file name and store it in a table using stored procedure

I have different types of files (pdf, csv, doc, txt) in a directory.
Name of the files are something like this:
John.Doe.19900101.TX.pdf //FirstName.LastName.DOB.StateOfResidence
Bruce.Banner.19700101.PA.doc
Steve.Rodgers.19760101.AR.csv
Tony.Stark.19901210.CA.txt
How to write a stored procedure in Oracle to read the files in a directory and extract FirstName, LastName, DOB, State and store it in a table in appropriate columns?
Ex: For the file John.Doe.19900101.TX.pdf, data should be extracted like this:
John in FirstName column
Doe in LastName column
19900101 in DOB column
TX in State column
whole file in CLOB column
You will have to work at the OS level to gather filenames from OS directory. Considering you are trying to get the information from Unix flavors; Following URL will help you grab the file listing into a table and even a view.
Code that you need is :
--drop directory SCRIPT_TEMP_DIR;
CREATE DIRECTORY SCRIPT_TEMP_DIR AS '/home/oracle/oracle_scripts'
;
GRANT EXECUTE ON DIRECTORY SCRIPT_TEMP_DIR TO USER_NAME
; -- Here USER_NAME will be your SCHEMA/USER NAME
drop table USER_NAME.home_directory purge;
CREATE TABLE USER_NAME.home_directory
(
filerecord VARCHAR2(15),
flink VARCHAR2(2),
fowner VARCHAR2(6),
fgroup VARCHAR2(8),
fsize VARCHAR2(32),
fdate_part1 VARCHAR2(16),
fdate_part2 VARCHAR2(16),
fdate_year_or_time VARCHAR2(16),
fname VARCHAR2(255)
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY SCRIPT_TEMP_DIR
ACCESS PARAMETERS
(
records delimited by newline
preprocessor SCRIPT_TEMP_DIR:'listing.sh'
fields terminated by whitespace
(
filerecord ,
flink ,
fowner ,
fgroup ,
fsize ,
fdate_part1 ,
fdate_part2 ,
fdate_year_or_time ,
fname
)
)
LOCATION ('listing.sh')
)
REJECT LIMIT UNLIMITED;
Once this is done, you just need to select from above table created.
SELECT *
FROM USER_NAME.home_directory;
Later you can apply substr/instr functions to split info. You may also use regex function to get the requried information.
SELECT fname,
regexp_substr(fname, '[^.]+', 1, 1) part1,
regexp_substr(fname, '[^.]+', 1, 2) part2,
regexp_substr(fname, '[^.]+', 1, 3) part3
FROM USER_NAME.home_directory;
And this gives you :
The required URL to follow is here
The code pasted above was modified where you need to change USER_NAME as well while granting permissions on directory.

ORA-29913: error in executing ODCIEXTTABLEOPEN callout

I am creating external table using hr schema but i get errors
"ORA-29913: error in executing ODCIEXTTABLEOPEN callout ORA-29400:
data cartridge error KUP-00554: error encountered while parsing access
parameters KUP-01005: syntax error: found "missing": expecting one of:
"column, (" KUP-01007: at line 4 column 3
29913. 00000 - "error in executing %s callout"
*Cause: The execution of the specified callout caused an error.
*Action: Examine the error messages take appropriate action."
----------------My Code-------------------
create directory ex_tab as 'C:\My Works\External Table';
create table strecords (
st_id number(4),
st_name varchar(10),
schl_name varchar(5),
st_city varchar(15),
st_year number(4)
)
ORGANIZATION EXTERNAL
(TYPE oracle_loader
DEFAULT DIRECTORY ex_tab
ACCESS PARAMETERS
(
RECORDS DELIMITED BY newline
FIELDS TERMINATED BY ','
REJECT ROWS WITH ALL NULL FIELDS
MISSING FIELDS VALUES ARE NULL
(
st_id number(4),
st_name char(10),
schl_name char(5),
st_city char(15),
st_year number(4)
)
)
LOCATION ('strecords.txt')
);
desc strecords;
select * from strecords;
This is my code, please check it and review it.
You have several issues here. The immediate one causing your problem is that you have the clauses in the wrong order, but you also have MISSING FIELDS instead of MISSING FIELD:
...
ACCESS PARAMETERS
(
RECORDS DELIMITED BY newline
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
REJECT ROWS WITH ALL NULL FIELDS
(
...
Then your field list contents have invalid data types for that part of the statement; you can just omit that entirely in this case as those match the table column definition.
If no field list is specified, then the fields in the data file are assumed to be in the same order as the fields in the external table.
So you can simplify it to:
create table strecords (
st_id number(4),
st_name varchar(10),
schl_name varchar(5),
st_city varchar(15),
st_year number(4)
)
ORGANIZATION EXTERNAL
(TYPE oracle_loader
DEFAULT DIRECTORY ex_tab
ACCESS PARAMETERS
(
RECORDS DELIMITED BY newline
FIELDS TERMINATED BY ','
MISSING FIELD VALUES ARE NULL
REJECT ROWS WITH ALL NULL FIELDS
)
LOCATION ('strecords.txt')
);
Some operations needed against an Oracle Bug called
Select From External Table Returns Errors ORA-29913 ORA-29400 KUP-554 KUP-1005 (Doc ID 302672.1)
When creating an external table, the access parameters should be specified in the following order:
Comments
Record Format Info
Field definitions
Specify Comments, Record Format Info and Field definitions in the correct order. Even inside the Record format Info, 'Records delimited by ...' clause should come before any other clause.
For more information, refer to the access_parameters clause.

Oracle loader - error defining access parameters (KUP-01005)

I'm having problem querying the following table loaded from a file.
CREATE TABLE "testTable"
(
"EAN" NUMBER(38,0),
"STOCK" NUMBER(38,0),
"SECCION" NUMBER(38,0)
)
organization external
( default directory xtern_data_dir
access parameters
( fields terminated by ';'
badfile xtern_data_dir:'testTable.bad'
logfile xtern_data_dir:'testTable.log'
discardfile xtern_data_dir:'testTable.dsc'
)
location ('0025_STOCK.csv')
)
But I'm getting the following error:
KUP-01005: syntax error: found "badfile": expecting one of: "column, enclosed, (, ltrim, lrtrim, ldrtrim, missing, notrim, optionally, rtrim, reject"
I have tried removing badfile, logfile and discardfile, but then I get another error, and I'm not sure how to make it work.
Please, help! Thanks in advance.
Try to use
CREATE TABLE "testTable"
(
"EAN" NUMBER(38,0),
"STOCK" NUMBER(38,0),
"SECCION" NUMBER(38,0)
)
organization external
( default directory xtern_data_dir
access parameters
( RECORDS DELIMITED BY newline
badfile xtern_data_dir:'testTable.bad'
logfile xtern_data_dir:'testTable.log'
discardfile xtern_data_dir:'testTable.dsc'
fields terminated by ';'
)
location ('0025_STOCK.csv')
)

example of an external table which accepts all records from a file

Please can any one tell me how to accept all records from a file so that no record should go to discard or bad file using external table concept.
Below is the code
CREATE TABLE ext_tab2 (
mprn CHAR(10))
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY IMPORT
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
BADFILE IMPORT:'test.bad'
LOGFILE IMPORT:'test.log'
FIELDS TERMINATED BY ','
(mprn char(10)))
LOCATION ('abc.txt')
)
PARALLEL 5
REJECT LIMIT UNLIMITED;
All the data from file('abc.txt') should get inserted into ext_tab2 table .

Error while loading file in Oracle

I have been trying to load a text file in oracle database using SQL loader.
Please find my query below:-
CREATE TABLE test_test
(
numb int
)
ORGANIZATION EXTERNAL
(
TYPE ORACLE_LOADER
DEFAULT DIRECTORY dir_test
ACCESS PARAMETERS
(
RECORDS DELIMITED BY NEWLINE
BADFILE 'testscript.bad'
DISCARDFILE 'testscript.dis'
LOGFILE 'testscript.log'
SKIP 0
(
numb int
)
)
LOCATION (dir_test:'test.txt')
)
REJECT LIMIT 0
PARALLEL (DEGREE DEFAULT INSTANCES DEFAULT)
NOMONITORING;
However I am getting the following errors when I try select * from test_test:
ERROR at line 1:
ORA-29913: error in executing ODCIEXTTABLEOPEN callout
ORA-29400: data cartridge error
KUP-00554: error encountered while parsing access parameters
KUP-01005: syntax error: found "(": expecting one of: "badfile, byteordermark,
characterset, data, delimited, discardfile, exit, fields, fixed, load, logfile,
nodiscardfile, nobadfile, nologfile, date_cache, processing, readsize, string,
skip, variable"
KUP-01007: at line 6 column 10
ORA-06512: at "SYS.ORACLE_LOADER", line 14
ORA-06512: at line 1
Please suggest wjhat is the probable reason for this ? I am trying to run this on sqlplus on unix environment. dir_test is the oracle directory which i created earlier
You are missing the keyword fields before (numb int) and int is not valid here.
CREATE TABLE test_test (numb int)
ORGANIZATION EXTERNAL (
TYPE ORACLE_LOADER
DEFAULT DIRECTORY dir_test
ACCESS PARAMETERS (
RECORDS DELIMITED BY NEWLINE
BADFILE 'testscript.bad'
DISCARDFILE 'testscript.dis'
LOGFILE 'testscript.log'
SKIP 0
FIELDS (numb integer))
LOCATION (dir_test:'test.txt'))
REJECT LIMIT 0
PARALLEL (DEGREE DEFAULT INSTANCES DEFAULT)
NOMONITORING;