Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $9.99/month after trial. Cancel anytime.

COBOL Language Fundamentals with DB2 Quick Start
COBOL Language Fundamentals with DB2 Quick Start
COBOL Language Fundamentals with DB2 Quick Start
Ebook422 pages2 hours

COBOL Language Fundamentals with DB2 Quick Start

Rating: 0 out of 5 stars

()

Read preview

About this ebook

This book will teach you the basic information and skills you need to develop applications with the COBOL programming language and the DB2 database management system on IBM mainframe computers running z/OS. The instruction, examples and sample programs in this book are a fast track to becoming productive with COBOL and DB2 as quickly as possible.  The content of this book is easy to read and digest, well organized and focused on honing real job skills. Acquiring these skills is a key step in mastering COBOL and DB2 application development so you'll be ready to perform effectively on an application development team.

LanguageEnglish
Release dateApr 18, 2020
ISBN9781393003762
COBOL Language Fundamentals with DB2 Quick Start
Author

Robert Wingate

Robert Wingate is a computer services professional with over 30 years of IBM mainframe and distributed programming experience. He holds several IBM certifications, including IBM Certified Application Developer - DB2 11 for z/OS, and IBM Certified Database Administrator for LUW. He lives in Fort Worth, Texas.  

Read more from Robert Wingate

Related to COBOL Language Fundamentals with DB2 Quick Start

Related ebooks

Programming For You

View More

Related articles

