Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
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
- Page 654 and 655: CHAPTER 13 ■ PARTITIONING 609 Row
- Page 656 and 657: CHAPTER 13 ■ PARTITIONING 611 So,
- Page 658 and 659: CHAPTER 13 ■ PARTITIONING 613 Aud
- Page 660 and 661: CHAPTER 14 ■ ■ ■ Parallel Exe
- Page 662 and 663: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 664 and 665: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 666 and 667: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 668 and 669: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 670 and 671: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 672 and 673: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 674 and 675: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 676 and 677: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 678 and 679: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 680 and 681: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 682 and 683: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 684 and 685: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 686 and 687: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 688 and 689: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 690 and 691: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 692 and 693: CHAPTER 14 ■ PARALLEL EXECUTION 6
- Page 694 and 695: CHAPTER 15 ■ ■ ■ Data Loading
- Page 696 and 697: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 698 and 699: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 700 and 701: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 702 and 703: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 706 and 707: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 708 and 709: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 710 and 711: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 712 and 713: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 714 and 715: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 716 and 717: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 718 and 719: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 720 and 721: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 722 and 723: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 724 and 725: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 726 and 727: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 728 and 729: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 730 and 731: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 732 and 733: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 734 and 735: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 736 and 737: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 738 and 739: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 740 and 741: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 742 and 743: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 744 and 745: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 746 and 747: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 748: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 751 and 752: 706 ■INDEX autonomous transaction
- Page 753 and 754: 708 ■INDEX CREATE ANY DIRECTORY f
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: