What Does The GO Command Do?

Published on: 2018-05-29

Watch this week’s episode on YouTube.

When I started working with T-SQL, I thought the GO command was optional, kind of like semicolons.  It appeared in plenty of SSMS generated scripts, but it seemed like I never had to add it to any queries of my own:

Turns out that GO isn’t T-SQL at all, but a command that allows apps (like SSMS) to send batches of queries to SQL Server.  It also turns out it has some more functionality than I originally thought.

So when should you use GO?

Using GO When Required

Just like semicolons not being entirely optional (ever try to run a CTE after a statement without a semicolon?), GO isn’t completely optional either.

Some operations require that GO appear immediately after them:

The above script will fail with the error message “‘CREATE/ALTER PROCEDURE’ must be the first statement in a query batch.”

There are multiple commands (“CREATE DEFAULT, CREATE FUNCTION, CREATE PROCEDURE, CREATE RULE, CREATE SCHEMA, CREATE TRIGGER, and CREATE VIEW”) that require being the first statement of a batch, so using GO is required if you are going to try running other statements as part of your script.

So how do you run that CREATE PROCEDURE statement after first checking and dropping that procedure?  Just add GO so that CREATE PROCEDURE is the first statement of the batch:

Executing Commands Multiple Times

So the previous example was one where SQL Server required me to type two extra characters to run certain commands.  Boo.  What about something actually useful I can do with GO?

Sometimes you may want to run a statement more than once.  You can do that by being trigger happy with the F5 key or your mouse button, but you don’t want to do that 10,000 times, do you?

That’s where GO shines.  Simply add an integer after GO and SQL Server will execute that batch of statements however many times you specified.  For example, the following code will insert 10,000 rows into a table:

This is really useful when creating test data or simulating workloads.

Thanks for reading. You might also enjoy following me on Twitter.

Want to learn even more SQL?

Sign up for my newsletter to receive weekly SQL tips!

4 thoughts on “What Does The GO Command Do?”

  1. Thank you Bert, nice video (loved the background music).
    I have a third way I use GO, to separate comment blocks for outlining…
    [+]/* Comment block 1[…]
    /* Comment block 2
    These might be long blocks that we may want to collapse independently using the outlining feature in SSMS or VS…
    [+]/* Comment block 3[…]

  2. Very useful, simple, straight to point article. I never had much use for the “annoying” (in my up to now opinion) go command. After reading your article, I have a more thorough understanding of its function, AND the added bonus of its usefulness to repeat the same batch/command/query as many times as I may need. Perfect. Thank you! Raphael.

  3. I also use GO when I want to split the execution plan. Just a note: creating table variable and then using GO finishes the scope of the table variable and you won’t be able to use it further.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.