Here I GO again!

The very first SQL Server article I ever published, in the old SQL Server Professional journal from Pinnacle Publishing, was about the use of GO. Twenty-five years ago, there was confusion about just what GO was, and there still is confusion. So maybe I should just post something like this once a year, whether I think people need it or not. (There are other things I feel I need to repeat, or even shout from the rooftops, that I see over and over... like CASE is an EXPRESSION, not a STATEMENT, but this post really isn't the place for that. :-) )

So what is GO? What is it NOT? Is is NOT a Transact-SQL keyword or command. In the current documentation it even says "GO is not a Transact-SQL statement".  But how often do people look up the documentation for GO? If you did, you'd also see that GO can take an argument, an integer that indicates how many times the preceding batch is to be executed. According to an old blog post of mine, this integer <count>  didn't used to be documented, but now it is. But if you never read this documentation, what does it matter? 

So GO is a command to the tool that you are using, and not every tool recognizes GO. SQL Server Management Studio does, in the query editor window. It's used to separate batches. So you need to know what a batch is. I usually define 'batch' as a unit of communication from the client (SSMS, in this case) to your SQL Server instance. You can send one statement, or multiple statements, all in a single batch. And the GO is not ever seen by SQL Server. It just receives the commands and statements before the GO. But if there is a compile-time error (like a misspelled keyword or a missing parenthesis) anywhere in the batch, none of the statements in the batch will be executed.  And there are rules about what can and cannot be combined in a batch, but this post is not the place for all those rules.  

Since GO is only recognized by the tool you are using, the tool has some control. In SSMS, you can actually change the batch separator to not be GO. Under the tools menu, choose Options and you'll see this dialog. In the left hand list, choose Query Execution|SQL Server|General, and you'll see the place to replace GO with some other string of your choosing. Devious tricksters have even suggested replacing it with SELECT on someone else's SSMS installation, if you're not interested in having that person for a friend. 

GO.jpg

I'm sure there is more I could say about GO, but I'll say that for tweets, or next year's post on this same topic. 

~Kalen