      $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).

       01 empnum                   pic x(6).
       01 di-begin-loc             pic s9(9) comp-5.
       01 di-end-loc               pic s9(9) comp-5.
       01 lobind                   pic s9(4) comp-5.
       01 ix1                      pic s9(4) comp-5.
       01 ix2                      pic s9(4) comp-5.
       01 last-length              pic s9(4) comp-5.

       *> LOB variables require a special type of COBOL variable
       *> that must include the clause SQL TYPE

       01 resume            USAGE IS SQL TYPE IS CLOB-LOCATOR.
       01 di-buffer         USAGE IS SQL TYPE IS CLOB-LOCATOR.
       01 buffer            USAGE IS SQL TYPE IS CLOB(2K).


       procedure division.

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


           display "Start of LOBDEMO 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

      *>
      *>  declare cursor to fetch resume text info from LOB
           EXEC SQL DECLARE csr1 CURSOR FOR
                    SELECT empno, resume
                     FROM  emp_resume
                    WHERE  resume_format = 'ascii'
                     AND   empno <> 'A00130'
           END-EXEC.


      *>  Open the cursor

           EXEC SQL OPEN csr1 END-EXEC.

           Move 0              to  buffer-length
           move 0              to  last-length
           move 1              to  ix1


      *>   Fetch data from LOB using a LOB LOCATOR

           perform until SQLCODE not = 0
              EXEC SQL FETCH csr1
                 INTO  :empnum, :resume :lobind
              END-EXEC
              if SQLCODE = 0
                 perform display-info
              end-if
           end-perform

           EXEC SQL
              FREE LOCATOR :resume, :di-buffer
           END-EXEC

           EXEC SQL CLOSE csr1 END-EXEC

           exec sql connect reset   end-exec

           display "End of LOBDEMO example "

           stop run.

       error-proc section.

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

       *>
       *> Display resume info

       Display-info Section.

       *>  check to see if the host variable indicator returns NULL
           if lobind < 0
              display "NULL LOB indicated"
              exit section
           end-if

       *> Otherwise evaluate the LOB locator
       *> Locate the beginning of "Department Information" section.

           EXEC SQL
             VALUES (POSSTR(:resume, 'Department Information'))
              INTO  :di-begin-loc
           END-EXEC

       *> Locate the beginning of "Education" section (end of Dept.Info)
           EXEC SQL
             VALUES (POSSTR(:resume, 'Education'))
              INTO  :di-end-loc
           END-EXEC

           subtract di-begin-loc from di-end-loc

       *> Obtain ONLY the "Department Information" section by using SUBSTR
           EXEC SQL
             VALUES (SUBSTR(:resume, :di-begin-loc, :di-end-loc))
              INTO  :di-buffer
           END-EXEC

       *>  Append the "Department Information" section to the :buffer var

           EXEC SQL
             VALUES (:buffer || :di-buffer)
               INTO :buffer
           END-EXEC

      * display contents of the buffer.
           compute ix2 = buffer-length - last-length
           display buffer-data(ix1:ix2)

           *> calculate new starting position for display
           compute ix1 = buffer-length + 1
           move buffer-length  to        last-length

           exit.

