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 (http://blog.SQLAuthority.com)

12 thoughts on “SQL SERVER – Execute Same Query and Statement Multiple Times Using Command GO

  1. 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’.

    Like

  2. 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)

    Like

  3. 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

    Like

  4. 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 ?

    Like

  5. Pingback: SQL SERVER – Weekly Series – Memory Lane – #011 « SQL Server Journey with SQL Authority

  6. 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.. :)

    Like

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