Learn more about SQL Server tools

   
   















































Executing a TSQL batch multiple times using GO

MSSQLTips author Greg Robidoux By:   |   Read Comments (7)   |   Related Tips: More > Scripts

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.  Whatever the need the easiest way to do this is to setup a while loop and execute your code, but in SQL 2005 there is an even easier way to do this.

Solution
In both SQL Server 2000 and SQL Server 2005 the keyword GO tells SQL Server to execute the preceding code as one batch.  In SQL Server 2005 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:

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 command 1000 times:

CREATE TABLE dbo.TEST (ID INT IDENTITY (1,1), ROWID uniqueidentifier)
GO
INSERT INTO dbo.TEST (ROWIDVALUES (NEWID()) 
GO 1000

Here is another example that executes both INSERT statements 1000 times.   As you can see you can add more and more statements to the batch to be run the set number of times that is specified after the GO.

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 (ROWIDVALUES (NEWID()) 
INSERT INTO dbo.TEST2 (ROWIDVALUES (NEWID()) 
GO 1000

To do something similar to this in SQL Server 2000 you would need to write code such as the following. It is not that big a deal, but writing GO 1000 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 
WHILE @counter 1000 
  
BEGIN
       INSERT INTO 
dbo.TEST (ROWIDVALUES (NEWID())
       
INSERT INTO dbo.TEST2 (ROWIDVALUES (NEWID())
  
SET @counter @counter 1
  
END

Next Steps



Last Update: 4/5/2007


About the author
MSSQLTips author Greg Robidoux
Greg Robidoux is the President of Edgewood Solutions and a co-founder of MSSQLTips.com.

View all my tips


print tip Print  
Become a paid author





join MSSQLTips for free SQL Server tips     



Learn more about SQL Server tools
Post a comment or let the author know this tip helped you.

       All comments are reviewed, so stay on subject or we may delete your comment.

*Name   *Email Notify for updates



       Note: your email address is not published. Required fields are marked with an asterisk (*)


Get free SQL tips:

*Enter Code refresh code     



Tuesday, May 28, 2013 - 5:04:18 AM - wattyjnr Read The Tip

Excellent! Thanks very much Greg.


Sunday, May 26, 2013 - 6:33:28 PM - Greg Robidoux Read The Tip

@wattyjnr - I don't think that is possible. 

You could create a loop as follows:

DECLARE @NoTimesToExecute INT = 2
DECLARE @counter int = 1

WHILE @counter <= @NoTimesToExecute
BEGIN
 SELECT TOP 1 * FROM sys.sysdatabases
 SET @counter = @counter + 1
END


Friday, May 24, 2013 - 10:26:21 AM - wattyjnr Read The Tip

Hi Greg,

Thanks for the nice tip. Is there anyway that I can use a parameter to derive the number of times to execute the GO statement, or do I always have to use a hard coded value?

 

i.e.

DECLARE @NoTimesToExecute INT = 2

INSERT INTO <schema>.<table> DEFAULT VALUES

GO @NoTimesToExecute


Friday, March 23, 2012 - 9:46:07 AM - nishant Read The Tip

Greg Thank you so much for this bit

i did not know this earlier and liked it .

Keep posting..


Thursday, April 21, 2011 - 12:43:02 PM - Greg Robidoux Read The Tip

Hi Kevin,

Not totally sure what you need, but you can either pass in the variable which means you would need to modify your SPs if you are passing it in as another parameter or do what you are mentioning with a global ## temporary table.

Greg


Thursday, April 21, 2011 - 12:26:30 PM - Kevin Read The Tip

Ok.  I need to stop using IE9.

 

Is there a way to keep the current iteration of the batch?  Using a variable like @GoCount won't work, because it goes out of scope with each time.  I can write it to a temp table and increment, but that seems a little heavy-weight.  Is there a method that is more straight-forward?


Saturday, August 21, 2010 - 8:18:39 AM - JTM Read The Tip
Hallo.can someone please help me with sql batches , i really dont know how to use them even though they seem to be the answer to the problem i have.here is my problem: im trying to write a student management system and the problem i have is to link student to their classes.the relationship beetween student and class is a many to many and i broke it with a StudentClass Table.so the problem im encountering now is to choose one student and assign him/her to their classes.and im using a checkedlist box control to poplulate classes and when i try to assign as many classes to a student,im getting a Violation of Foriegn Key constraint error and each time i use the GO keyword in my VB application, it is not recognise,so i don't really know how to approach the problem,please help.Thank you




 
Sponsor Information