T-SQL query advanced -- Flow Control Statement (1)

Source: Internet
Author: User

Overview

Like other advanced languages, the T-SQL also has statements used to control the flow. The flow control statement in the T-SQL further extends the power of the T-SQL ...... This allows most of the business logic to be performed at the database level. But many people do not have a systematic understanding of the flow control statement in the T-SQL, this article will systematically explain the flow control statement in the T-SQL statement.

Basic Concepts

In the absence of flow control statements, T-SQL statements are executed one by one in the order from top to bottom:

The process control statement allows developers to perform selective jump based on certain logic, and implements a jump structure similar to advanced languages:

Scope of use of flow control statements and GO keywords

Flow Control statements can only be used in a single Batch segment (Batch), user-defined functions and stored procedures. You cannot span multiple batch processing segments or customize functions and stored procedures.

Because the focus here is on the T-SQL query statement, so here only talk about the Batch processing segment (Batch ).

A batch segment is a batch of one or more statements. It is called batch processing because all statements are submitted to one SQL instance at a time. In this batch processing range, local variables are mutually visible.

To submit multiple statements to an SQL instance multiple times, you must use the GO keyword. The GO keyword itself is not an SQL statement. The GO keyword can be considered as an identifier of the end of batch processing. When the GO keyword is encountered, the statements before the current GO are directly transferred to the SQL instance for execution as a batch. Therefore, local variables are not visible in the same batch, and process control statements cannot be used for cross-Batch statements.

In the same batch processing, local variables are mutually visible:

Local variables are invisible in different batches:

In different batches, process control statements cannot be processed across batches:


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.