Click here to Skip to main content
Click here to Skip to main content

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

By , 25 May 2013
Rate this:
Please Sign up or sign in to vote.

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)

About the Author

Basavaraj P Biradar
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.... Pinmemberii_noname_ii24-Oct-11 20:26 
GeneralMSDN link http://msdn.microsoft.com/en-us/library/ms188037.a... PinmemberReiss24-Oct-11 0:54 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140421.2 | Last Updated 26 May 2013
Article Copyright 2011 by Basavaraj P Biradar
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid