SQL Flow Field Guide

DECLARE @local_variable

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

declares a batch/module-scoped variable

What changes

Input
optional initial value
Output or change
variable, cursor variable, or table variable

Syntax at a glance

DECLARE { { @local_variable [AS] data_type [ = value ] } | { @cursor_variable_name CURSOR } | { @table_variable_name [AS] <table_type_definition> } } [ , ...n ]

Work example

A batch needs a typed local value for a calculation or a parameter passed to a later statement.

Run the sample and change the declared type. Initialize values before reading them and avoid relying on implicit conversion.

Check your work DECLARE @local_variable. The output is the integer 3 and the declared type controls the expression result.

Practice SQL

Open in SQL Flow
DECLARE @Limit int = 3;
SELECT @Limit AS LimitValue;

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