Reviews for COBOL Language Fundamentals with DB2 Quick Start

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    COBOL Language Fundamentals with DB2 Quick Start - Robert Wingate

    LEGAL

    Disclaimer

    The contents of this book are based upon the author’s understanding of and experience with the following IBM products:  COBOL, DB2.  Every attempt has been made to provide correct information.  However, the author and publisher do not guarantee the accuracy of every detail, nor do they assume responsibility for information included in or omitted from it. All of the information in this book should be used at your own risk.

    Copyright

    The contents of this book may not be copied in whole, or in part, without the explicit written permission of the author. The contents are intended for personal use only. Secondary distribution for gain is not allowed.  Any alteration of the contents is absolutely forbidden. 

    Copyright, 2020 by Robert Wingate

    DB2, DB2 UDB, UDB, and MVS are all registered trademarks of the IBM Corporation. 

    Introduction

    Welcome

    Congratulations on your purchase of COBOL Language Fundamentals with DB2 Quick Start.  This book will teach you the basic information and skills you need to develop applications with COBOL and DB2 on IBM mainframes running z/OS. The instruction, examples and sample programs in this book are a fast track to becoming productive as quickly as possible using COBOL with DB2. The content is easy to read and digest, well organized and focused on honing real job skills.  

    This is not an everything you need to know about COBOL and DB2 book.  Rather, this text will teach you what you need to know to become productive quickly with COBOL and DB2.  For additional detail, you can download and reference the IBM manuals and Redbooks associated with these products. 

    Assumptions:

    While I do not assume that you know a great deal about IBM mainframe programming, I do assume that you’ve logged into an IBM mainframe and know your way around.  Also I assume that you have a working knowledge of computer programming in some language (it can be a language other than COBOL). All in all, I assume you have:

    1.   A working knowledge of ISPF navigation and basic operations such as creating data sets.

    2.   A basic understanding of structured programming concepts.

    3.   A basic understanding of SQL.

    4.   Access to a mainframe computer running z/OS and DB2 (with a COBOL compiler available).

    Approach to Learning

    I suggest you follow along and do the examples yourself in your own test environment.  There’s nothing like hands-on experience.  Going through the motions will help you learn faster.

    If you do not have access to a mainframe system through your job, I can recommend Mathru Technologies.  You can rent a mainframe account from them at a very affordable rate, and this includes access to DB2 (at this writing they offer DB2 version 10).  Their environment supports COBOL and PLI as well.  The URL to the Mathru web site is: 

    http://mathrutech.com/index.html

    Besides the instruction and examples, I’ve included questions at the end of each chapter. I recommend that you answer these and then check yourself against the answers in the back of the book.

    Knowledge, experience and practice questions.  Will that guarantee that you’ll succeed as a COBOL/DB2 application developer?  Of course, nothing is guaranteed in life.  But if you put sufficient effort into this well-rounded training plan that includes all three of the above, I believe you have a very good chance of becoming productive as a COBOL/DB2 Application Developer as soon as possible.  This is your chance to get a quick start!  

    Best of luck!

    Robert Wingate

    IBM Certified Application Developer – DB2 11 for z/OS

    C:\Books\IBM Books\COBOL Language Fundamentals with DB2 Quick Start\KOBO\COBOL Language Fundamentals with DB2 Quick Start_files\image001.jpg

    Part One : COBOL Language Basics

    Introduction

    COBOL is an acronym for Common Business-Oriented Language.  It’s a third generation procedural language that has been around since 1959.  COBOL was developed primarily for business use.  It generally focuses on record or database input/out, as well as calculations and reports. 

    You may hear that COBOL is gone or on the way out, but its demise is probably exaggerated.  While lots of mainframe COBOL programming has been rewritten in other languages (or replaced by commercial packages), COBOL is still heavily used in legacy applications that run on IBM mainframe computers.  This is true especially for banking and finance applications. Check out this article if you don’t believe it:

    https://thenewstack.io/cobol-everywhere-will-maintain/

    If you are used to programming in languages such as C or Java, you’ll find that COBOL is a bit more English-like.  This can be an advantage or disadvantage depending on how you look at it. For example, a data assignment to a variable in most programming languages can be as simple as:

    X = 27

    In COBOL you would code this same operation as:

    MOVE 27 TO X

    The latter is somewhat more verbose.  It also has the receiving variable on the right side of the equation which is different. 

    Also, in COBOL you can spell out Booleans such as > or < using English phrases, such as:

    IF X IS GREATER THAN 100

    Whatever your view of the verbosity, COBOL is a very usable language and worth learning. As legacy COBOL programmers retire, there is still much code to be maintained or converted.  This chapter will help make you productive for these tasks quickly.

    COBOL Language Basics

    Programming Format

    Unlike more freeform languages, COBOL is very particular about exactly where you can put executable code. Here is a summary of the formatting rules you must follow in COBOL.

    image.jpg

    If you do not follow these rules, you will receive compiler errors which tend to cascade (one error causes multiple others).  The model programs in this book will help keep you out of trouble.

    Four Divisions 

    A COBOL source program is grouped into the following four divisions:

    1.   Identification Division

    2.   Environment Division

    3.   Data Division

    4.   Procedure Division

    Only the Identification division is required, but you can’t do much without the others.

    IDENTIFICATION DIVISION

    The identification division can specify the program name, author, data written and a few other pieces of information. The only mandatory element is the program name. 

    ENVIRONMENT DIVISION

    The environment division consists of a configuration section and an input-output section. 

    Configuration Section.

    The Configuration Section is optional. If you include it you can specify the source and object computer upon which the program was written and run, respectively.  This is simply documentation and has no affect when the program is compiled.

    Input-Output Section.

    The Input-Output Section is used to define external input and output files, including the record description that the program requires to read and write to the files.

    File Control.

    We’ll get to this in the file I/O section of this text.

    I/O Control.

    The I/O-CONTROL paragraph is optional.  It specifies the storage areas to be shared by different files.  I’ve never actually seen this used.

    DATA DIVISION

    The Data Division is divided into four sections:

    File Section

    Describes externally stored data (including sort-merge files).

    Working-Storage Section

    Describes internal data such as variables and structures used for computation, strings manipulation, reports, etc.

    Local-Storage Section

    Describes internal data that is allocated on a per-invocation basis.

    Linkage Section

    Describes data made available by another program. It appears in the called program and describes data items that are provided by the calling program and are referred to by the called program. 

    PROCEDURE DIVISION

    The Procedure Division contains executable statements and sentences organized into sections or paragraphs. Statements start with a verb such as MOVE, COMPUTE or PERFORM.  Sentences consist of one or more statements.  Sentences always end with a period.  

    The program execution starts with the first statement.  The Procedure Division ends at the physical end of the source code (i.e., when no more statements appear).

    Optional Entries and Sections

    The following Identification Division entries are optional and have no affect on the compiler.

    AUTHOR

    Name of the author of the program. It is syntax checked only.

    INSTALLATION

    Name of the company or location. It is syntax checked only.

    DATE-WRITTEN

    Date the program was written. It is syntax checked only.

    DATE-COMPILED

    Date the program was compiled.

    SECURITY

    Level of confidentiality of the program.

    Variables, Data Types and Assignment

    Like most programming languages, COBOL requires you to declare the data elements (variables and structures) you will use in your program. You put your variable declarations in the Data Division of the program, typically either the Working Storage or Linkage sections. 

    A variable declaration must include a level number, variable name and variable type.  It can also include an initial value by using the VALUE clause.  Variable names are always preceded by a level number.  If you use level 01 or 77 for variable declarations, your declaration must begin in Area A.  All other level numbers must begin in Area B. Variable names must use alphanumeric characters and they can include a dash character  for readability (see examples below). 

    For top level or standalone variables, the number is 1 (or 01 by convention), or 77.  The difference between a 01 level and a 77 level is that the 77 level can only define a single variable.  In contrast, the 01 level can either be a single variable or it can define a structure with multiple variables (sometimes referred to as a record).  

    Here are some examples of declaring individual variables.  We’ll show examples of structures later.

    01 EMPLOYEE-NAME     PIC X(30).

    77 EMPLOYEE-COUNT    PIC S9(4) USAGE IS BINARY.

    Data Types

    You’ll notice the PIC clause in the above variable declarations.  A PICTURE (or PIC) clause is a sequence of characters, each of which represents a portion of the data item and what it can contain. Alphabetic-only data can use an A picture.  More commonly, we use an alphanumeric picture X to define data that is not exclusively numeric.  You also specify the length of the picture variable.  In the case of EMPLOYEE-NAME above we have specified 30 bytes – PIC X(30).  Notice also the declaration must end with a period.

    For numeric variables you specify PIC 9 and the number of digits, and optionally a sign character which is S.  The EMPLOYEE-COUNT variable is 4 digits plus a sign character S (meaning the positive or negative sign  is stored with the numeric value).  Our declaration also specifies USAGE IS BINARY which indicates it is to be stored in binary format (as opposed to display format which is the default).  You can always leave off the USAGE IS part of the phrase and simply specify the usage.  Variables which are to be used for computation or indexing usually are defined as BINARY or COMP.  

    Note finally that for EMPLOYEE-COUNT you can specify either 9(4) or 9999 for EMPLOYEE-COUNT.  The meaning is the same.

    77 EMPLOYEE-COUNT    PIC S9999 BINARY.

                            OR

    77 EMPLOYEE-COUNT    PIC S9(4) BINARY.

    You can also specify an implied decimal point in your numeric data by including a V in the picture definition.  For example:

    77 EMPLOYEE-PAY    PIC 9(5)V9(2).

    The above specifies 7 digits, two of which are on the right hand side of an implied decimal point.  This actually does not change the storage of the variable, but tells the compiler how to align the results of computations. 

    Finally, a bit more on the USAGE IS clause which specifies the data format that the data is stored in.  The default is DISPLAY format.  The formats are:

    USAGE DISPLAY, the default format, where data is stored as a string.

    USAGE BINARY where a minimum size is either specified by the PICTURE clause or by a USAGE clause such as BINARY-LONG.

    USAGE COMPUTATIONAL (or USAGE COMP) where data may be stored in whatever format the implementation provides; often equivalent to  USAGE BINARY.

    USAGE PACKED-DECIMAL, where data is stored in the smallest possible decimal format (typically packed binary-coded decimal).

    COMP Variables

    COMP variables are binary numeric types that vary in the precision and type of storage as follows:

    COMP (Computational) – the compiler decides the data type which is usually the most efficient (typically BINARY format).

    COMP-1 – typically a single precision floating point value, stored in 4 bytes. 

    COMP-2 – typically a double precision floating point value, stored in 8 bytes. 

    COMP-3 - stores data in a binary coded decimal format – the sign is placed after the least significant digit.  This is often referred to as packed decimal

    When declaring variables you can also assign an initial value using the VALUE clause.

    To continue the examples of the variables we declared earlier:

    01 EMPLOYEE-NAME     PIC X(30) VALUE SPACES.

    77 EMPLOYEE-COUNT    PIC S9(4) BINARY VALUE 0.

    Data Structures

    Data items in COBOL are declared hierarchically through the use of level-numbers which indicate if a data item is a grouping of items, a part of a grouping or a standalone data item. An item with a higher level-number is subordinate to an item with a lower one. Top-level data items, with a level-number of 01, are sometimes called records. Items that have subordinate aggregate data items are called group items; those that do not are called elementary items. Level-numbers used to describe standard data items are between 1 and 49.  Here is a sample COBOL data structure.

    01 EMPLOYEE-INFO,

       05 EMP-NAME,

          10 EMP-LAST-NAME      PIC X(25).

          10 EMP-FIRST-NAME     PIC X(15).

          10 EMP-MI             PIC X(01).

       05 EMP-ADDRESS,

          10 EMP-ADDR-1         PIC X(30).

          10 EMP-ADDR-2         PIC X(30).

          10 EMP-CITY           PIC X(25).

          10 EMP-STATE          PIC X(15).

          10 EMP-ZIP            PIC 9(05).

    Sample Program

    Okay, so let’s do the obligatory Hello World program. We only need the Identification division and the Procedure division for this program, so that’s all we’ll include.  We’ll name the program COBHELO. We’ll use the DISPLAY verb and the literal value HELLO WORLD to implement the program.  Keep in mind you can include comments as long as you code an asterisk in column 7 of the comment line.  Here is our code.

    IDENTIFICATION DIVISION.

           PROGRAM-ID.    COBHELO.

          *      SIMPLE HELLO WORLD PROGRAM                   *

           PROCEDURE DIVISION.

          * THIS IS A COMMENT

           MAIN-PARA.

               DISPLAY 'HELLO WORLD'

               GOBACK

    The GOBACK statement returns control to the operating system. 

    Now we must compile/link the program according to the procedures in our shop. I’ll show you the procedure I use in my shop, but you’ll need to get the correct procedure from your technical leader or supervisor.  I run the following JCL to compile the program:

    image.jpg

    The output appears in SDSF and you can browse it there.  If there are any errors, they will be flagged here. In this case,  we do have an error.  We forgot to put a period at the end of the last sentence in the program, so our source code was not ended correctly.

    Untitled.jpg

    So let’s fix the program.  You can see that the revised version has the period after the last sentence.

           IDENTIFICATION DIVISION.

           PROGRAM-ID.    COBHELO.

          *      SIMPLE HELLO WORLD PROGRAM                  

           PROCEDURE DIVISION.

          * THIS IS A COMMENT

           MAIN-PARA.

               DISPLAY 'HELLO WORLD'

               GOBACK.

    Once we correct the error we can recompile.  This time we have a good compile because it says no statements flagged (and we received a zero return code on both the compile and link edit steps).                                                                    

    image.jpg

    Now we can run the program using this JCL:

    //USER01D JOB MSGLEVEL=(1,1),NOTIFY=&SYSUID    

    //*                                            

    //*  RUN A COBOL PROGRAM                       

    //*                                            

    //STEP01  EXEC PGM=COBHELO

    //STEPLIB  DD  DSN=USER01.LOADLIB,DISP=SHR     

    //SYSOUT   DD  SYSOUT=*                        

    And we can review output on SDSF.  As you can see, the HELLO WORLD was printed.

    image.jpg

    Ok for our next program, let’s include all four divisions and we’ll also include some optional entries in the Identification division.  Instead of displaying the literal HELLO WORLD, let’s declare a variable and we’ll load the literal value into the variable at run time.  Finally, we’ll display the content of the variable.  This program will produce exactly the same result as the COBHELO program. 

    Our program name is COBTRN1 and here is the listing.  In the Working Storage section we have declared a variable named WS-MESSAGE as a 12 byte container for an alphanumeric value.  In the procedure MAIN-PARA we copy the literal HELLO WORLD into WS-MESSAGE.  Finally we display the value of WS-MESSAGE.

    IDENTIFICATION DIVISION.

           PROGRAM-ID.    COBTRN1.

           AUTHOR.        ROBERT WINGATE.

           INSTALLATION.  SUNSET SERVICES.

           DATE-WRITTEN.  JANUARY 15, 2018.

           DATE-COMPILED. JANUARY 15, 2018.

           SECURITY.      NON-CONFIDENTIAL.

          *      SIMPLE HELLO WORLD PROGRAM                  

           ENVIRONMENT DIVISION.

           CONFIGURATION SECTION.

           SOURCE-COMPUTER. IBM-ZOS.

           OBJECT-COMPUTER. IBM-ZOS.

           DATA DIVISION.

           WORKING-STORAGE SECTION.

           01 WS-MESSAGE    PIC X(12).

           PROCEDURE DIVISION.

          * THIS IS A COMMENT

           MAIN-PARA.

               MOVE HELLO WORLD TO WS-MESSAGE

               DISPLAY WS-MESSAGE

               GOBACK.

    This program is just to demonstrate some of the optional entries, and to introduce the use of variables.  Our next programs will not include most of the optional Identification division entries.  Just be aware that you can use these if you want to.

    Sequence, Selection, Iteration

    Structured programming involves writing code that controls the execution of the program. This includes the primary concepts sequence, selection and iteration.

    Sequence

    Sequence means that program statements are executed sequentially according to the order in which they occur either in the main procedure of a program, or within sub-procedures which are called either paragraphs or sections. 

    Note:  The only practical difference between paragraphs and sections is that sections can contain multiple paragraphs.  If you perform a section then all paragraphs in the section will be executed.  If you perform a paragraph, only the code in that one paragraph is executed. In this text, we will use paragraphs only.

    For example, assume we have two variables VARIABLE-A and VARIABLE-B already declared in a program.  The following code will execute sequentially. 

           ADD +1 to VARIABLE-A

           MULTIPLY VARIABLE-A BY 2 GIVING VARIABLE-B

           DISPLAY VARIABLE-B

    The above is an example of the sequence control structure and it will occur throughout the program unless one of the other two control structures intervenes.  Sequence also involves invoking other paragraphs or sections of the program by using the PERFORM verb (this is the same as the CALL verb in other languages).

    Selection

    Selection means the program will execute (or not execute) statements based on a condition.  For example, given a variable RECORD-COUNTER, we could display the number of records if the value is greater than zero, or display a literal if the value is zero.

    IF RECORD-COUNTER

    Enjoying the preview?
    Page 1 of 1