Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, ted.

Asked: July 05, 2000 - 11:50 am UTC

Last updated: August 04, 2010 - 11:19 am UTC

Version: 8i

Viewed 1000+ times

You Asked

tom, I am trying to use your sqlldr_exp with some modification

how do I extract corrected hh24.mi.ss using your sqlldr_exp ?
-i.e. how do I use decode to test date data_type and generated corrected select statement.
thnx as always
ted chyn
========================================

prompt select
select lower(column_name)||'||chr(124)||'
from user_tab_columns
where table_name = upper('$X') and
column_id != (select max(column_id) from user_tab_columns where
table_name = upper('$X'))



and Tom said...

Here is a revised SQLLDR_EXP script that does this:

$ cat sqlldr_exp
#!/bin/sh

if [ "$1" = "" ]
then
cat << EOF
usage: flat un/pw [tables|views]

example: flat scott/tiger emp dept

description: Select over standard out all rows of table or view with
columns delimited by tabs.
EOF
exit
fi


PW=$1
shift

for X in $*
do
sqlplus -s $PW << EOF > /tmp/flat$$.sql
set wrap off
set feedback off
set pagesize 0
set verify off


prompt prompt LOAD DATA
prompt prompt INFILE *
prompt prompt INTO TABLE $X
prompt prompt REPLACE
prompt prompt FIELDS TERMINATED BY '|'
prompt prompt (
select 'prompt ' || decode(column_id,1,'',',') ||
lower(column_name ) ||
decode(data_type, 'DATE',
' date ''DD-MON-YYYY HH24:MI:SS''', null )
from user_tab_columns
where table_name = upper('$X')
/
prompt prompt )
prompt prompt BEGINDATA


prompt select
select lower(column_name)||'||chr(124)||'
from user_tab_columns
where table_name = upper('$X') and
column_id != (select max(column_id)
from user_tab_columns where
table_name = upper('$X'))
/
select lower(column_name)
from user_tab_columns
where table_name = upper('$X') and
column_id =
(select max(column_id) from user_tab_columns where
table_name = upper('$X'))
/
prompt from $X
prompt /
prompt exit
exit
EOF
sqlplus -s $PW << EOF
set wrap off
set feedback off
set pagesize 0
alter session set nls_date_format='DD-MON-YYYY HH24:MI:SS';
set verify off
set trimspool on
set linesize 5000
start /tmp/flat$$.sql
exit
EOF
rm /tmp/flat$$.sql
done


When you run this now, it'll generate:



$ sqlldr_exp scott/tiger emp
LOAD DATA
INFILE *
INTO TABLE emp
REPLACE
FIELDS TERMINATED BY '|'
(
empno
,ename
,job
,mgr
,hiredate date 'DD-MON-YYYY HH24:MI:SS'

,sal
,comm
,deptno
)
BEGINDATA
7370|SMITH|CLERK|7902|17-DEC-1980 00:00:00|800||20
7499|ALLEN|SALESMAN|7698|20-FEB-1981 00:00:00|1600|300|30
7521|WARD|SALESMAN|7698|22-FEB-1981 00:00:00|1250|500|30
7566|JONES|MANAGER|7839|02-APR-1981 00:00:00|2975||20
7654|MARTIN|SALESMAN|7698|28-SEP-1981 00:00:00|1250|1400|30
7698|BLAKE|MANAGER|7839|01-MAY-1981 00:00:00|2850||30
7782|CLARK|MANAGER|7839|09-JUN-1981 00:00:00|2450||10
7788|SCOTT|ANALYST|7566|09-DEC-1982 00:00:00|3000||20
7839|KING|PRESIDENT||17-NOV-1981 00:00:00|5000||10
7844|TURNER|SALESMAN|7698|08-SEP-1981 00:00:00|1500|0|30
7876|ADAMS|CLERK|7788|12-JAN-1983 00:00:00|1100||20
7900|JAMES|CLERK|7698|03-DEC-1981 00:00:00|950||30
7902|FORD|ANALYST|7566|03-DEC-1981 00:00:00|3000||20
7934|MILLER|CLERK|7782|23-JAN-1982 00:00:00|1300||10


