Overview
Variables are an essential part of a language and, of course, the same for T-SQL. Variables are rarely used in simple queries, but variables are an essential part of complex queries or stored procedures.
Variable type
In the T-SQL, variables can be divided into Global variables and Local Variable according to the survival range ).
1. Global variables are defined by the system and can be accessed throughout the SQL SERVER instance. The global variables take @ as the first character. Users can only access them and cannot assign values.
2. The local variable is defined by the user, and the lifecycle is valid only in one batch. The local variable uses @ as the first character, which is defined and assigned by the user.
A simple example is as follows:
Because the global variables are only used to read some system parameters, please Google for the specific meaning of each global variable... This topic describes local variables (custom variables ).
Usage of local variables
In the T-SQL, local variables are an object that stores a single value of the specified data type. The definition of variables in the T-SQL is actually the same as in most advanced languages.
Local variables are often used for the following purposes:
1. record the number of cycles or the number of cycles used to control.
2. It is used to store process statements to control the process trend.
3. Store the returned values of stored procedures or functions
In fact, local variables that store any business data belong to this type of application.