Description
The statement is used to store the query result in a variable or a file. Specifically:
The
SELECT ... INTO OUTFILEstatement is used to store the result set in an external file and allows you to specify the output format.Note
When
SELECT ... INTO OUTFILEoutputs data, it supports using\Nto representNULL.The
SELECT ... INTO DUMPFILEstatement is used to write a single, unformatted line to an external file.The
SELECT ... INTO var_liststatement is used to store the result set in variables.
Privilege requirements
To execute the SELECT INTO statement, you must have the FILE privilege and the SELECT privilege on the target table. For more information about OceanBase Database privileges, see Privilege types in MySQL mode.
Here is an example:
You can use the following command to grant the FILE privilege to a user:
GRANT FILE ON *.* TO user_name;
where user_name is the username that will execute the SELECT INTO statement.
Syntax
select_stmt INTO
{OUTFILE 'file_name'
[{CHARSET | CHARACTER SET} charset_name]
[field_opt]
[line_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
Parameter explanation
Parameter |
Description |
|---|---|
| select_stmt | The query statement to be executed. The result set of the query statement must not be empty. For more information about the structure and options of the query statement, see SELECT statement.
NoteYou can also specify the |
| file_name | The path and name of the file to be exported. The file_name parameter can be in one of the following formats:
NoteDue to the file size limit of Alibaba Cloud OSS, if the size of the file to be exported exceeds 5 GB, the file will be split into multiple files, each smaller than 5 GB, when it is exported to Alibaba Cloud OSS. |
| CHARSET | CHARACTER SET charset_name | Specifies the character set of the external file. This parameter is optional. charset_name indicates the name of the character set. |
| field_opt | Specifies the format of fields in the output file. This parameter is optional. You can use the FIELDS or COLUMNS clause to specify the format. For more information, see field_term. |
| line_opt | Specifies the start and end characters of data rows. This parameter is optional. You can use the LINES clause to specify the characters. For more information, see line_term. |
field_term
[OPTIONALLY] ENCLOSED BY string: the characters used to enclose field values. The default value is no enclosing characters. For example,ENCLOSED BY '"'indicates that character values are enclosed in double quotation marks. If you use theOPTIONALLYkeyword, only string-type values are enclosed in the specified characters.TERMINATED BY string: the delimiter between field values. For example,TERMINATED BY ','indicates that a comma is used as the delimiter between field values.ESCAPED BY string: the escape character used to handle special characters or parse data in a special format. The default escape character is the backslash (\).
line_term
STARTING BY string: the start character of a row. The default value is no start character.TERMINATED BY string: the end character of a row. The default value is the line break character. For example,... LINES TERMINATED BY '\n' ...indicates that a line is terminated by the line break character.
Examples
Export a data file to the local server
Set the export file path.
To export a file, you need to set the system variable
secure_file_privto a path that can be accessed by the export file.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
mysql001tenant by using a local Unix socket.obclient -S /home/admin/oceanbase/run/sql.sock -uroot@mysql001 -p******Set the export path to
/home/admin/test_data.SET GLOBAL secure_file_priv = "/home/admin/test_data";Log out.
Reconnect to the database and use the
SELECT INTO OUTFILEstatement to export data. Use commas to separate the field values; enclose the string-type values in"characters; and use line breaks as the delimiter. Name the filetest_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 affected
Export a data file to Alibaba Cloud OSS
Use the SELECT INTO OUTFILE statement to export data from the test_tbl2 table to a specified Alibaba Cloud OSS storage location by partition. The partitioning key is the combination of the col1 and col2 columns. Rows with the same values in these two columns belong to the same partition and will be exported to the same directory.
SELECT /*+parallel(3)*/ * FROM test_tbl2
INTO OUTFILE 'oss://$DATA_FOLDER_NAME/?host=$OSS_HOST&access_id=$OSS_ACCESS_ID&access_key=$OSS_ACCESS_KEY';
The $DATA_FOLDER_NAME variable specifies the storage location, and you also need to provide the host address, access ID, and access key of Alibaba Cloud OSS.
