Click here to Skip to main content
15,881,248 members
Articles / Database Development / SQL Server
Tip/Trick

GO Statement can also be used to Execute Batch of T-SQL Statement Multiple Times

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
25 May 2013CPOL 29.3K   1   2
GO Statement can also be used to execute batch of T-SQL statement multiple times

Introduction

Till recently, I was under the impression that GO statement’s sole purpose is to convey to the SQL Server the end of a batch of T-SQL Statements. But recently, while searching for some SQL feature details, I landed on to the MSDN page for the GO statement. And to my surprise, I observed that GO statement also has an integer optional parameter, this parameter value signals SQL Server to execute the batch of T-SQL Statement prior to the GO statement to be executed for the specified number of times.

Let us understand this with the following simple example:

PRINT 'Hello'
GO 5

Result of executing the above statement is as follows:

Beginning execution loop
Hello
Hello
Hello
Hello
Hello
Batch execution completed 5 times

This statement is very handy for testing purposes and dev tasks, such as for testing purpose if we need to insert millions of records together, we can achieve this with simple two statements as explained in the above example.

Please correct me if my understanding is wrong. Comments are always welcome, hope this tip helped you. Visit my blog SqlHints.com for such SQL tips/tricks and articles.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Technical Lead http://SqlHints.com
India India
http://SqlHints.com/about/

Comments and Discussions

 
GeneralReason for my vote of 5 FINALLY, I see a purpose for the GO.... Pin
ii_noname_ii24-Oct-11 20:26
ii_noname_ii24-Oct-11 20:26 
GeneralMSDN link http://msdn.microsoft.com/en-us/library/ms188037.a... Pin
Reiss24-Oct-11 0:54
professionalReiss24-Oct-11 0:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.