Data Area processing in ILE RPG program
All examples in this section of article use a data area created by the following command
CRTDTAARA DTAARA(EMP_INFO) TYPE(*CHAR) LEN(10) VALUE('01TutoIndi')
To use a data area in an RPG IV program we need to first declare the data area as a variable in the source code of the program. This variable can either be a stand-alone or a data structure. In RPG IV a data area can be declared in the following two ways.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++
DW@DtaAra S 10A DTAARA(EMP_INFO)
Notice that the data type of the stand alone variable should be same as the data type of the data area. However, if you need to declare a data area which has multiple data formats you should declare it as a data structure. The above dclaration can be made as
DW@DtaAra DS DTAARA(EMP_INFO)
DW@Emp# 1 2 0
DW@EmpName 3 10
This method of declaration is usually a two step method.
Firstly, an RPG variable is declared. A stand alone variable is declared in the C Spec result section or D-Spec and a data structure is defined in the I Specification or D Spec in RPG IV.
After that, The data area is declared in the C-Spec using the opcode "Define" as given below. Notice that the first factor of this opcode is *DTAARA which tells the compiler that the factor 2 is a data area and look for it in the library list when compiling.
C *DTAARA Define EMP_INFO W@DtaAra
Here, W@DtaAra can be a data structure or a stand alone variable. If it's a stand alone variable, it can be defined here itself.
Note:- The idea is to declare variable and then the Data area. How you achieve this is upto you. Due to this flexibility, I have found several combinations of declaration of data area.
Reading a data area in an RPG IV program is a two step process. The first step is to read a data area using the opcode IN as given blow.
C *Lock In W@Dtaara
As soon as this statement is executed, the variable/s is/are populated with the data area value. Here it's important to note that the first factor '*LOCK' is required to maintain data integrity and hence can not be omitted. The exception to this rule is the local data area where no lock is required.
The second step would to unlock the data area. If you are using a data area for read only purpose, you need to unlock the data area using the opcode Unlock as given blow.
CL0N01Factor1+++++++Opcode&ExtFactor2++
C Unlock W@DtaAra
Other way to unlock a data area will be to update it. To update a data area we use the opcode 'OUT'. The opcode OUT updates the date area with the current value of the RPG variable/Data structure. The syntax to update the data area is as given below.
CL0N01Factor1+++++++Opcode&ExtFactor2+
C Out W@DtaAra
The OUT opcode requires a lock on the data area except in case of local data area where it's not required to take a lock beforehand.
That's all about data area processing in RPG IV. Now refer the examples given below to become more intimate with data area processing in RPG programs.
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++
DW@DtaAra DS DTAARA(EMP_INFO)
DW@Emp# 1 2 0
DW@EmpName 3 10
**
CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result++++
C *Lock In W@Dtaara
C W@Emp# Dsply
C W@EmpName Dsply
C Unlock W@DtaAra
**
C Return
Output:-
DSPLY 1
DSPLY TutoIndi
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++
DW@DtaAra DS DTAARA(EMP_INFO)
DW@Emp# 1 2 0
DW@EmpName 3 10
**
CL0N01Factor1+++++++Opcode&ExtFactor2+++++++Result+++++
C *Lock In W@Dtaara
C W@Emp# Dsply
C W@EmpName Dsply
**
C Eval W@Emp# +=1
C Out W@DtaAra
**
C W@Emp# Dsply
C W@EmpName Dsply
C Return
Output:-
DSPLY 1
DSPLY TutoIndi
DSPLY 2
DSPLY TutoIndi
The above program shows how to update specific portion of a data area. 'OUT' opcode in itself updates all values of the data area. However, with judicious use of data structure we accomplish partial update.
Comments and feedbacks are most welcome (Link at the end). If you have any doubts/questions, feel free to ask it in the forum.
Recent comments
5 days 1 hour ago
5 days 2 hours ago
5 days 2 hours ago
5 days 4 hours ago
5 days 15 hours ago
5 days 17 hours ago
5 days 18 hours ago
5 days 19 hours ago
6 days 8 hours ago
6 days 18 hours ago