Declares an SQL variable within a compound statement (BEGIN... END).
DECLARE variable_name [ , … ] data-type [{ = | DEFAULT} initial-value] initial-value - (back to Syntax) special-value | string | [ - ] number | ( constant-expression ) | built-in-function ( constant-expression ) | NULL special-value - (back to initial-value) CURRENT { DATABASE | DATE | PUBLISHER | TIME | TIMESTAMP | USER | UTC TIMESTAMP } | USER
BEGIN DECLARE varname CHAR(61); SET varname = 'Test name'; MESSAGE varname; END
Use the DECLARE statement to declare variables used in the body of a procedure. The variable persists for the duration of the compound statement in which it is declared and must be unique within the compound statement.
The body of a procedure is a compound statement, and variables must be declared immediately following BEGIN. In a Transact-SQL procedure or trigger, there is no such restriction.