none
What is "GO" used for in T-SQL?

    Question

  • What exactly is "GO" used for in T-SQL? Is it commonly used in standard SQL too or is it "Transact-SQL-specific"?

    Does it have, by any chance, the same function as "exec"?
    As I understand it, "exec" makes the statements independent from each other, it keeps one from messing with the other, makes possible the execution of a series of them, right?
    Monday, May 12, 2008 10:04 AM

Answers

  • GO is a batch separator

    It doesn't have to be GO you can change it to anything you want

    from Query analyzer go to tools-->options-->connection and you can change it there

     

    the reason you sometimes need a batch separator in a script is because certain operations require to be the first statement in a batch

     

     

    Denis The SQL Menace

    http://sqlservercode.blogspot.com

    http://sqlblog.com/blogs/denis_gobo/default.aspx

    Monday, May 12, 2008 1:50 PM
    Moderator
  • When working with SYBASE T-SQL on UNIX terminal, nothing happened until you typed in GO on a new line,  then whatever you typed in the previous lines, it got executed. The output limit was 200 lines in the interactive mode (client programs could process millions on the other hand).

    You still can try it today by entering SQLCMD / OSQL in the interactive mode. It is like a time machine which takes you back to the beginning years of RDBMS.


    Kalman Toth SQL SERVER 2012 & BI TRAINING
    New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012


    Wednesday, July 11, 2012 3:35 PM
    Moderator
  • GO states that a block of t-sql statements ends and executes that block.

     

    This means in practical, if you have defined variable @sql in a block which ends with GO you can again define a parameter named @sql in the following GO block.

     

    Otherwise you can not define two parameters with same name in the same block of sql codes.

     

    Also, if you create a SP the GO will define the boundaries of the SP code block.

     

    Actually you can think the GO as the delimeter of logical code blocks in t-sql

     

     

    Eralper

    SQL Server Articles

     

    Monday, May 12, 2008 10:23 AM
    Moderator
  • Also, from the historic perspective, "go" is a relic from the 80's as was part of Sybase.  Therefore, it is legacy code that dates back to when "Transact SQL" was Sybase only.

    "go" is an artifact from Sybase.

    Thursday, July 5, 2012 4:49 PM
    Moderator

All replies

  • GO states that a block of t-sql statements ends and executes that block.

     

    This means in practical, if you have defined variable @sql in a block which ends with GO you can again define a parameter named @sql in the following GO block.

     

    Otherwise you can not define two parameters with same name in the same block of sql codes.

     

    Also, if you create a SP the GO will define the boundaries of the SP code block.

     

    Actually you can think the GO as the delimeter of logical code blocks in t-sql

     

     

    Eralper

    SQL Server Articles

     

    Monday, May 12, 2008 10:23 AM
    Moderator
  • Add some points:

    "GO" is not sql language, it  ends a sql block.

    Don't mistake GO block with transaction. "GO" itself won't start or end a tansaction.

    Some sql scripts need start at the beginning of a block. Such stored procedure and trigger creation.
    So you need put "GO" at the beginning of it if exist some query before them.

    Thanks,
    Zuomin

    Monday, May 12, 2008 12:32 PM
  • GO is a batch separator

    It doesn't have to be GO you can change it to anything you want

    from Query analyzer go to tools-->options-->connection and you can change it there

     

    the reason you sometimes need a batch separator in a script is because certain operations require to be the first statement in a batch

     

     

    Denis The SQL Menace

    http://sqlservercode.blogspot.com

    http://sqlblog.com/blogs/denis_gobo/default.aspx

    Monday, May 12, 2008 1:50 PM
    Moderator
  • If we forget about this "variable" part. In some case, Is it the same if I do this:
    <statement 1>
    go
    <statement 2>
    go

    and this:
    exec <statement 1>;
    exec <statement 2>;


    Monday, May 12, 2008 3:10 PM
  • Nope, not with batch terminating errors

     

    run this

    Code Snippet
    exec('select convert(int,''b'')')
    exec ('select getdate()')

     

     

     

     

    and now run this

    Code Snippet

    exec('select convert(int,''b'')')
    go
    exec ('select getdate()')
    go

     

     

     

     

    In the first block the getdate() never gets displayed because the batch gets aborted because of the severity of the error

     

    Denis The SQL Menace

    http://sqlservercode.blogspot.com

    http://sqlblog.com/blogs/denis_gobo/default.aspx

    Monday, May 12, 2008 3:25 PM
    Moderator
  • Are these two equivalentn for the purposes of flow?

    <statement>
    GO
    <statement>
    GO
    BEGIN
       <statement>
    END
    BEGIN
       <statement>
    END
    It might be worth noting that in some SQL Server tools like SSIS there are some tasks where statements must be separated by GO.  Otherwise it tries to interpret the multiple SQL statements as one big one and syntactically it pukes.
    Thursday, July 5, 2012 2:37 PM
  • No, these two are not necessarily the equivalent. In many cases you may not even being able to execute all statements in one batch as is and may need to resort to dynamic SQL.

    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    Thursday, July 5, 2012 4:45 PM
    Moderator
  • Also, from the historic perspective, "go" is a relic from the 80's as was part of Sybase.  Therefore, it is legacy code that dates back to when "Transact SQL" was Sybase only.

    "go" is an artifact from Sybase.

    Thursday, July 5, 2012 4:49 PM
    Moderator
  • When working with SYBASE T-SQL on UNIX terminal, nothing happened until you typed in GO on a new line,  then whatever you typed in the previous lines, it got executed. The output limit was 200 lines in the interactive mode (client programs could process millions on the other hand).

    You still can try it today by entering SQLCMD / OSQL in the interactive mode. It is like a time machine which takes you back to the beginning years of RDBMS.


    Kalman Toth SQL SERVER 2012 & BI TRAINING
    New Book: Beginner Database Design & SQL Programming Using Microsoft SQL Server 2012


    Wednesday, July 11, 2012 3:35 PM
    Moderator