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!

2
  • Dup: stackoverflow.com/questions/2905306/… Commented May 31, 2010 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". Commented May 31, 2010 at 10:37

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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)

1 Comment

I know and I was asking about GO after USE which is exactly on page you reference.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.