PRINT (Transact-SQL)
SQL Server 2008 Books Online (October 2009)
PRINT (Transact-SQL)

Returns a user-defined message to the client.

Topic link icon Transact-SQL Syntax Conventions

Syntax

PRINT msg_str | @local_variable | string_expr
Arguments

msg_str

Is a character string or Unicode string constant. For more information, see Constants (Transact-SQL).

@ local_variable

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

string_expr

Is an expression that returns a string. Can include concatenated literal values, functions, and variables. For more information, see Expressions (Transact-SQL).

Remarks

A message string can be up to 8,000 characters long if it is a non-Unicode string, and 4,000 characters long if it is a Unicode string. Longer strings are truncated. The varchar(max) and nvarchar(max) data types are truncated to data types that are no larger than varchar(8000) and nvarchar(4000).

For information about how applications process the messages returned by the PRINT statement, see Handling Errors and Messages in Applications.

RAISERROR can also be used to return messages. RAISERROR has these advantages over PRINT:

  • RAISERROR supports substituting arguments into an error message string using a mechanism modeled on the printf function of the C language standard library.
  • RAISERROR can specify a unique error number, a severity, and a state code in addition to the text message.
  • RAISERROR can be used to return user-defined messages created using the sp_addmessage system stored procedure.
Examples

A. Conditionally executing print (IF EXISTS)

The following example uses the PRINT statement to conditionally return a message.

IF @@OPTIONS & 512 <> 0
    PRINT N'This user has SET NOCOUNT turned ON.';
ELSE
    PRINT N'This user has SET NOCOUNT turned OFF.';
GO

B. Building and displaying a string

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

-- Build the message text by concatenating
-- strings and expressions.
PRINT N'This message was printed on '
    + RTRIM(CAST(GETDATE() AS nvarchar(30)))
    + N'.';
GO
-- This example shows building the message text
-- in a variable and then passing it to PRINT.
-- This was required in SQL Server 7.0 or earlier.
DECLARE @PrintMessage nvarchar(50);
SET @PrintMessage = N'This message was printed on '
    + RTRIM(CAST(GETDATE() AS nvarchar(30)))
    + N'.';
PRINT @PrintMessage;
GO
See Also

Reference

Data Types (Transact-SQL)
DECLARE @local_variable (Transact-SQL)
RAISERROR (Transact-SQL)

Other Resources

Using PRINT
Using RAISERROR

Help and Information

Getting SQL Server 2008 Assistance
Community Content

Of course, it's not that easy...
Added by:Michael Nady
Suppose you want to print a variable:

DECLARE @testVar char ( 16 )
print N'"' + @testVar + '"'

You'll get blank lines because @testVar is NULL. (To be complete: this depends on SET CONCAT_NULL_YIELDS_NULL .)

A possible solution:

print N'"' + isnull(@testVar, '') + '"'

and here's another solution (replace 'empty' with whatever you like)

print

Coalesce(@testVar,'empty')
How to print strings larger than varchar(8000)
Added by:AidanHall

PRINT truncates long strings down to 8000 chars (4000 for unicode)
So how do you PRINT a long string?

A possible solution:

DECLARE @myText varchar(max) = 'This is the begining of a long string...';
/* ... */
SELECT @myText AS [string] FOR XML PATH('');
© 2009 Microsoft Corporation. All rights reserved.   Terms of Use | Trademarks | Privacy Statement
Page view tracker
Rate the Lightweight library
x
Lightweight builds on ScriptFree (loband) by adding features you've requested: a SearchBox and default code language selection.
Do you like the SearchBox?
Do you like the tabbed code blocks?
How useful is this topic?
Tell us more.
Thanks
x
You're helping to improve MSDN Online.
Feedback
Switch View
Classic
Lightweight Beta
ScriptFree
Switch View