Infolink

 

Search This Blog

Oct 15, 2012

RAISERROR statement in SQL server

RAISERROR statement is used to return error messages to the business applications that executes SQL statements. The usual errors returned by the SQL server may not make much sense to the business applications users hence we overwrite it by using RAISERROR to display meaningful messages. The statement uses same format as a system error or warning messages generated by SQL server.

You can return a user-defined error message by using RAISERROR.  The general syntax is as below

RAISERROR ( { msg_id | msg_str | @local_variable }

        { ,severity ,state }

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

        [ WITH option [ ,...n ] ]

Examples RAISERROR (‘Error raised in TRY block.’, — Message text.

16, — Severity.

               1 — State.

               );

RAISERROR (50005, — Message id.

           10, — Severity,

           1, — State,

           N’abcde’);


    BEGIN TRY

                    Declare @TotalHours int;

                    Select @TotalHours = datediff(hh, WorkStartTime,WorkEndTime)

                    From WorkTime

                    Where EMPID = 12343

                    If(@TotalHours <8)

                    RAISERROR(‘Error Raised’,16,1)

                    ELSE

                    Print ‘Work Done for the day’

    END  TRY

    BEGIN CATCH

                    Print ‘Number of hours is less than 8’

    END CATCH

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...