Microsoft Docs

SQL Server Documentation

PRINT (Transact-SQL)

Sends a message from a Transact-SQL batch, stored procedure, or trigger to the client application.

Syntax


PRINT { msg | '@variable' | 'string_constant' } [ + { msg | '@variable' | 'string_constant' } ]...
            

Parameters

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 (@).

Remarks

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.

Note

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.

Examples

Example 1: Printing a simple string


PRINT 'This is a simple message.'
            

Example 2: Printing a variable


DECLARE @message VARCHAR(50)
SET @message = 'The variable value is updated.'
PRINT @message
            

Example 3: Concatenating strings


DECLARE @firstName VARCHAR(50) = 'John'
DECLARE @lastName VARCHAR(50) = 'Doe'
PRINT 'Customer Name: ' + @firstName + ' ' + @lastName
            

Example 4: Using PRINT within a stored procedure


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
            

See Also