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.