Quick and dirty way to run a batch over and over

Everyone has their favorite way or running a block of code over and over again.  If you have to run an entire batch over and over again there is nothing as quick and easy as using the “GO n” syntax.  When you put a number after the GO batch separator SQL Server Management Studio (as well as SQLCMD) will run that entire batch however many times that you have specified.

Recently I needed to create some dummy data in a table, so instead of writing a loop that counted the number of times the loop ran, I simply used code like this.

[sql]CREATE TABLE SampleData
(id int identity(1,1),
SomeData varchar(100))
insert into SampleData
(‘This is my sample data’)
go 100000[/sql]

When run, the batch with the insert statement ran 100,000 times generating all the data that I needed with just 7 extra keystrokes.



One Response

  1. Hi Denny.
    Thanks for a great post.

    I just want to add my experience. GO n or using WHILE for inserting data is much slower then using set based solution:
    INSERT INTO SampleData(SomeData)
    SELECT TOP 100000 ‘This is my sample data’
    [master].[dbo].[spt_values] t1
    [master].[dbo].[spt_values] t2

    Have a nice day

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Trust DCAC with your data

Your data systems may be treading water today, but are they prepared for the next phase of your business growth?