************************************************************************* 
      ** 
      ** Source File Name = dbinst.cbl 
      ** 
      ** Licensed Materials - Property of IBM 
      ** 
      ** (C) COPYRIGHT International Business Machines Corp. 1995, 2000  
      ** All Rights Reserved. 
      ** 
      ** US Government Users Restricted Rights - Use, duplication or 
      ** disclosure restricted by GSA ADP Schedule Contract with 
      ** 
      **    PURPOSE : 
      **       an example showing how to use DATABASE INSTANCE API in order to: 
      **          - attach to an instance 
      **          - Retriece the instance 
      **          - Detach from the instance 
      ** 
      **    APIs USED : 
      **       ATTACH TO INSTANCE                       sqlgatin() 
      **       GET INSTANCE                             sqlggins() 
      **       DETACH FROM INSTANCE                     sqlgdtin() 
      ** 
      ** 
      ** For more information about these samples see the README file. 
      ** 
      ** For more information on Programming in COBOL, see the: 
      **    -  "Programming in COBOL" section of the Application Development Guide. 
      ** 
      ** For more information on Building COBOL Applications, see the: 
      **    - "Building COBOL Applications" section of the Application Building Guide. 
      ** 
      ** For more information on the SQL language see the SQL Reference. 
      ** 
      ************************************************************************* 

       Identification Division.
       Program-Id. "dbinst".

       Data Division.
       Working-Storage Section.

       copy "sqlenv.cbl".
       copy "sqlca.cbl".

      * Variables for attach to, detach from, get instance 

       01 inst.
          05 instance-len      pic s9(4) comp-5 value 0.
          05 instance          pic x(18).

       01 usr.
          05 usr-name-len     pic s9(4) comp-5 value 0.
          05 usr-name         pic x(18).

       01 pass.
          05 passwd-len        pic s9(4) comp-5 value 0.
          05 passwd            pic x(18).

      * Local Variables 
       77 rc                  pic s9(9) comp-5.
       77 errloc              pic x(80).

       Procedure Division.
       dbinst-pgm section.

           display "Sample COBOL Program : dbinst.cbl".

      * Initialize local variables 

           display "enter instance name : " with no advancing.
           accept instance.
           inspect instance tallying instance-len for characters
              before initial " ".

           display "enter user name : " with no advancing.
           accept usr-name.
           inspect usr-name tallying usr-name-len for characters
              before initial " ".

           move space to passwd.
           display "enter passwd name : " with no advancing.
           accept passwd.
           inspect passwd tallying passwd-len for characters
              before initial " ".
           display " ".

           display "ATTACH TO INSTANCE API called for instance : "
              , instance.

      ********************** 
      * ATTACH TO INSTANCE * 
      ********************** 
           call "sqlgatin" using
                                 by value     passwd-len
                                 by value     usr-name-len
                                 by value     instance-len
                                 by reference sqlca
                                 by reference passwd
                                 by reference usr-name
                                 by reference instance
                           returning rc.

           move "attach to instance" to errloc.
           call "checkerr" using SQLCA errloc.

           display "GET INSTANCE API called".

      **************** 
      * GET INSTANCE * 
      **************** 
           call "sqlggins" using
                                 by reference sqlca
                                 by reference instance
                           returning rc.

           move "get instance name" to errloc.
           call "checkerr" using SQLCA errloc.

           display "current instance = " , instance.

           display "DETACHed FROM INSTANCE API called ", instance.

      ************************ 
      * DETACH FROM INSTANCE * 
      ************************ 
           call "sqlgdtin" using
                                 by reference sqlca
                           returning rc.

           move "detach from instance" to errloc.
           call "checkerr" using SQLCA errloc.

       end-dbinst. stop run.