Skip to Content
avatar image
Former Member

ASSIGN COMPONENT ... OF STRUCTURE with dynamic work area

Here's a beginner's question.

The background: I'm getting data from on a table in client 000 via RFC_READ_TABLE. Now I'd like to append each line from that table to an internal table. That internal table is a field symbol <ttable_000> typed as ANY STANDARD TABLE, because I'm looping over a list of unknown tables.

Consequently, the work area <wa_ttable> I'm trying to fill and then append to <ttable_000> is also a field symbol:

 CREATE DATA lo_data_structure LIKE LINE OF <ttable_000> .
  ASSIGN lo_data_structure TO <wa_ttable>.

Now here's my code to fill the internal table <ttable_000> with the data from the table in client 000:

FIELD-SYMBOLS: <column> TYPE any.

  DATA: index           TYPE i VALUE 0,
        fieldname_000   TYPE string,
        fieldlength_000 TYPE i,
        fieldoffset_000 TYPE i,
        wa_data_000     LIKE LINE OF data_000,
        wa_fields_000   LIKE LINE OF fields_000.

LOOP AT data_000 INTO wa_data_000.
    CLEAR index.
    LOOP AT fields_000 INTO wa_fields_000.
      fieldname_000 = wa_fields_000-fieldname.
      fieldlength_000 = wa_fields_000-length.
      fieldoffset_000 = wa_fields_000-offset.
      index = index + 1.
      ASSIGN COMPONENT index OF STRUCTURE <wa_ttable> TO <column>.
      <column> = wa_data_000-wa+fieldoffset_000(fieldlength_000). <br>
    ENDLOOP.
    INSERT <wa_ttable> INTO TABLE <ttable_000>.

ENDLOOP.

When I now stop at this line in the debugger:

<column> = wa_data_000-wa+fieldoffset_000(fieldlength_000)

I notice the <column> field symbol has not been assigned, so I get a dump after that line.

