20

Looking at the msdn, there was an example on "GO" command. Why there is:

USE somedb
GO
...
...

It it neccesary to select db in different batch? Thanks for explanation!

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Petr
  • 7,787
  • 14
  • 44
  • 53
  • Dup: http://stackoverflow.com/questions/2905306/whats-the-meaning-of-the-go-statement-in-tsql – Alex K. May 31 '10 at 10:35
  • 1
    It is not. I know what is GO good for. But I cannot see any advantagee in using it after "USE db". – Petr May 31 '10 at 10:37

2 Answers2

20

Is it necessary to select db in different batch?

No, however, some commands have to be the first statement in the batch.

Examples include CREATE VIEW, CREATE PROCEDURE and CREATE TRIGGER.

Thus if you want to do:

USE DB

CREATE VIEW X AS SELECT * FROM Y

Then you need to do:

USE DB
GO

CREATE VIEW X AS SELECT * FROM Y

If you are only running one USE DB statement, the GO has no usefulness.

Some commands do not require that they are the first statement in a batch:

USE DB
SELECT * FROM X

Sometimes in code generation, all the GO commands might not be necessary, but it's just easier to generate them.

Cade Roux
  • 88,164
  • 40
  • 182
  • 265
2

It signals the end of a batch of Transact-SQL statements to the SQL Server utilities. You can check here for more details : GO (Transact-SQL)

Incognito
  • 16,567
  • 9
  • 52
  • 74
  • 4
    I know and I was asking about GO after USE which is exactly on page you reference. – Petr May 31 '10 at 10:34