SQL Flow Field Guide

SET @local_variable

Catalog support for SQL Server 2022 (16.x) and 2025 (17.x)

assigns one value

What changes

Input
an expression or scalar subquery
Output or change
one variable (or cursor variable)

Syntax at a glance

SET { @local_variable [ . { property_name | field_name } ] = { expression | udt_name { . | :: } method_name } } | { @SQLCLR_local_variable.mutator_method } | { @local_variable { += | -= | *= | /= | %= | &= | ^= | |= } expression } | { @cursor_variable = { @cursor_variable | cursor_name | { CURSOR [ [ LOCAL | GLOBAL ] ] [ FORWARD_ONLY | SCROLL ] [ STATIC | KEYSET | DYNAMIC | FAST_FORWARD ] [ READ_ONLY | ...

Work example

A local variable needs one explicit value from a scalar expression.

Run the sample and keep SET for one assignment. Use a declared type and test null and conversion cases separately.

Check your work SET @local_variable. The variable contains 7 after SET. The statement assigns one scalar value.

Practice SQL

Open in SQL Flow
DECLARE @Value int;
SET @Value = 7;
SELECT @Value AS AssignedValue;

Open Microsoft Learn for full syntax, permissions, and details for your version.