Wednesday, May 02, 2007

Batching with SQL Server 2005

ok, this is a really cool feature of SQL 2005 that I used the other day to quickly populate some test data....

SQL 2005 has a new "Batching" feature of the syntax that let's you repeat a statement any number of times you'd like by simply adding an integer to the end of the statement.

you can insert 100 test records into a table by issuing this command.

--Issue this command first.
CREATE TABLE dbo.TestTable (ID INT IDENTITY (1,1), RowGUID uniqueidentifier, InsertTime datetime)

--Now issue this command to insert the 100 records.
INSERT INTO dbo.TestTable (RowGUID, InsertTime) VALUES (NEWID(), GETDATE())
GO 100

isn't that cool?

If you execute both together it will try to do the CREATE TABLE 100 times and it will fail but you can see the power of running multiple lines together though...

No comments: