---
title: "SET statement (session variables)"
description: "Change session system parameters with the CoraDB SET statement, and create, assign and drop user-defined session variables."
url: "https://www.coradb.ai/manual/sql/query/set.html"
markdown_url: "https://www.coradb.ai/manual/markdown/sql/query/set.md"
product: "CoraDB"
version: "1.0"
keywords: "set statement, get statement, get transaction, get trigger, get optimization level, set system parameters, user-defined variable, session variable, @variable, := assignment, deallocate variable, drop variable, coradb set"
---

# SET statement (session variables)

The **SET** statement has two uses: changing the value of a system parameter, and assigning user-defined variables that keep a value for the rest of your session.

> **Try it yourself:**
> The examples run on the [e-commerce sample database](https://www.coradb.ai/manual/sample-database.html) and show the real results. Connect with `corasql -u dba e-commerce`; variables and parameter changes last only until you disconnect.


## System parameters

```text
SET SYSTEM PARAMETERS 'parameter_name=value [; parameter_name=value ...]'
```

Only some parameters can be changed while the database is running; which ones, and whether a change applies to your session or to the whole server, is listed in [Using SQL statements](https://www.coradb.ai/manual/admin/config.html#with-sql) of the configuration chapter. Set several parameters at once by separating them with semicolons:

```sql
SET SYSTEM PARAMETERS 'cte_max_recursions=100; lock_timeout=10';
```

Use `DEFAULT` as the value to go back to the parameter's default:

```sql
SET SYSTEM PARAMETERS 'lock_timeout=DEFAULT; cte_max_recursions=DEFAULT';
```

## User-defined variables

A user-defined variable is a name that starts with `@` and holds one value for the rest of your session (connection). Variables are private to the session that created them and disappear when it ends.

```text
SET @name = expression [, @name = expression ...]
SET @name := expression [, ...]

-- inside another statement
@name := expression

{DEALLOCATE | DROP} VARIABLE @name [, @name ...]
```

- Names are made of letters, digits and underscores, and are not case-sensitive: `@V1` and `@v1` are the same variable.
- A name cannot be a [reserved word](https://www.coradb.ai/manual/sql/keyword.html): `@limit` or `@last` is a syntax error. Choose names such as `@max_rows` or `@last_day`.
- In `SET`, both `=` and `:=` assign. Inside other statements, use `:=`, because `=` is the comparison operator there.
- Reading a variable that was never set, or was dropped, is an error: `Session variable '@v1' not defined.`

### Assigning and using variables

Store a value, then use it in a later query:

```sql
SET @min_price = 120;

SELECT id, name, unit_price
FROM product
WHERE unit_price > @min_price
ORDER BY unit_price DESC;
```

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

Capture a query result with `:=` and reuse it. The last order date is stored in `@last_day`, and the next query counts the orders of that day:

```sql
SELECT @last_day := MAX(order_date) FROM orders;
SELECT COUNT(*) FROM orders WHERE order_date = @last_day;
```

```text
5
```

Variables are also a convenient way to pass values to [prepared statements](https://www.coradb.ai/manual/sql/query/prepare.html#execute-statement):

```sql
SET @min_price = 120, @max_rows = 2;
PREPARE by_price FROM 'SELECT id, name FROM product WHERE unit_price > ? LIMIT ?';
EXECUTE by_price USING @min_price, @max_rows;
```

### Assigning in every row

An assignment in the select list runs once per row, so a variable can number the rows of a result. The five latest orders of customer 440:

```sql
SET @n = 0;

SELECT @n := @n + 1 AS row_no, id, order_date
FROM orders
WHERE customer_id = 440
ORDER BY order_date DESC
LIMIT 5;
```

| row_no | id | order_date |
|---:|---:|---|
| 1 | 1651 | 2025-12-27 |
| 2 | 462 | 2025-12-15 |
| 3 | 2544 | 2025-11-16 |
| 4 | 1611 | 2025-11-13 |
| 5 | 1831 | 2025-11-03 |

For numbering rows, the window function [ROW_NUMBER()](https://www.coradb.ai/manual/sql/function/analysis_fn.html#row_number) is usually the clearer choice. When a statement both assigns and reads the same variable in different clauses (for example, assigns it in the select list and reads it in `HAVING`), the order of evaluation is not guaranteed.

### Variable types

A variable takes the type of the value assigned to it, and assigning a new value can change the type:

```sql
SET @v1 = 1, @v2 = CAST(1 AS BIGINT), @v3 = '123', @v4 = DATE'2025-12-01';
SELECT TYPEOF(@v1), TYPEOF(@v2), TYPEOF(@v3), TYPEOF(@v4);
```

| typeof(@v1) | typeof(@v2) | typeof(@v3) | typeof(@v4) |
|---|---|---|---|
| integer | bigint | character (-1) | character varying (1073741823) |

Variables can hold `SHORT`, `INTEGER`, `BIGINT`, `FLOAT`, `DOUBLE`, `NUMERIC`, `CHAR`, `VARCHAR`, `BIT` and `BIT VARYING` values. Values of other types are converted to `VARCHAR`; that is why the date in `@v4` is stored as a character string. Comparisons such as `order_date = @last_day` above still work, because the string is converted back to a date for the comparison.

### Dropping variables

`DEALLOCATE VARIABLE` and `DROP VARIABLE` are the same statement:

```sql
DEALLOCATE VARIABLE @v1, @v2;
DROP VARIABLE @v3, @v4;
```

> **Note:**
> The session also keeps your [prepared statements](https://www.coradb.ai/manual/sql/query/prepare.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.


## Other SET and GET statements

Several other statements start with **SET** and change a setting of the current session; most have a **GET** counterpart that reads the setting, optionally into a host variable with `INTO :variable` (or `TO :variable`):

| Statement | Reads with | Described in |
|---|---|---|
| `SET TRANSACTION ISOLATION LEVEL level` | `GET TRANSACTION ISOLATION LEVEL` | [Isolation levels](https://www.coradb.ai/manual/sql/transaction_isolation.html) |
| `SET TRANSACTION LOCK TIMEOUT { INFINITE \| OFF \| seconds }` | `GET TRANSACTION LOCK TIMEOUT` | [Locking & deadlocks](https://www.coradb.ai/manual/sql/transaction_locking.html) |
| `SET OPTIMIZATION LEVEL level` | `GET OPTIMIZATION LEVEL` | [Query plans](https://www.coradb.ai/manual/sql/tuning_plan.html#viewing-query-plan) |
| `SET TRACE { ON \| OFF } [ OUTPUT { TEXT \| JSON } ]` | `SHOW TRACE` | [Query profiling](https://www.coradb.ai/manual/sql/tuning_plan.html#query-profiling) |
| `SET TRIGGER TRACE { ON \| OFF }` | `GET TRIGGER TRACE` | [Triggers](https://www.coradb.ai/manual/sql/trigger.html#viewing-trigger-execution-log) |
| `SET TRIGGER [MAXIMUM] DEPTH count` | `GET TRIGGER [MAXIMUM] DEPTH` | [Triggers](https://www.coradb.ai/manual/sql/trigger.html#limiting-nested-trigger) |
| `SET NAMES charset [COLLATE collation]` | | [SET NAMES](https://www.coradb.ai/manual/sql/i18n_collation.html#set-names-statement) |
| | `GET STATISTICS 'item' OF table` | [Statistics](https://www.coradb.ai/manual/sql/tuning.html#get-statistics) |

```sql
SET TRANSACTION LOCK TIMEOUT 5;
GET TRANSACTION LOCK TIMEOUT INTO :lt;
GET TRANSACTION ISOLATION LEVEL INTO :iso;
SELECT :lt, :iso;      -- 5, 4 (4 = READ COMMITTED)
```

**SET OPTIMIZATION COST** and **GET OPTIMIZATION COST** adjust and read internal cost settings of the optimizer, for diagnosis only; leave them at their defaults.

## See also

- [System parameters](https://www.coradb.ai/manual/admin/config.html)
- [Prepared statements](https://www.coradb.ai/manual/sql/query/prepare.html)
