A data area can be used whenever you need to store information of limited size, independent of the existence of procedures or files. Typical uses of data areas are:

  • To provide an area (perhaps within each job’s QTEMP library) to pass information within a job.
  • To provide a field that is easily and frequently changed to control references within a job, such as:
    • Supplying the next order number to be assigned
    • Supplying the next check number
    • Supplying the next save/restore media volume to be used
  • To provide a constant field for use in several jobs, such as a tax rate or distribution list.
  • To provide limited access to a larger process that requires the data area. A data area can be locked to a single user, thus preventing other users from processing at the same time.

Commands associated with data areas:

  • CHGDTAARA
  • CRTDTAARA
  • DLTDTAARA
  • DSPDTAARA
  • RTVDTAARA

RPG Code example
[cc lang=”php”]
*=====================================================
*——————————————————–
*
* Variable Definition
*
d CmdLength s 15 5 inz(0)
d CmdString s 256 inz(*blanks)
d reply s 1

//
// external calls
//

d $command pr extpgm(‘QCMDEXC’)
d command 5000 options(*varsize)
d Length 15 5

d program21 ds DTAARA(‘MYTWENTY1’)
d mytwenty1 21

/Free

//——————————————————–
// MAIN PROGRAM
//——————————————————–

cmdstring =
‘CRTDTAARA DTAARA(QTEMP/MYTWENTY1) TYPE(*CHAR) LEN(21)’;
cmdlength = %len(%trim(cmdstring));
monitor;
$command (cmdstring:cmdlength);
on-error;
endmon;

// populate it ….

*in99 = *on;
dow *in99 = *on;
in(e) *lock Program21;
*in99 = %error;
enddo;

Program21 = ‘Holy Crap Batman!’;
out Program21;

in Program21;
dsply program21 reply;

// now clear it ….

*in99 = *on;
dow *in99 = *on;
in(e) *lock Program21;
*in99 = %error;
enddo;
clear Program21;

out program21;

in Program21;

dsply program21 reply ;

// re-populate it ….

*in99 = *on;
dow *in99 = *on;
in(e) *lock Program21;
*in99 = %error;
enddo;

Program21 = ‘Lets try this again!’;
out Program21;
in Program21;
dsply program21 reply;

clear MyTwenty1;

*inlr = *on;
//——————————————————–
// *inzsr – initial one time subroutine
//——————————————————–

begsr *inzsr;

endsr;

//——————————————————–
/End-Free
[/cc]

Using QXXCHGDA API
[cc lang=”php”]
D ChgDtaAra PR ExtPgm(‘QXXCHGDA’)
D prQualDtaAra LikeDS(qualdtaara) Const
D prStart Like(startpos)
D prLength Like(length)
D prNewData *

D QualDtaAra DS Qualified
D Name 10a Inz(‘TESTARA’)
D Library 10a Inz(‘TESTLIB’)
D StartPos S 10i 0 Inz(1)
D Length S 10i 0 Inz(4)
D ptrNewData S * Inz(%Addr(newdata))
D NewData S 2000a Inz(‘Test’)

************************************************************************************
*inlr = *on;
ChgDtaAra(qualdtaara : startpos : length : ptrnewdata);

[/cc]

all free format data area definition
[cc lang=”php”]
dcl-s myDtaara char(200) dtaara(dtaaraName);
dcl-s dtaaraName char(21);

dtaaraName = whatever …
in *lock myDtaara;
out myDtaara;
[/cc]

Another RPG example

DDS for table

[cc lang=”php”]
* file
A R ##FILER
A F1TEXT1 10 TEXT(‘Text-1’)
A F1TEXT2 10 TEXT(‘Text-2’)
A F1TEXT3 10 TEXT(‘Text-3’)
A F1NUM1 5 0 TEXT(‘Number-1’)
A F1DATE L DATFMT(*USA)
A K F1DATE
[/cc]

RPG Sample
[cc lang=”php”]
F##FILE uf a e k disk
//————————– ##FILE ————————-
// A R ##FILER
// A F1TEXT1 10 TEXT(‘Text-1’)
// A F1TEXT2 10 TEXT(‘Text-2’)
// A F1TEXT3 10 TEXT(‘Text-3’)
// A F1NUM1 5 0 TEXT(‘Number-1’)
// A F1DATE L DATFMT(*USA)
// A K F1DATE
//———————————————————–
*
* Variable Definition
*
d CmdLength s 15 5 inz(0)
d CmdString s 256 inz(*blanks)
d foundrecord s n
d indataarea s 20
d inDtaAra s 10
d inLibrary s 10
d reply s 1

