PRINT

Transact-SQL Reference

Transact-SQL Reference

PRINT

Returns a user-defined message to the client.

Syntax

PRINT 'any ASCII text' | @local_variable | @@FUNCTION | string_expr

Arguments

'any ASCII text'

Is a string of text.

@local_variable

Is a variable of any valid character data type. @local_variable must be char or varchar, or be able to be implicitly converted to those data types.

@@FUNCTION

Is a function that returns string results. @@FUNCTION must be char or varchar, or be able to be implicitly converted to those data types.

string_expr

Is an expression that returns a string. Can include concatenated literal values and variables. The message string can be up to 8,000 characters long; any characters after 8,000 are truncated.

Remarks

To print a user-defined error message having an error number that can be returned by @@ERROR, use RAISERROR instead of PRINT.

Examples
A. Conditionally executed print (IF EXISTS)

This example uses the PRINT statement to conditionally return a message.

IF EXISTS (SELECT zip FROM authors WHERE zip = '94705')
   PRINT 'Berkeley author'
B. Build and display a string

This example converts the results of the GETDATE function to a varchar data type and concatenates it with literal text to be returned by PRINT.

PRINT 'This message was printed on ' + 
   RTRIM(CONVERT(varchar(30), GETDATE())) + '.'

See Also

Data Types

DECLARE @local_variable

Functions

RAISERROR