Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005
CHAPTER 15 ■ DATA LOADING AND UNLOADING 653 Number to load: ALL Number to skip: 0 Errors allowed: 50 Bind array: 64 rows, maximum of 256000 bytes Continuation: none specified Path used: Conventional Table DEPT, loaded from every logical record. Insert option in effect for this table: INSERT Column Name Position Len Term Encl Datatype ------------------------------ ---------- ----- ---- ---- --------------------- DEPTNO FIRST * , CHARACTER DNAME NEXT * , CHARACTER LOC NEXT * , CHARACTER Table DEPT: 4 Rows successfully loaded. 0 Rows not loaded due to data errors. 0 Rows not loaded because all WHEN clauses were failed. 0 Rows not loaded because all fields were null. Space allocated for bind array: Read buffer bytes: 1048576 49536 bytes(64 rows) Total logical records skipped: 0 Total logical records read: 4 Total logical records rejected: 0 Total logical records discarded: 0 Run began on Sat Jul 16 10:59:06 2005 Run ended on Sat Jul 16 10:59:06 2005 Elapsed time was: 00:00:00.15 CPU time was: 00:00:00.03 The log file tells us about many of the aspects of our load. We can see the options we used (defaulted or otherwise). We can see how many records were read, how many loaded, and so on. The log file specifies the locations of all BAD and DISCARD files. It even tells us how long it took. The log file is crucial for verifying that the load was successful, as well as for diagnosing errors. If the loaded data resulted in SQL errors (i.e., the input data was “bad” and created records in the BAD file), these errors would be recorded here. The information in the log file is largely self-explanatory, so we will not spend any more time on it. Loading Data with SQLLDR FAQs We will now cover what I have found to be the most frequently asked questions with regard to loading data in an Oracle database using SQLLDR.
654 CHAPTER 15 ■ DATA LOADING AND UNLOADING How Do I Load Delimited Data? Delimited data, or data that is separated by some special character and perhaps enclosed in quotes, is the most popular data format for flat files today. On a mainframe, a fixed-length, fixed-format file would probably be the most recognized file format, but on UNIX and NT, delimited files are the norm. In this section, we will investigate the popular options used to load delimited data. The most popular format for delimited data is the comma-separated values (CSV) format. In this file format, each field of data is separated from the next by a comma. Text strings can be enclosed within quotes, thus allowing for the string itself to contain commas. If the string must contain a quotation mark as well, the convention is to double up the quotation mark (in the following code we use "" in place of just "). A typical control file to load delimited data will look much like our first example earlier, but the FIELDS TERMINATED BY clause would generally be specified like this: FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' It specifies that a comma separates the data fields, and that each field might be enclosed in double quotes. If we were to modify the bottom of this control file to be FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' (DEPTNO, DNAME, LOC ) BEGINDATA 10,Sales,"Virginia,USA" 20,Accounting,"Va, ""USA""" 30,Consulting,Virginia 40,Finance,Virginia when we run SQLLDR using this control file, the results will be as follows: ops$tkyte@ORA10G> select * from dept; DEPTNO DNAME LOC ---------- -------------- ------------- 10 Sales Virginia,USA 20 Accounting Va, "USA" 30 Consulting Virginia 40 Finance Virginia Notice the following in particular: • Virginia,USA in department 10: This results from input data that was "Virginia,USA". This input data field had to be enclosed in quotes to retain the comma as part of the data. Otherwise, the comma would have been treated as the end-of-field marker, and Virginia would have been loaded without the USA text. • Va, "USA": This resulted from input data that was "Va, ""USA""". SQLLDR counted the double occurrence of " as a single occurrence within the enclosed string. To load a string that contains the optional enclosure character, you must ensure the enclosure character is doubled up.
- Page 648 and 649: CHAPTER 13 ■ PARTITIONING 603 5 s
- Page 650 and 651: CHAPTER 13 ■ PARTITIONING 605 Sur
- Page 652 and 653: CHAPTER 13 ■ PARTITIONING 607 On
- 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 700 and 701: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 702 and 703: CHAPTER 15 ■ DATA LOADING AND UNL
- Page 704 and 705: 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
654<br />
CHAPTER 15 ■ DATA LOADING AND UNLOADING<br />
How Do I Load Delimited Data?<br />
Delimited data, or data that is separated by some special character <strong>and</strong> perhaps enclosed in<br />
quotes, is the most popular data format for flat files today. On a mainframe, a fixed-length,<br />
fixed-format file would probably be the most recognized file format, but on UNIX <strong>and</strong> NT,<br />
delimited files are the norm. In this section, we will investigate the popular options used to<br />
load delimited data.<br />
The most popular format for delimited data is the comma-separated values (CSV) format.<br />
In this file format, each field of data is separated from the next by a comma. Text strings can<br />
be enclosed within quotes, thus allowing for the string itself to contain commas. If the string<br />
must contain a quotation mark as well, the convention is to double up the quotation mark (in<br />
the following code we use "" in place of just "). A typical control file to load delimited data will<br />
look much like our first example earlier, but the FIELDS TERMINATED BY clause would generally<br />
be specified like this:<br />
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'<br />
It specifies that a comma separates the data fields, <strong>and</strong> that each field might be enclosed<br />
in double quotes. If we were to modify the bottom of this control file to be<br />
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'<br />
(DEPTNO, DNAME, LOC )<br />
BEGINDATA<br />
10,Sales,"Virginia,USA"<br />
20,Accounting,"Va, ""USA"""<br />
30,Consulting,Virginia<br />
40,Finance,Virginia<br />
when we run SQLLDR using this control file, the results will be as follows:<br />
ops$tkyte@ORA10G> select * from dept;<br />
DEPTNO DNAME<br />
LOC<br />
---------- -------------- -------------<br />
10 Sales Virginia,USA<br />
20 Accounting Va, "USA"<br />
30 Consulting Virginia<br />
40 Finance Virginia<br />
Notice the following in particular:<br />
• Virginia,USA in department 10: This results from input data that was "Virginia,USA".<br />
This input data field had to be enclosed in quotes to retain the comma as part of the<br />
data. Otherwise, the comma would have been treated as the end-of-field marker, <strong>and</strong><br />
Virginia would have been loaded without the USA text.<br />
• Va, "USA": This resulted from input data that was "Va, ""USA""". SQLLDR counted<br />
the double occurrence of " as a single occurrence within the enclosed string. To load a<br />
string that contains the optional enclosure character, you must ensure the enclosure<br />
character is doubled up.