How To Dynamically Allocate Files from a Cobol Batch Program
Normally, in COBOL file based process we allocate file in the JCL using DD names. However, There can be situations when we might need to allocate files dynamically. For example, there is a requirement to allocate different file for different member ID. The total number of files needs to be allocated is not a constant and also the file names need to have the member ID in the file naming. So the files can not be allocated in the JCL, it has to be allocated at the run time when the Cobol program is processing data.
This type of requirement is the best use case for using 'dynamic file allocation technique'. IBM supplied routine BPXWDYN can be used to dynamically allocate files in batch programs. The below sample code snippet dynamically creates a new dataset and allocates it to DDname OUTFILE and then it opens the file in OUTPUT mode and writes a record to it.
IDENTIFICATION DIVISION.
PROGRAM-ID. MCANUP6.
ENVIRONMENT DIVISION.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT OUT-FILE ASSIGN TO OUTFILE
FILE STATUS OUT-STATUS.
DATA DIVISION.
FILE SECTION.
FD OUT-FILE.
01 OUT-REC PIC X(80).
WORKING-STORAGE SECTION.
01 OUT-STATUS PIC X(02).
01 FILE-NM PIC X(20).
01 WS-ALLOC-STRING PIC X(100).
01 WS-PGM PIC X(08) VALUE 'BPXWDYN'.
PROCEDURE DIVISION.
MOVE 'TESTMCN.AG.DYNM.FILE' TO FILE-NM.
STRING 'ALLOC DD(OUTFILE) DSN(' FILE-NM ') NEW '
'CATALOG ' 'LRECL(80) RECFM(F,B)'
DELIMITED BY SIZE
INTO WS-ALLOC-STRING
END-STRING.
DISPLAY ' WS-ALLOC-STRING *' WS-ALLOC-STRING '*'
CALL WS-PGM USING WS-ALLOC-STRING.
DISPLAY RETURN-CODE.
OPEN OUTPUT OUT-FILE.
MOVE 'TEST RECORD' TO OUT-REC.
WRITE OUT-REC.
DISPLAY 'OUT-STATUS :' OUT-STATUS
CLOSE OUT-FILE.
GOBACK.
Comments
Post a Comment