T-SQL query Advanced: Process Control statements

Source: Internet
Author: User

Overview

As with other high-level languages, there are statements in T-SQL that control the process. The process Control statements in T-SQL further extend the power of T-SQL ... So that most of the business logic can be done at the database level. But many people have no systematic understanding of the Process Control statements in T-SQL, and this article will systematically explain the process control statements in T-SQL statements.

Basic concepts

In the absence of a Process control statement, the T-SQL statements are executed one at a, in order from top to bottom:

Using Process Control statements enables developers to make selective jumps based on some logic, implementing a jump structure similar to a high-level language:

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/database/SQLServer/

Use scope and go keyword for process control statements

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

Because the focus here is on T-SQL query statements, only the batch segment (Batch) is spoken here.

A batch segment is a batch consisting of one or more statements, which is called a batch because all statements are submitted to an SQL instance at once. In this batch scope, local variables are visible to each other.

To have multiple statements submitted to the SQL instance multiple times, you need to use the GO keyword. The GO keyword itself is not an SQL statement, and the GOES keyword can be regarded as an identifier for a batch end, and when the GO keyword is encountered, the statement before the current goes is passed directly to the SQL instance as a batch process. Therefore, no local variables are visible within the same batch, and process Control statements cannot be used for statements that span batches.

Local variables are visible to each other in the same batch:

Local variables are not visible in different batches:

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.