Вы находитесь на странице: 1из 13

SAP HANA – SQL Data Import Export Statements

Different statements available in SAP HANA are,

EXPORT
IMPORT
IMPORT FROM
IMPORT SCAN

These statements are used to either import or export the objects from or to SAP HANA
system.

EXPORT
EXPORT:

 The EXPORT statement is used to exports tables, views, column views,


synonyms, sequences, or procedures in the specified format as BINARY or CSV.

Data stored in “no logging” tables can only be exported in CSV format. For global temporary
tables, only the table catalog can be exported. The export of local temporary tables is not
supported.

Detailed results of an export are stored in the session-local temporary table #EXPORT_RESULT.

Syntax: EXPORT <export_import_object_name_list> AS <export_format> INTO


<path> [WITH <export_option_list>]

We have following options while exporting objects using EXPORT statement in SAP
HANA.

Export_import_object_name:

 We can export all the objects from all schemas or all the objects from single
schema or specific objects from single or multiple schemas. We need to use below
syntax’s depending on the scenario.
o ALL – to export all the objects from all the schemas.
o <schema_name>.* – to export all the objects from one schema.
o <schema_name>.<object_name> – to export specific objects from one or
multiple schemas.

Export_format:

 We have two different export formats. They are


o BINARY
o CSV
Path:

 The server path where export files will be stored. To export the objects to local
system, please go through ‘Quick View screen in SAP HANA Studio’ article.

When using a distributed system, the FULL_PATH must point to a shared disk. For security
reasons, the path may not contain symbolic links and may not point inside the database instance
folder, except its ‘backup’ and ‘work’ subfolders. Examples for valid export path (assuming the
database instance is located at /usr/sap/HDB/HDB00):
‘/tmp’
‘/usr/sap/HDB/HDB00/backup’
‘/usr/sap/HDB/HDB00/work’

Export_options:

 Below are the different options we can use as part of export options.
 REPLACE:
o REPLACE removes previously exported data on disk and replaces it with
the new export. If the REPLACE option is not specified, an error will be
thrown if previously exported data already exists in the specified export
directory.
 CATALOG ONLY:
o Only the database catalog will be exported.
 NO DEPENDENCIES:
o The underlying dependencies of an export object will not be exported.
 SCRAMBLE BY:
o Obfuscates CSV format exported data. This option can be used to provide
an export of the structure of our database with the string data scrambled
so that it cannot easily be interpreted. When the optional
<scramble_seed> is not specified, a default scramble seed is used. SAP
recommends that the <scramble_seed> should be at least 8 to 10
characters.

Only CSV format character string data will be scrambled by this parameter.
STRIP:

 Attributes that can be reconstructed at import time will not be stored in the
export file. This option can reduce the size of the export file in some cases.

THREADS:

 The THREADS parameter specifies how many objects will be exported in parallel,
the default is 1. Increasing number of threads reduces export time, but can also
negatively affect database system performance.
 Following items should be considered when using this parameter:
o When exporting a single table THREADS has no effect.
o When exporting a view or procedure 2 or more threads should be used, up
to the number of dependent objects.
o When exporting a whole schema consider using more than 10 threads.
With a maximum being the number of CPU cores in the system.
o When exporting a whole BW / ERP system database with tens of
thousands of tables using the ALL keyword, a large number of threads can
be used (up to 256).

We can check the export monitoring status using M_EXPORT_BINARY_STATUS view under
SYS schema with the help of connection id.

We can even cancel/abort the import process using the below statement.

 ALTER SYSTEM CANCEL WORK IN SESSION ‘<CONNECTION_ID>’

Detailed results of the last execution of EXPORT statement are stored in the following
session-local temporary table.

Below is the table structure of #EXPORT_RESULT

Examples:
1. Export table ORDERS_DATA from SAP_STUDENT schema with REPLACE AND
SCRAMBLE options.

Ans: EXPORT SAP_STUDENT.ORDERS_DATA AS CSV INTO ‘/tmp’ WITH REPLACE SCR


AMBLE

2. Export all the objects from SAP_STUDENT schema WITH REPLACE and THREADS
10.