d MyTwenty1DS ds 21 dtaara(‘MYTWENTY1’)
d Program21 21

d DtaAraRcv ds
d AraBytes 10i 0
d AraBytesOut 10i 0
d AraDtaType 10a
d AraLibrary 10a
d AraLength 10i 0
d AraDecimals 10i 0
d AraValue 2000a

d APIError ds Qualified
d BytesP 10i 0 inz(%size(apiError))
d BytesA 10i 0 inz(0)
d Messageid 7
d Reserved 1
d messagedta 240

//
// external calls
//

d $command pr extpgm(‘QCMDEXC’)
d command 5000 options(*varsize)
d Length 15 5

d $GetData pr extpgm(‘QWCRDTAA’)
d thedata like(DtaAraRcv)
d thedatasize 10i 0 const
d libDtaara 20 const
d start 10i 0 const
d length 10i 0 const
d Error Like(ApiError)

// automatically qualified by datastructure name
d beforeDS ds LIKEREC(##FILER : *INPUT)
d afterDS ds LIKEREC(##FILER : *OUTPUT)
d KeyDS ds LIKEREC(##FILER : *KEY)

/free

//——————————————————–
// MAIN PROGRAM
//——————————————————–

// this allows me to read from a file
// keep a copy of the before record in datastructure BeforeDS
// keep a copy of the after record in datastructure AfterDS
// and update the table using the datastructure.
// only field changed was Text2…

read ##FILE beforeDS;
//move the fields
afterDS = beforeDS;

// increment the number field by 1
AfterDS.F1Num1 += 1;
// update the first record
update ##FILER afterDS;

// text2 update with dec field
AfterDS.F1text2 = ‘Counter:’ + %char(AfterDS.F1Num1);
// write a new record using datastructure
write ##FILER afterDS;

reset foundrecord;
keyds.F1date = %date();
setll %kds(KEYDS) ##FILER;
if %equal;
foundrecord = *on;
endif;

// more dataarea code

cmdstring = ‘CRTDTAARA DTAARA(QTEMP/MYTWENTY1) TYPE(*CHAR)’ +
‘ LEN(21)’;
cmdlength = %len(%trim(cmdstring));
monitor;
$command (cmdstring:cmdlength);
on-error;
endmon;

// populate it ….

*in99 = *on;
dow *in99 = *on;
in(e) *lock MyTwenty1DS;
*in99 = %error;
enddo;

Program21 = ‘Holy Crap Batman!’;
out MyTwenty1DS;
Unlock MyTwenty1DS;

in MyTwenty1DS; // no lock
dsply program21 reply;

// now clear it ….

*in99 = *on;
dow *in99 = *on;
in(e) *lock MyTwenty1DS ;
*in99 = %error;
enddo;
clear Program21;
out MyTwenty1DS;

in MyTwenty1DS;

dsply program21 reply ;

// re-populate it ….

*in99 = *on;
dow *in99 = *on;
in(e) *lock MyTwenty1DS;
*in99 = %error;
enddo;

Program21 = ‘Lets try this again!’;
out MyTwenty1DS;
in MyTwenty1DS;
dsply program21 reply;

// use API QWCRDTAA to retrieve data from dataarea
exsr $QWCRDTAA;

*inlr = *on;

//——————————————————–
// $QWCRDTAA – read dataarea with API
//——————————————————–

begsr $QWCRDTAA;

// these could be parameters nice if you need to access multiple
// dataareas in multiple libraries.
inDtaAra = ‘MYTWENTY1’;
inLibrary = ‘QTEMP’;

if InDtaAra = ‘*GDA’ or
InDtaAra = ‘*LDA’ or
InDtaAra = ‘*PDA’;
%subst(InDataArea:1:10) = InDtaARa;
%subst(indataarea:11:10) = *blanks;
else;
indataarea = inDtaAra + inlibrary;
endif;

$getData(DtaAraRcv :
%Size(DtaAraRcv):
InDataArea :
-1 :
512 :
ApiError );

dsply %subst(AraValue:1:25) reply;

endsr;
//——————————————————–

/end-free
[/cc]

Reading data area using QWCRDTAA
[cc lang=”php”]
dQWCRDRTN DS
d QWCBAVL 10i 0 Qwc Rdtaa Data
d QWCBRTN 10i 0 Bytes Available
d QWCTVRTN 10 Bytes Returned
d QWCLIBN 10 Type Value Return
d QWCLVRTN 10i 0 Library Name
d QWCNBRDP 10i 0 Length returned
d QWCVALUE 475 Number Decimal
//
//
//
dQUSEC DS
d QUSBPRV 10i 0
d QUSBAVL 10i 0
d QUSEI 7
d QUSERVED 1
d QUSED01 1

drcvvarsiz s 10i 0 inz(%size(QWCRDRTN))
ddtaaraname s 20 inz(‘IN1BSIN LBIFIL ‘)
dstrpos s 10i 0 inz(-1)
dstrlen s 10i 0 inz(%size(QWCVALUE))

//
// Required Parameter Group:
//
// 1 Receiver variable Output Char(*)
// 2 Length of receiver variable Input Binary(4)
// 3 Qualified data area name Input Char(20)
// 4 Starting position Input Binary(4)
// 5 Length of data Input Binary(4)
// 6 Error code I/O Char(*)
//

d $getdataarea pr extpgm(‘QWCRDTAA’)
d recieveVar 475
d lengthRecvar 10i 0
d dataareaLib 20
d startpositon 10i 0
d lengthofInp 10i 0
d errorcode 17

//
// run the api and retrieve the data
// this is useful if you have same dataarea in *multiple
// libraries and need info from them all via RPG.
// just change the data in “dtaaraname” and rerun for
// other library/dataarea’s
//

/free
QUSBPRV = 0;
$getdataarea(QWCRDRTN :
rcvvarsiz :
dtaaraname :
strpos :
strlen :
QUSEC
);

// The variable QWCVALUE now contains the *DTAARA value
dsply %subst(QWCVALUE:1:30) ‘ ‘;
*inlr = *on;

/end-free
[/cc]

here is an example where the same data area is located in 5 different libraries
and you need to display all on once screen. (in my case the intranet)

[cc lang=”php”]
//
// Variable Definition
//
d buyslips s 5 0
d cmpcount s 3 0
d company s 2s 0
d companynm s 3
d dataname s 21
d inqslips s 5 0
d rcvvarsiz s 10i 0
d strpos s 10i 0 inz(-1)
d strlen s 10i 0 inz(475)

d $retrievedta pr extpgm(‘QWCRDTAA’)
d QWCRDRTN 512 const
d rcvvarsiz 10i 0 const
d dataname 20 const
d strpos 10i 0 const
d strlen 10i 0 const
d QUSEC 17 const

dQWCRDRTN DS
d QWCBAVL 10i 0 Qwc Rdtaa Data
d QWCBRTN 10i 0 Bytes Available
d QWCTVRTN 10 Bytes Returned
d QWCLIBN 10 Type Value Return
d QWCLVRTN 10i 0 Library Name
d QWCNBRDP 10i 0 Length returned
d QWCVALUE 475 Number Decimal

dQUSEC DS
d QUSBPRV 10i 0
d QUSBAVL 10i 0
d QUSEI 7
d QUSERVED 1
d QUSED01 1

/Free

//——————————————————–
// MAIN PROGRAM
//——————————————————–

for cmpcount = 1 to 5;
select;
when cmpcount = 1;
dataname = ‘DATAARA LIBRARY1’;
company = 15;
companynm = ‘LSA’;
when cmpcount = 2;
dataname = ‘DATAARA LIBRARY2’;
company = 72;
companynm = ‘GMC’;
when cmpcount = 3;
dataname = ‘DATAARA LIBRARY3’;
company = 20;
companynm = ‘HSA’;
when cmpcount = 4;
dataname = ‘DATAARA LIBRARY4’;
company = 18;
companynm = ‘LSN’;
when cmpcount = 5;
dataname = ‘DATAARA LIBRARY5’;
company = 30;
companynm = ‘LSI’;
endsl;

$retrievedta(QWCRDRTN :
rcvvarsiz :
dataname :
strpos :
strlen :
QUSEC
);
buyslips = %dec(%subst(QWCRDRTN:37:5):5:0);
inqslips = %dec(%subst(QWCRDRTN:42:5):5:0);
endfor;

*inlr = *on;

/End-Free
[/cc]

[wordpress_file_upload multiple=”false” fitmode=”responsive” captcha=”true” captchatype=”RecaptchaV2 (no account)” postlink=”true”]

Working with Data Areas
Tagged on: