      $set db2(db=sample msgarea=sql-err-msg)
      ****************************************************************
      *  Copyright (C) 1998-2002 Micro Focus International Ltd.
      *  All Rights Reserved
      *  This demonstration program is provided for use by users of
      *  Micro Focus products 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.
      *
      *---------------------------------------------------------------
       working-storage section.

       exec sql include sqlca end-exec.

       01 sql-err-msg              pic x(512).

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


       *>
       *> In this example, the SMALLINT host variables use COMP-5
       *> variable
       *>

       01  dclstaff.
           10 staff-id                         pic s9(04)  comp-5.
           10 staff-name.
              49 staff-name-len                pic s9(04)  comp-5.
              49 staff-name-dat                pic x(9).
           10 staff-dept                       pic s9(04)  comp-5.
           10 staff-job                        pic x(5).
           10 staff-years                      pic s9(04)  comp-5.
           10 staff-salary                     pic s9(05)v9(02) comp-3.
           10 staff-comm                       pic s9(05)v9(02) comp-3.
      ******************************************************************
      * COBOL INDICATOR VARIABLES FOR TABLE                            *
      ******************************************************************
       *>
       *> In this example, the INDICATOR variables use COMP-5 variable
       *>
       01  dclstaff-null.
           10 staff-name-null                  pic s9(04)  comp-5.
           10 staff-dept-null                  pic s9(04)  comp-5.
           10 staff-job-null                   pic s9(04)  comp-5.
           10 staff-years-null                 pic s9(04)  comp-5.
           10 staff-salary-null                pic s9(04)  comp-5.
           10 staff-comm-null                  pic s9(04)  comp-5.

       procedure division.

           exec sql
                whenever SQLERROR  GO TO   error-proc
           end-exec

           exec sql
                whenever NOT FOUND perform no-record-proc
           end-exec


           display "Start of WHENEVER example "

      *>   Connect to an DB2 Sample database


           display "Enter database alias (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

      *>   Connect to the specified database
           if ws-usr not = spaces
              EXEC SQL
                   CONNECT TO :ws-db USER :ws-usr USING :ws-pass
              END-EXEC
           else
              EXEC SQL
                   CONNECT TO :ws-db
              END-EXEC
           end-if


       *> The following sql statement will get a no-find
       *> SQLCODE = +100 causing the no-record-proc to be executed
       *>

           exec sql
              select
                  a.name
                  ,a.salary
               into
                  :staff-name:staff-name-null
              ,   :staff-salary:staff-salary-null
             from staff a
              where ( a.id = 999       )
           end-exec


       *> The following  DISCONNECT statement will fail with a
       *> SQLCODE = -428 causing the program to branch to the
       *> error-proc routine
       *>

           exec sql disconnect all  end-exec

           stop run.

       error-proc.

           display "SQL Error"
           display "SQLCODE = " sqlcode
           display "SQLERRMC = " sqlerrmc
           display "SQL-ERR-AREA = "
           display sql-err-msg
           stop run
           .

       no-record-proc.

           display "No row found "
           .
