While generating the SQL scripts, you might have often seen the GO keyword. What it is ? Let's see what exactly it is.
At first look, it might seem to be a keyword of T-SQL like any other keyword like SELECT, DISTINCT, ORDER BY etc. However, it is not. Yes, it is not a T-SQL keyword. As per MSDN, this keyword is:
GO is not a Transact-SQL statement; it is a command recognized by the sqlcmd and osql utilities and SQL Server Management Studio Code editor.
SQL Server utilities interpret GO as a signal that they should send the current batch of Transact-SQL statements to an instance of SQL Server. The current batch of statements is composed of all statements entered since the last GO, or since the start of the ad hoc session or script if this is the first GO.
This means, it is not considered as a T-SQL keyword and is more of a command to SQL Server. This keyword basically act as a command for SQL to execute a group or batch of statements which are written before the GO keyword is encountered.
An important point to note here is that the GO keyword limits the scope of the variables to the batch of statements on which this command is used. This means if we try to use a declared variable after the GO keyword is executed, it will throw an error i.e. the keyword will not be available after it. See the following sample code:
Happy Querying...!!!