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: 

convert structure to list of fields

daniel_humberg
Contributor
0 Kudos

I have a variable whose type is a structure with 10 fields.

I would like to convert this data into a table with 10 lines, with the format:

FIELD_NAME | FIELD_VALUE (type STRING or REF TO DATA)

Is there any standard FM or method for this?

As a next step, i would like to compare two variables with the same (structured) type.

I would like to get all fields that are different into a table with the format above:

FIELD_NAME | FIELD_VALUE (type STRING or REF TO DATA)

Is there anything for that too?

1 ACCEPTED SOLUTION

Former Member
0 Kudos

hi,

You can try this this way...i guess if i am clear with your requirement ...

DATA: gt_itab TYPE REF TO data,

g_key_field1 TYPE char30,

g_key_field2 TYPE char30,

g_key_field3 TYPE char30,

g_descr_ref TYPE REF TO cl_abap_tabledescr.

FIELD-SYMBOLS: <gt_itab> TYPE STANDARD TABLE,

<g_key_comp> TYPE abap_keydescr,

<g_result> TYPE ANY.

PARAMETERS: p_table TYPE tabname16 DEFAULT 'SPFLI',

p_cond1 TYPE string DEFAULT sy-mandt,

p_cond2 TYPE string DEFAULT 'LH',

p_cond3 TYPE string DEFAULT '0123'.

START-OF-SELECTION.

  • Create and populate internal table

CREATE DATA gt_itab TYPE STANDARD TABLE OF (p_table).

ASSIGN gt_itab->* TO <gt_itab>.

(Till this part will give you a table where (p_table) can be your structure name.)

SELECT * FROM (p_table) INTO TABLE <gt_itab>.

  • Get the key components into the fields g_key_field1, ...

g_descr_ref ?= cl_abap_typedescr=>describe_by_data_ref( gt_itab ).

LOOP AT g_descr_ref->key ASSIGNING <g_key_comp>.

CASE sy-tabix.

WHEN 1.

g_key_field1 = <g_key_comp>-name.

WHEN 2.

g_key_field2 = <g_key_comp>-name.

WHEN 3.

g_key_field3 = <g_key_comp>-name.

ENDCASE.

ENDLOOP.

  • Finally, perform the search

READ TABLE <gt_itab> ASSIGNING <g_result>

WITH KEY (g_key_field1) = p_cond1

(g_key_field2) = p_cond2

(g_key_field3) = p_cond3.

IF sy-subrc = 0.

WRITE / 'Record found.'.

ELSE.

WRITE / / 'No record found.'.

ENDIF.

Thanks,

Richa

1 REPLY 1

Former Member
0 Kudos

hi,

You can try this this way...i guess if i am clear with your requirement ...

DATA: gt_itab TYPE REF TO data,

g_key_field1 TYPE char30,

g_key_field2 TYPE char30,

g_key_field3 TYPE char30,

g_descr_ref TYPE REF TO cl_abap_tabledescr.

FIELD-SYMBOLS: <gt_itab> TYPE STANDARD TABLE,

<g_key_comp> TYPE abap_keydescr,

<g_result> TYPE ANY.

PARAMETERS: p_table TYPE tabname16 DEFAULT 'SPFLI',

p_cond1 TYPE string DEFAULT sy-mandt,

p_cond2 TYPE string DEFAULT 'LH',

p_cond3 TYPE string DEFAULT '0123'.

START-OF-SELECTION.

  • Create and populate internal table

CREATE DATA gt_itab TYPE STANDARD TABLE OF (p_table).

ASSIGN gt_itab->* TO <gt_itab>.

(Till this part will give you a table where (p_table) can be your structure name.)

SELECT * FROM (p_table) INTO TABLE <gt_itab>.

  • Get the key components into the fields g_key_field1, ...

g_descr_ref ?= cl_abap_typedescr=>describe_by_data_ref( gt_itab ).

LOOP AT g_descr_ref->key ASSIGNING <g_key_comp>.

CASE sy-tabix.

WHEN 1.

g_key_field1 = <g_key_comp>-name.

WHEN 2.

g_key_field2 = <g_key_comp>-name.

WHEN 3.

g_key_field3 = <g_key_comp>-name.

ENDCASE.

ENDLOOP.

  • Finally, perform the search

READ TABLE <gt_itab> ASSIGNING <g_result>

WITH KEY (g_key_field1) = p_cond1

(g_key_field2) = p_cond2

(g_key_field3) = p_cond3.

IF sy-subrc = 0.

WRITE / 'Record found.'.

ELSE.

WRITE / / 'No record found.'.

ENDIF.

Thanks,

Richa