> ## 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/ja/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/ja/reference/settings/session-settings#allow_ddl) 設定が無効な場合、テーブル関数は使用できません。
</Note>
