A variable is a user-defined, assignable, and participating entity in Transact-SQL, which can be divided into global variables and local variables in two categories. Where global variables are customized and maintained by the system, global variable names are signed with @@ 字符, and any program can be called at any time. The local variable name is preceded by an @, which is defined and used by the user.
1. Local Variables
Local variables typically appear in batches, stored procedures, and triggers, and if you want to use local variables, you must define them in the Declare statement before use. A local variable exists only in the batch, stored procedure, or trigger that declares it, and the information stored in the local variable is lost after processing ends.
1.1 Local variable Declaration
When you use the DECLARE statement to declare a local variable, you must supply the variable name and data type, which must precede the name of the @ character, and the maximum length of the variable name is 30 characters. You can use any system-defined data type or user-defined data type when declaring a variable.
DECLARE syntax:
DECLARE @variable_name | [= value] [,... n]
Parameters:
◊ @variable_name: The name of the variable. Variable names must begin with @.
◊datatype: Any system-provided or user-defined data type. The variable data type cannot be text, ntext, or image.
In SQL Server, you can define more than one variable at a time.
DECLARE @Name VARCHAR (@AgeINT=
SQL Server series: Transact-SQL variables