A unique feature of SQL SERVER Loop with Go statement :)

Hello friends,
I hope you are aware of this SQL Server feature already but just sharing it for those who don’t know.
Suppose in some situation you want to repeat a particular SQL Statement block number of times than in that situation go statement help you.
Suppose I have a debug table with column name id. And I need to insert 1000 ids then I can use following statements.
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 more 1000 rows with different ids.
So it is a very nice feature you can enjoy looping with this .

Thanks
Rajat Jaiswal 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s