Purpose
You can use this statement to store the query results in a variable or file.
SELECT ... INTO OUTFILEstores the result set in an external file in the specified output format.SELECT ... INTO DUMPFILEwrites a single line without any format to an external file.SELECT ... INTO var_liststores the result set in variables.
Required privileges
To execute the SELECT INTO statement, you must have the FILE privilege and the SELECT privilege on the corresponding table. For more information about privileges in OceanBase Database, see Privilege types in MySQL mode.
Here is an example:
Grant the FILE privilege to a user. You can use the following command:
GRANT FILE ON *.* TO user_name;
Here, user_name is the username of the account that executes the SELECT INTO statement.
Syntax
select_stmt INTO
{OUTFILE 'file_name' [ {CHARSET | CHARACTER SET} charset_name] [field_opt] [line_opt] [file_opt]
| DUMPFILE 'file_name'
| into_var_list}
;
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}
Parameters
| Parameter | Description |
|---|---|
| select_stmt | The query statement to be executed. select_stmt must return a result set. For more information about the structure and options of the query statement, see SELECT.
NoteThe |
| file_name | The path and file name of the exported file. file_name can be in either of the following formats:
NoteAlibaba Cloud OSS does not support files larger than 5 GB. When you export a file larger than 5 GB to Alibaba Cloud OSS, it is split into multiple smaller files. |
| CHARSET | CHARACTER SET charset_name | Optional. The character set of the exported file. charset_name specifies the name of the character set. |
| field_opt | Optional. The field format options for the exported file. You can use the FIELDS or COLUMNS clause to specify the format of each field in the output file. For more information, see field_term. |
| line_opt | Optional. The start and end character options for exported data rows. LINES specifies the characters to enclose each line in the output file. For more information, see line_term. |
| file_opt | Optional. Specifies whether to export data as multiple files, and if so, the maximum size of each single file. For more information, see file_option. |
field_term
[OPTIONALLY] ENCLOSED BY string: the characters for enclosing field values. By default, the field values are not enclosed by any characters. For example,ENCLOSED BY '"'specifies that character values are enclosed within a pair of double quotation marks (""). If theOPTIONALLYkeyword is used, only values of the string type are enclosed within the specified symbols.TERMINATED BY string: the separator between field values. For example,TERMINATED BY ','specifies that field values are separated with commas (,).ESCAPED BY string: the escape character, which is used to process special characters or parse data in special formats. The backslash (\) is the default escape character.
line_term
STARTING BY string: the character to start each line. By default, the character is not specified.TERMINATED BY string: the character to end each line. The default value is a line feed. For example,... LINES TERMINATED BY '\n' ...specifies that a row ends with a line feed.
file_option
SINGLE [=] {TRUE | FALSE}: specifies whether to export data as a single file or multiple files.SINGLE [=] TRUE: exports data as a single file. This is the default value.SINGLE [=] FALSE: exports data as multiple files.
Notice
When the degree of parallelism (DOP) is greater than 1 and
SINGLE = FALSE, you can export data as multiple files to achieve parallel reading and writing for a higher export speed.MAX_FILE_SIZE [=] {int | string}: the maximum size of a single file whenSINGLE = FALSE.
Examples
Set a path for the exported file.
To export a file, you need to set the system variable
secure_file_privto configure the path that can be accessed for export.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 on to the OBServer node to connect to.
ssh admin@xxx.xxx.xxx.xxxExecute the following statement to connect to the
mysql001tenant through a local Unix Socket:obclient -S /home/admin/oceanbase/run/sql.sock -uroot@mysql001 -p******Set the path for the exported file to
/home/admin/test_data.SET GLOBAL secure_file_priv = "/home/admin/test_data";Log out of the database.
Reconnect to the database and execute the
SELECT INTO OUTFILEstatement to export data. Specify to separate two field values with a comma (,), enclose a field value of the string type in double quotation marks ("), and end each line with a line feed.Write a single file serially and specify the file name as
test_tbl1.csv.SELECT /*+parallel(2)*/ * FROM test_tbl1 INTO OUTFILE '/home/admin/test_data/test_tbl1.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n';The return result is as follows:
Query OK, 9 rows affectedWrite multiple files in parallel without specifying the file name prefix. That is, the file names use the default prefix
data. Set the maximum size of each file to 4 MB.SELECT /*+parallel(2)*/ * FROM test_tbl1 INTO OUTFILE '/home/admin/test_data/' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' SINGLE = FALSE MAX_FILE_SIZE = '4MB';The return result is as follows:
Query OK, 9 rows affectedWrite multiple files in parallel. Set the prefix of the file names to
dd2024and the maximum size of each file to 4 MB.SELECT /*+parallel(2)*/ * FROM test_tbl1 INTO OUTFILE '/home/admin/test_data/dd2024' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' SINGLE = FALSE MAX_FILE_SIZE = '4MB';The return result is as follows:
Query OK, 9 rows affected
Note
- When multiple export tasks with the same destination path are executed at the same time, errors may occur or only part of the data may be exported. Therefore, you need to plan the export paths reasonably.
Here is an example:
WhenSELECT /*+parallel(2)*/ * FROM t1 INTO OUTFILE 'test/data' SINGLE = FALSE;andSELECT /*+parallel(2)*/ * FROM t2 INTO OUTFILE 'test/data' SINGLE = FALSE;are executed at the same time, an error may be reported due to the same export file name. In this case, we recommend that you set the export paths totest/data1andtest/data2respectively. - When
SINGLE = FALSEand the export task fails due to reasons such asfile already exist, you can clear all files with the same file name prefix as the exported files in the export directory, or delete the export directory and create a new one, and then perform the export operation again.
Here is an example:
When the execution ofSELECT /*+parallel(2)*/ * FROM t1 INTO OUTFILE 'test/data' SINGLE = FALSE;fails, you can delete all files with the file name prefixdataunder thetestdirectory, or delete thetestdirectory and create it again, and then try the export operation again.
Log on to the server where the OBServer node resides and check the exported files under the directory
/home/admin/test_dataof the node.[xxx@xxx /home/admin/test_data]# lsThe return result is as follows:
data_0_0_0 data_0_1_0 dd2024_0_0_0 dd2024_0_1_0 test_tbl1.csvHere,
test_tbl1.csvis the serially exported single file.data_0_0_0anddata_0_1_0are the parallelly exported files without a specified file name prefix.dd2024_0_0_0anddd2024_0_1_0are the parallelly exported files with the specified file name prefixdd2024.