Prints a user-defined message on the user’s screen.
print {format_string | @local_variable | @@global_variable} [, arg_list]
Format strings can contain up to 20 unique placeholders in any order. These placeholders are replaced with the formatted contents of any arguments that follow format_string when the text of the message is sent to the client.
To allow reordering of the arguments when format strings are translated to a language with a different grammatical structure, placeholders are numbered. A placeholder for an argument appears in this format: “ %nn !”—a percent sign (%), followed by an integer from 1 to 20, followed by an exclamation point (!). The integer represents the argument number in the string in the argument list. “%1!” is the first argument in the original version, “%2!” is the second argument, and so on.
Indicating the position of the argument in this way makes it possible to translate correctly, even when the order in which the arguments appear in the target language is different.
%1! is not allowed in %2!.
%1! ist in %2! nicht zulässig.
The Japanese version of this message is:
In this example, “%1!” represents the same argument in all three languages, as does “%2!”. This example shows the reordering of the arguments that is sometimes necessary in the translated form.
if exists (select postalcode from authors where postalcode = '94705') print "Berkeley author"
declare @msg char (50) select @msg = "What's up, doc?" print @msg
What's up, doc?
declare @tabname varchar (30) select @tabname = "titles" declare @username varchar (30) select @username = "ezekiel" print "The table '%1!' is not owned by the user '%2!'.", @tabname, @username
The table 'titles' is not owned by the user 'ezekiel.'
The maximum output string length of format_string plus all arguments after substitution is 1023 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 also 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 print is executed.
The arg_list must include an argument for each placeholder in the format_string, or the transaction is aborted. You can use more arguments than placeholders.
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.
declare @arg varchar (30) select @arg = isnull (col1, "nothing") from table_a where ...
print "I think we have %1! here", @arg
You can add user-defined messages to the system table sysusermessages for use by any application. Use sp_addmessage to add messages to sysusermessages; use sp_getmessage to retrieve messages for use by print and raiserror.
Use raiserror instead of print to print a user-defined error message and have the error number stored in @@error.
See also sp_addmessage, sp_getmessage in Reference Manual: Procedures.
ANSI SQL – Compliance level: Transact-SQL extension.
No permission is required to use print.