Tuesday 24 February 2015

Go Statement in sql


Signals the end of a batch of Transact-SQL statements to the SQL Server utilities.

GO [count]

count
Is a positive integer. The batch preceding GO will execute the specified number of times.

USE AdventureWorks2012;
GO
DECLARE @MyMsg VARCHAR(50)
SELECT @MyMsg = 'Hello, World.'
Print @MyMsg 

GO 2-- @MyMsg is not valid after this GO ends the batch.

-- Yields an error because @MyMsg not declared in this batch.
PRINT @MyMsg
GO

SELECT @@VERSION;
-- Yields an error: Must be EXEC sp_who if not first statement in 
-- batch.
sp_who
GO

Batches

SQL Server 2000
5 out of 7 rated this helpful Rate this topic
  New Information - SQL Server 2000 SP3.
A batch is a group of one or more Transact-SQL statements sent at one time from an application to Microsoft® SQL Server™ for execution. SQL Server compiles the statements of a batch into a single executable unit, called an execution plan. The statements in the execution plan are then executed one at a time.
A compile error, such as a syntax error, prevents the compilation of the execution plan, so none of the statements in the batch are executed.
A run-time error, such as an arithmetic overflow or a constraint violation, has one of two effects:
  • Most run-time errors stop the current statement and the statements that follow it in the batch.
  • A few run-time errors, such as constraint violations, stop only the current statement. All the remaining statements in the batch are executed.
The statements executed before the one that encountered the run-time error are not affected. The only exception is if the batch is in a transaction and the error causes the transaction to be rolled back. In this case, any uncommitted data modifications made before the run-time error are rolled back.
Assume there are 10 statements in a batch. If the fifth statement has a syntax error, none of the statements in the batch are executed. If the batch is compiled, and the second statement then fails while executing, the results of the first statement are not affected because it has already executed.
These rules apply to batches:
  • CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE TRIGGER, and CREATE VIEW statements cannot be combined with other statements in a batch. The CREATE statement must begin the batch. All other statements that follow in that batch will be interpreted as part of the definition of the first CREATE statement.
  • A table cannot be altered and then the new columns referenced in the same batch.
  • If an EXECUTE statement is the first statement in a batch, the EXECUTE keyword is not required. The EXECUTE keyword is required if the EXECUTE statement is not the first statement in the batch.
    Security Note  Batch files may contain credentials stored in plain text. Credentials may be echoed to the user's screen during batch execution.



Rules for batch

CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE TRIGGER, and CREATE VIEW statements cannot be combined with other statements in a batch. The CREATE statement must begin the batch. All other statements that follow in that batch will be interpreted as part of the definition of the first CREATE statement.

No comments:

Post a Comment