How to execute a SQL batch multiple times with GO

30 September,2019 by Jack Vamvas

Question: I'd like to execute a batch sql statement through SQL Server management studio multiple times and avoid using a loop statement or other similar constructs . I'm doing some testing requiring multiple returns of recordsets and would like a simple approach - for ad hoc  queries 

Answer: Looking at the SQL Server documentation for the sql server utility GO - indicates the syntax is GO [count]. 

This means the queries triggered as part of the batch executed will execute the amount of times listed after GO.    By default it's 1 , but if you specify a positive integer - the statement will execute that amount of times

Note: you can't use this feature as part of a stored procedure, typically you use it through SSMS or sqlcmd.  

An example of the GO [count])  is the following - which executes the the select * from sys.databases 100 times

 

select * from sys.databases
GO 100

Read more on SQL Server and batch processes 

How to delete millions of rows in batches 

Powershell and Batch files


Author: Jack Vamvas (http://www.sqlserver-dba.com)


Share:

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment on How to execute a SQL batch multiple times with GO


sqlserver-dba.com | SQL Server Performance Tuning | SQL Server DBA:Everything | FAQ | Contact|Copyright & Disclaimer