Easytrieve Plus /DB2
Easytrieve Plus /DB2
Automatic Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Controlled Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
Controlled Processing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
SQL Exercise . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Course Pre-requisites
This course provides no training in Easytrieve Plus nor in the use of DB2 and SQL. These topics
should all be studied separately prior to this course.
SQL (Structured Query Language) provides the method of access to DB2 data structures and
remains the same no matter what application language or operating system is used. It is perfectly
reasonable to study DB2/SQL in a COBOL II environment before looking at its use in Easytrieve
Plus.
When the SQL statement is actually executed while the program is running (Dynamic SQL)
In advance by using the SQL pre-processor on the source code (Static SQL)
The Easytrieve Plus interface uses the Dynamic and Extended Dynamic interfaces supplied by IBM.
The latter of these implements Static SQL commands. Only commands which can be executed using
the Dynamic and Extended Dynamic interfaces can be embedded in an Easytrieve Plus program.
NB. In Information Systems (BTC), programs may be run dynamically while in development, but
must be handed over to run statically in the live environment.
Dynamic SQL
To run an Easytrieve Plus / DB2 program dynamically, simply use the EZPGO catalogue
procedure to compile and run it all in one step.
Static SQL
Before an Easytrieve Plus / DB2 program can be run using static SQL, it is first necessary to code a
PARM LINK statement at the top of the program. The syntax of this is as follows:
BIND (STATIC-ONLY)
The R is short for replace, and causes an existing load module of the same name to be over-written.
For the first compile of a program, the R can be left off, as the default is ADD.
The program name and plan name MUST be different from one another.
To run an Easytrieve Plus / DB2 program statically, it is necessary to use three separate catalogue
procedures. The first step uses DB2EZP to separate the program into its Easytrieve Plus and SQL
components. This step produces an Easytrieve Plus load module, which is given the program name,
and a Database Request Module (DBRM), which resides as a member in the partitioned dataset
DEV@@xx.DB2.DBRMLIB. Also produced, but invisible to the developer, is an Assembler
module, which is called dynamically at runtime to execute the SQL.
The second step of the process uses the Boots catalogue procedure DB2BIND to produce a
PLAN from the DBRM. The PLAN is given the same name as the DBRM.
DB2EZP catproc
ASM EZY +
DBRM LOAD MODULE
Assembler Module
Database Request Module
Load Library =
DB2BIND catproc
DEV@@xx.TEST.LOADLIB
DBRM Library =
DEV@@xx.DB2.DBRMLIB
The stages in running an Easytrieve Plus / DB2 program under static SQL.
The following table shows SQL data types and their corresponding Easytrieve Plus field definitions:
1
SQL does not support packed fields with a length greater than 8.
2
SQL does not support binary fields other than 2 B 0 and 4 B 0.
Automatic Processing
All Easytrieve Plus programs must begin with a command such as FILE, PARM or DEFINE.
Where there is no file input - and therefore no FILE statement - it is necessary to code the
command DEFINE in front of the field definitions in the program. This statement is otherwise
optional and would normally be left out.
To specify that the job will have automatic input from SQL, it is first necessary to code the job
statement as follows:
A SELECT statement must then immediately follow the JOB statement. The SELECT statement
will identify the rows and columns to be used as input to the job activity.
NB. Only one SELECT statement can be coded in each job activity.
Using this method it is possible to retrieve selected data from every row within a DB2 table.
eg. To select the contents of the column EMPNAME from the table PERSONNEL, one at a time,
using automatic input in Easytrieve Plus, code the following:
DEFINE EMP-NAMEW 5 A
SELECT EMPNAME +
FROM PERSONNEL +
INTO :EMP-NAME
Controlled Processing
As with Automatic input, it is necessary to use the Easytrieve Plus DEFINE command on the first
line of the program if there is no input file.
If controlled input is being used in an Easytrieve plus program, it is then necessary to define a cursor
in the Library section. This is done using the SQL DECLARE statement, followed by a cursor
name. Any host variables referred to in a DECLARE statement must already have been defined in
the Library section above. All other SQL statements, such as those to open, close and fetch the
cursor into a host variable are coded in the Activity section after the JOB statement, which is coded
as follows:
e.g.. To fetch the contents of the first row of the column EMPNAME from the table PERSONNEL,
using a cursor with controlled input in Easytrieve Plus, code the following:
DEFINE EMP-NAME W 15 A
SELECT EMPNAME +
FROM PERSONNEL
SQL OPEN C1
SQL FETCH C1 +
INTO :EMP-NAME
When using controlled input in Easytrieve Plus, it is necessary to code a STOP statement at the end
of the program.
The following SQL commands cannot be processed using Easytrieve Plus controlled SQL
processing:
DECLARE STATEMENT
DECLARE TABLE
DESCRIBE
EXECUTE
EXECUTE IMMEDIATE
INCLUDE
PREPARE
SELECT.......INTO.......
WHENEVER
It is very important that the SQLCODE field in the SQLCA is always tested to determine whether
the execution of each SQL statement is successful. If the code is not equal to 0 (successful
execution) or 100 (end of table reached), then the SQLCODE should be logged and the error
reported with a meaningful message in line with error processing standards.
e.g..
IF SQLCODE NE 0 AND +
SQLCODE NE 100 +
STOP
END-IF
DECLARE CONNECT
SELECT COMMIT
OPEN ROLLBACK
FETCH PUT
CLOSE UPDATE
DELETE INSERT
COMMENT EXPLAIN
For further Easytrieve Plus / DB2 examples, see the Easytrieve Plus Reference Manual.
Employee number
Department number
Salary
for those employees in the table DEV@@TR.TRTEMPL whose jobcode is greater than 52 and
were born before 1948. If you don't have one already, obtain a copy of the table from a trainer
before doing this exercise.