OCCUR(设置/获取数据结构的出现)
| 自由格式语法 | (不允许-使用 %发生 内置函数) |
| 代码 | 因子1 | 因子2 | 结果字段 | 指标 | ||
|---|---|---|---|---|---|---|
| 出现 (E) | 出现值 | 数据结构 | 出现值 | _ | ER | _ |
实例操作码指定在 RPG IV 程序中接下来要使用的数据结构的实例。
实例操作确定在程序中下次使用多实例数据结构的实例。 一次只能使用一个实例。 如果在操作中指定了具有多次出现的数据结构或该数据结构的子字段,那么将使用第一次出现的数据结构,直到指定了有所发生的操作为止。 指定发生操作后,将使用通过发生操作建立的数据结构的出现。
因子 1 是可选的; 如果指定了该因子,那么它可以包含数字,零小数位文字,字段名,命名常量或数据结构名。 在生成操作期间,将使用因子 1 来设置在因子 2 中指定的数据结构的出现。 如果因子 1 为空,那么因子 2 中当前出现的数据结构的值将在在场操作期间放在结果字段中。
如果因子 1 是数据结构名,那么它必须是多次出现的数据结构。 因子 1 中数据结构的当前实例用于设置因子 2 中数据结构的实例。
"因子 2" 是必需的,并且必须是多实例数据结构的名称。
结果字段是可选的; 如果指定了此字段,那么它必须是没有小数位的数字字段名称。 在发生操作期间,因子 2 中指定的数据结构当前出现的值 (由因子 1 中可选指定的任何值或数据结构设置后) 将放置在结果字段中。
必须至少指定因子 1 或结果字段中的一个。
如果出现在数据结构的有效范围集之外,那么会发生错误,并且因子 2 中数据结构的出现与处理 OCCURS 操作之前相同。
要处理发生异常 (程序状态码 122) ,可以指定操作码扩展器 "E" 或错误指示符 ER ,但不能同时指定两者。 有关错误处理的更多信息,请参阅 程序异常/错误。
导入或导出多实例数据结构时,不会导入或导出有关当前实例的信息。 请参阅 EXPORT{(external_name)} 和 IMPORT{(external_name)} 关键字以获取更多信息。

*...1....+....2....+....3....+....4....+....5....+....6....+....7....+....
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++
*
* DS1 and DS2 are multiple occurrence data structures.
* Each data structure has 50 occurrences.
D DS1 DS OCCURS(50)
D FLDA 1 5
D FLDB 6 80
*
D DS2 DS OCCURS(50)
D FLDC 1 6
D FLDD 7 11
*...1....+....2....+....3....+....4....+....5....+....6....+....7....+....
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq....
* DS1 is set to the third occurrence. The subfields FLDA
* and FLDB of the third occurrence can now be used. The MOVE
* and Z-ADD operations change the contents of FLDA and FLDB,
* respectively, in the third occurrence of DS1.
C
C 3 OCCUR DS1
C MOVE 'ABCDE' FLDA
C Z-ADD 22 FLDB
*
* DS1 is set to the fourth occurrence. Using the values in
* FLDA and FLDB of the fourth occurrence of DS1, the MOVE
* operation places the contents of FLDA in the result field,
* FLDX, and the Z-ADD operation places the contents of FLDB
* in the result field, FLDY.
C
C 4 OCCUR DS1
C MOVE FLDA FLDX
C Z-ADD FLDB FLDY
*
* DS1 is set to the occurrence specified in field X.
* For example, if X = 10, DS1 is set to the tenth occurrence.
C X OCCUR DS1
*
* 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
*
* The value of the current occurrence of DS1 is placed in the
* result field, Z. Field Z must be numeric with zero decimal
* positions. For example, if the current occurrence of DS1
* is 15, field Z contains the value 15.
C OCCUR DS1 Z
C
* DS1 is set to the current occurrence of DS2. The value of the
* current occurrence of DS1 is then moved to the result field,
* Z. For example, 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.
C
C DS2 OCCUR DS1 Z
*
* DS1 is set to the current occurrence of X. For example, if
* X = 15, DS1 is set to the fifteenth occurrence.
* If X is less than 1 or is greater than 50,
* an error occurs and %ERROR is set to return '1'.
* If %ERROR returns '1', the LR indicator is set on.
C
C X OCCUR (E) DS1
C IF %ERROR
C SETON LR
C ENDIF*...1....+....2....+....3....+....4....+....5....+....6....+....7....+....
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++++++++++++++++++
*
* Procedure P1 exports a multiple occurrence data structure.
* Since the information about the current occurrence is
* not exported, P1 can communicate this information to
* other procedures using parameters, but in this case it
* communicates this information by exporting the current
* occurrence.
*
D EXP_DS DS OCCURS(50) EXPORT
D FLDA 1 5
D NUM_OCCUR C %ELEM(EXP_DS)
D EXP_DS_CUR S 5P 0 EXPORT
*
*...1....+....2....+....3....+....4....+....5....+....6....+....7....+....
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq.
*
* Loop through the occurrences. For each occurrence, call
* procedure P2 to process the occurrence. Since the occurrence
* number EXP_DS_CUR is exported, P2 will know which occurrence
* to process.
*
C DO NUM_OCCUR EXP_DS_CUR
C EXP_DS_CUR OCCUR EXP_DS
C :
C CALLB 'P2'
C ENDDO
C :
*...1....+....2....+....3....+....4....+....5....+....6....+....7....+....
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords++++++++++++++++++++++++++
*
* Procedure P2 imports the multiple occurrence data structure.
* The current occurrence is also imported.
*
D EXP_DS DS OCCURS(50) IMPORT
D FLDA 1 5
D EXP_DS_CUR S 5P 0 IMPORT
*
*...1....+....2....+....3....+....4....+....5....+....6....+....7....+....
CL0N01Factor1+++++++Opcode(E)+Factor2+++++++Result++++++++Len++D+HiLoEq.
*
* Set the imported multiple-occurrence data structure using
* the imported current occurrence.
*
C EXP_DS_CUR OCCUR EXP_DS
*
* Process the current occurrence.
C :