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