Monday, August 31, 2009

MCTS 70-433 RAISERROR usage

This example gives a good overview of basic usage:


Without seeing your code, I can't tell what is wrong. But it sounds like

you should have an error handler like:


  BEGIN CATCH

     IF error_number() <> 2627

     BEGIN

        SELECT @msg = error_message(),

               @sev = error_severity(),

               @state = error_state()

        RAISERROR (@msg, @sev, @state)

     END

  END CATCH


Then again, I would sugest that it is better to add an IF NOT EXISTS

to avoid the PK violation altogether.


http://www.sqlmonster.com/Uwe/Forum.aspx/sql-server-programming/74698/Sql-server-2005-2008-try-catch-raiserror



For the technical details from msdn:


RAISERROR (Transact-SQL)


Generates an error message and initiates error processing for the session. RAISERROR can either reference a user-defined message stored in the sys.messages catalog view or build a message dynamically. The message is returned as a server error message to the calling application or to an associated CATCH block of a TRY…CATCH construct.

 Transact-SQL Syntax Conventions

  Syntax


RAISERROR ( { msg_id | msg_str | @local_variable }

    { ,severity ,state }

    [ ,argument [ ,...n ] ] )

    [ WITH option [ ,...n ] ]



RAISERROR (N'This is message %s %d.', -- Message text.

           10, -- Severity,

           1, -- State,

           N'number', -- First argument.

           5); -- Second argument.

-- The message text returned is: This is message number 5.

GO








No comments: