> ## Documentation Index
> Fetch the complete documentation index at: https://clickhouse.com/docs/llms.txt
> Use this file to discover all available pages before exploring further.

> 表函数文档

# 表函数

表函数是用于构建表的函数。

<div id="usage">
  ## 用法
</div>

表函数可用于 `SELECT` 查询的 [`FROM`](/docs/zh/reference/statements/select/from)
子句中。例如，你可以使用 `file` 表函数从本地
计算机上的文件中 `SELECT` 数据。

```bash title="Query" theme={null}
echo "1, 2, 3" > example.csv
```

```text title="Response" theme={null}
./clickhouse client
:) SELECT * FROM file('example.csv')
┌─c1─┬─c2─┬─c3─┐
│  1 │  2 │  3 │
└────┴────┴────┘
```

你也可以使用表函数创建仅在当前查询中可用的临时表。例如：

```sql title="Query" theme={null}
SELECT * FROM generateSeries(1,5);
```

```response title="Response" theme={null}
┌─generate_series─┐
│               1 │
│               2 │
│               3 │
│               4 │
│               5 │
└─────────────────┘
```

查询结束后，该表会被删除。

可以使用表函数创建表，语法如下：

```sql title="Query" theme={null}
CREATE TABLE [IF NOT EXISTS] [db.]table_name AS table_function()
```

例如：

```sql title="Query" theme={null}
CREATE TABLE series AS generateSeries(1, 5);
SELECT * FROM series;
```

```response title="Response" theme={null}
┌─generate_series─┐
│               1 │
│               2 │
│               3 │
│               4 │
│               5 │
└─────────────────┘
```

最后，表函数也可用于将数据 `INSERT` 到表中。例如，
我们可以再次使用 `file` 表函数，将前一个示例中创建的表的内容
写入磁盘上的文件：

```sql title="Query" theme={null}
INSERT INTO FUNCTION file('numbers.csv', 'CSV') SELECT * FROM series;
```

```bash title="Query" theme={null}
cat numbers.csv
1
2
3
4
5
```

<Note>
  如果 [allow\_ddl](/docs/zh/reference/settings/session-settings#allow_ddl) 设置已禁用，则无法使用表函数。
</Note>
