> ## Documentation Index
> Fetch the complete documentation index at: https://private-7c7dfe99-postgresql-tls-support.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

> The `eval` table function evaluates a constant expression to a query string and executes the resulting `SELECT` query.

# eval

The `eval` table function evaluates its argument to a query string, then executes that string as a single `SELECT` query.

This table function is experimental and disabled by default. Enable it with the `allow_experimental_eval_table_function` setting:

```sql theme={null}
SET allow_experimental_eval_table_function = 1;
```

It also requires the analyzer ([`enable_analyzer`](/reference/settings/session-settings#allow_experimental_analyzer), enabled by default).

<h2 id="syntax">
  Syntax
</h2>

```sql theme={null}
eval(expression)
eval(SELECT ...)
```

<h2 id="arguments">
  Arguments
</h2>

* `expression` — A constant expression that returns a query string. Query parameters and scalar subqueries are allowed.
* `SELECT ...` — A `SELECT` query that returns a query string. This form is syntactic sugar for a scalar subquery: `eval(SELECT ...)` is equivalent to `eval((SELECT ...))`, so the query must return exactly one row and one column.

The value of the expression must have one of these types:

* `String`
* `Nullable(String)`
* `LowCardinality(String)`
* `LowCardinality(Nullable(String))`

If the value is `NULL`, `eval` throws an exception.

<h2 id="returned-value">
  Returned Value
</h2>

Returns the result of the generated `SELECT` query as a table.

The output schema is determined when the `eval` table function is analyzed, so outer queries can refer to the generated query's real column names and types.

<h2 id="examples">
  Examples
</h2>

Evaluate a constant expression:

```sql theme={null}
SELECT * FROM eval('SEL' || 'ECT 1 AS x');
```

Result:

```text theme={null}
┌─x─┐
│ 1 │
└───┘
```

Use a query parameter:

```sql theme={null}
SET param_q = 'SELECT 2 AS y';
SELECT * FROM eval({q:String});
```

Result:

```text theme={null}
┌─y─┐
│ 2 │
└───┘
```

Evaluate an input `SELECT` query that returns query text:

```sql theme={null}
SELECT * FROM eval(SELECT 'SELECT 3 AS z');
```

Result:

```text theme={null}
┌─z─┐
│ 3 │
└───┘
```

Use the generated schema in the outer query:

```sql theme={null}
SELECT x + 1 FROM eval('SELECT 4 AS x');
```

Result:

```text theme={null}
┌─plus(x, 1)─┐
│          5 │
└────────────┘
```

<h2 id="restrictions">
  Restrictions
</h2>

* `eval` works only with query analyzer (`enable_analyzer = 1`).
* The generated query must be a single `SELECT` query without output options such as `INTO OUTFILE` or `FORMAT`. `eval` does not execute multiple statements.
* The generated query must be self-contained: it is resolved in its own scope and cannot reference `WITH` aliases or columns of the outer query.
* The generated query cannot use the `eval` table function again.
* `eval` cannot be used as an argument of another table function, such as `remote('host', eval(...))`. To execute the generated query on a remote server, wrap it into `view`: `remote('host', view(SELECT * FROM eval(...)))`.
* The argument is evaluated once while the query is analyzed, not once per row or block.
* The outer query log records the original query that contains `eval`; the generated `SELECT` query is not logged as a separate user query.

<h2 id="related">
  Related
</h2>

* [`view` table function](/reference/functions/table-functions/view)
* `allow_experimental_eval_table_function` setting
