• 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/