      $SET db2(db=sample)
      ****************************************************************
      *  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.

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

       exec sql include sqlca      end-exec

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

       01 char-col                 pic x(10).
       01 int-col                  pic S9(4) comp-5.
       01 ws-dyn-query.
          49 ws-dyn-query-len      pic s9(4) comp-5.
          49 ws-dyn-query-txt      pic x(255).

      *
       procedure division.
       loop-point.
           EXEC SQL
               declare vcurs cursor for dynamic_sql
           END-EXEC
           display "Create/insert/update/drop test"
           display " "
      *    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

	   if sqlcode not = 0
		display "Error: cannot connect "
		display sqlcode
		display sqlerrmc
		stop run
	   end-if

           display 'Drop table'
           move 1                to    ws-dyn-query-len
           string
               "drop table mfesqltest" delimited size
               " "                     delimited size
             into ws-dyn-query-txt     pointer ws-dyn-query-len
           end-string
           subtract 1           from   ws-dyn-query-len

           EXEC SQL
               prepare dynamic_sql from :ws-dyn-query
           END-EXEC
           if sqlcode not = 0
                display "Error: cannot prepare drop table "
                display sqlcode
                display sqlerrmc
           end-if
           EXEC SQL
               execute dynamic_sql
           END-EXEC
           if sqlcode not = 0
                display
                      "Error (anticipated): cannot execute drop table "
                display sqlcode
                display sqlerrmc
           end-if

           display 'Create table'
           move spaces                   to ws-dyn-query-txt
           move 1                        to ws-dyn-query-len
           string
               "create table mfesqltest ( " delimited size
               "    char_col char(10), "    delimited size
               "    int_col  integer)"      delimited size
            into ws-dyn-query-txt         pointer   ws-dyn-query-len
           end-string

           subtract 1             from      ws-dyn-query-len

           EXEC SQL
               prepare dynamic_sql from :ws-dyn-query
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot prepare create table "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           EXEC SQL
               execute dynamic_sql
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot execute create table "
              display sqlcode
              display sqlerrmc
              stop run
           end-if
	   
           display 'Insert row'
           move spaces                 to ws-dyn-query-txt
           move "ORIGINAL"             to char-col
           move 9999                   to int-col
           move 1                      to ws-dyn-query-len
           string
               "insert into mfesqltest ( "       delimited size
                "char_col,int_col) values (?,?)" delimited size
            into ws-dyn-query-txt      pointer   ws-dyn-query-len
           end-string
           subtract 1                  from      ws-dyn-query-len

           EXEC SQL
               prepare dynamic_sql from :ws-dyn-query
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot prepare insert row "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           EXEC SQL
               execute dynamic_sql using :char-col, :int-col
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot execute insert row "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           display 'Commit'

           EXEC SQL
               commit work
           END-EXEC

           if sqlcode not = 0
              display "Error: commit "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           display 'Update row'
           move spaces                  to ws-dyn-query-txt
           move "UPDATED"               to char-col
           move 9999                    to int-col
           move 1                       to ws-dyn-query-len
           string
               "update mfesqltest set char_col = ? " delimited size
               " where int_col = ?"                  delimited size
             into ws-dyn-query-txt        pointer    ws-dyn-query-len
           end-string
           subtract 1                      from      ws-dyn-query-len

           EXEC SQL
               prepare dynamic_sql from :ws-dyn-query
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot prepare update row "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           EXEC SQL
               execute dynamic_sql using :char-col, :int-col
           END-EXEC

           if sqlcode not = 0
              display "Error: cannot execute update row "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           display 'Verify data before rollback'
           move spaces                  to ws-dyn-query-txt
           move 9999                    to int-col
           move 1                       to ws-dyn-query-len
           string
               "select char_col from mfesqltest " delimited size
               "where int_col = ?"                delimited size
            into ws-dyn-query-txt       pointer   ws-dyn-query-len
           end-string
           subtract 1                    from     ws-dyn-query-len

           EXEC SQL
               prepare dynamic_sql from :ws-dyn-query
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot prepare select "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           EXEC SQL
               open vcurs using :int-col
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot open cursor "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           move spaces                  to char-col
           EXEC SQL
               fetch vcurs into :char-col
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot read row "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           if char-col <> "UPDATED"
              display "Data has wrong value(" char-col ")"
              stop run
           end-if

           EXEC SQL
               fetch vcurs into :char-col
           END-EXEC
           if sqlcode not = 100
              display "SQLCODE <> 100 reading second row"
              display sqlcode
              display sqlerrmc
              display char-col
              stop run
           end-if

           EXEC SQL
               close vcurs
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot close cursor "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           display 'Rollback'
           EXEC SQL
               rollback
           END-EXEC
           if sqlcode not = 0
              display "Error: rollback "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           display 'Verify data after rollback'
           move spaces                  to ws-dyn-query-txt
           move 9999                    to int-col
           move 1                       to ws-dyn-query-len
           string
               "select char_col from mfesqltest " delimited size
               "where int_col = ?"                delimited size
            into ws-dyn-query-txt    pointer      ws-dyn-query-len
           end-string
           subtract 1                  from       ws-dyn-query-len

           EXEC SQL
               prepare dynamic_sql from :ws-dyn-query
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot prepare select "
              display sqlcode
              display sqlerrmc
              stop run
           end-if
           EXEC SQL
                open vcurs using :int-col
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot open cursor "
              display sqlcode
              display sqlerrmc
              stop run
           end-if
           move spaces                  to char-col
           EXEC SQL
               fetch vcurs into :char-col
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot read row "
              display sqlcode
              display sqlerrmc
              stop run
           end-if
           if char-col <> "ORIGINAL"
              display "Data has wrong value(" char-col ")"
              stop run
           end-if
           EXEC SQL
               fetch vcurs into :char-col
           END-EXEC
           if sqlcode not = 100
              display "SQLCODE <> 100 reading second row"
              display sqlcode
              display sqlerrmc
              display char-col
              stop run
           end-if
           EXEC SQL
               close vcurs
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot close cursor "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

      *    Start a new transaction to avoid mixing DML and DDL
           EXEC SQL commit END-EXEC
           if sqlcode not = 0
              display "Error: on commit before drop "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           display 'Drop table'
           move 1                       to    ws-dyn-query-len
           move spaces                  to    ws-dyn-query-txt
           string
               "drop table mfesqltest" delimited size
               " "                     delimited size
            into ws-dyn-query-txt      pointer  ws-dyn-query-len
           end-string
           subtract 1           from    ws-dyn-query-len

           EXEC SQL
               prepare dynamic_sql from :ws-dyn-query
           END-EXEC
           if sqlcode not = 0
               display "Error: cannot prepare drop table "
               display sqlcode
               display sqlerrmc
           end-if
           EXEC SQL
               execute dynamic_sql
           END-EXEC
           if sqlcode not = 0
              display "Error: cannot execute drop table "
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           display 'Disconnect'
	   EXEC SQL
               commit work
	   END-EXEC
	   if sqlcode not = 0
              display "Error: commit release "
              display sqlcode
              display sqlerrmc
	   end-if

           EXEC SQL connect reset  END-EXEC

           display 'Create table after commit using static DDL '
           EXEC SQL
               create table mfesqltest (
                      char_col char(10),
                      int_col  integer)
           END-EXEC
           if sqlcode not = 0
              display "Cannot create table as expected"
              display sqlcode
              display sqlerrmc
           else
              display "Error: create table was OK"
              display sqlcode
              display sqlerrmc
              stop run
           end-if

           display "Test completed without error"

	   stop run.