What am I missing? It seems the ASSIGN COMPONENT ... OF STRUCTURE statement should assign the field symbol properly.

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Best Answer
    Nov 23, 2016 at 10:43 PM

    Hello Martin,

    If I'm correct in assessing your requirements, you could make further use of the ABAP RTTS-functionalities ...

    Please see the below Code-Sample and try it out :

    "AS A DEMO Fetch data that normally comes from the RFC ...
    DATA your_rfc_tab TYPE STANDARD TABLE OF mara.
    
    REFRESH your_rfc_tab.
    SELECT * UP TO 100 ROWS
      FROM mara
      INTO TABLE your_rfc_tab.
    IF sy-subrc <> 0.
      EXIT.
    ENDIF.
    
    DATA:
      lo_data           TYPE REF TO data,
      lo_rfc_tabdescr   TYPE REF TO cl_abap_tabledescr,
      lo_rfc_strucdescr TYPE REF TO cl_abap_structdescr,
      lo_rfc_elemdescr  TYPE REF TO cl_abap_elemdescr,
      lt_rfc_components TYPE cl_abap_structdescr=>component_table,
      lt_components     TYPE cl_abap_structdescr=>component_table.
    
    FIELD-SYMBOLS:
      <your_rfc_tab> TYPE STANDARD TABLE, "ANY TABLE,
      <value>        TYPE any.
    
    "Object-Reference
    GET REFERENCE OF your_rfc_tab INTO lo_data.
    ASSIGN lo_data->* TO <your_rfc_tab>.
    
    "Get Table-Struct-Components
    lo_rfc_tabdescr   ?= cl_abap_tabledescr=>describe_by_data( <your_rfc_tab> ).
    lo_rfc_strucdescr ?= lo_rfc_tabdescr->get_table_line_type( ).
    lt_rfc_components = lo_rfc_strucdescr->get_components( ).
    
    LOOP AT <your_rfc_tab> ASSIGNING FIELD-SYMBOL(<rfc_structure>).
      LOOP AT lt_rfc_components ASSIGNING FIELD-SYMBOL(<rfc_component>).
      "IF YOU NEED FIELDCATALOG-data ...
      "lo_rfc_elemdescr ?= cl_abap_elemdescr=>describe_by_name( <rfc_component>-type->get_relative_name( ) ).
    
        "Check NAME / ABSOLUTE / RELATIVE Name as Mapping-Source
        ASSIGN COMPONENT <rfc_component>-name OF STRUCTURE <rfc_structure> TO <value>.
        ASSIGN COMPONENT <rfc_component>-type->absolute_name+6 OF STRUCTURE <rfc_structure> TO <value>.
        ASSIGN COMPONENT <rfc_component>-type->get_relative_name( ) OF STRUCTURE <rfc_structure> TO <value>.
    
        IF <value> IS ASSIGNED.
          "DO YOUR THING ... 
          "Map with Table in Current System, Create New (Table)Type based on FieldCatalog-data, ...
        ENDIF.
    
      ENDLOOP.
      UNASSIGN <rfc_component>.
    ENDLOOP.
    UNASSIGN <rfc_structure>.


    I've tested this in this DEMO as well as in an RFC-scenario, it should work ...
    Via RTTS-functions you could also Dynamically create Structures and TableTypes to insert your <VALUE> accordingly in your (receiving) System if need be ...

    There's a lot of documentation on this, so please check the SAP Wiki / SAP Help Website / SAP Community Blogs ...

    Hope this helps you out ...

    Nic T.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Dec 14, 2016 at 09:08 PM

    Yes, that does indeed work. Many thanks! I have meanwhile found a solution on my own, strangely, sap.com did not alert me to your answer. For completeness sake, here's the code snippet that I'm currently using:

      DATA:
    
        obj_data   TYPE REF TO data,
    
        data_000   LIKE TABLE OF tab512,
    
        fields_000 LIKE TABLE OF rfc_db_fld.
    
    
    
      FIELD-SYMBOLS: <ttable_current> TYPE STANDARD TABLE,
    
                     <ttable_000>     TYPE STANDARD TABLE,
    
                     <wa_ttable>      TYPE any.
    
    
    
      CREATE DATA obj_data TYPE TABLE OF (wa_tablelist-tablename).
    
      CREATE DATA obj_data TYPE TABLE OF (wa_tablelist-tablename).
    
      ASSIGN obj_data->* TO <ttable_000>.
    
      FREE : obj_data.
    
      CREATE DATA obj_data TYPE (wa_tablelist-tablename).
    
      ASSIGN obj_data->* TO <wa_ttable>.
    
      FREE : obj_data.
    
    
    
    *get data from 000
    
      CALL FUNCTION 'RFC_READ_TABLE' DESTINATION 'NSP000'
    
        EXPORTING
    
          query_table          = wa_tablelist-tablename
    
        TABLES
    
          data                 = data_000
    
          fields               = fields_000
    
        EXCEPTIONS
    
          not_authorized       = 1
    
          data_buffer_exceeded = 2
    
          OTHERS               = 3.
    
    
    
      IF sy-subrc <> 0.
    
    
    
    *Implement error handling here
    
    
    
      ENDIF.
    
    
    
    *put data into table by getting length and string
    
      DATA: index         TYPE sy-tfill,
    
            wa_data_000   LIKE LINE OF data_000,
    
            wa_fields_000 LIKE LINE OF fields_000,
    
            columnvalue   TYPE string.
    
    
    
      FIELD-SYMBOLS: <column> TYPE any.
    
    
    
      DESCRIBE TABLE fields_000 LINES sy-tfill.
    
    
    
      IF sy-subrc IS INITIAL.
    
        index = sy-tfill.
    
      ENDIF.
    
    
    
      LOOP AT data_000 INTO wa_data_000.
    
        DO index TIMES.
    
          READ TABLE fields_000 INTO wa_fields_000 INDEX sy-index.
    
          ASSIGN COMPONENT wa_fields_000-fieldname OF STRUCTURE <wa_ttable> TO <column>.
    
          IF <column> IS ASSIGNED AND sy-subrc IS INITIAL.
    
            MOVE :  wa_data_000-wa+wa_fields_000-offset(wa_fields_000-length) TO <column>.
    
          ENDIF.
    
        ENDDO.
    
        APPEND <wa_ttable> TO <ttable_000>.
    
        CLEAR wa_data_000-wa.
    
      ENDLOOP.
    
    
    Add comment
    10|10000 characters needed characters exceeded