Dustin Lewis
Dustin Lewis

Reputation: 25

Can 'GO' be overused in SQL?

I'm pretty new to SQL and want to know if GO can overused. Does GO affect performance in any way or have any other unintended consequences?

Upvotes: 2

Views: 109

Answers (3)

user743382
user743382

Reputation:

As answered already, GO is not a statement but is used to separate a script into batches. Each batch gets sent to and executed by the server.

Because of that, yes, it is possible to overuse it. If you have multiple statements that logically belong together in the same batch, and there are no technical reasons to put them in separate batches, then yes, if you separate them anyway, you're overusing it.

A simple example is a CREATE TABLE followed by an INSERT INTO that newly created table. If they are in the same batch, and CREATE TABLE fails, then the whole batch is aborted, and the server doesn't even bother to attempt to insert into the non-existent table. If you separate them with GO, you won't just get an error about the bad CREATE TABLE, you'll also get an error from the INSERT INTO that the table doesn't exist. Or worse, the name could get resolved to a different table (same name, different schema) than the one you're attempting to insert into.

But there are also cases where statements logically belong together in a single batch, but aren't accepted by SQL Server in a single batch. IIRC, CREATE VIEW is an example of a statement that must be the first and only statement in a batch. If you intend to create a view A, and a view B that refers to A, you still can't put them in the same batch, you need GO between them or some other form of forcing separate batches. In that case, GO is definitely not overused.

Upvotes: 2

BrianAtkins
BrianAtkins

Reputation: 1349

No, it can't be overused and is not necessary. The GO statement isn't a Transact-SQL command, but a separator recognized by several MS utilities including SQL Server Management Studio code editor.

The GO separator is used to group SQL commands into batches which are sent to the server together. The commands included in the batch, that is, the set of commands since the last GO command or the start of the session, must be logically consistent. For example, you can't define a variable in one batch and then use it in another since the scope of the variable is limited to the batch in which it's defined.

For more information, see http://msdn.microsoft.com/en-us/library/ms188037.aspx.

Upvotes: 3

Randall
Randall

Reputation: 1521

I suppose anything can be overused, but go seems like a tough one. Go just simply executes the t-sql in front of it, and if you are pressing F5 anyway, you were planning on executing it regardless. It could cause some extra work such as re-declaring variables and what not, but it's mostly harmless.

The best analogy I can think of is go is like a comma; this, then that. Overusing a comma can make reading what you wrote confusing, and overusing go can do the same.

Upvotes: 1

Related Questions