Usage of RAISERROR in SQL Server
the role of RAISERROR: RAISERROR is used to throw an error . [The following information is derived from SQL Server 2005 Help]
The syntax is as follows:
RAISERROR ({msg_id | msg_str | @local_variable}
{, severity, state}
[, argument [,... N]]
)
[with O Ption [,... N]]
Briefly explain:
First parameter: {msg_id | msg_str | @local_variable}
MSG_ID: The expression can be a message code defined in a sys.messages table;
A user-defined error message number stored in the Sys.messages catalog view using sp_addmessage.
The error number of the user-defined error message should be greater than 50000.
MSG_STR: Indicates that it can also be a user-defined message, which can be up to 2047 characters long;
(if it is a constant, please use n ' xxxx ', because it is nvarchar)
When MSG_STR is specified, RAISERROR throws an error message with error number 5000.
@local_variable: Indicates that it can also be a formatted string variable in MSG_STR mode.
Second parameter: severity
The user-defined severity level associated with the message. (This is very important)
Any user can specify a severity level between 0 and 18.
[0,10] within the closed range, does not jump to catch;
If it is [11,19], skip to catch;
If [20, Infinity), the database connection is terminated directly;
Third parameter: state
If the same user-defined error is raised in multiple locations,
A unique status number is used for each location to help find the code snippet that raised the error.
Any integer between 1 and 127. (The state defaults to 1)
An error is generated when the state value is 0 or greater than 127!
Fourth parameter: argument
A parameter that is used instead of MSG_STR or a variable that corresponds to a definition in a msg_id message.
Fifth parameter: Option
Wrong customization options, which can be any of the values in the following table:
LOG: Errors are logged in the error log and the application log;
NoWait: Send the message to the client immediately;
SetError: SET @ @ERROR value and error_number value to msg_id or 50000;
[SQL] code example
--Example 1
DECLARE @raiseErrorCode nvarchar
SET @raiseErrorCode = CONVERT (nvarchar), YOUR uniqueidentifier KEY)
RAISERROR ('%s INVALID ID. There is no. in table ', 16,1, @raiseErrorCode)
--Example 2
RAISERROR (
n ' this are message%s%d. ')--message text,
Ten,-- Severity,
1, --state,
N ' number ', --the argument.
5 --Second argument.
);
--The message text returned Is:this was message number 5.
Go
--Example 3
RAISERROR (N ' <<%*.*s>> ',--message text.
-- Severity, 1,--state , 7,--the argument-used for width.
3, --Second argument used for precision.
N ' ABCDE '); --third argument supplies the string.
--The message text returned is: << abc>>.
Go
--Example 4
RAISERROR (N ' <<%7.3s>> ',--message text.
-- Severity, 1,--state ,
N ' ABCDE '); --The argument supplies the string.
--The message text returned is: << abc>>.
Go
--Example 5
--a. Returns an error message from a CATCH block
The following code example shows how to use RAISERROR in a TRY block to make execution jump into an associated CATCH block.
It also shows how to use RAISERROR to return information about an error that calls a CATCH block.
BEGIN try
RAISERROR (' Error raised in TRY blocks. ',--message text.
--Severity.
1-state.
);
End TRY
BEGIN CATCH
DECLARE @ErrorMessage NVARCHAR (4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = Error_message (),
@ErrorSeverity = Error_severity (),
@ErrorState = Error_state ( );
RAISERROR (@ErrorMessage,--message text.
@ErrorSeverity,--Severity.
@ErrorState -State.
);
End CATCH;
--Example 6
--b. Create ad hoc messages in sys.messages
The following example shows how to raise a message stored in the Sys.messages catalog view.
This message is added to the Sys.messages catalog view with message number 50005 through the sp_addmessage system stored procedure.
sp_addmessage @msgnum = 50005,
@severity = ten,
@msgtext = N ' <<%7.3s>> ';
Go
RAISERROR (50005,--the message ID.
-- Severity, 1,--state ,
N ' ABCDE ');------------------------argument
--The message text returned is: << abc>>.
Go
sp_dropmessage @msgnum = 50005;
Go
--Example 7
--c. Use local variables to provide message text
The following code example shows how to use local variables to provide message text for a RAISERROR statement.
sp_addmessage @msgnum = 50005,
@severity = ten,
@msgtext = N ' <<%7.3s>> ';
Go
RAISERROR (50005,--the message ID.
-- Severity, 1,--state ,
N ' ABCDE ');------------------------argument
--The message text returned is: << abc>>.
Go
sp_dropmessage @msgnum = 50005;
Go
Thank you for reading, I hope to help you, thank you for your support for this site!