use go inside a transaction

  • This is just for my curiosity. Why I can use GO after the use statement?

    Got syntax error if you execute the following.

    BEGIN TRY

    BEGIN TRAN T1

    Use AdventureWorks

    GO

    COMMIT TRAN T1

    END TRY

    BEGIN CATCH

    SELECT

    ERROR_NUMBER() AS ErrorNumber,

    ERROR_SEVERITY() AS ErrorSeverity,

    ERROR_STATE() AS ErrorState,

    ERROR_PROCEDURE() AS ErrorProcedure,

    ERROR_LINE() AS ErrorLine,

    ERROR_MESSAGE() AS ErrorMessage;

    ROLLBACK TRAN T1

    END CATCH;

  • Nothing to do with the transaction. You can't use GO within the TRY.

    GO is not a T-SQL command. It's a batch terminator. It marks where to break the code sent to SQL. With a GO there, you're telling SSMS to send the following as a batch to SQL

    BEGIN TRY

    BEGIN TRAN T1

    Use AdventureWorks

    I'm pretty sure you can agree that the block of code above is not valid.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Make sense and thank you again!

  • Just for the information to Know;

    Go : Signals the end of a batch of Transact-SQL statements to the SQL Server utilities.

    From the below you may get the idea:

    declare @i int

    set @i=1

    select @i

    GO

    select @i /*will fail since the batch has been complited*/

    Thanks
    Parthi

Viewing 4 posts - 1 through 3 (of 3 total)

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