SELECT @local_variable
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 FlowDECLARE @Value int;
SELECT @Value = 7;
SELECT @Value AS AssignedValue;
Open Microsoft Learn for full syntax, permissions, and details for your version.