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

The following procedure displays a message on the server message window:

CREATE PROCEDURE print_test
AS
PRINT 'Procedure called successfully' 

The statement

EXECUTE print_test

returns the string Procedure called successfully to the client.

Example 2

The following statement 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

The next example 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 is displayed 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