tags:

views:

23

answers:

2

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?

+3  A: 

RAISERROR() can not take calls as its parameters. Needs to be constants or variables.

Mitch Wheat
A terribly annoying "feature" in my opinion.
JC
A: 

+1 The RAISERROR statement generates an error message by either retrieving the message from the sys.messages catalog view or constructing the message string at runtime. So agreeing with the fellow @Mitch Wheat I will go with his recommendation.

VoodooChild