Another use of GO command in SQL Server 2005

As you all know, GO command signals the end of the batch of T-SQL statements

However in SQL Server 2005, it is also used to execute set of commands for a specified number of times

Consider that you want to create a table that should have hundred random integer values. You can the methods like the ones specified in http://sqlblogcasts.com/blogs/madhivanan/archive/2007/10/10/generating-random-numbers-part-ii.aspx or a while loop. You can also use GO command by specifying how many times it should run

create table #numbers(random_value int)
GO -- Signals the end of the batch

insert into #numbers(random_value) Select cast(100000*rand() as int)
GO 100 -- Signals that the above statement should run for 100 times

select random_value from #numbers

Now the table would have 100 random interger values

Here is Fun With Go

Published 06 August 2008 09:43 by Madhivanan
Filed under: , ,

Comments

# Fun with GO

05 September 2008 11:21 by Madhivanan

In this blog post , I explained different uses of GO command Here is a Fun when you use GO as object

# Fun with GO

05 September 2008 12:02 by SQL Server Transact-SQL (SSQA.net)

In this blog post , I explained different uses of GO command Here is a Fun when you use GO as object