What is Go in SQL Server and the Importance of Using Go

25-05-2012

Go is not a SQL or Transact SQL command. Go is a command recognized by
sqlcmd and osql facilities provided by SQL server studio.

Go specifies an end to a particular SQL batch. Once the parser interprets a
Go, that block is considered completed and the following SQL are considered
as new batch till the parser finds a new Go.

For example, even though following code isn’t syntactically wrong, it won’t
work because a create procedure has to be the first statement in a batch:


if object_id('GetRRTData') is not null
	drop procedure GetRRTData;

CREATE/ALTER PROCEDURE GetRRTData
@TargetDate datetime,
@NephFilter varchar (100),
@ElsewhereFilter varchar (100)
as
...
...

/* This will return following error :
 * Msg 111, Level 15, State 1, Procedure GetRRTData, Line 18
 * 'CREATE/ALTER PROCEDURE' must be the first statement in a query batch.
*/

You can eliminate this by breaking the SQL above the Create Procedure
Statement into a separate SQL batch using a Go.

Following code works without any complain:


if object_id('GetRRTData') is not null
	drop procedure GetRRTData;
Go

CREATE/ALTER PROCEDURE GetRRTData
@TargetDate datetime,
@NephFilter varchar (100),
@ElsewhereFilter varchar (100)
as
...
...

For an in-depth knowledge on this read this msdn article

Menol

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: