ClickHouse/docs/en/sql-reference/statements/select/into-outfile.md

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

45 lines
2.1 KiB
Markdown
Raw Normal View History

---
2022-08-28 14:53:34 +00:00
slug: /en/sql-reference/statements/select/into-outfile
sidebar_label: INTO OUTFILE
---
# INTO OUTFILE Clause
2021-11-28 08:20:57 +00:00
`INTO OUTFILE` clause redirects the result of a `SELECT` query to a file on the **client** side.
2022-07-07 08:31:55 +00:00
Compressed files are supported. Compression type is detected by the extension of the file name (mode `'auto'` is used by default). Or it can be explicitly specified in a `COMPRESSION` clause. The compression level for a certain compression type can be specified in a `LEVEL` clause.
2021-11-20 18:44:45 +00:00
**Syntax**
```sql
2023-12-13 13:04:07 +00:00
SELECT <expr_list> INTO OUTFILE file_name [AND STDOUT] [APPEND | TRUNCATE] [COMPRESSION type [LEVEL level]]
2021-11-20 18:44:45 +00:00
```
2021-11-30 08:36:03 +00:00
`file_name` and `type` are string literals. Supported compression types are: `'none'`, `'gzip'`, `'deflate'`, `'br'`, `'xz'`, `'zstd'`, `'lz4'`, `'bz2'`.
2022-07-07 01:59:00 +00:00
`level` is a numeric literal. Positive integers in following ranges are supported: `1-12` for `lz4` type, `1-22` for `zstd` type and `1-9` for other compression types.
2022-06-02 10:55:18 +00:00
## Implementation Details
- This functionality is available in the [command-line client](../../../interfaces/cli.md) and [clickhouse-local](../../../operations/utilities/clickhouse-local.md). Thus a query sent via [HTTP interface](../../../interfaces/http.md) will fail.
- The query will fail if a file with the same file name already exists.
- The default [output format](../../../interfaces/formats.md) is `TabSeparated` (like in the command-line client batch mode). Use [FORMAT](format.md) clause to change it.
- If `AND STDOUT` is mentioned in the query then the output that is written to the file is also displayed on standard output. If used with compression, the plaintext is displayed on standard output.
2023-04-25 18:05:56 +00:00
- If `APPEND` is mentioned in the query then the output is appended to an existing file. If compression is used, append cannot be used.
- When writing to a file that already exists, `APPEND` or `TRUNCATE` must be used.
2021-11-20 18:44:45 +00:00
**Example**
2021-11-30 08:36:03 +00:00
Execute the following query using [command-line client](../../../interfaces/cli.md):
2021-11-20 18:44:45 +00:00
```bash
clickhouse-client --query="SELECT 1,'ABC' INTO OUTFILE 'select.gz' FORMAT CSV;"
zcat select.gz
```
Result:
2021-11-28 19:26:51 +00:00
2021-11-20 18:44:45 +00:00
```text
1,"ABC"
```