T-SQL query advanced -- variable (1)

Source: Internet
Author: User

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.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.