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

Move Corresponding Fields

Former Member
0 Likes
14,467

Hi

I would like to move fields from a structure that has the following fields

data Struc1 type table1.

Fields:

BUKRS

WAERS

MENGE....

(similarly another 50 such fields)

into another structure that has following fields,

data Struc2 type table2.

Fields:

ZZ_WAERS

ZZ_MENGE

ZZ_BUKRS

The data elements linked to the fields in both the structures would be the same. Move-Corresponding will not work as the field names do not match... also the order of the fields in both the structures do not match

What is the easiest way to do this task other than moving each field one at a time?

Can I apply any field symbols concept here... please let me know

1 ACCEPTED SOLUTION
Read only

Clemenss
Active Contributor
4,385

Hi Grame,

if you want to create a generic solution, you can use RTTI Runtime Type Information to determine the datatype of the components in both structures. Then you could create assignments for equal types in source and target structure.

This could work as long as you do not have more than one component of the same data type (data element) in the structures.

Something like

FIELD-SYMBOLS:
    <data1> TYPE ANY,
    <data2> TYPE ANY.
  DATA:
    lo_typedescr1 TYPE REF TO cl_abap_typedescr,
    lo_typedescr2 TYPE REF TO cl_abap_typedescr.

  DO.
    ASSIGN COMPONENT sy-index OF STRUCTURE itab1 TO <data1>.
    IF sy-subrc NE 0.
      EXIT.
    ELSE.
      DO.
        ASSIGN COMPONENT sy-index OF STRUCTURE itab2 TO <data2>.
        IF sy-subrc NE 0.
          EXIT.
        ELSE.
          lo_typedescr1 = cl_abap_typedescr=>describe_by_data( <data1> ).
          lo_typedescr2 = cl_abap_typedescr=>describe_by_data( <data2> ).
          IF lo_typedescr1 =  lo_typedescr2.
            <data2> = <data1>.
          ENDIF.
        ENDDO.
      ENDIF.
    ENDDO.

I have some doubts about performance if you do this for every table line. It will be better to do it once, keep the results in some kind of internal assignment table and use it in the loop. For this I don't write the code, thats your homework

Regards,.

Clemens

10 REPLIES 10
Read only

Former Member
0 Likes
4,385

Well i was wrong i just tested and even i was not successfull with different field names but same type. Thanks for correcting me

Edited by: Vikranth.Reddy on Sep 19, 2009 2:39 PM

Read only

venkat_o
Active Contributor
0 Likes
4,385

Hi Grame, <li>You are right. Data is not passed between those structures until you find similar fields not data elements for the fields. If you use MOVE-CORRESPONDING fields must be matched to pass data between the structure fields <li> I tried with the below example. Unsuccessful.

REPORT ZTEST_NOTEPAD.
DATA: BEGIN OF IT_T100 OCCURS 0,
        SPRSL TYPE T100-SPRSL,
        ARBGB TYPE T100-ARBGB,
        MSGNR TYPE T100-MSGNR,
        TEXT  TYPE T100-TEXT,
      END OF IT_T100.

DATA: BEGIN OF IT_ZT100 OCCURS 0,
        ZZ_SPRSL TYPE T100-SPRSL,
        ZZ_ARBGB TYPE T100-ARBGB,
        ZZ_MSGNR TYPE T100-MSGNR,
        ZZ_TEXT  TYPE T100-TEXT,
      END OF IT_ZT100.

START-OF-SELECTION.
  SELECT * FROM T100 INTO TABLE IT_T100 UP TO 20 ROWS WHERE SPRSL = SY-LANGU.
  LOOP AT IT_T100.
    MOVE-CORRESPONDING IT_T100 TO IT_ZT100.
    APPEND IT_ZT100.
    CLEAR  IT_ZT100.
  ENDLOOP.
  LOOP AT IT_ZT100.
    WRITE:/  IT_ZT100-ZZ_SPRSL,
             IT_ZT100-ZZ_ARBGB,
             IT_ZT100-ZZ_MSGNR,
             IT_ZT100-ZZ_TEXT.
  ENDLOOP.
<li>The best is to pass field by field like below

  LOOP AT IT_T100.
    IT_ZT100-ZZ_SPRSL = IT_T100-SPRSL.
    IT_ZT100-ZZ_ARBGB = IT_T100-ARBGB.
    IT_ZT100-ZZ_MSGNR = IT_T100-MSGNR.
    IT_ZT100-ZZ_TEXT  = IT_T100-TEXT.
    APPEND IT_ZT100.
    CLEAR  IT_ZT100.
  ENDLOOP.
Thanks Venkat.O

Read only

venkat_o
Active Contributor
0 Likes
4,385

Hi Grame, <li>I got an idea of having deep structure so that you can use move-corresponding more effectively. <li>Try this way.


