T-sql: Batch Go usage example (14)

Source: Internet
Author: User

A batch is one or more statements executed by a client application as a unit to SQL Server if an error occurs in a batch, the entire batch is not handed to SQL Server for execution

PRINT 'First Batch';GO--Invalid BatchPRINT 'Second Instalment';SELECTCustID fromsales.customers;SELECTOrderID FOM sales.orders;GO--Valid BatchPRINT 'Third Instalment';SELECTEmpid fromHR. Employees;

Execution will find that the second batch of errors is not submitted for execution

Batch by GO command partition

DECLARE @i  as INT = Ten ; -- succeeds PRINT @i ; GO -- fails PRINT @i ; GO

Go partition the @i variable will be error

For example, there is another situation

IF object_id('Sales.myview','V') is  not NULL DROP VIEWSales.myview;CREATE VIEWSales.myview asSELECT  Year(OrderDate) asOrderYear,COUNT(*) asnumorders fromsales.ordersGROUP  by  Year(OrderDate);GO
CREATE VIEW

Create view must be the first statement in a batch so the GO command is truncated in front of the CREATE view

There is an error in the same batch to change the schema to be placed in two batches

CREATE TABLE INT ); GO -- following fails ALTER TABLE ADD INT ; SELECT  from dbo. T1; GO

You can also run batches with Go loops:

Print ' 1123 ' GO 5

can be used to batch new statements

T-sql: Batch Go usage example (14)

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.