Syntax
IMPORT FROM [<file_type>] <file_path> [INTO <table_name>] [WITH <import_from_option_list>]
Syntax Elements
<file_type> ::= CSV FILE | CONTROL FILE
The type of the file to be imported. You can specify either comma-separated values or control file formats. For more information on CSV and control file formats, see Examples.
待導入文件的類型,有兩種類型的文件:
- CSV FILE :該文件存儲的為表數據
- CONTROL FILE:該文件是控制文件,即將導入的腳本寫在這個文件里,然后還是通過這個 IMPORT 語句執行這個腳本控制文件即可,這樣就不需要將導數的語句直接貼在SQL編輯器里運行了
<file_path> ::= <string_literal>
The complete path and file name of the file to import.
文件路(注意是服務器上的,不是本機哦)
<table_name> ::= [<schema_name>.]<identifier><schema_name> ::= <unicode_name>
The target table name, with optional schema name, where the imported data will be stored.
將數據導到哪個表中
WITH <import_from_option_list> ::= <import_from_option>[{, <import_from_option>}...] <import_from_option> ::= THREADS <number_of_threads> | BATCH <number_of_records_of_each_commit> | TABLE LOCK | NO TYPE CHECK | SKIP FIRST <number_of_rows_to_skip> ROW | COLUMN LIST IN FIRST ROW [<with_schema_flexibility>] | COLUMN LIST ( <column_name_list> ) [<with_schema_flexibility>] | RECORD DELIMITED BY <string_for_record_delimiter> | FIELD DELIMITED BY <string_for_field_delimiter> | OPTIONALLY ENCLOSED BY <character_for_optional_enclosure> | DATE FORMAT <string_for_date_format> | TIME FORMAT <string_for_time_format> | TIMESTAMP FORMAT <string_for_timestamp_format> | ERROR LOG <file_path_of_error_log> | FAIL ON INVALID DATA
You execute the following command to import the data using the control file.
You execute the following commands to import the data using a column list.
A list of import options.
導入選項
THREADS <unsigned_integer>
The number of threads that can be used for concurrent import. The default value is 1 and maximum allowed is 256.
允許最大的並行線程數量,默認是1,最大是256
BATCH <number_of_records_of_each_commit> ::= <unsigned_integer>
The number of records to be inserted in each commit.
每批提交的條數,即多少條后提交
Note:
THREADS and BATCH can be used to achieve high loading performance by enabling parallel loading and also by committing many records at once. In general, for column tables, a good setting to use is 10 parallel loading threads, with a commit frequency of 10,000 records or greater.
注:
THREADS 、
BATCH 這兩個參數是用來提升並發導入數據的性能的參數,在通常情況中下對於列存儲表,推薦使用10個並發線程,每批1萬條就提交
TABLE LOCK
Can be used for faster data loading for column store tables. 可以加快列存儲表數據的導入
It is recommended to specify this option carefully as it incurs table locks in exclusive mode as well as explicit hard merges and savepoints after data loading is finished. The performance gain from this option can vary according to the table constraints (like primary key) and optimization of other layers (like persistency or DML command).
NO TYPE CHECK
Specifies that the record will be inserted without checking the type of each field.
導入時不檢查字段類型
SKIP FIRST <number_of_rows_to_skip> ROW <number_of_rows_to_skip> ::= <unsigned_integer>
Skips the specified number of rows in the import file.
指定跳過多少行后開始導入,比如有表頭時
COLUMN LIST IN FIRST ROW
Indicates that the column list is stored in the first row of the CSV import file.
第一行做為COLUMN LIST選項的值,這樣就可以使用COLUMN LIST IN FIRST ROW選項替代了COLUMN LIST選項(COLUMN LIST選項見下面)
COLUMN LIST ( <column_name_list> ) <column_name_list> ::= <column_name> [{, <column_name>}...]
The column list for the data being imported. The name list has one or more column names. The ordering of the column names has to match the order of the column data in the CSV file and the columns in the target table.
指定CSV文件里的數據列將要存儲到表中的哪些列中,因為有時CSV里提借的列數比表實際列數要少,或者CSV里的第一列要存儲到表里的第三列,等...為了解決這些列數不配以及存儲的位置不配時,需要通過這個選項來實現,請看后面的示例
RECORD DELIMITED BY <string_for_record_delimiter> ::= <string_literal>
The record delimiter used in the CSV file being imported.
行與行的分隔符
FIELD DELIMITED BY <string_for_field_delimiter> ::= <string_literal>
The field delimiter of the CSV file.
字段分隔符
OPTIONALLY ENCLOSED BY <character_for_optional_enclosure> ::= <character_literal>
The optional enclosure character used to delimit field data.
字符(串)使用什么引起來
DATE FORMAT <string_for_date_format> ::= <string_literal>
The format that date strings are encoded with in the import data:
指定日期格式
The format that timestamp strings are encoded with in the import data.
- Y : year
- MM : month
- MON : name of month
- DD : day
For example:
- 'YYYYMMDD' = 20120520
- 'YYYY-MM-DD' = 2012-05-20
- 'YYYY-MON-DD' : 2012-MAY-20
TIME FORMAT <string_for_time_format> ::= <string_literal>
The format that time strings are encoded with in the import data:
指定時間格式
- HH24 : hour
- MI : minute
- SS : second
- 'HH24MISS' : 143025
- 'HH24:MI:SS' : 14:30:25
TIMESTAMP FORMAT <string_for_timestamp_format> ::= <string_literal>
指定日期時間格式
For example:
- 'YYYY-MM-DD HH24:MI:SS' : 2012-05-20 14:30:25
ERROR LOG <file_path_of_error_log> ::= <string_literal>
When specified, a log file of errors generated is stored in this file. Please ensure the file path you use is writeable by the database.
指定錯誤日志文件(含路徑)
FAIL ON INVALID DATA
When specified, the IMPORT FROM command fails unless all the entries import successfully.
遇到無效數據會立即停止
IMPORT FROM命令
<with_schema_flexibility> ::= WITH SCHEMA FLEXIBILITY
The option WITH SCHEMA FLEXIBILITY will create missing columns in flexible tables during CSV imports, as specified in the header (first row) of the CSV file or column list. By default, missing columns in flexible tables are not created automatically during data imports.
根據CSV里的表頭行(第一行,使用COLUMN LIST IN FIRST ROW選項指定)或者是COLUMN LIST 選項中提供的列名來創建缺失的列,具體示例請參考后面示例。注:該選項是用在CREATE COLUMN TABLE...語句后面
For security reason, only CSV files located at paths defined in the
csv_import_path_filterconfiguration parameter are allowed to be loaded using the IMPORT FROM SQL statement. This feature can be disabled using the
enable_csv_import_path_filterconfiguration parameter. Two related configuration parameters are specified in the
import_exportsection of the indexserver (nameserver in case of multi-DB) configuration, so you can turn off this feature or update path filter like follows:
由於安全原因,需要配置csv_import_path_filter服務器參數配置導入的CSV文件的路徑,但該參數可以通過enable_csv_import_path_filter服務配置參數來禁用它,即將enable_csv_import_path_filter設置為false后,就不需要配置csv_import_path_filter路徑參數了,即該參數失效

