cancel
Showing results for 
Search instead for 
Did you mean: 

What is best ABAP logic to use; 2 internal tables plus a Mapping table.

former_member201275
Active Contributor

I have 2 tables; my Local table with 300 fields, and the Customer table with 300 fields, and a mapping table which maps the fields from the Local table to the Customer table. In my Local table are approximately 10000 entries which now have to be copied to the Customer table based on the Mapping table.

The mapping table could be changed from time to time so the solution must be dynamic.

The field names are completely different in both files and look something like this:

LOC_TAB (fields)

  • userid
  • first_name
  • field_0001
  • xyz_fld
  • … etc…

CUST_TAB (fields)

  • usrid
  • fname
  • tpid
  • example
  • … etc…

MAPPING_TAB

  • LOC_name CUST_name
  • userid usrid
  • first_name fname
  • field_0001 example
  • xyz_fld tpid
  • … etc…

So, for example the value in field 'userid' must be passed to the field 'usrid' in the Customer table. I can do something like:

LOOP at LOC_TAB into local_workarea.

LOOP at MAPPING_TAB into map_workarea.

  • what dynamic coding here???

append CUST_TAB????

ENDLOOP.

ENDLOOP.

Problem is that the above solution would mean looping on my mapping table for every single entry and Loop on the Local file. I cannot think of a better way.:(

All help greatly appreciated!

Accepted Solutions (1)

Accepted Solutions (1)

pokrakam
Active Contributor
0 Kudos
LOOP AT source_tab ASSIGNING <source_row>.
  APPEND INITIAL LINE TO target_tab ASSIGNING <target_row>.
  LOOP AT mapping_table INTO map.
    ASSIGN COMPONENT (map-source) OF STRUCTURE <source_row> TO <source_field>. 
    ASSIGN COMPONENT (map-target) OF STRUCTURE <target_row> TO <target_field>.
    <target_field> = <source_field>.
  ENDLOOP.
ENDLOOP.

Not tested or syntax verified, but should give you the basic idea.

Answers (2)

Answers (2)

DoanManhQuynh
Active Contributor
0 Kudos

I think this should work:

SELECT source_col, target_col 
 FROM MAPPING_TAB
 INTO TABLE @DATA(lt_mab).

LOOP AT lt_mab INTO DATA(ls_mab).
 IF lv_sel IS INITIAL.
 lv_sel = |{ ls_mab-source_col } AS { ls_mab-target_col }|.
 ELSE.
 lv_sel = |{ lv_sel } , { ls_mab-source_col } AS { ls_mab-target_col }|.
 ENDIF. 
ENDLOOP.
SELECT ( LV_SEL ) FROM LOC_TAB INTO TABLE @DATA(lt_target).
 
UPDATE CUST_TAB FROM lt_target.

horst_keller
Product and Topic Expert
Product and Topic Expert
0 Kudos

Use the CORRESPONDING operator with MAPPING or use CL_ABAP_CORRESPONDING?

https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abencorresponding....

former_member201275
Active Contributor
0 Kudos

Hi Horst,

Thank you for your answer. Sorry, I forgot to mention I am on SAP_BASIS version 701. Unfortunately this class is not available there. 😞

Kind regards

Warren

matt
Active Contributor
0 Kudos

Warren or Glen?

former_member201275
Active Contributor

Actually Warren but i use pseudoname here because i am ashamed that i am not clever and ask stupid questions.

matt
Active Contributor
0 Kudos

I've seen Glenn as well. 🙂

pokrakam
Active Contributor

I think there is nothing to be ashamed of, we're all learning something and nobody can be an expert at everything. The way the question is asked says a lot more about the person than the content. I found your questions generally well thought out and well described, and would count very much in your favor if I were researching an interview candidate.

former_member201275
Active Contributor

Thank you for your kind words, and also for your solution which worked! 🙂

matt
Active Contributor

There are no stupid questions. Though there are many inquisitive idiots, I don't think you're one of them. Well constructed non-FAQ basic questions are welcomed by me.