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

39 lines
1.4 KiB
Markdown
Raw Normal View History

---
toc_title: INTO OUTFILE
---
# INTO OUTFILE Clause {#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.
2021-11-30 08:36:03 +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.
2021-11-20 18:44:45 +00:00
**Syntax**
```sql
2021-11-28 08:20:57 +00:00
SELECT <expr_list> INTO OUTFILE file_name [COMPRESSION type]
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'`.
## Implementation Details {#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.
2021-11-20 18:44:45 +00:00
- 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.
**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"
```