Ans: EXPORT SAP_STUDENT.”*” AS CSV INTO ‘/tmp’ WITH REPLACE THREADS 10


IMPORT
IMPORT:

 The IMPORT statement is used to import catalog objects (tables, views,


synonyms, sequences, procedures) that have previously been exported with
EXPORT statement into SAP HANA Database.

To import external data into existing tables use IMPORT FROM.

The file format (BINARY | CSV) of the file being imported will be automatically detected.

Syntax: IMPORT <export_import_object_name_list> FROM <path> [WITH


<import_option_list>] [AT [LOCATION] <indexserver_host_port>]

We have following options while importing objects using IMPORT statement in SAP
HANA.

Export_import_object_name:

 We can import all the objects from all schemas or all the objects from single
schema or specific objects from single or multiple schemas. We need to use below
syntax’s depending on the scenario.
o ALL – to import all the objects from all the schemas.
o <schema_name>.* – to import all the objects from one schema.
o <schema_name>.<object_name> – to import specific objects from one or
multiple schemas.

Import_options:

Below are the different options we can use as part of import options.

 REPLACE:
o When specified, if a table defined in the import data currently exists in the
database it will be dropped and recreated before the data is imported.
o If the REPLACE option is not specified an error will be thrown if an existing
database table is defined in the import data.
 CATALOG ONLY:
o Specifies that only the database catalog should be imported.
 DATA ONLY:
o Specifies that only the data of the import file should be imported.
o This option can only be used when the existing table definitions in the
database match with those of the data to be imported. Note: For BINARY
export data, existing table data will be overwritten with the imported data.
o For CSV format data, the import data will be appended to existing table
data.
 NO DEPENDENCIES:
o When specified the underlying dependencies of an import object will not
be imported.

THREADS:

 The THREADS parameter specifies how many objects will be imported in parallel,
the default is 1. Increasing number of threads reduces import time, but can also
negatively affect database system performance.
 Following items should be considered when using this parameter:
o When importing a single table THREADS has no effect.
o When importing a view or procedure 2 or more threads should be used, up
to the number of dependent objects.
o When importing a whole schema consider using more than 10 threads.
With a maximum being the number of CPU cores in the system.
o When importing a whole BW / ERP system database with tens of
thousands of tables using the ALL keyword, a large number of threads can
be used (up to 256).

RENAME SCHEMA:

 It is possible to rename the objects’ schema during import.


 Multiple schemas can be renamed by specifying multiple
<rename_schema_token>. We cannot specify the same schema as both
<source_schema> and <target_schema> in same or different
<rename_schema_token>.

AT LOCATION:

 It is possible to specify the index server at which tables are created and
imported. If we specify the hostname and port, the tables will be created and
imported on there.

FAIL ON INVALID DATA:


 When specified the IMPORT command will fail unless all the entries have imported
successfully.

Progress of IMPORT job can be checked in view M_IMPORT_BINARY_STATUS under SYS


schema.

We can even cancel the running IMPORT job using connection ID from the session using
the below statement.

ALTER SYSTEM CANCEL WORK IN SESSION ‘<CONNECTION_ID>’

The results of the IMPORT job are stored in session-local temporary table
#IMPORT_RESULT and below is its structure.

Examples:

1. Import all the objects from path ‘/tmp/ to SAP HANA using options REPLACE and
threads 5.

Ans: IMPORT “sap_student”.”*” AS CSV FROM ‘/tmp’ WITH REPLACE THREADS 10

2. Import all the objects from path ‘/tmp/ and rename the schema from VENKATESH
to SAP_STUDENT

Ans: IMPORT “venkatesh”.”*” AS CSV FROM ‘/tmp’ WITH REPLACE THREADS


10 RENAME SCHEMA VENKATESH TOSAP_STUDENT

IMPORT FROM
IMPORT FROM:

 The IMPORT FROM statement is used to import external data from a file into an
existing table.
 To import catalog objects (tables, views etc) that have been exported with
EXPORT statement, we need to use IMPORT statement.

All in the support UTF-8 except surrogate-pair encoding.

Remember: For security reason, only CSV files located at paths defined in the
csv_import_path_filter configuration parameter are allowed to be loaded using the
IMPORT FROM SQL statement. This feature can be disabled using the
enable_csv_import_path_filter configuration parameter.

