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: 

Copy table to another ABAP 7.4

CarstenKasper
Active Contributor
0 Kudos

Hi guys,

today I was happy to try one of the new ABAP 7.4 language features. However I am not quite sure this is the best approach.

My goal: I have two internal tables. One with many attributes in the table line structure. One with just a single. Now I want to fill the second table with a specific attribute of all lines of the first table.

My approach as example (which is working):


REPORT zcka_test.

START-OF-SELECTION.

  DATA:

    lt_tcurt TYPE TABLE OF tcurt,

    ls_tcurt TYPE tcurt.

  SELECT *

    FROM tcurt

    INTO TABLE lt_tcurt.

  lt_record_id = VALUE #( FOR ls_line1 IN lt_tcurt ( ls_line1-KTEXT ) ).

Now I have all short texts in LT_RECORD_ID.

Is it the best approach? I thought about using REDUCE to only get the short text KTEXT. How would I go about this?


lt_record_id = VALUE #( ( LINES of lt_tcurt ) ).

This just copies the table. How do I REDUCE it to the relevant attribute?

cheers Carsten

PS: Please remember the above is just an example to give an idea. Please do not get at me about using SQL to only select the correct component! My exact case does not have SELECTs.

1 ACCEPTED SOLUTION

former_member210008
Active Participant
0 Kudos

Maybe it will be the best way to select right into the lt_record_id?..

But if you need to move from one internal table to another use move-corresponding (it works with tables) or, if your tables have different column names, use corrsponding #( mapping ).

9 REPLIES 9

Former Member
0 Kudos

This message was moderated.

former_member210008
Active Participant
0 Kudos

Maybe it will be the best way to select right into the lt_record_id?..

But if you need to move from one internal table to another use move-corresponding (it works with tables) or, if your tables have different column names, use corrsponding #( mapping ).

0 Kudos

Hi Evgeniy,

as written above in the last two lines: This is just an example. I do not have a SELECT my real scenario.

Thank you for your suggestion with MOVE. I know of the possibilities of old ABAP, but I would like to build some ABAP 7.4 skills. The functions in 7.4 seem way more powerful to me.

cheers Carsten

0 Kudos

Maybe I misunderstood you?..

move-corresponding worked only for structures before 7.40. For tables it works since 7.40SP05.

So it's a new feature.

1) move-corresponding lt_tcurt[] to lt_record_id[].

2) lt_record_id = corresponding #( lt_tcurt mapping fieldname = KTEXT ).

Can you provide small example with source and dest tables with data?

0 Kudos

Hi Carsten,

IMO Evgeniy has a valid point. With ABAP 740 you can now use MOVE-CORRESPONDING with internal tables too. Refer CORRESPONDING - mapping - ABAP Keyword Documentation.

For e.g.,


TYPES:

  BEGIN OF ty_many,

    fld1 TYPE string,

    fld2 TYPE string,

    fld3 TYPE string,

  END OF ty_many,

  tty_many TYPE STANDARD TABLE OF ty_many WITH EMPTY KEY,

  BEGIN OF ty_one,

    fld21 TYPE string,

  END OF ty_one,

  tty_one TYPE STANDARD TABLE OF ty_one WITH EMPTY KEY.

DATA it_one TYPE tty_one.

START-OF-SELECTION.

  DATA(it_many) = VALUE tty_many(

                                    (  fld1 = 'Bli'

                                       fld2 = 'Bla'

                                       fld3 = 'Bloop'

                                    ) " Record 1

                                    (  fld1 = 'I'

                                       fld2 = 'am'

                                       fld3 = 'Batman'

                                    ) " Record 2

                                ).

  " Write 1st ITAB to the output stream

  cl_demo_output=>write_data(

    EXPORTING

      value = it_many

      name  = |Many fields|    " Name

  ).

  " Transfer the data from ITAB1 to ITAB2 with field mappings

  it_one = CORRESPONDING #( it_many MAPPING fld21 = fld3 ).

  " Write 2nd ITAB to the output stream

  cl_demo_output=>write_data(

    EXPORTING

      value = it_one

      name  = |One field with mapping rules|    " Name

  ).

  " Display output

  cl_demo_output=>display( ).

May be i don't understand your requirement clearly

BR,

Suhas

0 Kudos

Thank you to both of you Evengiy and Suhas!

My requirement is quite easy: I have two internal tables. One with a structure and one without. I want to get a single attribute of the first table to the second.

I tried to outline it in the example above.

  1. REPORT zcka_test.
  2. START-OF-SELECTION.
  3.   DATA:
  4.     lt_tcurt TYPE TABLE OF tcurt,
  5. lt_record_id TYPE TABLE OF ktext_curt,
  6.     ls_tcurt TYPE tcurt.
  7.   SELECT *
  8.     FROM tcurt
  9.     INTO TABLE lt_tcurt.
  10.   lt_record_id = VALUE #( FOR ls_line1 IN lt_tcurt ( ls_line1-KTEXT ) ). 

Relevant is only the part in line 10. The rest ist setup.

The code is working as afterwards I have the short text in mit table lt_record_id.

I tried it with MOVE-CORRESPONDING and CORRESPONDING #, but could not get it to work.


  lt_record_id = CORRESPONDING #( lt_tcurt ).

  MOVE-CORRESPONDING lt_tcurt to lt_record_id.

Both give me an empty table as result which is completely understandable since there is no corresponding field in LT_RECORD_ID. I tried to adjust the first statement as:


  lt_record_id = CORRESPONDING #( lt_tcurt mapping table_line = ktext ).

For this I get the error "The type "C" is not a structure.

How would I get the attribute KTEXT of all lines of LT_TCURT into the lines of LT_RECORD_ID using CORRESPONDING # or another technique?

0 Kudos

Gotcha, the target table is not a structured type. You had missed the its definition in your original post, hence the confusion

I'll update the post if i find anything interesting for you

PS - As per SAP documentation you cannot use table_line in mapping rules.

0 Kudos

Hi Suhas,

I feared it would be like this, but I could it not find it in the documentation. Very sad, now I always have to declare structured tables. Welcome to the wonderful world of CRM where everything has got a GUID ;-).

Thanks to both of you!

cheers Carsten

0 Kudos

Hi Carsten,

Actually non-structured table types are "kind-of" supported by MOVE-CORRESPONDING. As per SAP documentation -

A non-structured row type is handled like a structure with a single component.

I have read the ABAP documentation on MOVE-CORRESPONDING (MOVE-CORRESPONDING - itab - ABAP Keyword Documentation) for int. tables. It states(check the "Notes" section) & i quote:


MOVE-CORRESPONDING never has an effect when an internal table with a non-structured row type is assigned to an internal table with a structured row type (or when the assignment is the other way round).

I guess this is the reason why pseudo-component table_line cannot be used in the mapping rules

BR,

Suhas

PS - Sorry for being the bearer of bad news