---
title: "Prepared statements: PREPARE, EXECUTE, DEALLOCATE"
description: "Prepare an SQL statement once and run it many times with CoraDB PREPARE, EXECUTE ... USING and DEALLOCATE PREPARE."
url: "https://www.coradb.ai/manual/sql/query/prepare.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/query/prepare.md"
product: "CoraDB"
version: "1.0"
keywords: "prepare statement, execute using, deallocate prepare, drop prepare, bind parameter, prepared statement sql, coradb prepare"
---

# Prepared statements: PREPARE, EXECUTE, DEALLOCATE

A prepared statement is an SQL statement that is compiled once and then run as often as needed, with different values bound to its `?` placeholders. Applications normally prepare statements through their driver (for example `PreparedStatement` in [JDBC](https://www.coradb.ai/manual/api/jdbc.html) or `prepare()` in the [PHP](https://www.coradb.ai/manual/api/php.html), [Python](https://www.coradb.ai/manual/api/python.html), [Node.js](https://www.coradb.ai/manual/api/node_js.html) and [ADO.NET](https://www.coradb.ai/manual/api/adodotnet.html) drivers). The same can be done in SQL with three statements:

```text
PREPARE stmt_name FROM 'preparable_stmt'
EXECUTE stmt_name [USING value [, value] ...]
{DEALLOCATE | DROP} PREPARE stmt_name
```

> **Note:**
> - SQL-level `PREPARE` is meant for interactive use in the [corasql interpreter](https://www.coradb.ai/manual/corasql.html). In applications, use the prepared statements of your driver.
> - A connection can hold at most 20 SQL-level prepared statements, because they are kept in the database server's memory. Driver-level prepared statements are limited by the broker parameter [MAX_PREPARED_STMT_COUNT](https://www.coradb.ai/manual/admin/config_broker.html#max_prepared_stmt_count).


## PREPARE

```text
PREPARE stmt_name FROM 'preparable_stmt'
```

- *stmt_name*: the name used to run or drop the statement later. If a prepared statement with this name already exists in the session, it is replaced. If preparing fails, any existing statement with that name is dropped as well.
- *preparable_stmt*: exactly one SQL statement, as a string. Use `?` (without quotes) for each value to bind at execution time.

Prepare a product search with two parameters, a minimum price and a row count:

```sql
PREPARE by_price FROM
    'SELECT id, name, unit_price FROM product WHERE unit_price > ? ORDER BY unit_price DESC LIMIT ?';
```

Prepared statements belong to the session: they live until you drop them or the connection ends. The session also keeps your [user-defined variables](https://www.coradb.ai/manual/sql/query/set.html), the last generated `AUTO_INCREMENT` value ([LAST_INSERT_ID](https://www.coradb.ai/manual/sql/function/information_fn.html#last_insert_id)) and the row count of the last statement ([ROW_COUNT](https://www.coradb.ai/manual/sql/function/information_fn.html#row_count)). An idle session expires after `session_state_timeout` seconds (see [Other parameters](https://www.coradb.ai/manual/admin/config_other.html#other-parameters)).


## EXECUTE

```text
EXECUTE stmt_name [USING value [, value] ...]
```

- *stmt_name*: the prepared statement to run. An error is returned if it does not exist.
- *value*: the values for the `?` placeholders, in order. The number of values must match the number of placeholders. A value can be a literal or a user-defined variable.

```sql
EXECUTE by_price USING 90, 3;
```

| id | name | unit_price |
|---:|---|---:|
| 38 | Côte de Blaye | 263.50 |
| 29 | Thüringer Rostbratwurst | 123.79 |
| 454 | Lando Gym Jacket | 99.00 |

The same statement with values taken from user-defined variables:

```sql
SET @min_price = 120, @max_rows = 2;
EXECUTE by_price USING @min_price, @max_rows;
```

| id | name | unit_price |
|---:|---|---:|
| 38 | Côte de Blaye | 263.50 |
| 29 | Thüringer Rostbratwurst | 123.79 |

The type of a placeholder is decided by how it is used and by the value you bind. When two string values are bound to `? + ?`, `+` concatenates them:

```sql
PREPARE st FROM 'SELECT ? + ?';
EXECUTE st USING 'a', 'b';
```

```text
ab
```

## DEALLOCATE PREPARE / DROP PREPARE

```text
{DEALLOCATE | DROP} PREPARE stmt_name
```

Both forms drop a prepared statement and free its memory on the server. Statements that are not dropped explicitly are dropped when the session ends.

```sql
DEALLOCATE PREPARE by_price;
DROP PREPARE st;
```

## See also

- [SET](https://www.coradb.ai/manual/sql/query/set.html): user-defined variables
- [Drivers & APIs](https://www.coradb.ai/manual/api.html)
