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

Fill Table Witout Loop

srahemi
Participant
4,265

Hi Every One

i have one column internal table Like

i want add 2 column with Fixed value Like :

How to do it in the best way ?

1 ACCEPTED SOLUTION
Read only

yunustuzun
Participant
3,672

Here another solution with CL_ABAP_CORRESPONDING class. It works on ABAP 7.50 . I didn't write any loop. Inside kernel module ab_kmMvcdExecute_752 maybe some loop exist . I am not sure : )

cl_abap_corresponding=>create_with_value(
  source            = first_table
  destination       = second_table
  mapping           = VALUE  cl_abap_corresponding=>mapping_table_value(
      ( level = 0 kind = cl_abap_corresponding=>mapping_component   srcname = 'column1' dstname = 'column1'  )
      ( level = 0 kind = cl_abap_corresponding=>mapping_value dstname = 'column2' value = REF #( `X` ) )
      ( level = 0 kind = cl_abap_corresponding=>mapping_value dstname = 'column3' value = REF #( `Y` ) ) )
   )->execute( EXPORTING source      = first_table
                CHANGING  destination = second_table ).<br>
7 REPLIES 7
Read only

joltdx
Active Contributor
3,672

Here's one way to do it when 'x' and 'y' are fixed...

TYPES:
  BEGIN OF ty_one_column,
    column TYPE i,
  END OF ty_one_column.

TYPES:
  BEGIN OF ty_three_columns,
    column   TYPE i,
    column_2 TYPE string,
    column_3 TYPE string,
  END OF ty_three_columns.

DATA one_column_table TYPE STANDARD TABLE OF ty_one_column WITH EMPTY KEY.
DATA three_column_table TYPE STANDARD TABLE OF ty_three_columns WITH EMPTY KEY.

DO 5 TIMES.
  INSERT VALUE #( column = sy-index ) INTO TABLE one_column_table.
ENDDO.

three_column_table = CORRESPONDING #( one_column_table ).
MODIFY three_column_table
  FROM VALUE #( column_2 = 'x'
                column_3 = 'y' )
  TRANSPORTING column_2
               column_3
  WHERE column IS NOT INITIAL.
Read only

0 Likes
3,672

Thank you, Sir!
Genial in its simplicity.

Read only

Patrick_vN
Active Contributor
0 Likes
3,672

I'd say there are different possibilities, though you'll end up with a loop somewhere.

Option 1 - in the SQL statement

SELECT column,
       'x' as column2,
       'Y' as column3
       FROM [..]
       INTO TABLE @DATA([..])
       ..

Option 2 - the FOR statement

see this blog for different possibilities (though technically, this remains a loop of course)

    [table_2] = VALUE #(  FOR data
                          IN [table_1]
                          ( column_1 = data-column_1
                            column_2 = 'x'
                            column_3 = 'Y'
                          )
                       ).
Read only

joltdx
Active Contributor
3,672

Unfortunately the CORRESPONDING operator will only map field names and not constants like that. That would have been a good one if it hade worked. I know I would have used it many times if it had been supported... 🙂

Read only

3,672

Yeah, like I wrote in the answer, I wasn't sure the current syntax supports it. I ended up removing the option to prevent minsunderstandings 🙂

Read only

Sandra_Rossi
Active Contributor
3,672

SAP Community is not a "code it for me" service.

Read only

yunustuzun
Participant
3,673

Here another solution with CL_ABAP_CORRESPONDING class. It works on ABAP 7.50 . I didn't write any loop. Inside kernel module ab_kmMvcdExecute_752 maybe some loop exist . I am not sure : )

cl_abap_corresponding=>create_with_value(
  source            = first_table
  destination       = second_table
  mapping           = VALUE  cl_abap_corresponding=>mapping_table_value(
      ( level = 0 kind = cl_abap_corresponding=>mapping_component   srcname = 'column1' dstname = 'column1'  )
      ( level = 0 kind = cl_abap_corresponding=>mapping_value dstname = 'column2' value = REF #( `X` ) )
      ( level = 0 kind = cl_abap_corresponding=>mapping_value dstname = 'column3' value = REF #( `Y` ) ) )
   )->execute( EXPORTING source      = first_table
                CHANGING  destination = second_table ).<br>