# SET Variable

## Syntax

```sql
SET var_name = expr [, var_name = expr] ...
```

## Description

The `SET` statement in [stored programs](/docs/server/server-usage/stored-routines.md) is an extended version of the general [SET](/docs/server/reference/sql-statements/administrative-sql-statements/set-commands.md) statement. Referenced variables may be ones declared inside a stored program, global system variables, or user-defined variables.

The `SET` statement in stored programs is implemented as part of the pre-existing [SET](/docs/server/reference/sql-statements/administrative-sql-statements/set-commands/set.md) syntax. This allows an extended syntax of `SET a=x, b=y, ...` where different variable types (locally declared variables, global and session server variables, user-defined variables) can be mixed. This also allows combinations of local variables and some options that make sense only for system variables; in that case, the options are recognized but ignored.

`SET` can be used with both [local variables](/docs/server/reference/sql-statements/programmatic-compound-statements/declare-variable.md) and [user-defined variables](/docs/server/reference/sql-structure/sql-language-structure/user-defined-variables.md).

When setting several variables using the columns returned by a query, [SELECT INTO](/docs/server/reference/sql-statements/programmatic-compound-statements/selectinto.md) should be preferred.

To set many variables to the same value, the [LAST\_VALUE( )](/docs/server/reference/sql-functions/secondary-functions/information-functions/last_value.md) function can be used.

Below is an example of how a user-defined variable may be set:

```sql
SET @x = 1;
```

## See Also

* [SET](/docs/server/reference/sql-statements/administrative-sql-statements/set-commands/set.md)
* [SET STATEMENT](/docs/server/reference/sql-statements/administrative-sql-statements/set-commands/set-statement.md)
* [DECLARE Variable](/docs/server/reference/sql-statements/programmatic-compound-statements/declare-variable.md)

<sub>*This page is licensed: GPLv2, originally from*</sub> [<sub>*fill\_help\_tables.sql*</sub>](https://github.com/MariaDB/server/blob/main/scripts/fill_help_tables.sql)

{% @marketo/form formId="4316" %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://mariadb.com/docs/server/reference/sql-statements/administrative-sql-statements/set-commands/set-variable.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
