Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Dinamically choose field of internal table

Former Member
0 Kudos

Hello

Lets assume internal table has fields F1, F2, F3 of the same length and type.

Is it posible to dynamically assign choose a field of the table like

Field = 'Field1'

Loop at itab assigning <FS>.

<FS>-FIELD = 100.

Endloop.

1 ACCEPTED SOLUTION

vinod_vemuru2
Active Contributor
0 Kudos

Hi,

With slight modification to your code it is possible.check below sample code.

TABLES vbak.

PARAMETERS: po_1(10) TYPE c .

DATA: l_ref TYPE REF TO DATA.

FIELD-SYMBOLS: <vbak> TYPE ANY,
               <temp> TYPE ANY.

CREATE DATA l_ref LIKE vbak.
ASSIGN l_ref->* TO <vbak>.
<vbak> = '10012345'.
"po_1 has value VBELN
ASSIGN COMPONENT po_1 OF STRUCTURE <vbak> TO <temp>.
WRITE: /1 <temp>.
<temp> = '54321'.

WRITE:/1  <temp>. "At this point <vbak>-vbeln  also has the same value(54321) 

Thanks,

Vinod.

3 REPLIES 3

former_member226239
Contributor
0 Kudos

try the following:

field_symbols: <fs_any> type any.

loop at itab assigning <fs>

assign component Field of structure <fs> to <fs_any>. " <fs_any> will have Field1

<fs_any> = '100'.

endloop.

vinod_vemuru2
Active Contributor
0 Kudos

Hi,

With slight modification to your code it is possible.check below sample code.

TABLES vbak.

PARAMETERS: po_1(10) TYPE c .

DATA: l_ref TYPE REF TO DATA.

FIELD-SYMBOLS: <vbak> TYPE ANY,
               <temp> TYPE ANY.

CREATE DATA l_ref LIKE vbak.
ASSIGN l_ref->* TO <vbak>.
<vbak> = '10012345'.
"po_1 has value VBELN
ASSIGN COMPONENT po_1 OF STRUCTURE <vbak> TO <temp>.
WRITE: /1 <temp>.
<temp> = '54321'.

WRITE:/1  <temp>. "At this point <vbak>-vbeln  also has the same value(54321) 

Thanks,

Vinod.

Former Member
0 Kudos

Hi

the following code will pick fields dynamically and table and also sort.


PARAMETERS dbtab TYPE c LENGTH 30.

SELECT-OPTIONS columns FOR dbtab NO INTERVALS.

DATA: otab  TYPE abap_sortorder_tab,
      oline TYPE abap_sortorder,
      dref  TYPE REF TO data.

FIELD-SYMBOLS: <column> LIKE LINE OF columns,
               <itab> TYPE STANDARD TABLE,
               <fs> type any,
               <comp> type any
               .
set pf-status '100'.
TRY.
    CREATE DATA dref TYPE STANDARD TABLE OF (dbtab).
    ASSIGN dref->* TO <itab>.
  CATCH cx_sy_create_data_error.
    MESSAGE 'Wrong data type!' TYPE 'I' DISPLAY LIKE 'E'.
    LEAVE PROGRAM.
ENDTRY.
TRY.
    SELECT *
           FROM (dbtab)
           INTO TABLE <itab>.
  CATCH cx_sy_dynamic_osql_semantics.
    MESSAGE 'Wrong database table!' TYPE 'I' DISPLAY LIKE 'E'.
    LEAVE PROGRAM.
ENDTRY.

LOOP AT columns ASSIGNING <column>.
  oline-name = <column>-low.
  oline-descending = 'X'.
  APPEND oline TO otab.
ENDLOOP.

TRY.
    SORT <itab> BY (otab).
  CATCH cx_sy_dyn_table_ill_comp_val.
    MESSAGE 'Wrong column name!' TYPE 'I' DISPLAY LIKE 'E'.
    LEAVE PROGRAM.
ENDTRY.
loop at <itab> assigning <fs>.
do.
  ASSIGN COMPONENT sy-index OF STRUCTURE <fs> TO <comp>.
  WRITE: <comp>.
  if sy-subrc ne 0.
exit.
endif.

ENDdo.
new-line.
endloop.

I hope it helped you.

Regards and Best wishes.