除了通過界面配置外,還可以通過下面命令來修改服務器配置參數:
Note that once you add a path '/A' to path filter every sub-path of '/A' will be automatically added as well.
ALTER SYSTEM ALTER CONFIGURATION ('indexserver.ini', 'system') set ('import_export', 'enable_csv_import_path_filter') = 'false' with reconfigure ALTER SYSTEM ALTER CONFIGURATION ('indexserver.ini', 'system') set ('import_export', 'csv_import_path_filter') = '/A;/B' with reconfigure
注:一旦配置了'/A'路徑,則其下面的子文件夾也會生效,不需要另外配置,就都可以做為CSV文件的導入路徑了
This feature is used with restrictions and/or is extended by the following SAP HANA option:
- Dynamic Tiering
Examples
Example 1 - Importing CSV Data
You create a table mytable to store the imported data.
CREATE
TABLE
mytable ( A INT, B VARCHAR(10), C DATE, D TIME, E DECIMAL );
You create a CSV text file /usr/sap/HDB/home/Desktop/data/data.csv and add the following contents.
1,"DATA1","2012-05-20","14:30:25",123456 2,"DATA2","2012-05-21","15:30:25",234567 3,"DATA3","2012-05-22","16:30:25",345678 4,"DATA4","2012-05-23","17:30:25",456789

