Skip to content

Latest commit

 

History

History
115 lines (81 loc) · 4.22 KB

print-transact-sql.md

File metadata and controls

115 lines (81 loc) · 4.22 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs monikerRange
PRINT (Transact-SQL)
PRINT (Transact-SQL)
rwestMSFT
randolphwest
05/16/2024
sql
t-sql
reference
PRINT_TSQL
PRINT
PRINT statement
user-defined messages [SQL Server]
messages [SQL Server], PRINT statement
displaying user-defined messages
viewing user-defined messages
conditionally returning messages [SQL Server]
TSQL
>=aps-pdw-2016 || =azuresqldb-current || =azure-sqldw-latest || >=sql-server-2016 || >=sql-server-linux-2017 || =azuresqldb-mi-current || =fabric

PRINT (Transact-SQL)

[!INCLUDE sql-asdb-asdbmi-asa-pdw-fabricse-fabricdw]

Returns a user-defined message to the client. For example, in [!INCLUDE ssmanstudiofull-md], PRINT outputs to the Messages tab of the query results window.

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: Transact-SQL syntax conventions

Syntax

PRINT msg_str | @local_variable | string_expr

Arguments

msg_str

A character string or Unicode string constant. For more information, see Constants.

@local_variable

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

An expression that returns a string. Can include concatenated literal values, functions, and variables. For more information, see Expressions.

Remarks

A message string can be up to 8,000 characters long if it's a non-Unicode string, and 4,000 characters long if it's 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).

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 (Transact-SQL) 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. Build and display a string

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

PRINT N'This message was printed on ' + RTRIM(CAST(GETDATE() AS NVARCHAR(30))) + N'.';
GO

The following example shows building the message text in a variable.

DECLARE @PrintMessage NVARCHAR(50);
SET @PrintMessage = N'This message was printed on ' + RTRIM(CAST(GETDATE() AS NVARCHAR(30))) + N'.';
PRINT @PrintMessage;
GO

Examples: [!INCLUDE ssazuresynapse-md] and [!INCLUDE ssPDW]

C. Conditionally execute print

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

IF DB_ID() = 1
    PRINT N'The current database is ''master''.';
ELSE
    PRINT N'The current database is not ''master''.';
GO

Related content