SQL Flow Field Guide

SELECT @local_variable

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

assignment from a query; no result set

What changes

Input
query rows
Output or change
variables; with many rows the last row processed wins (order not guaranteed)

Syntax at a glance

SELECT { @local_variable { = | += | -= | *= | /= | %= | &= | ^= | |= } expression } [ , ...n ] [ ; ]

Work example

One scalar result must be assigned to a local variable for the next statement.

Run the sample where the SELECT returns exactly one row. Do not use SELECT assignment for an unordered multirow result.

Check your work SELECT @local_variable. The variable contains 7. A query that returns no rows would leave its previous value unchanged.

Practice SQL

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

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