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: 

Read dynamic internal table - addressing certain field

Former Member
0 Kudos

Hi experts,

I'm new to a dynamic itabs. How do I read a value of a particular field from such a table?

I've found the below code by which I'm able to read a name of the field and later on the value. The problem is, that I need to address all the values from the line directly from the field - e.g. <dyn_wa>-kokrs.

LOOP AT <dyn_table> INTO <dyn_wa>. "ASSIGNING

LOOP AT fields_table ASSIGNING <field>.

CONCATENATE '<dyn_wa>' <field>-name INTO w_field

SEPARATED BY '-'.

ASSIGN (w_field) TO <aux>.

  • WRITE: <aux>.

APPEND <aux> TO itab_fin.

write:/ <dyn_wa>.

Is it possible or do I need to copy the values into a non-dynamic itab with predefined structure - because in this case I quite lose the biggest advantage of dynamic itabs;)

Thanks,

Petr

14 REPLIES 14

former_member555112
Active Contributor
0 Kudos

Hi,

Make use of ASSIGN COMPONENT Statement.

LOOP AT <dyn_table> INTO <dyn_wa>.
 LOOP AT fields_table ASSIGNING <field>.

 ASSIGN COMPONENT <field>-name OF STRUCTURE <dyn_wa>
to <AUX>.
if <aux> is assigned.
 APPEND <aux> TO itab_fin.
endif.
unassign <aux>.

ENDLOOP.
ENDLOOP.

Check this wiki for more details.

