PRINT statement [T-SQL]

Description

Displays a message on the message window of the database server.

Syntax

PRINT format-string [, arg-list]

Examples

Example 1

Displays a message on the server message window:

CREATE PROCEDURE print_test
AS
PRINT 'Procedure called successfully' 

This statement returns the string “Procedure called successfully” to the client:

EXECUTE print_test

Example 2

Illustrates the use of placeholders in the PRINT statement:

DECLARE @var1 INT, @var2 INT
SELECT @var1 = 3, @var2 = 5
PRINT 'Variable 1 = %1!, Variable 2 = %2!', @var1, @var2

Example 3

Uses RAISERROR to disallow connections:

CREATE procedure DBA.login_check() 
begin
  // Allow a maximum of 3 concurrent connections
  IF( db_property('ConnCount') > 3 ) then
  raiserror 28000
    'User %1! is not allowed to connect -- there are
      already %2! users logged on',
      current user,  
      cast(db_property('ConnCount') as int)-1;
    ELSE 
  call sp_login_environment; 
    end if;
end
go
grant execute on DBA.login_check to PUBLIC
go
set option PUBLIC.Login_procedure='DBA.login_check' 
go

For an alternate way to disallow connections, see “LOGIN_PROCEDURE option” or “sp_iqmodifylogin procedure”.

Usage

The PRINT statement returns a message to the client window if you are connected from an Open Client application or JDBC application. If you are connected from an Embedded SQL or ODBC application, the message displays on the database server window.

The format string can contain placeholders for the arguments in the optional argument list. These placeholders are of the form %nn!, where nn is an integer between 1 and 20.


Side effects

None.

Standards

Permissions

Must be connected to the database.

See also

MESSAGE statement