Returns a user-defined message to the client.
PRINT msg_str | @local_variable | string_expr
Is a character string or Unicode string constant. For more information, see Constants (Transact-SQL).
Is a variable of any valid character data type. *@local\_variable* must be char or varchar, or it must be able to be implicitly converted to those data types.
Is an expression that returns a string. Can include concatenated literal values, functions, and variables. The message string can be up to 8,000 characters long; any characters after 8,000 are truncated. For more information, see Expressions (Transact-SQL).
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.
A. Conditionally executed print (IF EXISTS)
This 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. 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.
-- Build the message text by concatenating -- strings and expressions using functionality -- available in SQL Server 2000 and SQL Server 2005. 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