Click here to Skip to main content
15,868,016 members
Articles / Database Development / SQL Server / SQL Server 2008
Tip/Trick

A Unique Feature of SQL SERVER Loop with Go Statement

Rate me:
Please Sign up or sign in to vote.
4.57/5 (6 votes)
10 Nov 2013CPOL 9.3K   3  
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:

SQL
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)


Written By
Team Leader
India India
I am Rajat Jaiswal from India. I am working as a Technology specialist in one of the reputed company in India with 12+ years of experience. I am a Microsoft Data Platform MVP & Alibaba Cloud MVP.
I have always had an affinity to learn something new in technology. Database, Web development, Cloud computing are the areas of my interests always.
I have been a regular contributor to technologies with more than 300+ tips. Learning & Sharing is one of my aims always. Writing blogs, delivering sessions, helping on forums is my hobby.

Comments and Discussions

 
-- There are no messages in this forum --