[http://wiki.sdn.sap.com/wiki/x/UYA_Bg]

Regards,

Ankur Parab

Former Member
0 Kudos

Refer this code

DATA: wa_konp LIKE LINE OF it_konp.

FIELD-SYMBOLS: <w_knumh> TYPE ANY.

DATA: wa_final TYPE ty_final.

FIELD-SYMBOLS:

<fl> TYPE ANY,

<fs_knumh> TYPE ANY.

*Looping and assigning each record in <FS> to <FS1>

LOOP AT <fs> ASSIGNING <fl>.

ASSIGN COMPONENT 'VKORG' OF STRUCTURE <fl> TO <fs_knumh>.

wa_final-vkorg = <fs_knumh>.

UNASSIGN <fs_knumh>.

ASSIGN COMPONENT 'MATNR' OF STRUCTURE <fl> TO <fs_knumh>.

wa_final-matnr = <fs_knumh>.

UNASSIGN <fs_knumh>.

ASSIGN COMPONENT 'DATAB' OF STRUCTURE <fl> TO <fs_knumh>.

wa_final-datab = <fs_knumh>.

UNASSIGN <fs_knumh>.

ASSIGN COMPONENT 'KSCHL' OF STRUCTURE <fl> TO <fs_knumh>.

wa_final-kschl = <fs_knumh>.

UNASSIGN <fs_knumh>.

ASSIGN COMPONENT 'KNUNH' OF STRUCTURE <fl> TO <fs_knumh>.

READ TABLE it_konp INTO wa_konp WITH KEY knumh = <fs_knumh>.

IF sy-subrc EQ 0.

*move-the data from wa_konp to it_final work area

  • wa_FINAL-KBETR = wa_KONP-KBETR.

wa_final-konwa = wa_konp-konwa.

wa_final-kpein = wa_konp-kpein.

wa_final-kmein = wa_konp-kmein.

wa_final-loevm_ko = wa_konp-loevm_ko.

  • FL_FINAL-VKORG = <FL>-VKORG.

ENDIF.

APPEND wa_final TO it_final.

CLEAR : wa_konp,<fl>,wa_final,<fs_knumh>.

ENDLOOP.

Former Member
0 Kudos

Hi ,

check this link and follow the syntax for dynamic internal table.

[http://www.sap-img.com/ab030.htm]

Please let me know if you still need any more help.

Thanks and regards,

Rajeshwar

0 Kudos

Thanks to all of you for quick answers.

Anyway non of them seems to solve my problem for 100%.

During the program run, my <dyn_table> values looks like this:

kokrs kostl datab datbi ktext

1 3190 0 20090101 99991231 aaaaaa

2 3190 10202 20090101 99991231 bbbbbbbbb

3 3190 10246 20090101 99991231 cccccccccccc

4 3190 10257 20090101 99991231 dddddddddddddd

Now I need to something like:

LOOP AT <dyn_table> TO <dyn_wa>.

<dyn_wa>-kokrs.

<dyn_wa>-ktext.

*....

ENDLOOP.

But as neither <dyn_table> nor <dyn_wa> have a header I can't address it like that.

Thanks for your patience.

Petr

0 Kudos

Hi,

Do like this.

Create a data object type reference to data.


Data : LV_OBJ type ref to data.

field-symbols: <dyn_wa>  type any,
                       <dyn_field> type any.

Create data LV_OBJ like like of <dyn_table>.

assign LV_OBJ->* to <DYN_WA>

IF <DYN_WA> is assigned.

LOOP AT <dyn_table> into <DYN_WA>.


assign component 'KOKRS' of structure DYN_WA> to <dyn_field> .
if <dyn_field>  is assigned.

* <dyn_field> has the vlaue which you want.
* similarly do for other fields.
endif.

unassign <dyn_field>. 

ENDLOOP.

I hope you get the concept.

Note that instead of specifying the name of the field you can also specify the postion like this

assign component '2' of structure DYN_WA> to <dyn_field> .

regards,

Ankur Parab

0 Kudos

Or, if you don't know the structure of dynamic table use this


DATA: r_struct TYPE REF TO cl_abap_structdescr,
      table_fields TYPE abap_component_tab.

r_struct ?= cl_abap_typedescr=>describe_by_data( <dyn_wa> ).
table_fields = r_struct->get_components( ).   "here you have components name of dynamic table

"now use the code given by Ankur before
field-symbols: <field>, <value>.

LOOP AT <dyn_table> INTO <dyn_wa>.
   LOOP AT table_fields ASSIGNING <field>.
 
   ASSIGN COMPONENT <field>-name OF STRUCTURE <dyn_wa> to <value>.
   if sy-subr = 0.
      <value> = ...  "here you have value for particular field i.e. kokrs, so it is similar to <dyn_wa>-korks etc
    endif.
endif.

Regards

Marcin

0 Kudos

Hi,

I think I'm almost there, but I still get an error message: "The data object "<FIELD>" has no structure and therefore no component called "NAME". ."

Thanks.

0 Kudos

Hi,

Use <field> type abap_component as below:

DATA: r_struct TYPE REF TO cl_abap_structdescr,

table_fields TYPE abap_component_tab.

r_struct ?= cl_abap_typedescr=>describe_by_data( <dyn_wa> ).

table_fields = r_struct->get_components( ). "here you have components name of dynamic table

"now use the code given by Ankur before

field-symbols: <field> type abap_component, <value>.

LOOP AT <dyn_table> INTO <dyn_wa>.

LOOP AT table_fields ASSIGNING <field>.

ASSIGN COMPONENT <field>-name OF STRUCTURE <dyn_wa> to <value>.

if sy-subr = 0.

<value> = ... "here you have value for particular field i.e. kokrs, so it is similar to <dyn_wa>-korks etc

endif.

endif.

Best regards,

Kazmi

0 Kudos

" I think your fields_table number of records are same order as <dyn_table>

LOOP AT <dyn_table> INTO <dyn_wa>.
 read table fields_table into <field> with key name = 'KOKRS'.
 if sy-subrc eq 0.
 assign component sy-tabix of structure <dyn_wa> to <aux>.
 endif.
 write <aux> to itab_fin-kokrs. " which field you want to move
 append itab_fin.
endloop.

0 Kudos

Thank for your proposals. I don't know the structure, so I can't really use 'KOKRS'

There is an error message saying Type ABAP_COMPONENT is unknown while using your definition:

field-symbols: <field> TYPE abap_component.

Or using the old solution, I also got a short dump saying, that only numerical types can be used.

thanks

0 Kudos

ABAP_COMPONENT is defined in type pools: ABAP, so simply add its declaration before using it.


TYPE-POOLS: ABAP.  "now ABAP_COMPONENT will be recognizable

Regards

Marcin

0 Kudos

I'm using this type-pool and I also went into it to see if there is a declaration of abap_component.

There is nothing like abap_component;(

Maybe I have bad spelling or I'm blind;)

0 Kudos

Should be abap_component_tab not abap_component

Regards

Marcin

former_member218674
Contributor
0 Kudos

Hello,

Check if this link helps.

Thanks,

Augustin.