Question about Transact SQL syntax

Posted by Yousui on Stack Overflow See other posts from Stack Overflow or by Yousui
Published on 2010-06-01T02:29:48Z Indexed on 2010/06/01 2:33 UTC
Read the original article Hit count: 192

Filed under:
|

Hi guys,

The following code works like a charm:

BEGIN TRY
    BEGIN TRANSACTION

    COMMIT TRANSACTION
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK;

    DECLARE @ErrorMessage NVARCHAR(4000),
            @ErrorSeverity int;
    SELECT @ErrorMessage = ERROR_MESSAGE(),
           @ErrorSeverity = ERROR_SEVERITY();    
    RAISERROR(@ErrorMessage, @ErrorSeverity, 1);
END CATCH

But this code gives an error:

BEGIN TRY
    BEGIN TRANSACTION

    COMMIT TRANSACTION
END TRY
BEGIN CATCH
    IF @@TRANCOUNT > 0
        ROLLBACK;

    RAISERROR(ERROR_MESSAGE(), ERROR_SEVERITY(), 1);
END CATCH

Why?

© Stack Overflow or respective owner

Related posts about tsql

Related posts about syntax