Thursday, 25 August 2011

T-SQL: Use GO to run the same batch more than once

A simple post this one, but something I only became aware of in the past few months. One of our devs hadn't seen it either so i thought i'd pop it down on this blog.

Consider a scenario where you are generating test data, perhaps with GETDATE() and you want to quickly generate lots of rows. By simply adding a number after the GO batch separator you can get you batch to execute multiple times. No need to write any crazy loops or any joins.

So, something like the following will generate 55 rows:

CREATE TABLE t (id IDENTITY(1,1), dt DATETIME)
GO;
INSERT INTO t SELECT GETDATE()
GO 55;


Simple, but effective.

No comments:

Post a Comment

/* add this crazy stuff in so i can use syntax highlighter