3
votes

There is an inline-declared table generated from a SELECT statement as below:

SELECT *
INTO TABLE @DATA(lt_result)
FROM scarr.

How can lt_result be assigned to a Field Symbol?

I tried the following way:

  FIELD-SYMBOLS: <fs_lt_result> TYPE ANY.

  LOOP AT lt_result ASSIGNING <fs_lt_result>.

But I'm not able to call any components in the field symbol inside the loop like:

WRITE / <fs_lt_result>-carrid.

(syntax error: The data object "<FS_LT_RESULT>" does not have a structure and therefore does not have a component called "CARRID".)

3

3 Answers

6
votes

Inline declaration of a field symbol for an internal table in an ASSIGN statement and inline declaration of a field symbol for the rows of the table in a LOOP.

LOOP AT <lt_result> ASSIGNING FIELD-SYMBOL(<line>).
  ...
ENDLOOP. 

source: https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abenfield-symbol_inline.htm

1
votes

You declared that the field symbol is of unknown type (ANY, i.e. the exact type is known only at run time), so the compiler can't be sure that the mentioned component (CARRID) exists, hence the syntax error.

If you want to mention a component statically, the compiler must be informed of the exact type (what components exist).

For instance, this would work:

SELECT *
    INTO TABLE @DATA(lt_result)
    FROM scarr.

FIELD-SYMBOLS: <fs_lt_result> TYPE scarr.

LOOP AT lt_result ASSIGNING <fs_lt_result>.
  WRITE / <fs_lt_result>-carrid.
ENDLOOP.

Or use the inline declaration of the field symbol as you proposed in your own answer/solution:

LOOP AT lt_result ASSIGNING FIELD-SYMBOL(<fs_lt_result>).

NB: if your internal table had a type decided at run time only, it would be impossible to indicate statically the component name and you'd need to refer to the component dynamically:

ASSIGN COMPONENT ('CARRID') OF STRUCTURE <fs_lt_result> TO FIELD-SYMBOL(<field>).
IF sy-subrc = 0.
  WRITE / <field>.
ENDIF.
-3
votes

EXAMPLE: FIELD SYMBOL : TYPE VBAK. DATA: ITAB TYPE TABLE OF VBAK .

SELECT * FROM VBAK INTO TABLE ITAB UP TO 10 ROWS.

LOOP AT ITAB ASSIGNING WRITE : / -VBELN . (WRITE WHICH OUTPUT YOU WANT TO DISPLAY) ENDLOOP.