Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

FORMATMESSAGE (Transact-SQL)

Constructs a message from an existing message in sys.messages. The functionality of FORMATMESSAGE resembles that of the RAISERROR statement. However, RAISERROR prints the message immediately, while FORMATMESSAGE returns the formatted message for further processing.

Topic link icon Transact-SQL Syntax Conventions


FORMATMESSAGE ( msg_number , [ param_value [ ,...n ] ] )
msg_number

Is the ID of the message stored in sys.messages. If msg_number is <= 13000, or if the message does not exist in sys.messages, NULL is returned.

param_value

Is a parameter value for use in the message. Can be more than one parameter value. The values must be specified in the order in which the placeholder variables appear in the message. The maximum number of values is 20.

nvarchar

Like the RAISERROR statement, FORMATMESSAGE edits the message by substituting the supplied parameter values for placeholder variables in the message. For more information about the placeholders allowed in error messages and the editing process, see RAISERROR (Transact-SQL).

NoteNote

FORMATMESSAGE works only with messages created using sp_addmessage.

FORMATMESSAGE looks up the message in the current language of the user. If there is no localized version of the message, the U.S. English version is used.

For localized messages, the supplied parameter values must correspond to the parameter placeholders in the U.S. English version. That is, parameter 1 in the localized version must correspond to parameter 1 in the U.S. English version, parameter 2 must correspond to parameter 2, and so on.

This example uses a hypothetical message 50001 stored in sys.messages as, "The number of rows in %s is %1d." FORMATMESSAGE substitutes the values Table1 and 5 for the parameter placeholders. The resulting string, "The number of rows in Table1 is 5," is stored in the local variable @var1.

DECLARE @var1 VARCHAR(100)
SELECT @var1 = FORMATMESSAGE(50001, 'Table1', 5)
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.