-3

I need to use RAISERROR to throw a message(pop up message) and commit that transaction.Is there any option?

For severity 18 the transaction got rollback.I have changed the severity to 10 and tried like

RAISERROR('Your Reference Number is %s',10,0,@param); 

this but it commits the transaction but doesnt show message. What i need here is Message has to be thrown and transaction has to be commited

Any suggestion?

bala3569
  • 10,832
  • 28
  • 102
  • 146

3 Answers3

5

Don't use exceptions to pass back "OK" messages. You wouldn't in a c# or java program. Exception means "I've aborted because SHTF"

You'd use thsi to return meaningful data

SELECT 'Your Reference Number is ' + @param

In a typical template (from my answer Nested stored procedures containing TRY CATCH ROLLBACK pattern?)

SET XACT_ABORT, NOCOUNT ON


BEGIN TRY

    BEGIN TRANSACTION

       [...Perform work, call nested procedures...]

    COMMIT TRANSACTION

    SELECT 'Your Reference Number is ' + @param

END TRY
BEGIN CATCH
    IF XACT_STATE() <> 0
        ROLLBACK TRANSACTION
    RAISERROR [rethrow caught error using @ErrorNumber, @ErrorMessage, etc]
END CATCH
Community
  • 1
  • 1
gbn
  • 422,506
  • 82
  • 585
  • 676
5

RAISERROR with severity above 10 will be treated like an exception by the ADO.Net client. Depending on how your call context looks like, this may or may not rollback the transaction. If you use a SqlTransaction or a TransactionScope in the client, or a BEGIN TRY/BEGIN CATCH block on the server side, this will likely roll back the transaction. Point is that RAISERROR neither commits nor rolls back the transaction, is your own code that rolls back or commits and we cannot know what you're doing from your post.

RAISERROR with severity bellow 10 will be considered a informational message and not cause an exception. See Database Engine Error Severities. This is probably why you say that the 'it doesn't show the message' (whatever that means). Client side frameworks treat the informational messages differently, for instance ADO.Net will raise an SqlConnection.InfoMessage event on the connection but will not raise an exception. You probably don't have anything set up in your application for this event and your code is simply ignoring the info messages. For example how to use the InfoMessage event see Connection Events (ADO.NET)

Remus Rusanu
  • 288,378
  • 40
  • 442
  • 569
  • **RAISERROR with severity above 10 will be treated like an exception by the ADO** saved me :) The code was using severity 1 and hence catch block was not called. – TheTechGuy Nov 08 '14 at 04:32
0

It sounds like you need to use the WITH NOWAIT parameter for RAISERROR - this will output it to the message window immediately:

RAISERROR('Your Reference Number is %s',10,0,@param) WITH NOWAIT

JNK
  • 63,321
  • 15
  • 122
  • 138