Rating

  (7 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

FLAT script

A reader, April 15, 2002 - 11:40 am UTC

Tom, I just bought your new book BEGINGING ORACLE Programming

Great book, simple and lucid.

Iam trying to use the flat.sql script. It outputs the contents of a table to the screen. But I want it to spool it to a file.

Can you suggest the changes.


Tom Kyte
April 16, 2002 - 7:27 am UTC

redirection. It works on all platforms.

FLAT was written to be run from the command line using redirection:

flat scott/tiger EMP > emp.dat



A reader, April 15, 2002 - 11:53 am UTC

Tom, I fixed the above request I had put in the above comment.

Instead what Iwant to know is

in the below two queries what do the below conditions imply, why are they there

1.column_id != (select max(column_id)
from user_tab_columns
where table_name = upper('&1'))
2.column_id = (select max(column_id)
from user_tab_columns
where table_name = upper('&1'))



select lower (column_name)||'||chr(44)||'
from user_tab_columns
where table_name = upper('&1')
and column_id != (select max(column_id)
from user_tab_columns
where table_name = upper('&1'))
/
select lower(column_name)
from user_tab_columns
where table_name = upper('&1')
and column_id = (select max(column_id)
from user_tab_columns
where table_name = upper('&1'))
/
thank you

Tom Kyte
April 16, 2002 - 7:31 am UTC

Just getting the commas right. Wanted all but the last column -- then wanted the last column.

Yes, there are 50 other ways to implement this query...

Very good script, but...

Tom, April 15, 2002 - 4:57 pm UTC

Tom,

I've tried to run the sqlldr_exp script, but I kept on getting the following error:

$sqlldr_exp username/password@DB EMPLOYEE
LOAD DATA
INFILE *
INTO TABLE eai_lookup
REPLACE
FIELDS TERMINATED BY '|'
(
)
BEGINDATA
from EMPLOYEE
*
ERROR at line 2:
ORA-00936: missing expression

Does anyone have the same problem? Please help.

Thanks,
Tom

Tom Kyte
April 16, 2002 - 7:38 am UTC

sqlldr_exp just writes sql to /tmp/

modify the script to NOT erase the generated script, check out the SQL and see whats wrong with it.

Thank you, but I think it would have pay attention column order.

OMATA Ken-ichi, April 24, 2002 - 9:36 am UTC

Very very useful. thank you Tom.
But I have looked following case.
...
FIELDS TERMINATED BY '|'
(
,ename
empno
,job
...
So, I think It would be append "order by column_id" phrase.
...
select 'prompt ' || decode(column_id,1,'',',') ||
lower(column_name ) ||
decode(data_type, 'DATE',
' date ''DD-MON-YYYY HH24:MI:SS''', null )
from user_tab_columns
where table_name = upper('$X')
order by column_id << here
/
...
select lower(column_name)||'||chr(124)||'
from user_tab_columns
where table_name = upper('$X') and
column_id != (select max(column_id)
from user_tab_columns where
table_name = upper('$X'))
order by column_id << here
/
...


Tom, it is useful

Ramesh, July 17, 2002 - 6:33 pm UTC

Hey Tom,

I would like to know how to select few columns from the flat file.

There are 9 fields in the following dat file.

123|TEST|1200|12/12/2001|30000|4000|Testing|Valid|Request

Just I want to insert only 4 fields ( f1, f3, f6, f9)

Can you please reply.

Thanks in advance for your kind reply.

RM

Tom Kyte
July 18, 2002 - 8:20 am UTC

use the FILLER keyword.

see
</code> http://asktom.oracle.com/~tkyte/SkipCols/index.html <code>


FLAT script

Balvinder Gill, July 15, 2005 - 10:53 am UTC

Tom,

This is a great script! I've been searching for most of the day for this.

Cheers,
Bal

Great

A reader, August 04, 2010 - 4:12 am UTC

How to put a condition on a data in this script like

1)We need to select rows only between 2006 and 2009
2)We need all TEXT data to appear in " "
Tom Kyte
August 04, 2010 - 11:19 am UTC

ok, go ahead, you have my permission???

here is some plsql that takes a query instead of just a tablename

http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:68212348056