Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / database / SQL-Server / SQL-Server-2008

A Unique Feature of SQL SERVER Loop with Go Statement

4.57/5 (6 votes)
10 Nov 2013CPOL 9.6K  
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)