      $set db2(db=sample)
      ************************************************************************
      * Copyright (C) 1998-1999 Micro Focus International Ltd.
      * All rights reserved.
      *  
      * This demonstration program is provided for use by users of 
      * Micro Focus products (the "Software") and may be used, 
      * modified and distributed as part of your application provided 
      * that you properly acknowledge the copyright of Micro Focus in this
      * material.
      *     
      * All conditions, warranties and undertakings, express or implied, 
      * statutory or otherwise, on the part of Micro Focus are excluded, 
      * including without limitation those of merchantability or fitness  
      * for purpose. This shall not, however, purport to exclude or 
      * restrict the liability of Micro Focus to any extent not 
      * permitted by law.
      *
      * IN NO EVENT WILL MICRO FOCUS BE LIABLE FOR ANY INDIRECT, 
      * INCIDENTAL, SPECIAL, CONSEQUENTIAL OR OTHER DAMAGES ARISING OUT 
      * OF THE USE OF THE DEMONSTRATION PROGRAM DISTRIBUTED WITH THE 
      * SOFTWARE, WHETHER OR NOT INFORMED OF THE POSSIBILITIES OF 
      * DAMAGES IN ADVANCE.  THESE LIMITATIONS APPLY TO ALL CAUSES OF 
      * ACTION, INCLUDING BREACH OF CONTRACT, BREACH OF WARRANTY, STRICT
      * LIABILITY, AND ANY ACTIONS IN TORTS.
      ************************************************************************
       working-storage section.

      *  Include the SQL Communications Area. This includes the
      *  definitions of SQLCODE, etc

           EXEC SQL INCLUDE SQLCA END-EXEC

       *> DB2 UDB limits database alias names to 8 characters
       *> other DB2 servers such DB2/MVS can have database names up to
       *> 18 characters which you can still access via DB2 CONNECT
       *> by creating an ALIAS name for the database with a max of 8
       *> characters

       01 ws-db        pic x(08).

       *> DB2 UDB has a limit of 8 characters for a logon id.
       *> Passwords may be up 18 characters but if you may run into case
       *> and length problems going to other DB2 servers if you use more
       *> than 8 characters or if you use lower case characters in a
       *> password

       01 ws-usr       pic x(08).
       01 ws-pass      pic x(18).

       01 ws-var-pass.
          49 ws-var-pass-len   pic s9(04) comp-5.
          49 ws-var-pass-dat   pic x(18).


       procedure division.

       *>  Connect to an IBM DB2 Sample database
           display "Connect statement tests"
           display " "
           display "Enter database alias to connect to (Eg Sample) "
		   with no advancing
           accept ws-db
           display "Enter username  "
		    with no advancing
           accept ws-usr
           display "Enter password  "
                    with no advancing
           accept ws-pass


       *>  test 1 - basic type 1 CONNECT without id or password
           display "Test 1:"

	   EXEC SQL
               CONNECT TO :ws-db
	   END-EXEC
	   if sqlcode not = 0
		display "Error: cannot connect "
                display sqlcode
                display sqlerrmc
                stop run
           else
              display "Test 1: OK"
              EXEC SQL
                   DISCONNECT CURRENT
              END-EXEC
              if sqlcode not = 0
                  display "Error: cannot disconnect "
                  display sqlcode
                  display sqlerrmc
                  stop run
              end-if
          end-if

        *>test2
        *>type 1 connect with id / password passed as char fields
           display "Test 2:"
           EXEC SQL
               CONNECT TO :ws-db USER :ws-usr  USING :ws-pass
           END-EXEC
           if sqlcode not = 0
                display "Error: cannot connect Test 2 "
                display sqlcode
                display sqlerrmc
                stop run
           end-if
           display "Test 2: OK"

           EXEC SQL
               DISCONNECT CURRENT
           END-EXEC
           if sqlcode not = 0
                display "Error: cannot disconnect "
                display sqlcode
                display sqlerrmc
                stop run
           end-if

        *>test3
        *>type 1 connect with id / password passed as varchar field
           display "Test 3:"
           move 0                                   to  ws-var-pass-len
           move ws-pass                             to  ws-var-pass-dat
           inspect ws-pass tallying ws-var-pass-len for characters
             before initial " "

           EXEC SQL
               CONNECT TO :ws-db USER :ws-usr  USING :ws-var-pass
           END-EXEC
           if sqlcode not = 0
                display "Error: cannot connect Test 3 "
                display sqlcode
                display sqlerrmc
                stop run
           end-if
           display "Test 3: OK"

           EXEC SQL
               CONNECT RESET
           END-EXEC
           if sqlcode not = 0
                display "Error: cannot disconnect "
                display sqlcode
                display sqlerrmc
                stop run
           end-if
	   stop run.
