What is the syntax meaning of RAISERROR() What is the syntax meaning of RAISERROR() database database

What is the syntax meaning of RAISERROR()


It is the severity level of the error. The levels are from 11 - 20 which throw an error in SQL. The higher the level, the more severe the level and the transaction should be aborted.

You will get the syntax error when you do:

RAISERROR('Cannot Insert where salary > 1000').

Because you have not specified the correct parameters (severity level or state).

If you wish to issue a warning and not an exception, use levels 0 - 10.

From MSDN:

severity

Is the user-defined severity level associated with this message. Whenusing msg_id to raise a user-defined message created usingsp_addmessage, the severity specified on RAISERROR overrides theseverity specified in sp_addmessage. Severity levels from 0 through 18can be specified by any user. Severity levels from 19 through 25 canonly be specified by members of the sysadmin fixed server role orusers with ALTER TRACE permissions. For severity levels from 19through 25, the WITH LOG option is required.

state

Is an integer from 0 through 255. Negative values or valueslarger than 255 generate an error. If the same user-defined error israised at multiple locations, using a unique state number for eachlocation can help find which section of code is raising the errors.For detailed description here


16 is severity and 1 is state, more specifically following example might give you more detail on syntax and usage:

BEGIN TRY    -- RAISERROR with severity 11-19 will cause execution to     -- jump to the CATCH block.    RAISERROR ('Error raised in TRY block.', -- Message text.               16, -- Severity.               1 -- State.               );END TRYBEGIN CATCH    DECLARE @ErrorMessage NVARCHAR(4000);    DECLARE @ErrorSeverity INT;    DECLARE @ErrorState INT;    SELECT         @ErrorMessage = ERROR_MESSAGE(),        @ErrorSeverity = ERROR_SEVERITY(),        @ErrorState = ERROR_STATE();    -- Use RAISERROR inside the CATCH block to return error    -- information about the original error that caused    -- execution to jump to the CATCH block.    RAISERROR (@ErrorMessage, -- Message text.               @ErrorSeverity, -- Severity.               @ErrorState -- State.               );END CATCH;

You can follow and try out more examples from http://msdn.microsoft.com/en-us/library/ms178592.aspx


according to MSDN

RAISERROR ( { msg_id | msg_str | @local_variable }    { ,severity ,state }    [ ,argument [ ,...n ] ] )    [ WITH option [ ,...n ] ]

16 would be the severity.
1 would be the state.

The error you get is because you have not properly supplied the required parameters for the RAISEERROR function.