I have mixed feelings about the GO command. It is not a T-SQL statement, it is just something that Management Studio and the other SQL Server tools understand as a batch separator command. Indeed, you can change it to whatever you wish in Management Studio under Tools->Options->Query Execution->SQL Server->General->Batch separator.
So you can write queries like
SELECT * FROM sys.objects
SELECT * FROM sysobjects
Of course I’m still struggling to find a reason why someone would change the GO command.
One thing I’ve found out recently is its parameter. SQL Server Management Studio seems to accept an integer after the GO command, and this will start an execution loop. For example if you write
PRINT ‘Hello word’
The result will be:
Beginning execution loop
Batch execution completed 5 times.
This is perfect for lazy moments when I want to populate a test table with some default values like:
CREATE TABLE foo
( a INT PRIMARY KEY IDENTITY
, b INT DEFAULT 1
I usually write something like:
INSERT TOP (10) INTO foo (b) SELECT 1 FROM sys.objects
but with the parameter to the GO command the above can be achieved with even less typing:
INSERT INTO foo DEFAULT VALUES
The above will also insert 10 rows 🙂
Do let me know if you find a more interesting use for this parameter.