SQL SERVER – Execute Same Query and Statement Multiple Times Using Command GO

Following question was asking by one of long time reader who really liked trick of SQL SERVER – Explanation SQL Command GO and SQL SERVER – Insert Multiple Records Using One Insert Statement – Use of UNION ALL. She asked how can I execute same code multiple times without Copy and Paste multiple times in Query Editor. The answer to this question is very simple. Use the command GO. Following example demonstrate how GO can be used to execute same code multiple times.
SELECT GETDATE() AS CurrentTime
GO 5

Above code will return current time 5 times as GO is followed with five times. The result is displayed with “Result to Text” mode on.
Beginning execution loop
CurrentTime
———————–
2008-01-19 1:52:54.200

(1 row(s) affected)

CurrentTime
———————–
2008-01-19 11:52:54.217

(1 row(s) affected)

CurrentTime
———————–
2008-01-19 11:52:54.217

(1 row(s) affected)

CurrentTime
———————–
2008-01-19 11:52:54.217

(1 row(s) affected)

CurrentTime
———————–
2008-01-19 11:52:54.233

(1 row(s) affected)
Batch execution completed 5 times.

Reference : Pinal Dave (https://darkslategrey-bat-805937.hostingersite.com)

SQL DateTime, SQL Scripts
Previous Post
SQL SERVER – 2005 – Introduction and Explanation to SYNONYM – Helpful T-SQL Feature for Developer
Next Post
SQL SERVER – 2005 – Display Fragmentation Information of Data and Indexes of Database Table

Related Posts

Leave a Reply