Application Development and Automation 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 only

How I can get the data from an structure dynamically?

Former Member
0 Likes
806

Hi Guys,

I would like to get all fields and values from an structure dynamically, How I could do this?

Thanks

1 ACCEPTED SOLUTION
Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
777

You can use the ASSIGN COMPONENT OF STRUCTURE statement.

Hi Guys,

I would like to get all fields and values from an structure dynamically, How I could do this?

Thanks

4 REPLIES 4
Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
778

You can use the ASSIGN COMPONENT OF STRUCTURE statement.

Read only

Former Member
0 Likes
777

An example please?

Read only

kakshat
Product and Topic Expert
Product and Topic Expert
0 Likes
777

An F1 help on the keyword will be helpful.

You will need to have a DO loop inside which you will have to use the ASSIGN COMPONENT statement.

i = 1.

DO.

   ASSIGN COMPONENT i OF ls_struc TO <field_value>.

   IF <field_value> IS NOT ASSIGNED.

        EXIT.

   ENDIF.

   i = i + 1.

ENDDO.

If you want the structure's field names as well, then you can get them using FM DDIF_NAMETAB_GET. And, in that case, instead of using the counter i, you can use the field name in the ASSIGN COMPONENT statement.

Read only

0 Likes
777

You may also use the name of a field instead of it's component index.

TYPES:

  BEGIN OF struct,

       field1 TYPE c,

          field2 TYPE c,

End of struct

.

DATA:

     ls_struct TYPE struct

     .

ASSIGN COMPONENT 'FIELD1'

  OF STRUCTURE ls_struct

     TO <fs> .

That will allow you to access the fields by name. You may also use a variable in place of a literal string; so you could use variables to store the names of the fields you want to access.