raiserror

Prints a user-defined error message on the user’s screen and sets a system flag to record that an error condition has occurred.

Syntax

raiserror error_number 
	[{format_string | @local_variable}] [, arg_list]
	[with errordata restricted_select_list]

Parameters

Examples

Usage

  • User-defined messages can be generated ad hoc, as in Example 1 and Example 3, or they can be added to the system table sysusermessages for use by any application, as shown in Example 2. Use sp_addmessage to add messages to sysusermessages; use sp_getmessage to retrieve messages for use by print and raiserror.

  • Error numbers for user-defined error messages must be greater than 20,000. The maximum value is 2,147,483,647 (231 -1).

  • The severity level of all user-defined error messages is 16. This level indicates that the user has made a nonfatal error.

  • The maximum output string length of format_string plus all arguments after substitution is 1024 bytes.

  • If you use placeholders in a format string, keep this in mind: for each placeholder n in the string, the placeholders 1 through n-1 must exist in the same string, although they do not have to be in numerical order. For example, you cannot have placeholders 1 and 3 in a format string without having placeholder 2 in the same string. If you omit a number in a format string, an error message is generated when raiserror is executed.

  • If there are too few arguments relative to the number of placeholders in format_string, the SAP ASE server displays an error message and aborts the currently executing statement, but does not abort any open transactions. However, if this error occurs within a stored procedure, the SAP ASE server continues with the next statement at the line that called raiserror, and any open transactions remain open. If the error occurs in a batch of SQL code, the SAP ASE server aborts the batch, and any open transactions remain open.

  • To include a literal percent sign as part of the error message, use two percent signs (‘‘%%’’) in the format_string. If you include a single percent sign (‘‘%’’) in the format_string that is not used as a placeholder, the SAP ASE server returns an error message.

  • If an argument evaluates to NULL, it is converted into a zero-length char string. If you do not want zero-length strings in the output, use the isnull function.

  • When raiserror is executed, the error number is placed in the global variable @@error, which stores the error number that was most recently generated by the system.

  • Use raiserror instead of print if you want an error number stored in @@error.

  • To include an arg_list with raiserror, put a comma after error_number or format_string before the first argument. To include extended error data, separate the first extended_value from error_number, format_string, or arg_list using a space (not a comma).

See also sp_addmessage, sp_getmessage in Reference Manual: Procedures.

Standards

ANSI SQL – Compliance level: Transact-SQL extension.

Permissions

No permission is required to use raiserror.

Related reference
declare
print