Show TOC

PRINT Statement [T-SQL]Locate this document in the navigation structure

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

Syntax
PRINT <format-string> [, <arg-list>]
Examples

(back to top)

  • 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 uses placeholders in the PRINT statement; execute these statements inside a procedure:
    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, use the LOGIN_PROCEDURE option or the sp_iqmodifylogin system stored procedure.

Usage

(back to top)

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.

Standards

(back to top)

  • SQL—Transact-SQL extension to ISO/ANSI SQL grammar.
  • SAP Database products—Supported by SAP ASE.
Permissions

(back to top)

Must be connected to the database.