T-sql:go Statement and batch processing (update ing)

Source: Internet
Author: User
Tags management studio

own understanding: With go separate, is equivalent to in different query window execution SQL, go requires a separate commit transaction

Refer to the blog post when finishing:

Http://www.cnblogs.com/kissdodog/p/3163880.html

http://lockrock.blog.51cto.com/2147255/775783

A batch is a T-SQL statement that acts as a logical unit. In a full batch, no statement is run if the first statement cannot be parsed. Statements are executed sequentially, and if there is an error in the execution of a statement, The times are wrong, but statements with no syntax errors are already running until the wrong statement is generated.

To divide a script into batches, you can use the GO statement:

The GO statement must be self-made, and only comments can be on the same line;

 Use [adventureworks2008r2] SELECT *  from WHERE =  - GO

This situation will be an error.

All SQL statements that are separated by the GO statement are compiled into an execution plan and sent to the server, regardless of any other batches, each batch is sent to the server separately, so that in the process of execution, because each batch is processed separately, the error in one batch does not prevent another batch from running;

SELECT 1 / 0 GO SELECT 0 / 1

In this case, the first sentence will be the error, the second sentence returns the result normally, if there is a dependency between these batches, then each batch after the error occurs will fail. Dependency refers to the following statement, depending on the result or variable that was executed earlier, and so on.

A common mistake is to think of go as a T-SQL command, in fact go is a command that can only be recognized by the editing tool (Management Studio). If you use a third-party client tool to connect to MSSQLSERVER, you cannot use it.

When the edit tool encounters a go statement, it sees the GO statement as a token that terminates the batch, packages it, and sends it as a standalone unit to the server, excluding go. Because the server itself doesn't know what go means.

Case:

BEGIN TRUNCATE TABLE [Tabletest]
GO END

The statement in which the parcel is wrapped represents a complete batch, where go is used to separate the error.

Errors in the batch are divided into the following two classes: 1, syntax error 2, run-time error

Syntax errors, which are typically used before execution, can be detected: If the Query Analyzer finds a syntax error, the batch processing is immediately canceled. Because a grammar check occurs before a batch is compiled or executed, a failure during a grammar check means that no batch has been executed-regardless of where the syntax error occurs in the batch:

CREATE DATABASE Test  Use Test

This analysis will not be passed, the execution of a sentence will not be executed, but in between the two sentences added to go, although the analysis will be error, but the actual implementation can be normal execution;

If you do not join go, you will be prompted directly that the database does not exist.

Run-time errors work in a very different way, this situation analysis will not report an error, only in the process of running the errors will be terminated, because any run-time errors before the execution of the statement has been completed, so unless it is part of the uncommitted transaction, the statements do anything that is already realistic.

In general, run-time errors will terminate the execution of batches from where the error occurred to the end of this batch process. The next batch does not affect.

T-sql:go Statement and batch processing (update ing)

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.