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: