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

A Unique Feature of SQL SERVER Loop with Go Statement

, 10 Nov 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
A unique feature of SQL SERVER Loop with Go statement

Introduction

I hope you are aware of this SQL Server feature already, but I am just sharing it for those who don't know one more hidden feature. What is the hidden thing in "GO" Statement.

Background

Suppose in some situation, you want to repeat a particular SQL statement block number of times, then in such situation, the "GO" statement will help you.

Using the Code

Suppose I have a debug table with column name "Id" and I need to Insert 1000 ids, then I can use the following statement:

Go
 DECLARE @id AS INT
 SELECT @id = COUNT(1) FROM dbo.Debug WITH(NOLOCK)
 INSERT INTO dbo.Debug(id) VALUES(@id + 1)
 GO 1000

Now when you run this statement, you will get 1000 more rows with different ids. So it is a very nice feature you can enjoy looping with.

Thanks & best regards.

License

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

Share

About the Author

Rajat-Indiandotnet
Technical Lead
India India
No Biography provided
Follow on   Twitter   LinkedIn

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 10 Nov 2013
Article Copyright 2013 by Rajat-Indiandotnet
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid