<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Create internal table with contructor expression for a complex scenario in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674745#M2016528</link>
    <description>&lt;P&gt;The syntax check complains with CORRESPONDING and DISCARDING DUPLICATES if the itab is built using a constructor expression or a functional method, it can work only on an internal table (tested in 7.52).&lt;/P&gt;&lt;P&gt;I can't find any source in the ABAP documentation to support this assumption.&lt;/P&gt;&lt;P&gt;To have DISCARDING DUPLICATES work with a constructor expression or a functional method, I had to use an ugly solution.&lt;/P&gt;&lt;P&gt;Simplifying your example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CLASS lcl_app DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
  PRIVATE SECTION.
    TYPES: ts_k1      TYPE n LENGTH 1,
           ts_k2      TYPE n LENGTH 1,
           ts_val     TYPE c LENGTH 1,
           ts_country TYPE c LENGTH 2.

    TYPES: BEGIN OF ts_source,
             k1      TYPE ts_k1,
             k2      TYPE ts_k1,
             val     TYPE ts_val, " field mapping
             country TYPE ts_country, " Where condition
           END OF ts_source,
           tt_source TYPE STANDARD TABLE OF ts_source WITH NON-UNIQUE DEFAULT KEY.

    TYPES: BEGIN OF ts_destination,
             k1    TYPE ts_k1,
             k2    TYPE ts_k1,
             zzval TYPE ts_val, " field mapping
             extra TYPE ts_val, " extra field
           END OF ts_destination,
           tt_destination TYPE SORTED TABLE OF ts_destination WITH UNIQUE KEY k1 k2.
    METHODS without_discarding_duplicates FOR TESTING.
    METHODS with_discarding_duplicates FOR TESTING.
    METHODS setup.
    METHODS itab_method returning value(result) TYPE tt_source.
    DATA: source_t      TYPE tt_source,
          destination_t TYPE tt_destination.
ENDCLASS.

CLASS lcl_app IMPLEMENTATION.
  METHOD setup.
    source_t = VALUE #(
        ( k1 = '1' k2 = '1' val = 'A' country = 'DE' )
        ( k1 = '1' k2 = '2' val = 'B' country = 'DE' )
        ( k1 = '1' k2 = '3' val = 'C' country = 'EN' ) " &amp;lt;- exclude by WHERE condition
        ( k1 = '1' k2 = '4' val = 'D' country = 'DE' ) " &amp;lt;- dupplicate key
        ( k1 = '1' k2 = '4' val = 'D' country = 'DE' ) " &amp;lt;- dupplicate key
        ( k1 = '1' k2 = '5' val = 'E' country = 'DE' ) ).
  ENDMETHOD.
  METHOD without_discarding_duplicates.
    " RUNTIME ERROR ITAB_DUPLICATE_KEY AS EXPECTED - CAN'T BE CAUGHT BY ABAP UNIT
*    destination_t = CORRESPONDING tt_destination(
**                        itab_method( ) "&amp;lt;=== just to play a little bit
*                        VALUE tt_source( FOR wa2 IN source_t
*                                         WHERE ( country = 'DE' )
*                                         ( wa2 ) )
**                        DISCARDING DUPLICATES "&amp;lt;=== just to play a little bit
*                        ).
  ENDMETHOD.
  METHOD itab_method.
  ENDMETHOD.
  METHOD with_discarding_duplicates.
    destination_t = VALUE #(
                      LET aux_itab = VALUE tt_source( FOR wa2 IN source_t
                                         WHERE ( country = 'DE' )
                                         ( wa2 ) )
                      IN
                      FOR &amp;lt;line&amp;gt; IN CORRESPONDING tt_destination(
                                  aux_itab
                                  DISCARDING DUPLICATES
                                  )
                      ( &amp;lt;line&amp;gt; ) ).
    cl_abap_unit_assert=&amp;gt;assert_equals(
      act = destination_t
      exp = VALUE tt_destination(
            ( k1 = '1' k2 = '1' )
            ( k1 = '1' k2 = '2' )
            ( k1 = '1' k2 = '4' )
            ( k1 = '1' k2 = '5' ) ) ).
  ENDMETHOD.
