Technical used of GO statement

  • Hi all,

    I am relatively new to SQL Server. I have seen that at the end of various statement we give GO. So my question is the query would get executed if we give GO on the other side it would also get executed if we did not provide GO. So whats the technical used of GO. Is it used for maintaining some Standard

  • GO is a batch sperator.

    So you could say

    Do Something

    Do Something Else

    Do Something More

    GO

    Do Something Even More

    Do Something Better Than Before

    GO

    And it would execute the first three things in one batch, and the last two things in another batch.

    Without the GO's it would execute all 5 things in one batch

  • anthony.green (12/4/2012)


    GO is a batch sperator.

    So you could say

    Do Something

    Do Something Else

    Do Something More

    GO

    Do Something Even More

    Do Something Better Than Before

    GO

    And it would execute the first three things in one batch, and the last two things in another batch.

    Without the GO's it would execute all 5 things in one batch

    Thanks antony for the reply but since i am relatively new to SQL Server, i was wondering in what scenario would we like the above mention thing to happen. I mean to say there is no conditional break and my objective is to excute all the 5 statement, so to me executing them one after another in sequence matter, why should i used GO.

  • Shadab Shah (12/4/2012)


    anthony.green (12/4/2012)


    GO is a batch sperator.

    So you could say

    Do Something

    Do Something Else

    Do Something More

    GO

    Do Something Even More

    Do Something Better Than Before

    GO

    And it would execute the first three things in one batch, and the last two things in another batch.

    Without the GO's it would execute all 5 things in one batch

    Thanks antony for the reply but since i am relatively new to SQL Server, i was wondering in what scenario would we like the above mention thing to happen. I mean to say there is no conditional break and my objective is to excute all the 5 statement, so to me executing them one after another in sequence matter, why should i used GO.

    GO is ONLY used in SSMS. It is NOT a t-sql statement. It is a way to control what batches run. Think of a batch as something you would execute from another program. Consider the following code.

    declare @MyInt int = 42

    select @MyInt

    Select @MyInt / 2

    GO

    select @MyInt

    This all looks perfectly fine. Try running it. The last select statement is after the GO batch separator. That means the variable @MyInt is not in the current batch. The above will return 2 rows and error on the last select.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

  • Sean Lange (12/4/2012)


    Shadab Shah (12/4/2012)


    anthony.green (12/4/2012)


    GO is a batch sperator.

    So you could say

    Do Something

    Do Something Else

    Do Something More

    GO

    Do Something Even More

    Do Something Better Than Before

    GO

    And it would execute the first three things in one batch, and the last two things in another batch.

    Without the GO's it would execute all 5 things in one batch

    Thanks antony for the reply but since i am relatively new to SQL Server, i was wondering in what scenario would we like the above mention thing to happen. I mean to say there is no conditional break and my objective is to excute all the 5 statement, so to me executing them one after another in sequence matter, why should i used GO.

    GO is ONLY used in SSMS. It is NOT a t-sql statement. It is a way to control what batches run. Think of a batch as something you would execute from another program. Consider the following code.

    declare @MyInt int = 42

    select @MyInt

    Select @MyInt / 2

    GO

    select @MyInt

    This all looks perfectly fine. Try running it. The last select statement is after the GO batch separator. That means the variable @MyInt is not in the current batch. The above will return 2 rows and error on the last select.

    Thanks sean that was indeed a very good example, cleared the concept clearly but has anybody used this in real application. I have never seen such GO written in between the lines of code in SP or in that matter in any other script

  • You must be very careful using the Batch Separator Sentence: GO

    If the previous batch failed, the result will be an error message, and the next batch will be executed. Example:

    USE TestDB

    GO

    DELETE FROM dbo.TableName_In_TestServer_AND_ProductionServer

    GO

    If you execute the previous query on Test Server Instance, it will be ok, but if you execute it on Production Server Instance it will be a serious pain to you: An error message will be shown because TestDB does not exists on Production Server on the first batch and the Delete Batch will be executed.

    A peculiarity is that GO cannot be on the same line of any query and throws an error message:

    DELETE FROM dbo.TableName_In_TestServer_AND_ProductionServer GO

    And you could not use a variable declared on a previous batch:

    DECLARE @Var VARCHAR(10)

    SET @Var = 'Hello'

    GO

    Print @Var

    GO

  • It would not work inside a stored proc. It would end the batch. 🙂

    For example if you have a script that create a stored proc and a view you HAVE to use a batch separator.

    Take a look at this.

    create proc MyProc as

    begin

    select 1

    end

    create view MyView as

    (

    select top 5 * from sys.columns

    )

    These are obviously greatly simplified but serves the point. This script won't run and you will get an "incorrect syntax" exception. If you mouse over the red squiggly line for the create view line it will tell that "CREATE VIEW must be the only statement in the batch".

    So to save us the hassle of making us open every single statement in a new window we have a batch separator. Here is the same proc and view but I added GO so it will run. I also added drop statements at the end.

    create proc MyProc as

    begin

    select 1

    end

    GO

    create view MyView as

    (

    select top 5 * from sys.columns

    )

    GO

    drop procedure MyProc

    drop view MyView

    This type of thing is often used for deployment scripts or other times you need to execute multiple batches at one time.

    _______________________________________________________________

    Need help? Help us help you.

    Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

    Need to split a string? Try Jeff Modens splitter http://www.sqlservercentral.com/articles/Tally+Table/72993/.

    Cross Tabs and Pivots, Part 1 – Converting Rows to Columns - http://www.sqlservercentral.com/articles/T-SQL/63681/
    Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs - http://www.sqlservercentral.com/articles/Crosstab/65048/
    Understanding and Using APPLY (Part 1) - http://www.sqlservercentral.com/articles/APPLY/69953/
    Understanding and Using APPLY (Part 2) - http://www.sqlservercentral.com/articles/APPLY/69954/

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply