100% found this document useful (1 vote)
534 views439 pages

DB2 For The COBOL Programmer Part 1 PDF

Uploaded by

Roger Aguiar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
100% found this document useful (1 vote)
534 views439 pages

DB2 For The COBOL Programmer Part 1 PDF

Uploaded by

Roger Aguiar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 439
(SECOND EEYRIET DB2 for the 7 ae COB L Programmer Curtis Garvin Steve Eckols ih MIKE MurRACH & ASSOCIATES 2560 West Shaw» Lane, Suite 101 # Fresno, CA 93711-2765 Authors: Curtis Garvin Steve Eckols Editor: Mike Murach Cover design: Zylka Design Design and production: ‘Tom Murach Related Murach books: DB? for the COBOL Programmer, Part 2 Murach’s Mainframe COBOL Murach’s CICS for the COBOL Programmer Murach’s Oracle SQL and PUSQL Murach’s 05/390 and /OS JCL Murach’s Java SE 6 Murach’s Java Servlets and JSP © 1999, Mike Murach & Associates, Ine All rights reserved, Printed in the United States of America, 109876543 ISBN: 1-890774.02-2 Library of Congress Cataloging-in-Publication Data Garvin, Curtis, 1961- DB2 for the COBOL programmer: version 4.1/Custis Garvin, Steve Eckols. ~ 2nd ed p. em. ISBN 1-890774-02-2 (pbk: ¥, 1) 1. Database management. 2. IBM Database. 3. COBOL (Computer program language) 4. SQL. (Computer programm language) 1. Eckols, Steve. I Title, (QA76.9.D3G385 1998 005.75"6: 98-40935 cP. Contents Introduction Section 1 Chapter 1 Chapter 2 Chapter 3 Chapter 4 Section 2 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9 Chapter 10 Chapter 11 Section 3 Chapter 12 Chapter 13 Chapter 14 Chapter 15 Appendixes Appendix A. Appendix B Index The basics of DB2 programming in COBOL Introduction to DB2 and embedded SQL How to retrieve B2 data from a single table 41 How to modify DB2 tables How to use unions and joins 103 Other programming essentials 143 How to use advanced selection conditions 145 ‘How to use column functions : 165 How to use the data manipulation features 179 How to use variable-length data and nulls 215 How to use subqueries 235 How to use the features for error processing 263 How to maximize locking efficiency 283 Program development skills 301 How to use DB2I to prepare and run programs 303 How to develop programs outside DB2L 339 How to use SPUFI 351 How (0 use QMF to update tables 371 385 Source code generated by the DB2 precompiler 385 SQLCODE and SQLSTATE values 330 421 Introduction DB2 is the primary database management system (DBMS) for IBM mainframe computers that run under the MVS operating system. Although you can use several different methods to work with DB2 databases, most DB2 work on a mainframe is done through COBOL programs. The intent of this book is to show you how to develop those COBOL programs, 4 ways t book differs from other DB2 books * This is the only DB2 book that’s designed just for COBOL application program- mers. In contrast, most other DB2 books focus on database administration or ad hoc processing to the exclusion of many of the DB2 essentials for COBOL, programming. * When you complete section | of this book...just 142 pages....you'll be a confi- dent, competent, beginning DB2 programmer. You'll be developing COBOL, programs that access, add, delete, or update the data in one or more DB2 tables. Just as important, you'll understand how DB2 works...and you'll find that lets ‘you add to your skills more quickly than you'd be able to otherwise. * To help you learn more easily, this book presents 12 complete COBOL programs run under the MVS operating system using DB2 version 4.1, These programs let you see clearly how the DB2 code you're learning fits in with your COBOL code. ‘And once you've used these programs for training, th models for the programs you develop on the job. * After you use this book to learn DB2 programming, it becomes the best reference guide you've ever used. Why? Because all the content is logically organized by function under headings that clearly identify the information chunks, so it’s easy and fast to find what you're looking for. And all of the essential information for each function is summarized in the illustrations, so you read less to get the information you need. become ti Who this book is for This book assumes that you already have COBOL experience or at least have taken a first course in COBOL. If you have any doubts about your COBOL skills, though, you may want to get a copy of Murach’s Structured COBOL. It teaches all of the COBOL skills that are used in this book; it is a terrific desk reference; and it is the perfect companion to this DB2 book. What this book teaches Specitically, this book teaches you how to use Structured Query Language (SQL) within a COBOL program to retrieve and update DB2 databases. To get you started right, chapters | and 2 show you how to retrieve data from a single DB2 table. Chapter 3 shows you how to add rows to, delete rows from, and update rows in a single table. And chapter 4 shows you how to retrieve data from two or more related tables at the same time by using unions and joins. When you complete this first section, you'll be able to write production COBOL programs, In section 2, each of the seven chapters lets you expand your basic skills so you can process DB? data with more expertise. In chapter 6, for example, you'll learn how to use the column functions to summarize or average the data in one column of a table. In chapter 8, you'll learn how to work with variable-length columns and nulls, And in chapter I, you'll learn how to maximize locking efficiency so your programs don’t tie up the system. In this section, each chapter is written as an independent unit, so you can read the chapters in whatever sequence you prefer. In section 3, you'll learn the skills you need for binding, precompiling, compil- ing, link editing, and testing your COBOL programs. In chapter 12, you'll learn to use DB2I, which is a development tool for preparing a COBOL program online. In chapter 13, you'll learn how to do these programming tasks in batch, without using DB2L In chapter 14, you'll learn how to use SPUFI, a DB2I facility, for testing ‘SQL statements and creating test tables. And in chapter 15, you'll learn how to use the Query Management Facility (QMF) to add data to the test tables you create with SPUFI. You can read the chapters in this section any time after you complete chapter 1 What versions of DB2 this book teaches This book teaches the DB2 essentials that don’t change much from one version of DB2 to the next. As a result, you can use all the skills that you learn in this book with any version of DB2 from version 4 on, Although a few significant enhance- ‘ments were added to the essentials with version 4, subsequent versions have focussed on performance improvements rather than language enhancements. What about Part 2? When you complete DB? for the COBOL Programmer, Part 1, you'll have the DB2 skills that every application programmer needs. Then, to raise your skills to the next professional level, you'll want to get DB2 for the COBOL Programmer, Part 2. When you complete that book, you"ll have the skills of a senior programmer or programmerfanalyst. That includes skills like: using dynamic SQL; working with distributed DB2 data; using stored procedures; handling DB2 data in CICS pro- grams; and using the database administration features that let you set up a quality assurance environment for testing DB2 programs. How to download the 12 example programs and other files from our web site As we mentioned earlier, the 12 programs in this book not only teach you DB2 processing, they're also time-saving models for your own programs, That's why we make them available to you on our web site, along with some other useful files that you can use for program development tasks (there are JCL job streams, for ex- ample). By downloading these files to your system, you'll save hours of entry time. To download the files, go to the Downloads portion of our web site (vwizanurach.com/downloads). From there, you can download the zip file for this book to a default folder on your system. Then, from your Windows Explorer, double-click on the name of the downloaded file to expand it into its component files in this folder: C:\Murach\DB2\Part. Since all of the files are in ASCII (text) format, you can then open them with a word processor or a text editor. Please let us know how this book works for you If you have any comments about this book, we would enjoy hearing from you. ‘That's why there’s a postage-paid comment form at the back of this book. And if this book helps you lear what you wanted to know, we'd be especially delighted to hear about it. That's what makes it all worthwhile, [retptlonin We Yad Curtis Garvin Mike Murach Author Editor Section 1 The basics of DB2 programming in COBOL ‘The goal of this section is to get you started with DB2 programming in CO- BOL as quickly and easily as possible. As a result, chapter | introduces you to DB2, the Structured Query Language, and the essential skills for developing COBOL programs that use embedded SQL to access data from DB2 databases When you complete this chapter, you'll be able to code simple programs of your own. Alter this introduetion, chapter 2 shows you how to write programs that get data from one database table. Chapter 3 shows how to write programs that modify one database table by adding, deleting, or updating rows. And chapter 4 shows how to use unions and joins to get data from two or more tables. When you complete this section, you'll have the basic skills that you need for developing any program that retrieves or modifies DB2 data. 1 Introduction to DB2 and embedded SQL In this chapter, you'll be introduced to DB2 and to the SQL language that you use for working with the data in a DB2 database. Then, you'll see how embed- ded SQL can be used in a COBOL program. By the time you complete this, chapter, you'll know how to code simple COBOL programs that get data from DB2 databases on IBM mainframe computers, DB2 and the relational database model 4 How a relational database table is organized 4 How the tables in a relational database are related 6 How to use SQL to work with a DB2 database... How co query a single database table How to modify the data in a table How DB2 handles referential integrity and locking How to join data from two or more tables How to create and use a view A COBOL program that uses embedded SQL ‘The interactive sereen ‘The DCLGEN output The structure chart The COBOL listing Three skills for using embedded SQL How to code embedded SQL statements, How to use INCLUDE statements How (0 use the SQL communication area How DB2 works with MVS systems How DB2 and VSAM work together How to develop a COBOL program that accesses DB2 data 34 How to use DB2I as you develop a COBOL program 36 How 10 use the Query Management Facility 38 Perspective Section | The basics of DB2 programming DB2 and the relational database model In 1970, Dr. E. F. Codd developed a model for a new type of database called a relational database. This type of database eliminated some of the problems that were associated with flat files and hierarchical databases like IMS. By using the relational model, you can reduce data redundancy, which saves disk storage and leads to efficient data retrieval. You can also access and ‘modify the data in a way that is both intuitive and efficient. DB2, of Database 2, is a relational database management system (RDBMS) that runs on IBM mainframe computers. Because DB2 also runs on AS/400 mid-range computers and on PCs, DB2 can be used throughout an organization. In this book, you'll learn how to use DB2 through COBOL programs on an IBM mainframe. How a relational database table is organized The model for a relational database states that data is stored in one or more tables, It also states that each table can be viewed as a two-dimensional array consisting of horizontal rows and vertical columns. This is illustrated by the relational database table in figure 1-1. In this table, each row contains informa tion about a single customer. At the intersection of each column and row is a vale. In this figure, for example, the highlighted value is DENVILLE, which is the city for customer number 400002. The term value can be misleading, though, since it can be an alphanumeric (or string) value, a numeric value, or nothing (a null value). In practice, the rows and columns of a relational database table are often referred to by the terms records and fields. This makes sense because each row is comparable to a record in a traditional file, and each column is comparable to a field, As a result, these terms can be used interchangeably. If a table contains one or more columns that uniquely identify each row in the table, you can define these columns as the primary key of the table. For instance, the primary key of the customer table in this figure is the customer number, which has been given the name CUSTNO. Indexes provide an efficient way to access the rows in a table based on the values in one or more columns. Because applications typically access table rows by referring to their key values, each table requires a unique index for its, primary key. But you can define indexes for other columns as well. Like a key, an index can include one or more columns Chapter 1 Introduction to DB2 and embedded SQL A customer table Primary Columns Value key | I | CUSTNO FNAME LNAME ADDR erly STATE ZIPCODE 400001 KEITH JONES 4501 WMOCKINGBIRD _DAL.LAS 1% 75208 400002 «KAREN © ANNELLI. 40 FORD AD DENVILLE NJ 07834 400003 SUSAN HOWARD «1107 SECONDAVE-—«-REDWOODCITY CA oes Rows 400004 CAROLANN EVANS 74 SUTTONCT GREATLAKES L_—_—60088 400005 ELAINE —AOBERTS 12914 BRACKNELL CERRITOS, cA s0701 Concepts + A relational database uses tables to store and manipulate data. Each table consists, of one or more rows, or records, that contain the data for a single entity, Each row contains one or more columns, or fields, with each column representing a single item of data. + The data in a specific column of a specific row can be referred to as a value. + Most tables contain a primary key that uniquely identifies each row in the table. The primary key often consists of a single field, but it can also consist of two or more fields. In the table above, the customer number (CUSTNO) is the primary key. + An index can be applied to any column in a table to improve performance when rows are accessed based on the values in that column, At the least, each table requires a unique index for its primary key. Figure 1-1 How a relational database table is organized 5 Section | The basics of DB2 programming How the tables in a relational database are related The tables in a relational database can be related to other tables by values in specific columns. The two tables shown in figure 1-2 illustrate this concept. Here, each row in the customer table is related to one or more rows in the invoice table. This is called a one-to-many relationship. Typically, relationships exist between the primary key in one table and a ‘foreign key in another table, The foreign key is one or more columns in a table that refer to the primary key in another table. Although one-to-many relationships are the most common, two tables can also have a one-to-one or many-to-many relationship. If a table has a one-to- one relationship with another table, the data in the two tables could be stored in assingle table. Because of that, one-to-one relationships are used infrequently. In contrast, a many-to-many relationship is usually implemented by using an intermediate table that has a one-to-many relationship with the two tables in the many-to-many relationship. In other words, a many-to-many relationship can usually be broken down into two one-to-many relationships Chapter 1 Inuroduction to DB2 and embedded SQL. The relationship between a customer table and an invoice table CUSTNO FNAME NAME ADDR cry STATE ZIPCODE 400001 KEITH ONES 4501 W MOCKINGBIRD DALLAS % 75209 /400002 KAREN ANNELLI. «40 FORD RD DENVILLE Nu o7834 400003 SUSAN HOWARD = “1107 SECONDAVE —»-REDWOOD CITY CA 4063 400004 CAROL ANN EVANS 74 SUTTON CT GAEAT LAKES IL 60088 409005 ELAINE ROBERTS 12914 BRACKNELL___CERRITOS. cA 80701 Primary Foreign key key INVNO INVCUST INVDATE INVTOTAL PAYTOTAL CRTOTAL, 003584 400004 1998.04.28 51.75 eae 400005 1098-04-28 202.88 oss 400002 1998-04-28 68.87 ear 003587 400001 1998-04-28 22.09 003568 400008 1998-04-28 57.63 eesse2—400005 1906.04.28 71105 150.00 003590 400001 1908.04.28 110.49 110.48 Concepts + The tables in a relational database are related to each other through their key fields, In the tables above, the customer number is used to relate the customer and invoice tables, ‘+ Inthe customer table, the customer number field is the primary key. In the invoice table, the invoice number field is the primary key. + Inthe invoice table, the customer field is called a foreign key because it identifies a related row in the customer table. + Three types of relationships can exist between tables. The most common type is @ one-to-many relationship as illustrated above. A table can also have one-to-one relationship or & many-to-many relationship with another table. Figure 1-2 How the tables in a relational database are related Section | The basics of DB2 programming How to use SQL to work with a DB2 database ‘To work with the data in a relational database, you use Structured Query Language, or SQL. Although SQL is a standard language, each database ‘management system is likely to have its own extensions and variations, com- monly called a dialect. So when you use SQL, you need to make sure that you're using the dialect that’s supported by your RDBMS. In this book, of Course, you'll learn the dialect for DB2. Figure 1-3 summarizes some of the common SQL statements for DB2. As you can see, these statements can be divided into two categories. The state~ ‘ments that you use for defining and managing a database are called the data definition language, ot DDL, statements. These statements are normally used by database administrators, of DBAs, as they do jobs like defining tables, ‘modifying and deleting tables, and managing the disk space used for the tables. In contrast, the statements that you use to work with the data in a database are called the data manipulation language, or DML, statements. These include the SELECT, INSERT, UPDATE, and DELETE statements, and these are the statements that the application programmer uses. Although the execution of any one of these statements can be referred to as a query, the term query is com- ‘monly used to refer to just the execution of SELECT statements, while the action query is used to refer to the other DML statements, Three DML statements specific to the DB2 dialect are the OPEN, FETCH, and CLOSE statements. You use these statements after you use a SELECT statement that returns more than one row to the pro- gram, These statements then use a cursor to move from one row to another. Incidentally, SQL is pronounced either S-Q-L or sequel in conversation. Throughout this book, we use the first pronunciation so we refer to “an SQL. statement,” not “a SQL statement.” rm Chapter 1 Introduction to DB2 and embedded SQL SQL statements for data definition Souculs Peed acu) CREATE/ALTER/DROP DATABASE Creates, alters, or drops a database. CREATE/ALTER/DROP STOGROUP Creates, alters, or drops a storage group. CREATE/ALTER/DROP TABLESPACE Creates, alters, or drops a table space in the specified database, (CREATE/ALTER/DROP TABLE Creates, alters, or drops a table in the specified database (CREATE/ALTER/DROP INDEX Creates, alters, or drops an index for the specified table. CREATE/DROP VIEW Creates or drops a view of the specified database. CREATE/DROP ALIAS Creates or drops an alias ofa table or a view that may or ‘may not be on the current server. CREATE/DROP SYNONYM Creates or drops a synonym of a table or a view that must be on the current server, SQL statements for data manipulation Cuca PEE acu) SELECT Retrieves data from one or more tables. INSERT. Inserts one or more new rows into a table. UPDATE. ‘Updates one or more rows in a table. DELETE Deletes one or more rows from a table. OPEN: Opens a cursor, FETCH ms a cursor on the next row of its result table and assigns the values ofthat row to host variables CLOSE Closes a cursor. Concepts + SQL statements can be divided into two categories: the dara definition language (DDL) that lets you define the objects in a database, and the data manipulation language (DML) that lets you work with the data in a database, + Application programmers typically work with the DML statements, while database administrators (DBAs) use the DDL statements. ‘+ The execution of a SELECT statement is commonly referred to as a query. In contrast, the execution of an INSERT, UPDATE, or DELETE statement can be referred to as an action query. + Although SQL is a standard language, each vendor has its own SOL dialect, which may include extensions to the standards like the OPEN, FETCH, and CLOSE statements above. Figure 1-3 An introduction to the Structured Query Language 10 Section |The basies of DB2 programming How to query a single database table Figure 1-4 shows how to use a SELECT statement to get data from a single database table. Although the syntax of the SELECT statement presented in this figure has been simplified, it illustrates its basic function, which is to retrieve selected columns and rows from a database. ‘The syntax summary at the top of this figure uses conventions that are similar to those in COBOL manuals. Capitalized words are keywords; lower case words indicate the variables that you must supply; brackets [ ] indicate that a phrase is optional; and the ellipsis (...) indicates that you can code a series of like items. To separate the items in an SQL statement, you can use one or more spaces, and you can continue an SQL statement on the next line with just one restriction that you'll learn about later in this chapter. If you study the SELECT statement below the syntax summary, you can see how the two are related. Here, the SELECT statement retrieves columns from a table named MMO1,.INVOICE. It selects a row from this invoice table only if it has a balance owed that’s greater than zero, And it sorts the retumed rows by the balance due in descending sequence. Please note in this SELECT statement that the last column in the query (BALANCE is calculated by subtracting the payment total (PAYTOTAL) and the credit total (CRTOTAL) from the invoice total (INVTOTAL). In other words, the column named BALANCE doesn’t actually exist in the database, This type of column is called a calculated value, and it exists only in the results, of the query, Although it’s not indicated in the figure, you can select all of the columns in a table by coding an asterisk (*) in place of the column names. For example, this statement will select all of the columns from the invoice table: SELECT + PROM ¥OI01.ZNVOICE For efficiency, though, you should only select the columns that you need from a table. Figure 1-4 also shows the result table that is returned by the SELECT statement. A result table is a logical table that’s created temporarily within the database. When you use a SELECT statement in an application program to return a DB2 result table that consists of more than one row, you must code the SELECT statement within a DECLARE CURSOR statement. Then, you can. use the cursor to move from one row to another as explained in the next chapter. As you might guess, queries can have a significant effect on the perfor- ‘mance of a program, In general, the more columns and rows that are requested by a query, the longer it takes to return the result table. When you design a query, then, you should try to keep the number of columns and rows to a minimum, Chapter 1 Introduction to DB2 and embedded SQL W The syntax of a SELECT statement that retrieves data from one table SELECT colunn-list FROM table-name [WHERE selection-condition] [ORDER BY eort-columa [DESC] [, sort-column (DESC)] A SELECT statement that retrieves and sorts selected columns and rows from an INVOICE table SELECT INVNO, INVDATE, INVTOTAL, INVIOTAL - PAYTOTAL - CRTOTAL AS BALANCE FROM MMO1. INVOICE WHERE INVTOTAL - PAYTOTAL - CRTOTAL > 0 ORDER BY BALANCE DESC The INVOICE table INVCUST INVDATE INVTOTAL PAYTOTAL CRTOTAL 003584 400004 1998-04-28, 51.75 003585 400005 «1998.04.28, 292.83 003586 400002 «1998.04.28 68.87 68.87 003587 400001 1998.04.28 22.09 003588 400003 «1998-04-28 57.63, 003589 400005 «1998.04.28, 711.05 150.00 003590 400001___1998-04.28 110.49 110.49 The result table INVNO INVDATE INVTOTAL BALANCE 003589 1998-04-28 711.05 561.05, 003585 1998-04-28 202.83 292.83 003588 1998-04-28 57.63, 57.63, 003584 1998-04-28 5175 5175 003587 1998-04-28 22.09 22.09 Concepts + The result of a SELECT query isa result table like the one above. It consists of all of the columns and rows requested by the SELECT statement * Values in the result table that are calculated from values in the base table are called. calculated values + When a result table contains more than one row, a cursor is used to identify the individual rows in the table and to scroll through those rows. When you use DB2 from a COBOL program, this means that the SELECT statement is coded within a DECLARE CURSOR statement. This is explained in chapter 2. Figure 1-4 How to query a single database table 12 Section | The basics of DB2 programming How to modify the data in a table Figure 1-5 presents the basic syntax of the SQL INSERT, UPDATE, and DELETE statements. You use the INSERT statement to insert one or more rows into a table. As you can see, the syntax of this statement is different depending on whether you're adding a single row or selected rows. To add a single row to a table, you specify the name of the table you want to add the row to, the names of the columns you're supplying data for, and the values for those columns, The example in this figure adds a row to a customer table and supplies the values for each of the columns in the table. Here, the names that are preceded by a colon represent fields in the COBOL program called host variables. These variables must be coded in the same sequence as the columns in the column list. To add more than one row to a table, you include a SELECT statement within the INSERT statement. Then, the SELECT statement retrieves rows from one or more tables based on the conditions you specify, and the INSERT statement adds those rows to another table. In the example in this figure, the SELECT statement selects all the columns from the rows in the customer table with customer numbers less than 300000 and inserts them into an old customer table named MMO1.OLDCUST. ‘To change the values of one or more columns in a table, you use the UPDATE statement. On this statement, you specify the name of the table you want to update, expressions that indicate the columns you want to change and how you want to change them, and a condition that indicates the row or rows you want to change. In the example in this figure, the row with a customer ‘number equal to the value in the host variable named

You might also like