Highlighted
Absent Member.
Absent Member.
1685 views

Micro Focus COBOL new style and old style

Jump to solution

I compared side by side of 2 COBOL code styles and hopefully someone can tell me how to do it:

Old style: ---Connect.sqb

==============================================
       Working-Storage Section.

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

           EXEC SQL BEGIN DECLARE SECTION END-EXEC.
       01 dbalias.
          49 dbalias-len    pic s9(4) comp-5 value 0.
          49 dbalias-buf    pic x(9) value " ".
       01 userid.
          49 userid-len     pic s9(4) comp-5 value 0.
          49 userid-buf     pic x(128) value " ".
       01 pswd.
          49 pswd-len       pic s9(4) comp-5 value 0.
          49 pswd-buf       pic x(19).
           EXEC SQL END DECLARE SECTION END-EXEC.

      PROCEDURE DIVISION.
           display "Enter database alias( 'sample' by default): "
              with no advancing.
           accept dbalias-buf.
           if dbalias-buf = spaces
              move "sample" to dbalias-buf.
           inspect dbalias-buf
              tallying dbalias-len for characters before initial " ".

           display "Enter user id( current by default)        : "
              with no advancing.
           accept userid-buf.
           inspect userid-buf
              tallying userid-len for characters before initial " ".
           if userid-buf not equal spaces
              display "Enter password                            : "
                 with no advancing
              accept pswd-buf.
              inspect pswd-buf
                 tallying pswd-len for characters before initial " ".

           if userid-buf = spaces
              EXEC SQL CONNECT TO :dbalias END-EXEC
           else
              EXEC SQL
                CONNECT TO :dbalias USER :userid USING :pswd
              END-EXEC.

===================================================

I can use db2 command to create both  bind file and cbl file:

db2 prep Connect.sqb  bindfile target mfcob CALL_RESOLUTION DEFERRED

New style: ---Connect.sqb

==============================================

working-storage section.

           EXEC SQL INCLUDE SQLCA END-EXEC.

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

procedure division.

           EXEC SQL
               CONNECT TO :ws-db USER :ws-usr  USING :ws-pass
           END-EXEC

       procedure division.

           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

           EXEC SQL
               CONNECT TO :ws-db USER :ws-usr  USING :ws-pass
           END-EXEC

==========================================

With the new style I can create  both Connect.bnd file and executable from  Visual COBOL eclipse

But with db2 command,

db2 prep Connect.sqb  bindfile target mfcob CALL_RESOLUTION DEFERRED

I create Connect.cbl but no Connect.bnd file with  error:

==================================================

   57   SQL4911N  The host variable data type is not valid.
   81   SQL4002N  "WS-PASS" and "WS-USR" are undeclared host
                  variables that cannot both be used as descriptor names in a
                  single SQL statement.
  110   SQL4002N  "WS-VAR-PASS" and "WS-USR" are undeclared host
                  variables that cannot both be used as descriptor names in a
                  single SQL statement.
        SQL0095N  No bind file was created because of previous
                  errors.

===============================================

Is there anyway -- without changing the code style,  I can make all Visual COBOL embedded samples  working under db2 command line?

Thank you in advance,

-Jack

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Micro Focus COBOL new style and old style

Jump to solution

I am not a DB2 expert so I am hoping others will chime in here also but the original set of samples that you are using are delivered by IBM and are written to support the syntax supported by the DB2 PREP precompiler. The Samples that are part of Visual COBOL are written to support the Micro Focus DB2 ECM (External Compiler Module) which is the Micro Focus precompiler for DB2. The ECM and PREP precompilers are not the same as there are many extensions made to the ECM to support additional syntax.

In order to get the Connect.cbl sample delivered with Visual COBOL to compile using DB2 PREP I had to add the BEGIN DECLARE SECTION and END DECLARE SECTION headers around the host variables and also move the comments within the section to begin in column 7.

There may be some additional PREP directives to change this requirement but that would be more of a question for IBM.

Why is it that you wish to use the DB2 PREP for everything?

If you wish to compile these programs from a .bnat file or from the command line then you can do so from a Visual COBOL Command prompt using the cobol and cbllink commands directly.

The connect.sqb after modification looks like:

     *set db2(db=sample)
     ****************************************************************
     * Copyright (C) Micro Focus 2010-2013. All rights reserved.
     *
     * This sample code is supplied for demonstration purposes only
     * on an "as is" basis and is for use at your own risk.
     *
     ****************************************************************
      working-storage section.
     *  Include the SQL Communications Area. This includes the
     *  definitions of SQLCODE, etc
          EXEC SQL INCLUDE SQLCA END-EXEC.          
          EXEC SQL BEGIN DECLARE SECTION 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).

          EXEC SQL END DECLARE SECTION END-EXEC.

      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.

View solution in original post

0 Likes
2 Replies
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: Micro Focus COBOL new style and old style

Jump to solution

I am not a DB2 expert so I am hoping others will chime in here also but the original set of samples that you are using are delivered by IBM and are written to support the syntax supported by the DB2 PREP precompiler. The Samples that are part of Visual COBOL are written to support the Micro Focus DB2 ECM (External Compiler Module) which is the Micro Focus precompiler for DB2. The ECM and PREP precompilers are not the same as there are many extensions made to the ECM to support additional syntax.

In order to get the Connect.cbl sample delivered with Visual COBOL to compile using DB2 PREP I had to add the BEGIN DECLARE SECTION and END DECLARE SECTION headers around the host variables and also move the comments within the section to begin in column 7.

There may be some additional PREP directives to change this requirement but that would be more of a question for IBM.

Why is it that you wish to use the DB2 PREP for everything?

If you wish to compile these programs from a .bnat file or from the command line then you can do so from a Visual COBOL Command prompt using the cobol and cbllink commands directly.

The connect.sqb after modification looks like:

     *set db2(db=sample)
     ****************************************************************
     * Copyright (C) Micro Focus 2010-2013. All rights reserved.
     *
     * This sample code is supplied for demonstration purposes only
     * on an "as is" basis and is for use at your own risk.
     *
     ****************************************************************
      working-storage section.
     *  Include the SQL Communications Area. This includes the
     *  definitions of SQLCODE, etc
          EXEC SQL INCLUDE SQLCA END-EXEC.          
          EXEC SQL BEGIN DECLARE SECTION 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).

          EXEC SQL END DECLARE SECTION END-EXEC.

      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.

View solution in original post

0 Likes
Highlighted
Absent Member.
Absent Member.

RE: Micro Focus COBOL new style and old style

Jump to solution

Thank you, Chris.

  Your code works,  My purpose for that question is -- I am looking for the COBOL syntax working for both Micro Focus DB2 ECM (External Compiler Module)   and  DB2 PREP Precompiler.

 So far as  I can see -- Micro Focus DB2 ECM  makes embedded SQL and COBOL code more seamlessly.

while DB2 PREP Precompiler requires more embedded directives,  but Micro Focus DB2 ECM  is still compatible with it.  (Can I call it backward compatibility?)

If I stay with DB2 Prep Style, it should be OK to exchange code between PC and Mainframe.

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.