SQL Command GO in SQL Server

GO statement is not a Transact-SQL statement, it signals the end of a batch of Transact-SQL statements to the SQL Server utilities. It is a batch separator. 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 statement. The statements in one batch are compiled into a single execution plan.

A T-SQL statement cannot be written in the same line as a GO command, but the line can contain comments.

GO command can also accept an integer as an argument. All the statements preceding the GO with an integer argument will execute as many times as specified by the argument:

PRINT 'Hello'
GO 3

If you try to run this script:

CREATE TABLE test_table_2 (Col1 int)
INSERT INTO test_table_2 VALUES (1)
GO 10

...you will receive an error:
"Beginning execution loop
Msg 2714, Level 16, State 6, Line 1
There is already an object named 'test_table_2' in the database."

But if you separate the create table from inserting values in the table, with the GO Batch Separator:

drop table test_table_2
create table test_table_2 (Col1 int)
INSERT INTO test_table_2 VALUES (1)
GO 10

The script will execute correctly, because only the INSERT operation will be executed 10 times.

The default Batch Separator is GO, we can change it, but I don't recommend that you do so. You can encounter problems with your code when executed on a different SSMS, because these changes are in effect only on the SSMS where you made the changes. Other SQL Server utilities like SQLCMD are unaware of this change. To change the Batch Separator in SQL Server Management Studio:
Click: Tools -> Options -> Query Execution -> SQL Server ->Batch Separator

The change will take effect only from the next opened SQL Query window, and not in any of the already opened Query windows.

To change the Batch Separator in SQLCMD to "Run", go to Command prompt and type:

The new Batch Separator "Run" will work only as long as this current connection is open.