The print keyword, used in the previous example, displays a user-defined message or the contents of a local variable on the user’s screen.
The local variable must be declared within the same batch or procedure in which it is used. The message can be up to 255 bytes long.
The syntax is:
print {format_string | @local_variable | @@global_variable} [,arg_list]
For example:
if exists (select postalcode from authors where postalcode = "94705") print "Berkeley author"
Here is how to use print to display the contents of a local variable:
declare @msg char(50) select @msg = "What’s up, doc?" print @msg
print recognizes placeholders in the character string to be printed out. 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!. The components are a percent sign, followed by an integer from 1 to 20, followed by an exclamation point. The integer represents placeholder position in the string in the original language. “%1!” is the first argument in the original version, “%2!” is the second argument, and so on. Indicating the position of the argument makes translations correctly, even when the order in which the arguments appear in the target language is different from their order in the source language.
%1! is not allowed in %2!.
%1! ist in %2! nicht zulässig.
The Japanese version of the message is:
The characters “%1!” are in different places in the phrase. In this example, “%1!” in English, German, and Japanese represents the same argument, and “%2!” also represents a single argument in all three languages.
You cannot skip placeholder numbers when using placeholders in a format string, although you do not need to use placeholders in numerical order. For example, you cannot have placeholders 1 and 3 in a format string without having placeholder 2 in the same string.
The optional arg_list can be a series of either variables or constants. An argument can be any datatype except text or image; it is converted to the char datatype before it is included in the final message. If no argument list is provided, the format string must be the message to be printed, without any placeholders.
The maximum output string length of format_string plus all arguments after substitution is 512 bytes.