This weeks tip:
I just found this one out the other day – GO has a repeat option! You can execute a batch multiple times by adding a count after go.
Here’s an example:
Select Getdate()
GO 5
Here are the results….

This may not seem like much, but for creating sets of test data or random data, this is very useful.
Here are some related posts from the intraweb:
- From MSSQLTips.com – a couple of good examples here: Executing a T-SQL batch multiple times using GO
- I found this tip in Belle’s SQL Musings here: Executing Batches Multiple Times (SQL Server 2005 only) – note it does work for 2008, 2008 R2 and Denali.
Note when I was reading up on this, I remember Rob Farley (Blog/Twitter) mentioning a trick about this in his talk – The Incredible Shrinking Execution Plan at PASS 2010.
His trick, which I’ve tried, is to change the batch separator in a co-workers SSMS to “Select”, as shown below – it doesn’t take effect until the next connection and it will drive them crazy:

Note for this tip, they will still get the same error:

Kudos Rob.


