Problem
Sometimes there is a need to execute the same command or set of commands over and over again. This may be to insert or update test data or it may be to put a load on your server for performance testing. One way to do this is to setup a while loop and execute the code over and over again, but there is another way this can be done with the SQL Server GO command.
Solution
The GO keyword tells SQL Server to execute the preceding code as one batch. In SQL Server, you have the ability to add a number after the GO command to tell SQL Server how many times to execute the batch. So, let’s take a look at a couple of examples of using SQL Server GO.
Use GO to execute a SQL batch X number of times
Let’s say you want to create a test table and load it with 1000 records. You could issue the following command and it will run the same INSERT command 1000 times:
CREATE TABLE dbo.TEST (ID INT IDENTITY (1,1), ROWID uniqueidentifier)
GO
INSERT INTO dbo.TEST (ROWID) VALUES (NEWID())
GO 1000Another example of using GO with a SQL Server batch
Here is another example that executes two INSERT statements 100 times. As you can see you can add more statements to the batch to be run (two inserts in this case), X number of times specified after GO command.
CREATE TABLE dbo.TEST (ID INT IDENTITY (1,1), ROWID uniqueidentifier)
CREATE TABLE dbo.TEST2 (ID INT IDENTITY (1,1), ROWID uniqueidentifier)
GO
INSERT INTO dbo.TEST (ROWID) VALUES (NEWID())
INSERT INTO dbo.TEST2 (ROWID) VALUES (NEWID())
GO 100Using a loop to run a batch over and over again
To do something similar with a loop, you would need to write code such as the following. It is not that big a deal, but writing GO 100 seems a bit easier to me.
CREATE TABLE dbo.TEST (ID INT IDENTITY (1,1), ROWID uniqueidentifier)
CREATE TABLE dbo.TEST2 (ID INT IDENTITY (1,1), ROWID uniqueidentifier)
GO
DECLARE @counter INT
SET @counter = 0
WHILE @counter < 100
BEGIN
INSERT INTO dbo.TEST (ROWID) VALUES (NEWID())
INSERT INTO dbo.TEST2 (ROWID) VALUES (NEWID())
SET @counter = @counter + 1
ENDNext Steps
- Add this new trick to your list of tricks to make running the same batch faster then having to write a while loop
- Check out these other SQL Server tips:

Greg Robidoux has been working with databases for 35+ years with extensive hands on SQL Server experience from version 6.5 to 2025. He has authored over 250 technical articles and delivered several presentations online and at various conventions. Greg is also the President and founder of Edgewood Solutions, a technology services company delivering services and solutions for Microsoft SQL Server.
And I thought it would be a call using GO (the language) :)
Thanks John.
Greg, The GO batch separator is a function of the Client (typically SSMS) and not of SQL Server itself.
Hi Tony,
Never tried it a job step before. It could be the GO command is just recognized in a query window.
I’ll do some testing to see what I find out.
Glad you found a work around. Another option is to write a while loop.
-Greg
An agent has a job that executes a SP to delete top(4000) rows older than <days parameter>.
I wanted to alter the job to execute the SP five times to draw down the history a bit faster.
I added “go 5” after the execution statement – but it still executed only once.
Curious!
I simply copy&pasted the execute statement four more times… it now deletes 20000 rows.
Hi Tony, can you elaborate on what you have setup and what you are trying to do.
Thanks
Greg
Interesting that when I use this method for multiple executions in an agent job step – it only executes the “batch” once.