Thursday, December 15, 2011

Defining VSAM Files in Your Program

    

 After identifying and describing VSAM files in the Environment and Data
 Division, you can process the records in these files in the Procedure
 Division of your program.

Remember that VSAM data sets must be defined with AMS before your program
can do any file processing.  

       1 Environment Division Entries for VSAM Files

       2 Data Division Entries for VSAM Files
             
1 Environment Division Entries for VSAM Files

  Use the File-Control entry to define the VSAM file organizations and
  access methods for the files in your COBOL program.   shows
  typical File-Control entries for VSAM files.  The first example is for a
  VSAM sequential file (ESDS).  The second example shows the statements for
  a VSAM indexed file (KSDS) that will be accessed dynamically.  In addition
  to the primary key, COMMUTER-NO, there is an alternate key for this file,
  LOCATION-NO.  Example 3 is for a relative-record data set (RRDS) to be
  accessed randomly by the value placed in the relative key, ITEM-NO.

    ________________________________________________________________________ 
   |                                                                        |
   | Example 1:  VSAM Sequential File                                       |
   |                                                                        |
   |                                                                        |
   |      SELECT S-FILE                                                     |
   |        ASSIGN TO SEQUENTIAL-AS-FILE                                    |
   |        ORGANIZATION IS SEQUENTIAL                                      |
   |        ACCESS IS SEQUENTIAL                                            |
   |        FILE STATUS KEY IS FSTAT-CODE VSAM-CODE.                        |
   |                                                                        |
   |                                                                        |
   | Example 2:  VSAM Indexed File                                          |
                                                                                                                                                
   |      SELECT I-FILE                                                     |
   |        ASSIGN TO INDEXED-FILE                                          |
   |        ORGANIZATION IS INDEXED                                         |
   |        ACCESS IS DYNAMIC                                               |
   |        RECORD KEY IS IFILE-RECORD-KEY                                  |
   |        ALTERNATE RECORD KEY IS IFILE-ALTREC-KEY                        |
   |        FILE STATUS KEY IS FSTAT-CODE VSAM-CODE.                        |
   |                                                                        |
   |                                                                        |
   | Example 3:  VSAM Relative File                                         |
   |                                                                        |
   |                                                                        |
   |      SELECT R-FILE                                                     |
   |        ASSIGN TO RELATIVE-FILE                                         |
   |        ORGANIZATION IS RELATIVE                                        |
   |        ACCESS IS RANDOM                                                |
   |        RELATIVE KEY IS RFILE-RELATIVE-KEY                              |
   |        FILE STATUS KEY IS FSTAT-CODE VSAM-CODE.                        |
   |                                                                        |
   |                                                                        |
   |________________________________________________________________________|
    Example File-Control Entries for VSAM Files

  Notice how the ORGANIZATION clause is:

 °   ORGANIZATION IS SEQUENTIAL--for VSAM sequential files (ESDS)
 °   ORGANIZATION IS INDEXED--for VSAM indexed files (KSDS)
 °   ORGANIZATION IS RELATIVE--for VSAM relative files (RRDS).

  The FILE STATUS clause specifies fields that are updated after each
  input/output statement to indicate the success or failure of the
  operation.  See "File Status Key"  and "VSAM Codes (VSAM
  Files Only)"for information on how to set up the fields to
  check the returned values.

2 Data Division Entries for VSAM Files
  
 VSAM records can be fixed or variable in length.  COBOL determines the
  record format from the RECORD clause and the record descriptions
  associated with your FD entry for the file.

  Because the concept of blocking has no meaning for VSAM files, you may
  omit the BLOCK CONTAINS clause.  The clause is syntax-checked, but it has
  no effect on the execution of the program.



      1 Fixed-Length Records

      2 Variable-Length Records       

1 Fixed-Length Records


  The compiler determines the records to be fixed length, if you do one of
  the following:

  °   Use the RECORD CONTAINS integer clause (RECORD Clause Format 1)

      When you use this clause, the records will be fixed in size with a
      length of integer, even if there are multiple level-01 record
      description entries with different lengths associated with the file.

  °   Omit the RECORD CONTAINS integer clause, but define all the level-01
      record descriptions entries associated with the file to be the same
      fixed size and none contain an OCCURS DEPENDING ON clause.  This fixed
      size is the record length.
              


2 Variable-Length Records


  The compiler determines the records to be variable length, if you do one
  of the following:

  °   Use the RECORD IS VARYING clause (RECORD Clause Format 3).

      If you specify values for integer-1 and integer-2 (RECORD IS VARYING
      FROM integer-1 TO integer-2), the maximum record length is the value
      specified for integer-2, regardless of the lengths specified in the
      level-01 record description entries associated with the file.  If you
      omit integer-1 and integer-2, the maximum record length is determined
      to be the size of the largest level-01 record description entry
      associated with the file.

  °   Use the RECORD CONTAINS integer-1 TO integer-2 clause (RECORD Clause
      Format 2) with integer-1 and integer-2 matching the minimum length and
      the maximum length of the level-01 record description entries
      associated with the file.

      The maximum record length is the value specified for integer-2.

  °   Omit the RECORD clause, but specify multiple level-01 records
      (associated with the file) that are of different sizes, or some of
      which contain an OCCURS DEPENDING ON clause.

      The maximum record length is determined to be the size of the largest
      level-01 record description entry associated with the file.

  The RECORD clause is sensitive to the CMPR2 compiler option.  See
  VS COBOL II Migration Guide for MVS and CMS for more information on VS
  COBOL II Release 2 compatibility and migration.

2 VSAM Indexed File Organization

   In a VSAM indexed file (KSDS), the records are ordered according to the
   collating sequence of an embedded prime key field, which you define.  The
   prime key consists of one or more consecutive characters within the
   records.  The prime key uniquely identifies the record and determines the
   sequence in which it is accessed with respect to other records.  A prime
   key for a record might be, for example, an employee number or an invoice
   number.  In your COBOL program, you specify this key through the clause:

     RECORD KEY IS data-name

   where data-name is the name of the key field as you defined it in the
   record description entry in the Data Division.

   You can also specify one or more alternate keys to use for retrieving
   records.  Using alternate keys, you can access the file to read records in
   some sequence other than the prime key sequence.  For example, you could
   access the file through employee department rather than through employee
   number.  Alternate keys need not be unique.  More than one record will be
   accessed, given a department number as a key.  This is permitted if
   alternate keys are specified as allowing duplicates.

   You define the alternate key in your COBOL program with the ALTERNATE
   RECORD KEY IS clause:

    ALTERNATE RECORD KEY IS data-name

  where data-name is the name of the key field as you defined it in the
  record description entry in the Data Division.

   To use an alternate index, you need to define a data set (using access
   method services) called the Alternate Index (AIX).  
   This file contains one record for each value of a given
   alternate key; the records are in sequential order by alternate key value.
   Each record contains the corresponding primary keys of all records in the
   associated indexed files that contain the alternate key value. 

No comments:

Post a Comment