The GO command in SQL Server
Posted by decipherinfosys on October 11, 2007
The GO command in SQL Server is not a T-SQL command. It is a command that is recognized by the SQL Server utilities like osql or sqlcmd or the editors – Query Analyzer or SSMS. This just signals these utilities/editors to batch up the data and send it to the server. The batch comprises of all the statements entered since the last GO statement.
So, if you are playing with your scripts in the editors and are using local variables, remember to put the GO command only at the right place else the local variables will go out of the scope as soon as the batch is over, example:
declare @test int
set @test = 1
will give an error:
Msg 137, Level 15, State 2, Line 2
Must declare the scalar variable “@test”.
Another little improvement that was made in version 2005 was that you can assign a number to make the same batch execute multiple times – this helps when trying to create some volume data using scripts that change data in their select list using functions. Here is a small example to illustrate that:
SET NOCOUNT ON
CREATE TABLE #TEST_TBL (COL1 INT IDENTITY, COL2 UNIQUEIDENTIFIER)
INSERT INTO #TEST_TBL (COL2) VALUES (NEWID())
SELECT * FROM #TEST_TBL
And the output:
Beginning execution loop
Batch execution completed 10 times.
Sorry, the comment form is closed at this time.