Using PRINT

The PRINT statement takes either one character or a Unicode string expression as a parameter. It returns the string as a message to the application. The message is returned as an informational error in ADO, OLE DB, and ODBC applications. SQLSTATE is set to 01000, the native error is set to 0, and the error message string is set to the character string specified in the PRINT statement. The string is returned to the message handler call-back function in DB-Library applications.

The PRINT statement accepts any character string expression, including character or Unicode constants, a character or Unicode local variable name, or a function that returns a character or Unicode string. With Microsoft® SQL Server™ version 7.0, PRINT also accepts complex strings built by concatenating two or more constants, local variables, or functions.

Use PRINT to help in troubleshooting Transact-SQL code, to check the values of data, or to produce reports.

This example uses PRINT inside an IF statement to return a message to the application:

IF (SELECT COUNT(au_lname) FROM authors WHERE state = 'UT') > 0

    PRINT 'More than one author resides in the state of Utah.'

  

This example prints a combination of a local variable, system functions, and a text string using concatenation:

USE Northwind

GO

DECLARE @MyObject NVARCHAR(128)

  

SET @MyObject = 'Products'

  

PRINT 'Object Name: ' + @MyObject

PRINT '   Object ID: ' + STR(Object_ID(@MyObject))

PRINT 'The computer ' + RTRIM(@@SERVERNAME) + ' is running '

    + RTRIM(@@VERSION)

GO

-- This shows building a character variable into a print

-- message. This is required for earlier versions of SQL

-- Server, in which the PRINT statement did not support

-- concatentation.

DECLARE @Msg VARCHAR(255)

SELECT @Msg = 'The computer ' + RTRIM(@@SERVERNAME) + ' is running '

    + RTRIM(@@VERSION)

PRINT @Msg

  

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.