As we know, CL program is nothing but a bunch of commands which are executed by the system one after other. Hence, All commands which we have used previously (On the previous sections) for a data area like CRTDTAARA, DSPDTAARA, CHGDTAARA, DLTDTAARA are valid for the CL program also. However, the commands CRTDTAARA (Create a data area) and DLTDTAARA(Delete a data area) are seldom used in a CL Program as Data area's are mostly permanently objects. Yet, these two commands could be used in a CL program when we have to create a temporary data area.
The command DSPDTAARA is not used in a CL program (Though one is free to use it, if required (to see the changed values of data area while debugging)). The command CHGDTAARA is extensively used in a CL Program to change/update the value of an AS400 data area.
Retrieve value of a data area in CL program:
As we know the command DSPDTAARA just displays (Or prints a spool) the current value of a data area. It does not return anything. If we need to retrieve the value of a data area as a variable, we need to execute the command RTVDTAARA. However, The RTVDTAARA command is valid only in a CL program. It can be run independantly from the command line.
The RTVDTAARA command takes the following arguments.
- Name of Data area: Enter the name of the data area which you want to read.
- Library name: Enter the name of the library where the data area object is lying.
- Optional Substring start position Enter the substring start position if you are interested in a specific portion of the data area. To retrieve the complete data area write *ALL (Default value)
- Optional Substring length: Enter the substring length if you are interested in specific portion of the data area. Leave this field blank if you entered *ALL in the previous option.
- Name of CL Varialble: The last thing is what we are interested in. Enter the name of the CL varialble which is to receive the returned value. The returned value may be the complete data area or its specific portion. You will be knowing beforehand the return value and its type. The CL variable should be declared of appropriate type accordingly.
A typical syntax of the command RTVDTAARA is given here for your reference. Most of the times the library will not be required so it's omitted for simplicity. However, you can add it if required.
RTVDTAARA DTAARA(NAME (5 10)) RTNVAR(&VAR1)
Example of a Data Area Processing in CL
Before running the program create a data area named EMP# by running the following command.
CRTDTAARA DTAARA(EMP#) TYPE(*DEC) LEN(10 0) TEXT(1)
The source code of the Data Area Example is given below.
PGM
/* Declare Variable */
DCL VAR(&EMP#) TYPE(*DEC) LEN(10 0) VALUE(0)
/* Retrive data area into CL variable */
RTVDTAARA DTAARA(EMP# *ALL) RTNVAR(&EMP#)
/* Increment the variable by one */
CHGVAR VAR(&EMP#) VALUE(&EMP# + 1)
/* Update the data area with next employee number */
CHGDTAARA DTAARA(EMP#) VALUE(&EMP#)
ENDPGM
To test this program see the value of the data area before and after running it by running the command DSPDTAARA from command line. You will see that the program updates the data area's value after incrementing it by one.
Note:- To retrieve specific portion of the data area just use the command
RTVDTAARA DTAARA(EMP# (1 5)) RTNVAR(&EMP#)
That's how data area is handled in a CL Program. Do write your comments below.
Next we will learn how to handle a data area in an RPG program.