REPORT ZTEST_NOTEPAD.
TYPES: BEGIN OF TY_T100,
        SPRSL TYPE T100-SPRSL,
        ARBGB TYPE T100-ARBGB,
        MSGNR TYPE T100-MSGNR,
        TEXT  TYPE T100-TEXT,
      END OF TY_T100.
TYPES: BEGIN OF TY_ZT100.
TYPES:  TY_T100 TYPE TY_T100,
        MATNR   TYPE MARC-MATNR,
        WERKS   TYPE MARC-WERKS,
        MINBE   TYPE MARC-MINBE,
        EISBE   TYPE MARC-EISBE,
      END OF TY_ZT100.

DATA:WA_T100  TYPE TY_T100,
     WA_ZT100 TYPE TY_ZT100.
DATA:IT_T100  TYPE STANDARD TABLE OF TY_T100,
     IT_ZT100 TYPE STANDARD TABLE OF TY_ZT100.

START-OF-SELECTION.
  SELECT * FROM T100 INTO TABLE IT_T100 UP TO 20 ROWS WHERE SPRSL = SY-LANGU.
  LOOP AT IT_T100 INTO WA_T100.
    MOVE-CORRESPONDING WA_T100 TO WA_ZT100-TY_T100.
    APPEND WA_ZT100 TO IT_ZT100.
    CLEAR  WA_ZT100.
  ENDLOOP.

  LOOP AT IT_ZT100 INTO WA_ZT100.
    WRITE:/  WA_ZT100-TY_T100-SPRSL,
             WA_ZT100-TY_T100-ARBGB,
             WA_ZT100-TY_T100-MSGNR,
             WA_ZT100-TY_T100-TEXT.
  ENDLOOP.
Thanks Venkat.O

Read only

Former Member
0 Likes
4,385

Thanks for the answers Vikranth and Venkat...

Venkat,

This will not work out for me as i cannot declare a structure inside another structure in my case and that too i want field values to move into fields starting with 'ZZ_', this cannot happen in the case you have suggested

Read only

Clemenss
Active Contributor
4,386

Hi Grame,

if you want to create a generic solution, you can use RTTI Runtime Type Information to determine the datatype of the components in both structures. Then you could create assignments for equal types in source and target structure.

This could work as long as you do not have more than one component of the same data type (data element) in the structures.

Something like

FIELD-SYMBOLS:
    <data1> TYPE ANY,
    <data2> TYPE ANY.
  DATA:
    lo_typedescr1 TYPE REF TO cl_abap_typedescr,
    lo_typedescr2 TYPE REF TO cl_abap_typedescr.

  DO.
    ASSIGN COMPONENT sy-index OF STRUCTURE itab1 TO <data1>.
    IF sy-subrc NE 0.
      EXIT.
    ELSE.
      DO.
        ASSIGN COMPONENT sy-index OF STRUCTURE itab2 TO <data2>.
        IF sy-subrc NE 0.
          EXIT.
        ELSE.
          lo_typedescr1 = cl_abap_typedescr=>describe_by_data( <data1> ).
          lo_typedescr2 = cl_abap_typedescr=>describe_by_data( <data2> ).
          IF lo_typedescr1 =  lo_typedescr2.
            <data2> = <data1>.
          ENDIF.
        ENDDO.
      ENDIF.
    ENDDO.

I have some doubts about performance if you do this for every table line. It will be better to do it once, keep the results in some kind of internal assignment table and use it in the loop. For this I don't write the code, thats your homework

Regards,.

Clemens

Read only

Former Member
0 Likes
4,385

That's cool dude, that worked

Read only

Former Member
0 Likes
4,385

Hi,

Our project also has some requiremtn like this and we have developed our own piece of code to achieve this.

In our project we call SD condition technique to determine Rate and Final Commission in fields like KWERT and then finally we move the results back to Application Specific fields.

To achieve this we use a concept termed as 'Copy Services', in this we maintain following set of customizing -

a) Source Structure - Target Structure

b) Fields to Map from Source Structure to Target Structure

and then at runtime we read this customizing and move data from SD fields to our application fields.

Hope this idea can help you achieve ur desired functionality in more generic way.

Regards

Manas Dua

Read only

Former Member
0 Likes
4,385

Hello Grame,

even this thread is solved by Clemens' pretty nice solution, there are not only doubts regarding the performance (as he mentioned) but also regarding the correctness!! When you are using n different fields (in a structure) with exactly n different field types, then Clemens' method works. Let us assume your structure has some fields with the SAME type (e.g. CHAR(4)) then all these target fields will be populated by the same source field. My recommendation: For a 100% accuracy you have to adjust the field names in the source and target structure or you have to move field by field. In the case the are no type-duplicates you can use Clemens.

Good luck,

Heinz

Read only

0 Likes
4,385

Helo Heinz,

thank you for repeating and elaborating on what I said:

"This could work as long as you do not have more than one component of the same data type (data element) in the structures."

Regards,

Clemens

Read only

0 Likes
4,385

Heinz,

You are right, perfomance wise the approach is not right and also in cases where two fields have the same description the approach may create anomalies