PRINT Statement

Returns a user-defined message to the client's message handler.

Syntax

PRINT {'any ASCII text' | @local_variable | @@global_variable}

Remarks

The message can have as many as 255 characters.

The local variable must be of type char or varchar and must be declared within the batch or procedure in which it is used.

The global variable must be of type char or varchar or automatically convertible to those types, such as @@VERSION. Currently, @@VERSION is the only character-type global variable.

You can generate a "formatted" message with more than one parameter by using the concatenation operator.

To print a user-defined error message and have the error number stored in @@ERROR, use RAISERROR instead of PRINT.

Examples

A.    Conditionally Executed Print (IF EXISTS)
IF EXISTS (SELECT zip 
                FROM authors
                    WHERE zip = '94705')
PRINT 'Berkeley author'
B.    Build and Display a String

This example defines a local variable (@returnmessage) and then builds the string (combining functions and strings) for display.

DECLARE @returnmessage        varchar(255)
SELECT @returnmessage = 'This record was updated on ' + 
                RTRIM(CONVERT(char(30), GETDATE())) + '.'
PRINT @returnmessage

See Also

DECLARE Variables
RAISERROR