原创作品,出自 “深蓝的blog” 博客,欢迎转载,转载时请务必注明以下出处,否则追究版权法律责任。
深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/45165707
举例一个行政区划的excel数据表,如下:
把文件保存成txt文档,如下:
使用PL/SQL Developer将数据导入到oracle中,在Tools栏找到Text importer选项,如下:
把txt文件路径添加到工具中,如下:
进行相关设置,如下:
点击Import按钮完成数据的导入。
勘误:
日期:2015年6月4日星期四
注意在上文中,在添加完txt文档这步以后,需要切换到“Data to Oracle”项目下,进行对应关系设置,参考如下图说明:
刚刚发现了这个错误,没来的及再从头到尾演示一遍了,暂时补充上了这一步说明一下,上图中只是把思路说明了一下。因为这一步的设置是必须的,否则无法完成字段的对应,如果不进行“Data to Oracle”设置。import按钮会为“灰”,是无法点击的。
今天把这部分的官方文档贴下面,可以参考下。
PL/SQL Developer官方文档该功能的官方文档说明如下:
21.7 Text Importer
With the text importer you can import ASCII files into the database. Most line oriented formats like
comma and tab separated fields are supported. The importer will try to determine the file format
automatically, so most of the time you don’t need to define anything, just select the file, select a table,
and that’s it.
When you open the text importer and load a text file, you will get something like the following:
The importer loads the first 100 lines from the file. This raw data is shown in the top “File Data”
section. The middle section allows you to configure the file definition. The bottom “Result Preview”
has a grid with the data as it would be imported.
PL/SQL Developer 10.0 User’s Guide 185
The toolbar has buttons to select a text file, or paste text from the clipboard. Files can be of virtually
unlimited size, they are not read entirely into memory. The “New” button will clear everything in the
text importer. The open and save definition buttons allow you to re-use the definition.
If you want to import the text into a new table, press the Create table button. This will invoke the Table
Definition Editor with a column definition that matches the configuration.
The configuration is auto determined as soon as you load a file, but if this is not correct, you can create
of modify this yourself. Set the Fieldcount to the correct number, and select a field from the field list to
define a field definition. In the file data section, the specified field data is highlighted, allowing you to
check if the field definition is correct. You have the following options to configure the text import
definition:
• General – Fieldcount
The number of fields per record.
• Gereral – End at line end
Indicates that the record data ends at the end of a line.
• General – Name in Header
Indicates if the first record holds the fieldnames.
• General – Skip empty lines
When enabled, empty lines in the text file will be skipped.
• General – Quote Character
Indicates the string delimiter, usually double or single quotes.
• General – Comment line
Here you can select a character or the characters that indicate a comment line in the text file.
• General – Import lines
The first and last lines in the text file that will be imported. If you leave this empty, the entire file
will be imported.
• Field Start – Relative Position
The field starts at a relative position to the end of the previous field. 0 indicates that the field starts
where the previous field ended.
• Field Start – Absolute Position
This indicates that the field starts at a fixed position.
• Field Start – Character
This indicates that the field starts with a specific character. This is relative to the end of the
previous field.
• Field End – Length
Indicates that the field has a fixed length.
• Field End – Character
Indicates that the field end with a specific character.
For standard comma separated files (csv) all fields will have a relative start position of 0, end the field
end character should be a comma. The line end (cr/lf or lf or cr) will also indicate field end, so for the
last field on a line, the end character doesn’t matter.
To filter the file contents you can enter any valid SQL expression in the Filter field. You can refer to a
field value through a bind variable with the field name. For example, to include only employees from
department 10 with a job other than 'CLERK', you can enter the following filter expression:
:deptno = 10 and :job <> 'CLERK'
186 PL/SQL Developer 10.0 User’s Guide
When you have a correct file definition, you need to select an Oracle table and indicate which text fields
should be imported in which Oracle fields. You can do this on the second tab page:
The top section has some general import parameters. The “Fields” section allows you to associate text
file fields and Oracle fields. The bottom section is the result preview for your information.
You can set the following general preferences:
• Owner / Table
The Oracle table (or view) you want to import the data to. After selecting a table, the importer will
try to determine the field and field types automatically. This will only work if the text file has a
header.
• Clear table
When enabled, the all records in the table will be deleted before the import.
• Commit every…
Indicates after how many records you want to do a commit. If you set this to 0, all data will be
committed at the end of the entire import.
• Overwrite Duplicates
A duplicate record will be updated in the database. Duplicates are based on the primary key
value(s) of the table.
• Ignore Duplicates
Duplicate records will be ignored.
• Initializing Script / Finalizing Script
These scripts will be executed before / after importing the text file. You can use any SQL
command or PL/SQL Block, separated by semi-colons or forward slashes.
PL/SQL Developer 10.0 User’s Guide 187
In the field definition you see a list with fields from the text file. For every field you can set the
following:
• Field
The Oracle field you want to associate with the text field. You can leave this empty if you don’t
want to import this field.
• Fieldtype
The basic fieldtype: String, Number or Date.
• SQL function
This option allows you to define additional SQL processing. For date fields a to_date function is
added automatically. This is a very powerful option, allowing you to convert the imported data.
Basically, you can enter anything that can be processed by Oracle. You can add a # to indicate the
data. The “Create SQL” button will fill this field with a to_date function for date fields.
When the definition is complete, you can decide to save it. The toolbar has buttons to save and load
definition files. The text importer will remember which definition file was used for which text file, and
the definition file will be loaded automatically the next time you open the same text file.
You have two import buttons, “Import” and “Import to Script”. The first option will start importing the
data into the selected table. The second option will create an SQL script with insert statements.
原创作品,出自 “深蓝的blog” 博客,欢迎转载,转载时请务必注明以下出处,否则追究版权法律责任。
深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/45165707