Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 15 ■ DATA LOADING AND UNLOADING 659 For example, if we alter our DEPT table again: ops$tkyte@ORA10G> alter table dept add last_updated date; Table altered. we can load it with the following control file: LOAD DATA INFILE * INTO TABLE DEPT REPLACE FIELDS TERMINATED BY ',' (DEPTNO, DNAME, LOC, LAST_UPDATED date 'dd/mm/yyyy' ) BEGINDATA 10,Sales,Virginia,1/5/2000 20,Accounting,Virginia,21/6/1999 30,Consulting,Virginia,5/1/2000 40,Finance,Virginia,15/3/2001 The resulting DEPT table will look like this: ops$tkyte@ORA10G> select * from dept; DEPTNO DNAME LOC LAST_UPDA ---------- -------------- ------------- --------- 10 Sales Virginia 01-MAY-00 20 Accounting Virginia 21-JUN-99 30 Consulting Virginia 05-JAN-00 40 Finance Virginia 15-MAR-01 It is that easy. Just supply the format in the control file and SQLLDR will convert the date for us. In some cases, it might be appropriate to use a more powerful SQL function. For example, if your input file contains dates in many different formats: sometimes with the time component, sometimes without; sometimes in DD-MON-YYYY format; sometimes in DD/MM/YYYY format; and so on. You’ll learn in the next section how to use functions in SQLLDR to overcome these challenges. How Do I Load Data Using Functions? In this section, you’ll see how to refer to functions while loading data. Using functions in SQLLDR is very easy once you understand how SQLLDR builds its INSERT statement. To have a function applied to a field in a SQLLDR script, simply add it to the control file in double quotes. For example, say you have the DEPT table from earlier, and you would like to make sure the data being loaded is in uppercase. You could use the following control file to load it:

660 CHAPTER 15 ■ DATA LOADING AND UNLOADING LOAD DATA INFILE * INTO TABLE DEPT REPLACE FIELDS TERMINATED BY ',' (DEPTNO, DNAME "upper(:dname)", LOC "upper(:loc)", LAST_UPDATED date 'dd/mm/yyyy' ) BEGINDATA 10,Sales,Virginia,1/5/2000 20,Accounting,Virginia,21/6/1999 30,Consulting,Virginia,5/1/2000 40,Finance,Virginia,15/3/2001 The resulting data in the database will be as follows: ops$tkyte@ORA10G> select * from dept; DEPTNO DNAME LOC ENTIRE_LINE LAST_UPDA ------ -------------- ------------- ----------------------------- --------- 10 SALES VIRGINIA 01-MAY-00 20 ACCOUNTING VIRGINIA 21-JUN-99 30 CONSULTING VIRGINIA 05-JAN-00 40 FINANCE VIRGINIA 15-MAR-01 Notice how you are able to easily uppercase the data just by applying the UPPER function to a bind variable. It should be noted that the SQL functions could refer to any of the columns, regardless of the column the function is actually applied to. This means that a column can be the result of a function on two or more of the other columns. For example, if you wanted to load the column ENTIRE_LINE, you could use the SQL concatenation operator. It is a little more involved than that, though, in this case. Right now, the input data set has four data elements in it. If you were to simply add ENTIRE_LINE to the control file like this: LOAD DATA INFILE * INTO TABLE DEPT REPLACE FIELDS TERMINATED BY ',' (DEPTNO, DNAME "upper(:dname)", LOC "upper(:loc)", LAST_UPDATED date 'dd/mm/yyyy', ENTIRE_LINE ":deptno||:dname||:loc||:last_updated" ) BEGINDATA

CHAPTER 15 ■ DATA LOADING AND UNLOADING 659<br />

For example, if we alter our DEPT table again:<br />

ops$tkyte@ORA10G> alter table dept add last_updated date;<br />

Table altered.<br />

we can load it with the following control file:<br />

LOAD DATA<br />

INFILE *<br />

INTO TABLE DEPT<br />

REPLACE<br />

FIELDS TERMINATED BY ','<br />

(DEPTNO,<br />

DNAME,<br />

LOC,<br />

LAST_UPDATED date 'dd/mm/yyyy'<br />

)<br />

BEGINDATA<br />

10,Sales,Virginia,1/5/2000<br />

20,Accounting,Virginia,21/6/1999<br />

30,Consulting,Virginia,5/1/2000<br />

40,Finance,Virginia,15/3/2001<br />

The resulting DEPT table will look like this:<br />

ops$tkyte@ORA10G> select * from dept;<br />

DEPTNO DNAME LOC LAST_UPDA<br />

---------- -------------- ------------- ---------<br />

10 Sales Virginia 01-MAY-00<br />

20 Accounting Virginia 21-JUN-99<br />

30 Consulting Virginia 05-JAN-00<br />

40 Finance Virginia 15-MAR-01<br />

It is that easy. Just supply the format in the control file <strong>and</strong> SQLLDR will convert the date<br />

for us. In some cases, it might be appropriate to use a more powerful SQL function. For example,<br />

if your input file contains dates in many different formats: sometimes with the time<br />

component, sometimes without; sometimes in DD-MON-YYYY format; sometimes in DD/MM/YYYY<br />

format; <strong>and</strong> so on. You’ll learn in the next section how to use functions in SQLLDR to overcome<br />

these challenges.<br />

How Do I Load Data Using Functions?<br />

In this section, you’ll see how to refer to functions while loading data.<br />

Using functions in SQLLDR is very easy once you underst<strong>and</strong> how SQLLDR builds its<br />

INSERT statement. To have a function applied to a field in a SQLLDR script, simply add it to the<br />

control file in double quotes. For example, say you have the DEPT table from earlier, <strong>and</strong> you<br />

would like to make sure the data being loaded is in uppercase. You could use the following<br />

control file to load it:

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!