You Asked
Tom --
I was wondering if you could provide an example of how to read from a file in PL/SQL, then sort it by, say, two fields - first name and zip code and read the result into variables.
thank you in advance!
AM
and Tom said...
don't use PLSQL!
use an EXTERNAL TABLE. it can't get any easier then this:
ops$tkyte@ORA9IR2> create or replace directory data_dir as '/tmp/'
2 /
Directory created.
ops$tkyte@ORA9IR2> host flat scott/tiger emp > /tmp/emp.dat
ops$tkyte@ORA9IR2> host head /tmp/emp.dat
7369,SMITH,CLERK,7902,17-DEC-80,800,,20
7499,ALLEN,SALESMAN,7698,20-FEB-81,1600,300,30
7521,WARD,SALESMAN,7698,22-FEB-81,1250,500,30
7566,JONES,MANAGER,7839,02-APR-81,2975,,20
7654,MARTIN,SALESMAN,7698,28-SEP-81,1250,1400,30
7698,BLAKE,MANAGER,7839,01-MAY-81,2850,,30
7782,CLARK,MANAGER,7839,09-JUN-81,2450,,10
7788,SCOTT,ANALYST,7566,19-APR-87,3000,,20
7839,KING,PRESIDENT,,17-NOV-81,5000,,10
7844,TURNER,SALESMAN,7698,08-SEP-81,1500,0,30
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> create table external_table
2 (EMPNO NUMBER(4) ,
3 ENAME VARCHAR2(10),
4 JOB VARCHAR2(9),
5 MGR NUMBER(4),
6 HIREDATE DATE,
7 SAL NUMBER(7, 2),
8 COMM NUMBER(7, 2),
9 DEPTNO NUMBER(2)
10 )
11 ORGANIZATION EXTERNAL
12 ( type oracle_loader
13 default directory data_dir
14 access parameters
15 ( fields terminated by ',' )
16 location ('emp.dat')
17 )
18 /
Table created.
ops$tkyte@ORA9IR2>
ops$tkyte@ORA9IR2> select empno, ename from external_table order by empno;
EMPNO ENAME
---------- ----------
7369 SMITH
7499 ALLEN
7521 WARD
7566 JONES
7654 MARTIN
7698 BLAKE
7782 CLARK
7788 SCOTT
7839 KING
7844 TURNER
7876 ADAMS
7900 JAMES
7902 FORD
7934 MILLER
14 rows selected.
ops$tkyte@ORA9IR2> select empno, ename from external_table order by ename;
EMPNO ENAME
---------- ----------
7876 ADAMS
7499 ALLEN
7698 BLAKE
7782 CLARK
7902 FORD
7900 JAMES
7566 JONES
7839 KING
7654 MARTIN
7934 MILLER
7788 SCOTT
7369 SMITH
7844 TURNER
7521 WARD
14 rows selected.
Rating
(21 ratings)
Is this answer out of date? If it is, please let us know via a Comment