ENDCLASS.
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 25 Apr 2023 15:27:42 GMT</pubDate>
    <dc:creator>Sandra_Rossi</dc:creator>
    <dc:date>2023-04-25T15:27:42Z</dc:date>
    <item>
      <title>Create internal table with contructor expression for a complex scenario</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674741#M2016524</link>
      <description>&lt;P&gt;Hello ABAP Experts!&lt;BR /&gt;I'm looking for a solution to the following complex scenario (to deepen my understanding of constructor expressions and how they can be combined). &lt;BR /&gt;&lt;BR /&gt;Task: Creation of an internal (sorted) table from another standard internal table (which contains duplicates)&lt;/P&gt;
  &lt;OL&gt; 
   &lt;LI&gt; in one expression&lt;/LI&gt; 
   &lt;LI&gt; without loops&lt;/LI&gt; 
   &lt;LI&gt; existing data records are to be kept&lt;/LI&gt; 
   &lt;LI&gt; avoiding duplicate problems&lt;/LI&gt; 
   &lt;LI&gt; select records from source table by condition (WHERE)&lt;/LI&gt; 
   &lt;LI&gt; perform field mapping (fields have different names)&lt;/LI&gt; 
   &lt;LI&gt; perform value assignment for an extra field in destination table&lt;/LI&gt; 
  &lt;/OL&gt;
  &lt;P&gt;&lt;BR /&gt; I found two solutions, but each has a deficit&lt;BR /&gt; a) Either the duplicates are taken into account, but then the WHERE condition is not possible, or&lt;BR /&gt; b) the WHERE is possible, but then the problem of duplicates cannot be addressed.&lt;BR /&gt; &lt;BR /&gt;Is there any solution at all?&lt;BR /&gt;Thank your for your help. &lt;BR /&gt;&lt;BR /&gt;This is my test set-up: &lt;/P&gt;
  &lt;PRE&gt;&lt;CODE&gt; TYPES: ts_k1 TYPE n LENGTH 1,
        ts_k2 TYPE n LENGTH 1,
        ts_val TYPE c LENGTH 1, 
        ts_country TYPE c LENGTH 2. 

 TYPES: BEGIN OF ts_source,           
           k1      TYPE ts_k1, 
           k2      TYPE ts_k1,
           val     TYPE ts_val,     " field mapping 
           country TYPE ts_country, " Where condition 
       END OF ts_source, 
       tt_source TYPE STANDARD TABLE OF ts_source WITH NON-UNIQUE DEFAULT KEY. 

 TYPES: BEGIN OF ts_destination,
          k1    TYPE ts_k1, 
          k2    TYPE ts_k1,
          zzval TYPE ts_val, " field mapping 
          extra TYPE ts_val, " extra field 
       END OF ts_destination, 
       tt_destination TYPE SORTED TABLE OF ts_destination WITH UNIQUE KEY k1 k2.&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;PRE&gt;&lt;CODE&gt; START-OF-SELECTION. &lt;BR /&gt; *1| Create test data 

 DATA(source_t) = VALUE tt_source( ( k1 = '1' k2 = '1' val = 'A' country = 'DE' ) &lt;BR /&gt;                                   ( k1 = '1' k2 = '2' val = 'B' country = 'DE' )
                                   ( k1 = '1' k2 = '3' val = 'C' country = 'EN' ) " &amp;lt;- exclude by WHERE condition 
                                   ( k1 = '1' k2 = '4' val = 'D' country = 'DE' ) " &amp;lt;- dupplicate key
                                   ( k1 = '1' k2 = '4' val = 'D' country = 'DE' ) " &amp;lt;- dupplicate key 
                                   ( k1 = '1' k2 = '5' val = 'E' country = 'DE' ) ). 

DATA(destination_t) = VALUE tt_destination( ( k1 = '1' k2 = '6' zzval = 'F' extra = '' ) ). 

*** solution a) addressing duplicates, but missing: WHERE country 

