Rational Developer for System z
PL/I for Windows, Version 8.0, Programming Guide

Using a DIRECT file to access a workstation VSAM keyed data set

You can open a DIRECT file that is used to access a workstation VSAM keyed data set with the INPUT, OUTPUT, or UPDATE attribute.

If you use a DIRECT OUTPUT file to add records to the data set, and if an attempt is made to insert a record with the same key as a record that already exists, the KEY condition is raised.

If you use a DIRECT INPUT or DIRECT UPDATE file, you can read, write, rewrite, or delete records in the same way as for a KEYED SEQUENTIAL file.

Figure 22 shows one method you can use to update a keyed data set. You can also use this program to process a DDM or SFS file by modifying the AMTHD specification.

Figure 22. Updating a workstation VSAM keyed data set
 /********************************************************************/
 /*                                                                  */
 /*                                                                  */
 /*  DESCRIPTION                                                     */
 /*    Update an ISAM keyed data set by key.                         */
 /*                                                                  */
 /*                                                                  */
 /*  USAGE                                                           */
 /*    The following  commands are required to establish   */
 /*    the environment variables to run this program:                */
 /*                                                                  */
 /*      SET DD:DIREC=ISAM2.OUT,AMTHD(ISAM)                          */
 /*      SET DD:SYSIN=ISAM3.INP,RECSIZE(80)                          */
 /*                                                                  */
 /*    Note: This program is using isam2.out file created by the     */
 /*          previous sample program NAMELD.                         */
 /*                                                                  */
 /********************************************************************/

  DIRUPDT: proc options(main);

   put skip list('ISAM3 TEST START');
  
   dcl Direc file record keyed update
       env(organization(indexed)
           recsize(23)
           keyloc(1)
           keylength(20)
          );

   dcl 1 IoArea,
         5 NewArea,
           10 Name char(20),
           10 Number char(3),
         5 Code char(1);

   dcl oncode builtin;
   dcl Eof bit(1) init('0'b);

   on endfile(sysin) Eof = '1'b;

   on key(Direc)
   begin;
      if oncode=51 then put file(sysprint) skip edit
         ('Not found: ',Name)(a(15),a);
      if oncode=52 then put file(sysprint) skip edit
         ('Duplicate: ',Name)(a(15),a);
   end;

   open file(Direc) direct update;
   get file(sysin) edit (Name,Number,Code) (a(20),a(3),a(1));
   do while (¬Eof);
      put file(sysprint) skip edit (' ',Name,'#',Number,' ',Code)
          (a(1),a(20),a(1),a(3),a(1),a(1));
      select (Code);
             when('A') write file(Direc) from(NewArea) keyfrom(Name);
             when('C') rewrite file(Direc) from(NewArea) key(Name);
             when('D') delete file(Direc) key(Name);
             otherwise put file(sysprint) skip edit
                ('Invalid code: ',Name) (a(15),a);
       end;
       get file(sysin) edit (Name,Number,Code) (a(20),a(3),a(1));
   end;

   close file(Direc);
   put file(sysprint) page;

   /* Display the updated file                          */

   open file(Direc) sequential input;

   Eof = '0'b;
   on endfile(Direc) Eof = '1'b;

   read file(Direc) into(NewArea);
   do while(¬Eof);
      put file(sysprint) skip edit(Name,Number)(a,a);
      read file(Direc) into(NewArea);
   end;
   close file(Direc);
   
   put skip list('ISAM3 TEST END');
   
 end DIRUPDT;

An input file for this program might look like this one:

NEWMAN,M.W.         516C
GOODFELLOW,D.T.     889A
MILES,R.               D
HARVEY,C.D.W.       209A
BARTLETT,S.G.       183A
CORY,G.                D
READ,K.M.           001A
PITT,W.H.              X
ROLF,D.E.              D
ELLIOTT,D.          291C
HASTINGS,G.M.          D
BRAMLEY,O.H.        439C
GABRIELLI, M.          X

The program uses a DIRECT update file and alters the data according to a code that is passed into the records in the file SYSIN:

A
Add a new record
C
Change the number of an existing name
D
Delete a record

The program reads the name, number, and code and takes action according to the value of the code. A KEY ON-unit is used to handle any incorrect keys. When the updating is finished the file DIREC is closed and reopened with the attributes SEQUENTIAL INPUT. The file is then read sequentially and printed.

Rational Developer for System z
PL/I for Windows, Version 8.0, Programming Guide