Why make it difficult

Some times it is not necessary to write long blogs to write about nice features in the SQL Server. This time I was watching a video for the Master programme on the SQL Server, and then I learned a little trick that I will share with you.

you all know the little command

go

What happens if you write

go xxx

where xxx is a number?

Well, it simply runs the batch xxx times.

That can be very useful if you want to create some testdata and insert rows many times to a table. Lets try it, and first we create the database and a table-

— CODE SNIPPET START —

use master
go

if DATABASEPROPERTYEX(‘testGO’,‘Version’)>0
drop databasetestGO
GO

create database testGO
go

use testGO
go

Create table testTable ( C1 int identity, C2 varchar(20) default ‘testdata’)
go

— CODE SNIPPET END —

Now we have a database called testGO and a table called testTable, and now I want to insert 10000 rows of dummy data. How do I do that?

— CODE SNIPPET START —

insert into testTable default values

go 10000

— CODE SNIPPET END —

and we can test it

— CODE SNIPPET START —

select COUNT(*)
from testTable

— CODE SNIPPET END —

GO xxx is really nice.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s