destination_t = CORRESPONDING #( BASE ( destination_t ) 
                    VALUE tt_destination( FOR wa IN CORRESPONDING tt_destination( source_t DISCARDING DUPLICATES MAPPING zzval = val ) 
                       " WHERE country .... not possible, because table already created! 
                          ( VALUE #( BASE CORRESPONDING #( wa ) extra = 'X' ) ) ) ). 

*** solution b) with WHERE condition (country), but missing: duplicates handling

destination_t = CORRESPONDING tt_destination( 
                    VALUE tt_destination( FOR line IN source_t 
                       WHERE ( country = 'DE' ) 
                      ( CORRESPONDING #( BASE ( VALUE #( extra = 'X' ) ) line MAPPING zzval = val ) ) 
                  ) ). "DISCARDING DUPLICATES not possible by syntax checker, why? 
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;SPAN { font-family: "Courier New"; font-size: 10pt; color: #000000; background: #FFFFFF; }.L0S31 { font-style: italic; color: #808080; }.L0S32 { color: #3399FF; }.L0S33 { color: #4DA619; }.L0S52 { color: #0000FF; }.L0S55 { color: #800080; } &lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 12:56:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674741#M2016524</guid>
      <dc:creator>miszabo</dc:creator>
      <dc:date>2023-04-25T12:56:51Z</dc:date>
    </item>
    <item>
      <title>Re: Create internal table with contructor expression for a complex scenario</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674742#M2016525</link>
      <description>&lt;P&gt;Why do you use "Unique key" if there are duplicates entries ? &lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 13:48:44 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674742#M2016525</guid>
      <dc:creator>FredericGirod</dc:creator>
      <dc:date>2023-04-25T13:48:44Z</dc:date>
    </item>
    <item>
      <title>Re: Create internal table with contructor expression for a complex scenario</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674743#M2016526</link>
      <description>&lt;P&gt;Please edit your question, select your code and press the button [CODE], which makes the code appear colored/indented, it will be easier for people to look at it. Thank you!&lt;/P&gt;&lt;P&gt;Moreover, your post contains SPAN { font-family ...&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 13:50:03 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674743#M2016526</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2023-04-25T13:50:03Z</dc:date>
    </item>
    <item>
      <title>Re: Create internal table with contructor expression for a complex scenario</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674744#M2016527</link>
      <description>&lt;P&gt;Thank you for your suggestions. I applied the code button. Should be reader friendly now. &lt;BR /&gt;&lt;BR /&gt; &lt;SPAN class="mention-scrubbed"&gt;frdric.girod&lt;/SPAN&gt; : &lt;BR /&gt;Having duplicates in the source table (which must therefore be a standard table) and have them properly handled before the lines are copied into the target table is part of the scenario. &lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 15:22:39 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674744#M2016527</guid>
      <dc:creator>miszabo</dc:creator>
      <dc:date>2023-04-25T15:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: Create internal table with contructor expression for a complex scenario</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674745#M2016528</link>
      <description>&lt;P&gt;The syntax check complains with CORRESPONDING and DISCARDING DUPLICATES if the itab is built using a constructor expression or a functional method, it can work only on an internal table (tested in 7.52).&lt;/P&gt;&lt;P&gt;I can't find any source in the ABAP documentation to support this assumption.&lt;/P&gt;&lt;P&gt;To have DISCARDING DUPLICATES work with a constructor expression or a functional method, I had to use an ugly solution.&lt;/P&gt;&lt;P&gt;Simplifying your example:&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;CLASS lcl_app DEFINITION FOR TESTING DURATION SHORT RISK LEVEL HARMLESS.
  PRIVATE SECTION.
    TYPES: ts_k1      TYPE n LENGTH 1,
           ts_k2      TYPE n LENGTH 1,
           ts_val     TYPE c LENGTH 1,
           ts_country TYPE c LENGTH 2.

    TYPES: BEGIN OF ts_source,
             k1      TYPE ts_k1,
             k2      TYPE ts_k1,
             val     TYPE ts_val, " field mapping
             country TYPE ts_country, " Where condition
           END OF ts_source,
           tt_source TYPE STANDARD TABLE OF ts_source WITH NON-UNIQUE DEFAULT KEY.

    TYPES: BEGIN OF ts_destination,
             k1    TYPE ts_k1,
             k2    TYPE ts_k1,
             zzval TYPE ts_val, " field mapping
             extra TYPE ts_val, " extra field
           END OF ts_destination,
           tt_destination TYPE SORTED TABLE OF ts_destination WITH UNIQUE KEY k1 k2.
    METHODS without_discarding_duplicates FOR TESTING.
    METHODS with_discarding_duplicates FOR TESTING.
    METHODS setup.
    METHODS itab_method returning value(result) TYPE tt_source.
    DATA: source_t      TYPE tt_source,
          destination_t TYPE tt_destination.
ENDCLASS.

CLASS lcl_app IMPLEMENTATION.
  METHOD setup.
    source_t = VALUE #(
        ( k1 = '1' k2 = '1' val = 'A' country = 'DE' )
        ( k1 = '1' k2 = '2' val = 'B' country = 'DE' )
        ( k1 = '1' k2 = '3' val = 'C' country = 'EN' ) " &amp;lt;- exclude by WHERE condition
        ( k1 = '1' k2 = '4' val = 'D' country = 'DE' ) " &amp;lt;- dupplicate key
        ( k1 = '1' k2 = '4' val = 'D' country = 'DE' ) " &amp;lt;- dupplicate key
        ( k1 = '1' k2 = '5' val = 'E' country = 'DE' ) ).
  ENDMETHOD.
  METHOD without_discarding_duplicates.
    " RUNTIME ERROR ITAB_DUPLICATE_KEY AS EXPECTED - CAN'T BE CAUGHT BY ABAP UNIT
*    destination_t = CORRESPONDING tt_destination(
**                        itab_method( ) "&amp;lt;=== just to play a little bit
*                        VALUE tt_source( FOR wa2 IN source_t
*                                         WHERE ( country = 'DE' )
*                                         ( wa2 ) )
**                        DISCARDING DUPLICATES "&amp;lt;=== just to play a little bit
*                        ).
  ENDMETHOD.
  METHOD itab_method.
  ENDMETHOD.
  METHOD with_discarding_duplicates.
    destination_t = VALUE #(
                      LET aux_itab = VALUE tt_source( FOR wa2 IN source_t
                                         WHERE ( country = 'DE' )
                                         ( wa2 ) )
                      IN
                      FOR &amp;lt;line&amp;gt; IN CORRESPONDING tt_destination(
                                  aux_itab
                                  DISCARDING DUPLICATES
                                  )
                      ( &amp;lt;line&amp;gt; ) ).
    cl_abap_unit_assert=&amp;gt;assert_equals(
      act = destination_t
      exp = VALUE tt_destination(
            ( k1 = '1' k2 = '1' )
            ( k1 = '1' k2 = '2' )
            ( k1 = '1' k2 = '4' )
            ( k1 = '1' k2 = '5' ) ) ).
  ENDMETHOD.
ENDCLASS.
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 25 Apr 2023 15:27:42 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674745#M2016528</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2023-04-25T15:27:42Z</dc:date>
    </item>
    <item>
      <title>Re: Create internal table with contructor expression for a complex scenario</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674746#M2016529</link>
      <description>&lt;PRE&gt;&lt;CODE&gt;SELECT DISTINCT * FROM  @your_itab as your_itab INTO TABLE  @your_new_table.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;should work... or should not?&lt;/P&gt;</description>
      <pubDate>Tue, 25 Apr 2023 15:37:45 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674746#M2016529</guid>
      <dc:creator>VXLozano</dc:creator>
      <dc:date>2023-04-25T15:37:45Z</dc:date>
    </item>
    <item>
      <title>Re: Create internal table with contructor expression for a complex scenario</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674747#M2016530</link>
      <description>&lt;P&gt;Thank you  @Sandra Rossi, great help. &lt;BR /&gt;With your suggestion as a basis, the solution covering all requirements would look like this: &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;    destination_t = VALUE #(                                             " one statement, no loop
                      LET aux_itab = VALUE tt_source( FOR wa2 IN source_t   " handle condition
                                         WHERE ( country = 'DE' )
                                         ( wa2 ) )
                      IN
                      FOR &amp;lt;line&amp;gt; IN CORRESPONDING tt_destination(
                                  aux_itab
                                  DISCARDING DUPLICATES                    " handle duplicates
                                  MAPPING zzval = val                        " handle mapping
                                  )
                                  ( VALUE #(
                                      BASE CORRESPONDING #( &amp;lt;line&amp;gt; )
                                      extra = 'X' ) ) ).                   " handle extra field&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;BR /&gt;Any recommendations for improvements? &lt;BR /&gt;Or ist this the best we can currently achieve (with one statement and without loop)?&lt;BR /&gt;&lt;BR /&gt;In contrast: The solution with loop would look like this: &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;LOOP AT source_t ASSIGNING FIELD-SYMBOL(&amp;lt;source_s&amp;gt;) WHERE country = 'DE'.   " &amp;lt;- handle condition
