SELECT INTO
Description
SELECT INTO writes query results to a local file on the seekdb server or assigns them to variables:
SELECT ... INTO OUTFILE: Writes a result set to a local text or CSV file.SELECT ... INTO DUMPFILE: Writes a single-row result to a local file without formatting.SELECT ... INTO var_list: Assigns a single-row result to user or PL variables.
V1.4.0 does not support writing to remote object storage such as OSS or S3, or writing Parquet or ORC files with this statement.
Privilege requirements
INTO OUTFILE and INTO DUMPFILE require the SELECT privilege on the source table and the global FILE privilege. For example:
GRANT FILE ON *.* TO user_name;
INTO var_list does not require the FILE privilege.
Syntax
select_stmt
INTO {
OUTFILE 'file_name'
[{CHARSET | CHARACTER SET} charset_name]
[FIELDS field_term [, field_term ...]]
[LINES TERMINATED BY string]
[file_option [, file_option ...]]
| OUTFILE 'file_name'
FORMAT = (TYPE = 'CSV' [, csv_option ...])
[file_option [, file_option ...]]
| DUMPFILE 'file_name'
| var_name [, var_name ...]
};
field_term:
TERMINATED BY string
| [OPTIONALLY] ENCLOSED BY string
| ESCAPED BY string
csv_option:
FIELD_DELIMITER = expr
| LINE_DELIMITER = expr
| ESCAPE = expr
| FIELD_OPTIONALLY_ENCLOSED_BY = expr
| FIELD_ENCLOSED_BY = expr
| ENCODING = 'charset_name'
| COMPRESSION = {NONE | GZIP | DEFLATE | ZSTD}
| FILE_EXTENSION = 'string'
file_option:
SINGLE [=] {TRUE | FALSE}
| MAX_FILE_SIZE [=] {int | string}
| BUFFER_SIZE [=] {int | string}
The INTO clause can also appear between the select list and FROM:
SELECT id, name
INTO OUTFILE '/data/export/users.csv'
FROM users;
Parameters
| Parameter | Description |
|---|---|
file_name | An absolute local path on the seekdb server. The destination directory must exist and the path must satisfy secure_file_priv. |
charset_name | The output character set. Valid values are binary and utf8mb4. |
TERMINATED BY | Specifies field or line delimiters. By default, fields are tab-delimited and lines are newline-delimited. |
[OPTIONALLY] ENCLOSED BY | Specifies a field enclosure character. With OPTIONALLY, only string values are enclosed. |
ESCAPED BY | Specifies the escape character. The default is a backslash (\\). |
FORMAT = (TYPE = 'CSV', ...) | Uses CSV format options. In V1.4.0, CSV is the only supported TYPE. |
SINGLE | TRUE writes one file. FALSE allows multiple files. The default is TRUE. |
MAX_FILE_SIZE | The maximum size of each output file when SINGLE=FALSE. |
BUFFER_SIZE | The buffer size used by each output thread. |
var_name | A variable that receives a value from a single-row result. The number of variables must equal the number of selected columns. |
INTO OUTFILE does not overwrite an existing file. If the destination exists, the statement returns an error. NULL is written as \N in text output.
Examples
Write a local CSV file
Set secure_file_priv to a local export directory, and then reconnect:
SET GLOBAL secure_file_priv = '/data/export';
Write query results to a local file:
SELECT id, name
FROM users
INTO OUTFILE '/data/export/users.csv'
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n';
You can also use CSV format options and enable ZSTD compression:
SELECT id, name
FROM users
INTO OUTFILE '/data/export/users.csv.zst'
FORMAT = (
TYPE = 'CSV',
FIELD_DELIMITER = ',',
LINE_DELIMITER = '\n',
ENCODING = 'utf8mb4',
COMPRESSION = ZSTD
);
Assign values to user variables
SELECT id, name
INTO @user_id, @user_name
FROM users
WHERE id = 1;
SELECT @user_id, @user_name;
The query must return at most one row. More than one row causes an error; if no row is returned, the variable values remain unchanged.