Vous êtes sur la page 1sur 18

Data Structures

Session 10
RPG/400

Introduction
Data structures allow to define an area of space
within memory to store individual elements of data.
Purpose:
Subdividing fields into subfields
Restructure records into different layouts
Change field data types
Define character fields longer than 256 bytes
To add second dimension to arrays
Data structures can also be defined externally.
TYPES:
Simple Data Structure
Multiple occurrence data structure
Data Area Data structure
File information data structure
Program status data structure

Simple Data Structure


To define data structure specify DS in 24-25.
Follow the definition with subfield definitions.
From to positions indicate the subfields
positions on IPD prompt.
Example 1.
DNameDta
D Name
D Address
D City
D State
D PinCode
D Phone

DS
1
35
36 70
71 91
92 93
94 103
104 114

Instead of specifying absolute from and to


positions the length can be specified instead.
Example 2.
DNameDta
D Name
D Address
D City
D State
D PinCode
D Phone

DS
35
35
20
02
10
10

From..to and length notations can also be mixed


if required.
Fields that overlap can be specified with
overlapping positions.

Example 3.
DISODATE
DS
D YYYY
1
04
D Datsep
05
05
D MM
06
07
D Datsep
08
08
D DD
09
10
D YEAR
03
04
D Century
01
02
Or in positional form the same is accomplished using
OVERLAY keyword.
Example 4.
DISODATE
D YYYY
D Datsep
D MM
D Datsep
D DD
D YEAR
D Century

DS
04
01
02
01
02
02
02

OVERLAY(YYYY:3)
OVERLAY(YYYY:1)

Multiple occurrence data structure


Allows to store multiple sets of data structure fields.
Combines multiple set capabilities of array with data
structure capability to define individual fields.
OCCURS keyword in D spec defines the elements or
occurrences of the data structure.
Example 5.
FTESTFILE
IF
DSTUDENTS
D Name
D TESTSCORE

E
DS

DISK
OCCURS(50) INZ
LIKE(NAMEFLD)
LIKE(SCOREFLD)

Like keyword copies the data type and length from the
defined field variables of the file specified.

CODE

Factor 1

OCCUR

Occurrence Data
value
structure

Factor 2

Result
Field

INDICATORS

Occurrence
value

ER

Specifies the occurrence of the data structure that is to be used next within the
program.
Factor 1 is optional; value is used during the OCCUR operation to set the
occurrence of the data structure specified In factor 2.
If factor 1 is blank, the value of the current occurrence of the data structure in
factor 2 is placed in the result field during the OCCUR operation.
If factor 1 is a data structure name, it must be a multiple occurrence data
structure.
The current occurrence of the data structure in factor 1 is used to set the
occurrence of the data structure in factor 2.
Factor 2 is required and must be the name of a multiple occurrence data structure.
The result field is optional; if specified, it must be a numeric field name with no
decimal positions. During the OCCUR operation, the value of the current
occurrence of the data structure specified in factor 2, after being set by any value
or data structure that is optionally specified in factor 1, is placed in the result
field.At least one of factor 1 or the result field must be specified.
If the occurrence is outside the valid range set for the data structure, an error
occurs, and the occurrence of the data structure in factor 2 remains the same as
before the OCCUR operation was processed.

C
3
OCCUR DS1
C
MOVE
'ABCDE FLDA
C
Z-ADD
22
FLDB
---------------------------------------------------------* DS1 is set to the current occurrence of DS2. For example, if
* the current occurrence of DS2 is the twelfth occurrence, DSI
* is set to the twelfth occurrence.

C
DS2 OCCUR DS1
---------------------------------------------------------*if the current occurrence of DS2 is the fifth
* occurrence, DS1 is set to the fifth occurrence. The result
* field, Z, contains the value 5.

DS2

OCCUR

DS1 Z

Example 6.

Data area Data structures


Require U in column 23
Program will read/lock the data area named in the
data structure at the beginning of the program and
it will update/unlock the data area at the end of the
program
If data area name is not specified program uses
Local data area (*LDA).
Example 1
DCOMPANY
UDS
D CPYNAME
35
D CPYAddr
35
D CPYCity
21
D CpyState
2
D CpyPinCode
10

Example 2
DCOMPANY UDS
DATAARA(Company)
D CPYNAME
35
D CPYAddr
35
D CPYCity
21
D CpyState
2
D CpyPinCode 10
Example 3 same as example 1
DCOMPANY UDS
DATAARA(*LDA)
D CPYNAME
35
D CPYAddr
35
D CPYCity
21
D CpyState
2
D CpyPinCode 10
Types
1. Named data area data structure 2. Local data area data
structure.

Example 4. PGM
DChar10
UDS
DATAARA(Char10)
D Char3
3
D Num7
7
0
*Load data area contents into the program. Lock the
*data area for update. Add one to the numeric and write
*back OUT.
C
*LOCK
IN
Char10
C
Eval Num7 = Num7 + 1
C
OUT Char10
C
Eval *INLR = *on
C
Return

File information data structure


FMaintCust

CF

DSDisplay

DS

D FileName

Workstn

*File

DFileOpen

DFileEof

10

10

DMsgId

46

52

DSCRSize

INFDS(DSDisplay)

*SIZE

DKeyPress

369

369

DCursorloc

370

371B

INFDS keyword is specified in the F-spec


against the file declaration.

File Information Data Structure:


*FILE File Name
*RECORD
External file RECORD FORMAT
Program file Record ID
*OPCODE
*STATUS Error Code
*ROUTINE Name of the routine where
error occurred.

File Status codes


00000000130102101041012110121501218012210124101331-

No Error
Subfile Full
Duplicate Key Not allowed
Array/Table Sequence error
I/O operation to closed file
OPEN isued to file already open
Unable to allocate Record
Update without prior read
Record number not found
Wait time exceeded for read from work
station file.

Program Status Data structure


DProgStatus
DProgName
DErrmsgId
DErrMsg
DJobName
DUserId
DJobNumber

SDS
*PROC
40
91
244
254
264

S in position 23 is required for PSDS

46
169
253
263
269

Program status codes


00121- Invalid array index
00202 Called program failed
00211- Called program not found
00231 Called program Halt indicator On
00333- Error on DSPLY operation
00414- Not authorized to use data area
00415 Not authorized to update data area
00431- Data area preiusly locked
00907- Decimal data error
09999 System program exception error

Structure of PSDS
16-20
Previous status code
21-28
RPG Source sequence number
40-46
Exception Error ID MCH or CPF
201-208
Name of the file last used
244-253
Job Name
254-263
User name
264-269
Job Number
276-281 -System Date in Udate Format
288-293
Date Compiled
304-333
Sourcefile/library/member

Vous aimerez peut-être aussi