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)