Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
1) Use an editor (MS-Word, for example) that recognises carriage-return characters:
a) Ensure that all columns are delimited with single quotes and commas: Example 'Hunt','David','value-3','value-4',...
b) Replace all carriage returns with leading and trailing values for a valid Oracle INSERT statement. In Word, for example, following your invocation of a "global-replace" command ("Edit"..."Replace"), you would:
Find what: ^p
Replace with: );^pINSERT INTO table_name VALUES (
[Replace All]
c) Check the first and last lines of your delimited file to ensure that they comply with correct INSERT-command behaviour.
d) Save the file.
2) Execute the script your just created. From SQL*Plus: "@ScriptName.sql". The script should then insert all rows into the pre-existing table you specified in the "INSERT INTO..." statements.
REM **************************************************************
REM David L. Hunt (file author) distributes this and other
REM files/scripts for educational purposes only, to illustrate the
REM use or application of various computing techniques. Neither the
REM author nor Dasages, LLC makes any warranty regarding this
REM script's fitness for any industrial application or purpose nor is
REM there any claim that this or any similarly-distributed scripts
REM are error free or should be used for any purpose other than
REM illustration.
REM **************************************************************
accept x prompt "Enter the table to load: "
accept y prompt "Enter the flat file to read: "
set trimspool on
set verify off
set echo off
set feedback off
set heading off
set pagesize 0
spool temp.fil
select 'load data' from dual;
select 'infile "&y" replace' from dual;
select 'into table &x' from dual;
select 'fields terminated by ''"''' from dual;
select '(' from dual;
select decode (rownum,1,null,',') || column_name
from user_tab_columns
where table_name = upper('&x');
select ')' from dual;
prompt
spool off
prompt
prompt At the DOS prompt, enter:
prompt sqlldr <username>/<password> temp.fil
prompt
@genload
Enter the table to load: s_dept
Enter the flat file to read: tempascii.sql
load data
infile "tempascii.sql" replace
into table s_dept
fields terminated by '"'
(
ID
,NAME
,REGION_ID
)
At the DOS prompt, enter:
sqlldr <username>/<password> temp.fil
D:\oracle\ora92\bin\sqlldr test/test temp.fil
SQL*Loader: Release 9.2.0.4.0 - Production on Tue Nov 30 09:42:47 2004
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Commit point reached - logical record count 12
D:\
(As a DBA):
create directory MyFlatFiles as 'd:\dhunt\sqldba';
Directory created.
grant read on directory MyFlatFiles to public; -- or to specific user(s);
Grant succeeded.
(As regular user):
create table dept_ext
( id number,
name varchar2(50),
region_id number
)
organization external
( type oracle_loader
default directory MyFlatFiles
access parameters
(records delimited by newline
fields terminated by ','
)
location ('TempASCII.sql')
)
reject limit 1000;
Table created.
(Note: For simplicity, I removed the quotes surrounding the values in TempASCII.sql and used just commas to delimit values, thus the "fields terminated by ','" entry.)
col a heading "ID" format 99
col b heading "NAME" format a14
col c heading "REGION" format 9
select id a, name b, region_id c from dept_ext;
ID NAME REGION
--- -------------- ------
10 Finance 1
31 Sales 1
32 Sales 2
33 Sales 3
34 Sales 4
35 Sales 5
41 Operations 1
42 Operations 2
43 Operations 3
44 Operations 4
45 Operations 5
50 Administration 1
12 rows selected.