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://blog.sqlauthority.com)

,
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

11 Comments. Leave new

  • Kirti Sardana
    July 8, 2008 12:20 pm

    i m running the same script but its showing below error msg……

    Server: Msg 170, Level 15, State 1, Line 3
    Line 3: Incorrect syntax near ‘go’.

    Reply
  • Hii pinal,
    Really a good script
    Thank you
    Regards,
    Suvsaish

    Reply
  • please reply me …
    my question is….

    if i have a table with a field date_time..
    and i want to run a query when i found date_time matching with GETDATE() and continuously checking the date_time field.

    so this is how i have to write a loop or what ever you suggest …. to continuously running a loop ( endless loop)

    Reply
  • Hello,

    I have currently installed My SQL Server 2005, every program get installed either on the Desktop or in the Start menu but when I checked in the Start it does not show anything, but i am very sure I have successfully completed the SQL Server 2005 setup. I am not getting any interface to write my Query. Or do I need to write my query in CMD, if it is where should i give the right path to execute that, I am very much confused.

    My question is that How and Where should I execute my SQL Query? I know the current topic is different but please help me.

    I posted my question here as I found genuine answers are given here.

    Thanks

    Reply
  • Here is Fun with GO

    Madhivanan

    Reply
  • good help

    Reply
  • hii i have a marksheet table and i want the user to insert record in datagrid in vb and i want to execute the insert statement based on the row count …how can i loop the insert query ?

    Reply
  • Really good i know nice

    Reply
  • Hi sir,
    I want to execute function multiple times without using loops and cursors . can this solution could be an alternative?.
    Thank you in advance.. :)

    Reply

Leave a Reply

Menu