Can we use 'GO' multiple times in SQL Transaction?

前端 未结 3 2332
旧时难觅i
旧时难觅i 2021-02-18 21:15

Can We use GO statement mutiple times in a SQL Transaction. I am having a long T-SQL script and I want to run it in a SQL Transaction. If

相关标签:
3条回答
  • 2021-02-18 21:28

    Note that GO is not a SQL keyword. It is a client-side batch separator used by SQL Server Management Studio and other client tools.

    GO has no effect on transaction scope. BEGIN TRAN will start a transaction on the current connection. COMMIT and ROLLBACK will end the transaction. You can execute as many statements as you want in-between. GO will execute the statements separately.

    As specified by MSDN:

    A TRY…CATCH construct cannot span multiple batches.

    So BEGIN TRY, END TRY, BEGIN CATCH, and END CATCH cannot be separated into separate batches by a GO separator. They must appear in the same query.

    If you do try to include a batch separator in a TRY/CATCH statement like the invalid SQL below:

    begin try
        go
    end try
    begin catch
        go
    end catch
    

    This will execute 3 different queries that return syntax errors:

    1) begin try

    Msg 102, Level 15, State 1, Line 1
    Incorrect syntax near 'begin'.
    

    2) end try begin catch

    Msg 102, Level 15, State 1, Line 3
    Incorrect syntax near 'try'.
    

    3) end catch

    Msg 102, Level 15, State 1, Line 6
    Incorrect syntax near 'catch'.
    
    0 讨论(0)
  • 2021-02-18 21:29

    You are mixing concepts. GO is not a Transact-SQL concept, not part of the language, and not understood by SQL Server. GO is the tools batch delimiter. sqlcmd.exe and SSMS both are using, by default, GO as the batch delimiter. The batch delimiter is used to identify the individual batches inside the SQL source file. The client tool sends to the server one batch at a time (of course, omitting the delimiter).

    Transactions can span batches. TRY/CATCH blocks cannot. CREATE/ALTER statements must be the only statement in a batch (comments are not statements, and statements contained in a function procedure body are,well, contained).

    Something similar to what you want to do can be achieved by starting a transaction and abortign the execution on first error (-b at sqlcmd.exe start, or use :on error exit in SSMS).

    But doing DDL inside long transactions is not going to work. Specially if you plan to mix it with DML. Most corruptions I had to investigate come from this combination (Xact, DDL + DML, rollback). I strongly recommend against it.

    The sole way to deploy schema updates safely is to take a backup, deploy, restore from backup if something goes wrong.

    Note that what Dan recommends (dynamic SQL) works because sp_executesql starts a new, inner, batch. This batch will satisfy the CREATE/ALTER restrictions.

    0 讨论(0)
  • 2021-02-18 21:43

    GO is a nice keyword to use. The GO will complete the last code block and continue on to the next block. Yes you can use multiple GOs in a statement to break it up into multiple batches. But it would be better to use try/catch logic with a combination of GOs since you are doing transaction based logic. https://msdn.microsoft.com/en-us/library/ms175976.aspx this site gives you some examples on hoe to use it and if you run into a hitch you can output that error and continue on if you choose.

    0 讨论(0)
提交回复
热议问题