| 自由形式構文 | (許可されていない - %OCCUR 組み込み関数を使用) |
| コード | 演算項目 1 | 演算項目 2 | 結果フィールド | 標識 | ||
|---|---|---|---|---|---|---|
| OCCUR (E) | オカレンス値 | データ構造 | オカレンス値 | _ | ER | _ |
OCCUR 命令コードは、RPG IV プログラムの中で次に使用されるデータ構造のオカレンスを指定します。
OCCUR 命令は、プログラムの中で次に使用される複数オカレンス・データ構造の オカレンスを設定します。 一度に使用できるオカレンスは 1 つだけです。 操作に複数オカレンス・データ構造またはそのデータ構造のサブフィールドが指定 されている場合には、OCCUR 命令が指定されるまで、そのデータ構造の最初の オカレンスが使用されます。 OCCUR 命令が指定されると、その OCCUR 命令によって設定されたデータ構造の オカレンスが使用されます。
演算項目 1 は任意指定です。指定する場合には、数値、小数点以下の桁数が ゼロのリテラル、フィールド名、名前のついた定数、またはデータ構造名 を入れることができます。 演算項目 1 は、OCCUR 命令の実行中に演算項目 2 に指定されたデータ構造のオ カレンスを設定するために使用されます。 演算項目 1 がブランクの場合には、 OCCUR 命令の実行中に演算項目 2 の現在のデータ構造のオカレンスの値が結果 フィールドに入れられます。
演算項目 1 がデータ構造名の場合には、複数オカレンス・データ構造でなければ なりません。 演算項目 1 の現在のデータ構造のオカレンスが、演算項目 2 のデ ータ構造のオカレンスを設定するために使用されます。
演算項目 2 は必須で、複数オカレンス・データ構造の名前でなければなりません。
結果フィールドは任意指定です。指定する場合には、小数点以下の桁数がゼ ロの数値フィールド名でなければなりません。 OCCUR 命令の実行中に、演算項目 2 に指定された現在のデータ構造のオカレン スの値が (演算項目 1 に任意に指定された値またはデータ構造によって設定 された後で) 結果フィールドに入れられます。
少なくとも演算項目 1 または結果フィールドのいずれかを指定しなければな りません。
オカレンスがデータ構造について設定された有効な範囲外の場合には、エラーが 発生し、演算項目 2 のデータ構造のオカレンス は、OCCUR 命令が処理される前と同じままで変更されません。
OCCUR 例外 (プログラム状況コード 122) を 処理するために、命令コード拡張 'E' またはエラー標識 ER を指定できますが、 両方を指定することはできません。 エラー処理の詳細については、プログラム例外/エラーを 参照してください。
複数オカレンス・データ構造がインポートまたはエクスポートされる場合には、 現在のオカレンスについての情報はインポートまたはエクスポートされません。 詳細については、EXPORT{(外部名)}および IMPORT{(外部名)}キーワードを参照してください。

*...1....+....2....+....3....+....4....+....5....+....6....+....7....+....
DName+++++++++++ETDsFrom+++To/L+++IDc.Keywords+++++++++++++++++++++++++++
*
* DS1 および DS2 は複数オカレンス・データ構造です。
* それぞれのデータ構造のオカレンス数は 50 です。
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 :