INSERT CORRESPONDING #( BASE ( VALUE #( extra = 'X' ) ) &amp;lt;source_s&amp;gt; MAPPING zzval = val ) INTO TABLE destination_t.
ENDLOOP.
&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;
SPAN {
font-family: "Courier New";
font-size: 10pt;
color: #000000;
background: #FFFFFF;
}.L0S31 {
font-style: italic;
color: #808080;
}.L0S33 {
color: #4DA619;
}.L0S52 {
color: #0000FF;
}.L0S55 {
color: #800080;
}&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2023 09:18:40 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674747#M2016530</guid>
      <dc:creator>miszabo</dc:creator>
      <dc:date>2023-04-26T09:18:40Z</dc:date>
    </item>
    <item>
      <title>Re: Create internal table with contructor expression for a complex scenario</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674748#M2016531</link>
      <description>&lt;P&gt;Of course I would choose the LOOP AT solution, just because it's easy to understand and maintainable.&lt;/P&gt;&lt;P&gt;Thinking that constructor expressions are faster than the old ABAP is a myth. Constructor expressions are to be used to make the code more understandable and maintainable, if it ends in code too much complex, forget it.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Apr 2023 18:11:53 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/create-internal-table-with-contructor-expression-for-a-complex-scenario/m-p/12674748#M2016531</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2023-04-26T18:11:53Z</dc:date>
    </item>
  </channel>
</rss>