Disable or Add paths to Import from Configuration:

Two related configuration parameters are specified in the import_export


section of the indexserver (nameserver in case of multi-DB) configuration, so
we can turn off this feature or update path filter like follows:

1. Disable path filter.

Ans: ALTER SYSTEM ALTER CONFIGURATION (‘indexserver.ini’,


‘system’) set (‘import_export’, ‘enable_csv_import_path_filter’) =
‘false’ with reconfigure

2. Enable path filter with specific location.

Ans: ALTER SYSTEM ALTER CONFIGURATION (‘indexserver.ini’,


‘system’) set (‘import_export’, ‘csv_import_path_filter’) =
‘/A;/B’ with reconfigure

 Note that once we add a path ‘/A’ to path filter every sub-path of ‘/A’
will be automatically added as well.
 We can also import the data from wither csv files or excel files using
‘Import’ option available in ‘Quick View’ screen of SAP HANA Studio
which is front end tool for SAP HANA Database.

Please go through the article ‘Data loading into SAP HANA from Flat Files’ for
more details.

Syntax: IMPORT FROM [<file_type>] <file_path> [INTO


<schema_name>.<table_name>] [WITH <import_from_option_list>]

We have following options while importing external data using IMPORT FROM
statement in SAP HANA.

File_type:
 The type of the file to be imported. We can specify either comma-
separated values or control file formats. File type should be either CSV
FILE or CONTROL FILE.

File_Path:

 The complete path and file name of the file to import.

Table_Name:

 The target table name, with optional schema name, where the imported
data will be stored. The format should be
<schema_name>.<table_name>

Import_from_options_list:

 We have below options as part of importing external data into SAP HANA.

THREADS:

 The number of threads that can be used for concurrent import. The
default value is 1 and maximum allowed is 256.

Contd..

IMPORT FROM
BATCH:

 The number of records to be inserted in each commit.

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.

TABLE LOCK:

 Uses table locks for fast import of column store tables.

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:

 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_name_list> ):

 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.

RECORD DELIMITED BY:

 The record delimiter used in the CSV file being imported.

FIELD DELIMITED BY:

 The field delimiter of the CSV file.

OPTIONALLY ENCLOSED BY:

 The optional enclosure character used to delimit field data.

DATE FORMAT:

 The format that date strings are encoded with in the import data:
o Y : year
o MM : month
o MON : name of month
o DD : day
 Examples:
o ‘YYYYMMDD’ = 20120520
o ‘YYYY-MM-DD’ = 2012-05-20
o ‘YYYY-MON-DD’ = 2012-MAY-20

TIME FORMAT:

 The format that time strings are encoded with in the import data:
o HH24 : hour
o MI : minute
o SS : second
 Examples:
o ‘HH24MISS’ : 143025
o ‘HH24:MI:SS’ : 14:30:25
TIMESTAMP FORMAT:

 The format that timestamp strings are encoded with in the import data.
 Example:
o ‘YYYY-MM-DD HH24:MI:SS’ : 2012-05-20 14:30:25

ERROR LOG:

 When specified a log file of errors generated will be stored in this file.
 Please ensure the file path we use is writeable by the database.

FAIL ON INVALID DATA:

 When specified the IMPORT FROM command will fail unless all the entries have
imported successfully

WITH SCHEMA FLEXIBILITY:

 This option automatically creates missing columns based on the column names in
the header row of CSV files or the column names specified in a column name list.

Examples:

1. Import data from csv file to ORDERS_DATA table under SAP_STUDENT schema in
SAP HANA.

Ans:

First, Create a column table in SAP HANA using below statement.

CREATE TABLE SAP_STUDENT.ORDERS_DATA (ORD_NUMBER INTEGER,


CUST_NO NVARCHAR(10), ORD_DATE DATE, ORD_TIME TIME, QUANTITY DECIMAL);

Create a csv file with below content and store it in folder /data/data.csv

30011,”C_91″,”2012-05-20″,”14:30:25″,130
30012,”C_92″,”2012-05-21″,”15:30:25″,180
30013,”C_92″,”2012-05-22″,”16:30:25″,20
30014,”C_93″,”2012-05-23″,”17:30:25″,70

