简体   繁体   中英

How to loop at a dynamic internal table?

I'm working on an Enhancement Implantation on ZXMBCU10 which is called in a custom program couple of levels down the execution path. Inside ZXMBCU10 I want to access the a table in the parent program, which I do in the following method;

  1. Declare the parent program name;

    DATA: ex_tbl_name TYPE char100 VALUE '(ZPROGRAM)G_TAB'.

  2. Getting the value through field symbol assignment.

    FIELD-SYMBOLS: <fs> TYPE any.

    ASSIGN (ex_tbl_name) TO <fs>.

Then I check for successful assignment (which is true).

IF <fs> IS ASSIGNED.

访问父表

Problem I have is how to read the data in the <fs> field symbol.

I've tried LOOP and READ TABLE, but getting the following;

循环和读取表示例

Both Read Table and Loop is added here just to get the syntax checks

LOOP;

Internal table " <FS> " has no header line - one of the additions "INTO wa", "ASSIGNING", "REFERENCE INTO", "TRANSPORTING NO FIELDS" required. required.

READ TABLE;

You cannot use explicit or implicit index operations on tables with types "HASHED TABLE" or "ANY TABLE". " <FS> " has the type "ANY TABLE". It is possible that the "TABLE" addition was not specified before " <FS> ".

LOOP AT

The error about LOOP AT ( Internal table " <FS> " has no header line - one of the additions "INTO wa", "ASSIGNING", "REFERENCE INTO", "TRANSPORTING NO FIELDS" required ), is that you don't indicate the "result" part of LOOP AT ie ASSIGNING, REFERENCE INTO... (as said in the message).

For a field symbol, LOOP AT alone is always invalid, and if it's a variable instead of a field symbol it's obsolete because that would imply the use of a header line.

LOOP AT <fs>. " always invalid !

A valid syntax could be as follows: you must declare the field symbol as being an internal table (with at least the word TABLE , or refer to a "Table Type"), any category of internal table is supported for LOOP AT (hashed, sorted, standard), so you can use TYPE ANY TABLE :

DATA: ex_tbl_name TYPE char100 VALUE '(ZPROGRAM)G_TAB'.
FIELD-SYMBOLS: <fs> TYPE ANY TABLE.

ASSIGN (ex_tbl_name) TO <fs>.
LOOP AT <fs> ASSIGNING FIELD-SYMBOL(<line>).
ENDLOOP.

READ TABLE

The error about READ TABLE ( You cannot use explicit or implicit index operations on tables with types "HASHED TABLE" or "ANY TABLE". " <FS> " has the type "ANY TABLE". It is possible that the "TABLE" addition was not specified before " <FS> " ) is that you used READ TABLE ... INDEX ... whose INDEX means that it can only be used with an internal table with category SORTED or STANDARD .

The next code is invalid because of the combination of ANY TABLE and READ TABLE INDEX, because <FS> could eventually be a hashed internal table (who knows), then READ TABLE INDEX would fail, hence the compiler error:

DATA: ex_tbl_name TYPE char100 VALUE '(ZPROGRAM)G_TAB'.
FIELD-SYMBOLS: <fs> TYPE ANY TABLE. " <=== impossible with READ TABLE INDEX !

ASSIGN (ex_tbl_name) TO <fs>.
READ TABLE <fs> ASSIGNING FIELD-SYMBOL(<line>) INDEX 1. " <=== impossible with ANY TABLE !

Solution: to use READ TABLE <fs> INDEX ... you may declare the field-symbol as SORTED, STANDARD, or INDEX (the latter is a generic name corresponding to SORTED and STANDARD).

This code is valid:

DATA: ex_tbl_name TYPE char100 VALUE '(ZPROGRAM)G_TAB'.
FIELD-SYMBOLS: <fs> TYPE INDEX TABLE.

ASSIGN (ex_tbl_name) TO <fs>.
READ TABLE <fs> ASSIGNING FIELD-SYMBOL(<line>) INDEX 1.

Of course, it's assumed that G_TAB is an "index" table, not a hashed table!

PS: in your code you used INTO DATA(lv_fs) but usually if you have a generic internal table ASSIGNING is preferred.

change field symbol type to

any table.

instead of:

any.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM