Description
This statement enables you to store query results in a variable, file, or table. Specifically:
The
SELECT ... INTO OUTFILEstatement stores the result set in an external file and allows you to specify the output format.The
SELECT ... INTO DUMPFILEstatement writes a single, unformatted line to an external file.The
SELECT ... INTO var_liststatement stores the result set in variables.
Privilege requirements
To execute the SELECT INTO statement, you must have the SELECT privilege on the source table. For more information about privileges in OceanBase Database, see Privilege types in Oracle mode.
syntax
SELECT [/*+parallel(N)*/] column_list_option
INTO {OUTFILE 'file_name' [ {CHARSET | CHARACTER SET} charset_name] [field_opt] [line_opt] [file_opt]
| DUMPFILE 'file_name'
| into_var_list}
FROM table_name_list
[WHERE where_conditions]
[GROUP BY group_by_list [HAVING having_search_conditions]]
[ORDER BY order_expression_list];
column_list_option:
column_name [, column_name ...]
field_opt:
{COLUMNS | FIELDS} field_term_list
field_term_list:
field_term [, field_term ...]
field_term:
{[OPTIONALLY] ENCLOSED | TERMINATED | ESCAPED} BY string
line_opt:
LINES line_term_list
line_term_list:
line_term [line_term]
line_term:
{STARTING | TERMINATED} BY string
file_opt:
file_option [, file_option ...]
file_option:
SINGLE [=] {TRUE | FALSE}
| MAX_FILE_SIZE [=] {int | string}
Parameter explanation
| Parameter | Description |
|---|---|
| parallel(N) | The number of parallel threads for executing the statement. It is an optional parameter. |
| column_list_option | Specifies the options for the columns to be exported. You can use * to select all columns. column_name: the name of the column. For more information about column options in a query statement, see SIMPLE SELECT. |
| file_name | The path and name of the file where the data is to be exported. file_name is in the following format:
NoteDue to the file size limit of Alibaba Cloud OSS, if the size of a file exceeds 5 GB, the file is split into multiple files that are each less than 5 GB when it is exported to Alibaba Cloud OSS. |
| CHARSET | CHARACTER SET charset_name | The character set of the external file. It is an optional parameter. charset_name indicates the name of the character set. |
| field_opt | The format options for fields. You can use the FIELDS or COLUMNS clause to specify the format of fields in the output file. For more information, see field_term. |
| line_opt | The options for delimiting rows. You can use the LINES clause to specify the start and end characters of rows. For more information, see line_term. |
| file_opt | The options for controlling whether to export data to multiple files and the size of each file when multiple files are exported. For more information, see file_option. |
| FROM table_name_list | Specifies the objects from which to select data. |
| WHERE where_conditions | The conditions for filtering data. Only data that meets the conditions is included in the query result. For more information, see SIMPLE SELECT. |
| GROUP BY group_by_list | The fields for grouping. Generally, the fields for grouping are used in conjunction with aggregate functions.
NoteColumns without aggregate functions among all columns after the |
| HAVING having_search_conditions | The conditions for filtering the grouped data. The HAVING clause is similar to the WHERE clause, but the HAVING clause can be used with cumulative functions (such as SUM and AVG). |
| ORDER BY order_expression_list | The columns for sorting the result set and the sorting order. The default sorting order is ASC. You can sort the query result by one or more columns in ascending (ASC) or descending (DESC) order.
|
field_term
[OPTIONALLY] ENCLOSED BY string: specifies the symbols that wrap the field values. The default is no quotation marks. For example,ENCLOSED BY '"'indicates that character values are enclosed in double quotation marks. If theOPTIONALLYkeyword is used, the specified characters are used to wrap only string-type values.TERMINATED BY string: specifies the delimiter between field values. For example,TERMINATED BY ','uses a comma as the delimiter between two field values.ESCAPED BY string: specifies the escape character to process special characters or parse data in a special format. The default escape character is the backslash (\).
line_term
STARTING BY string: specifies the starting characters of each line.TERMINATED BY string: the delimiter that indicates the end of a row. The default delimiter is the line break. For example,... LINES TERMINATED BY '\n' ...indicates that a row ends with a line break.
file_option
SINGLE [=] {TRUE | FALSE}: specifies whether to export data to a single file or multiple files.SINGLE [=] TRUE: The default value. Specifies that data can be exported only to a single file.SINGLE [=] FALSE: Specifies that the results can be exported to multiple files.Notice
When the degree of parallelism is greater than 1 and
SINGLE = FALSE, you can export data to multiple files to achieve parallel reading, parallel writing, and increased export speed.
MAX_FILE_SIZE [=] {int | string}: the maximum size of a single file when exporting data, which takes effect only whenSINGLE = FALSE.
Examples
Set the path for exporting files.
Before you export files, set the system variable
secure_file_privto the path that the export file can access.Notice
For security reasons, when you set the system variable
secure_file_priv, you can connect to the database only through a local socket to execute the SQL statement that modifies the global variable. For more information, see secure_file_priv.Log in to the OBServer node where you want to connect to OceanBase Database.
ssh admin@xxx.xxx.xxx.xxxConnect to the
oracle001tenant through a local Unix socket.obclient -S /home/admin/oceanbase/run/sql.sock -usys@oracle001 -p******Set the export path to
/home/admin/test_data.SET GLOBAL secure_file_priv = "/home/admin/test_data";Log out.
After reconnecting to the database, use the
SELECT INTO OUTFILEstatement to export data. Use a comma to separate the values of the two fields; enclose the values of the string type with"; and use line breaks as the end markers.Export data to a single file named
test_tbl1.csv.SELECT /*+parallel(2)*/ * INTO OUTFILE '/home/admin/test_data/test_tbl1.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM test_tbl1;The return result is as follows:
Query OK, 9 rows affectedExport data to multiple files without specifying file names, with a size of no more than 4 MB for each file.
SELECT /*+parallel(2)*/ * INTO OUTFILE '/home/admin/test_data/' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' SINGLE = FALSE MAX_FILE_SIZE = '4MB' FROM test_tbl1;The return result is as follows:
Query OK, 9 rows affectedExport data to multiple files with the file name prefix
dd2024, with a size of no more than 4 MB for each file.SELECT /*+parallel(2)*/ * INTO OUTFILE '/home/admin/test_data/dd2024' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' SINGLE = FALSE MAX_FILE_SIZE = '4MB' FROM test_tbl1;The return result is as follows:
Query OK, 9 rows affected
Note
- When multiple export tasks are exporting data to the same path at the same time, errors may occur or only part of the data may be exported. In this case, try to use different export paths.
For example:SELECT /*+parallel(2)*/ * INTO OUTFILE 'test/data' SINGLE = FALSE FROM t1;andSELECT /*+parallel(2)*/ * INTO OUTFILE 'test/data' SINGLE = FALSE FROM t2;may fail with an error if they attempt to use the same file name. In this case, you can set the export path totest/data1andtest/data2respectively. - After
SINGLE = FALSE, if the export fails due to the file already existing, you can delete all files with the same prefix in the export directory or delete the export directory and rebuild it, and then retry the export operation.
For example:
After the export operation specified bySELECT /*+parallel(2)*/ * INTO OUTFILE 'test/data' SINGLE = FALSE FROM t1;fails, you can delete all files with thedataprefix in thetestdirectory or directly delete thetestdirectory and rebuild it, and then retry the export operation.
Log in to the server and view the exported files in the
/home/admin/test_datadirectory on the OBServer node.
[xxx@xxx /home/admin/test_data]# ls
The return result is as follows:
data_0_0_0 data_0_1_0 dd2024_0_0_0 dd2024_0_1_0 test_tbl1.csv
Here, test_tbl1.csv is the file name in the example of exporting data to a single file; data_0_0_0 and data_0_1_0 are the file names in the example of exporting data to multiple files without specifying file names; and dd2024_0_0_0 and dd2024_0_1_0 are the file names in the example of exporting data to multiple files with the file name prefix dd2024.