Second, Execute the below statement to load the content into ORDERS_DATA table in
SAP_STUDENT schema.
IMPORT FROM CSV FILE ‘/data/data.csv’ INTO “SAP_STUDENT.”ORDERS_DATA”
WITH RECORD DELIMITED BY ‘\n’
FIELD DELIMITED BY ‘,’;

2. Import the data from csv file to ORDERS_DATA table under SAP_STUDENT
schema in SAP HANA using control file.

Ans:

First, Create a control file with below content and keep it folder /data/data.ctl

IMPORT DATA INTO TABLE “SAP_STUDENT”.”ORDERS_DATA” FROM ‘/data/data.csv’


RECORD DELIMITED BY ‘\n’
FIELD DELIMITED BY ‘,’
OPTIONALLY ENCLOSED BY ‘”‘
ERROR LOG ‘/data/data.err’

Next, Run the below statement to import data into table ORDERS_DATA

IMPORT FROM CONTROL FILE ‘/data/data.ctl’;

3. Import the data from csv file to SAP HANA using DATE FORMATS and COLUMN
LIST options.

Ans:

First, Create a csv file with below content and store it in folder /data/data.csv

30011,”C_91″,”05-20-2012″,”14:30:25″,130
30012,”C_92″,”05-21-2012″,”15:30:25″,180
30013,”C_92″,”05-22-2012″,”16:30:25″,20
30014,”C_93″,”05-23-2012″,”17:30:25″,70

Second, Execute the below statement to load the content into ORDERS_DATA table in
SAP_STUDENT schema where specify date format as ‘MM_DD_YYYY’ because this is not
SAP HANA default date format.

IMPORT FROM CSV FILE


‘/data/data_different_date.csv’ INTO “SAP_STUDENT.”ORDERS_DATA” WITH RECORD
DELIMITEDBY ‘\n’
FIELD DELIMITED BY ‘,’
DATE FORMAT ‘MM-DD-YYYY’;

Lastly, Run the below statement to execute the same statement by specifying column
names as well.
IMPORT FROM CSV FILE
‘/data/data_col_list.csv’ INTO “SAP_STUDENT”.”ORDERS_DATA”
WITH RECORD DELIMITED BY ‘\n’
FIELD DELIMITED BY ‘,’
DATE FORMAT ‘MM-DD-YYY’
COLUMN LIST (“ORD_NUMBER”, “CUST_NO”, “ORD_DATE”,
“ORD_TIME”,”ORD_QUANTITY”);

4. Import the data from csv file to SAP HANA using SCHEMA FLEXIBILITY.

Ans:

First, Create a column table ORDERS_DATA with below columns in SAP HANA.

CREATE TABLE SAP_STUDENT.ORDERS_DATA (ORD_NUMBER INTEGER,


CUST_NO NVARCHAR(10), ORD_DATE DATE, ORD_TIME TIME)

Now run the below statement to import data for 5 columns by specifying schema
flexibility and column names.

IMPORT FROM CSV FILE


‘/data/data_col_list.csv’ INTO “SAP_STUDENT”.”ORDERS_DATA”
WITH RECORD DELIMITED BY ‘\n’
FIELD DELIMITED BY ‘,’
DATE FORMAT ‘MM-DD-YYY’
COLUMN LIST (“ORD_NUMBER”, “CUST_NO”, “ORD_DATE”,
“ORD_TIME”,”ORD_QUANTITY”)
WITH SCHEMA FLEXIBILITY;

CREATE
IMPORT SCAN:

 The IMPORT SCAN statement searches the given path for objects exported using
EXPORT statement and stores its result in the following session-local temporary
table

The temporary table is #IMPORT_RESULTS and below is it’s structure.


Syntax: IMPORT SCAN <path>

Examples:

1. Scan the path ‘/data/import_path’ using IMPORT_SCAN function

Ans: IMPORT SCAN ‘/data/import_path’

2. Run select statement on #IMPORT_SCAN_RESULT to see the results of the scan.

Ans: SELECT * FROM #IMPORT_SCAN_RESULT