SQL Server Documentation
Sends a message from a Transact-SQL batch, stored procedure, or trigger to the client application.
PRINT { msg | '@variable' | 'string_constant' } [ + { msg | '@variable' | 'string_constant' } ]...
msg
Is an expression that returns one of the character data types: char
, varchar
, nchar
, nvarchar
, text
, ntext
, or a Unicode character string.
'string_constant'
Is a character string literal. String literals must be enclosed in single quotation marks (').
'@variable'
Is a variable that is declared with one of the character data types. Variables must be preceded by an at sign (@).
The PRINT
statement is useful for debugging and for providing feedback to the user running the batch. The output of the PRINT
statement is displayed in the client application's message window.
If multiple PRINT
statements are used in a batch, the messages are concatenated and displayed as a single message in the client application's message window. If you need to send multiple messages separately, use multiple batches.
The maximum length of the message that can be sent by PRINT
is 8000 bytes (8 KB) for non-Unicode strings and 4000 bytes (4 KB) for Unicode strings. If the string exceeds this length, it will be truncated.
PRINT 'This is a simple message.'
DECLARE @message VARCHAR(50)
SET @message = 'The variable value is updated.'
PRINT @message
DECLARE @firstName VARCHAR(50) = 'John'
DECLARE @lastName VARCHAR(50) = 'Doe'
PRINT 'Customer Name: ' + @firstName + ' ' + @lastName
CREATE PROCEDURE DisplayMessage
AS
BEGIN
PRINT 'Executing stored procedure DisplayMessage...'
-- Other T-SQL statements
PRINT 'Stored procedure execution complete.'
END
GO
-- To execute the stored procedure and see the output:
-- EXEC DisplayMessage