---
title: DECLARE VARIABLE
description: Declare a session-scoped temporary variable with DECLARE VARIABLE in Databricks SQL and Databricks Runtime.
canonical_url: https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-ddl-declare-variable
---

# DECLARE VARIABLE

**Applies to:** ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks SQL ![check marked yes](https://docs.databricks.com/gcp/en/images/icons/check.png) Databricks Runtime 14.1 and above

Creates a session private, temporary variable you can reference wherever a [constant expression](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-expression.md#constant-expression) can be used.
You can also use variables in combination with the [IDENTIFIER clause](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-names-identifier-clause.md) to parameterize identifiers in SQL statements.

Variables are modified using the [SET VARIABLE](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-aux-set-variable.md) statement.

Temporary variables cannot be referenced within:

* a check constraint
* a generated column
* a default expression
* the body of a persisted SQL UDF
* the body of a persisted view

Temporary variables are also called *session* variables.

## Syntax

```
DECLARE [ OR REPLACE ] [ VARIABLE ] variable_name [, ...]
    [ data_type ] [ { DEFAULT | = } default_expression ]
```

Prior to Databricks Runtime 17.2 you can only specify one `variable_name` at a time.

## Parameters

* **OR REPLACE**

  If specified, the variable with the same name is replaced.

* **[variable\_name](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-names.md#variable-name)**

  A name for the variable.
  The name may be qualified with `session` or `system.session`.
  Unless `OR REPLACE` is specified, the variable name must be unique within the session and must not duplicate any other variable name in the statement.

* **[data\_type](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-datatypes.md)**

  Any supported data type. If `data_type` is omitted, you must specify `DEFAULT`, and the type is derived from the `default_expression`.

* **DEFAULT [default\_expression](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-expression.md)** or **= [default\_expression](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-expression.md)**

  Defines the initial value of the variable after creation.
  `default_expressio`n must be castable to `data_type`.
  If no default is specified, the variable is initialized with `NULL`.

  If expression includes a subquery Databricks raises a [INVALID\_DEFAULT\_VALUE.SUBQUERY\_EXPRESSION](https://docs.databricks.com/gcp/en/error-messages/invalid-default-value-error-class.md) error.

## Examples

```sql
-- Create a variable with a default
> DECLARE VARIABLE myvar INT DEFAULT 5;
> VALUES (myvar);
 5

-- Setting a variable
> SET VAR myvar = (SELECT sum(c1) FROM VALUES(1), (2) AS T(c1);
> VALUES (myvar);
 3

-- Variables are the outermost scope.
> SELECT myvar, t.myvar, session.myvar FROM VALUES(1) AS T(myvar);
  1  1  3

> DROP TEMPORARY VARIABLE myvar;

-- A minimalist variable declaration
> DECLARE myvar = 5;
> VALUES (myvar);
 5

-- Declaring multiple variables
> DECLARE var1, var2 DOUBLE DEFAULT rand();
> VALUES (var1, var2);
 0.3745401188473625  0.3745401188473625

-- Using a variable with an IDENTIFIER clause
> DECLARE colname STRING;
> SET VAR colname = 'c1';
> SELECT IDENTIFIER(colname) FROM VALUES(1, 2) AS T(c1, c2);
  1

> SET VAR colname = 'c2';
> SELECT IDENTIFIER(colname) FROM VALUES(1, 2) AS T(c1, c2);
  2
```

## Related articles

* [DROP VARIABLE](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-ddl-drop-variable.md)
* [IDENTIFIER clause](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-names-identifier-clause.md)
* [SET variable](https://docs.databricks.com/gcp/en/sql/language-manual/sql-ref-syntax-aux-set-variable.md)