You execute the following command to import the data.
IMPORT
FROM
CSV FILE '/usr/sap/HDB/home/Desktop/data/data.csv'
INTO
"MYTABLE"
WITH RECORD DELIMITED BY '\n'
FIELD DELIMITED BY ',';
Example 2 - Importing using a control file
In the example below, you import the CSV data from Example 1 using a control file.
在這個示例中,我們使用控制文件來完成上面示例同樣的功能 You can create a control file
/usr/sap/HDB/home/Desktop/data
/data.ctl and add the contents shown below to the
file.
創建
/usr/sap/HDB/home/Desktop/data
/data.ctl控制文件,並在文件中輸入以下內容:
IMPORT
DATA
INTO
TABLE
"MYTABLE"
FROM
'/usr/sap/HDB/home/Desktop/data/data.csv'
RECORD DELIMITED BY '\n'
FIELD DELIMITED BY ','
OPTIONALLY ENCLOSED BY '"'
ERROR LOG '/usr/sap/HDB/home/Desktop/data/data.err'

IMPORT FROM CONTROL FILE '/usr/sap/HDB/home/Desktop/data/data.ctl';
執行后,不管是否產生了錯誤,都會生成data.err文件
Example 3 - Import using date formats
In the example below, the date format is of the CSV import data is different to the default date format 'YYYY-MM-DD'. In this import data the date format used is 'MM-DD-YYYY'. You create a CSV text file /usr/sap/HDB/home/Desktop/data/data_different_date.csv and add the following contents.
1,"DATA1","05-20-2012","14:30:25",123456 2,"DATA2","05-21-2012","15:30:25",234567 3,"DATA3","05-22-2012","16:30:25",345678 4,"DATA4","05-23-2012","17:30:25",456789
You execute the following command to import the data.
IMPORT FROM CSV FILE '/usr/sap/HDB/home/Desktop/data/data_different_date.csv' INTO "MYTABLE" WITH RECORD
DELIMITED BY '\n'
FIELD DELIMITED BY ','
DATE FORMAT 'MM-DD-YYYY';
Example 4 - Import using COLUMN LIST
You create a table called COLLIST to store the imported data.
CREATE TABLE COLLIST ( A INT, B VARCHAR(10), C DATE, D DECIMAL );
You create a CSV text file '/usr/sap/HDB/home/Desktop/data/data_col_list.csv' and add the following contents.現在
data_col_list.csv文件里的內容如下,B列值沒有提供,並且CSV里的第一列要存到D列里,第二列要存到C里,第三列要存儲到A列里
123456,"2012-05-20",1 234567,"2012-05-21",2 345678,"2012-05-22",3 456789,"2012-05-23",4
IMPORT FROM CSV FILE '/usr/sap/HDB/home/Desktop/data/data_col_list.csv' INTO "COLLIST"
WITH RECORD DELIMITED BY '\n'
FIELD DELIMITED BY ','
COLUMN LIST ("D", "C", "A");

In order to import data without dealing with the proper table layout, it is possible to use WITH SCHEMA FLEXIBILITY as extended option of COLUMN LIST to import into a flexible table. You create a flexible table to store the imported data.
You execute the following commands to import previously created data_col_list.csv without explicitly adding columns.
CREATE COLUMN TABLE FLEX ( X INT ) WITH SCHEMA FLEXIBILITY;--WITH SCHEMA FLEXIBILITY選項只能用於列式存儲的表
創建一個可伸縮的表,只有X一列,使用上面的data_col_list.csv文件,文件里有3列,且都沒存儲到X列里
IMPORT FROM CSV FILE '/usr/sap/HDB/home/Desktop/data/data_col_list.csv' INTO "FLEX"
WITH RECORD DELIMITED BY '\n'
FIELD DELIMITED BY ','
COLUMN LIST ("A", "B", "C", "D") --注:如果使用了 WITH SCHEMA FLEXIBILITY選項,則一定要指定COLUMN LIST,或者使用COLUMN LIST IN FIRST ROW選項來指定CSV里的第一行為COLUMN LIST
WITH SCHEMA FLEXIBILITY;


在自動創建缺失的列時,好像無法指定類型,默認類型全為 nvarchar(5000)?這不太好吧!是否有辦法指定呢?