<?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>Question Re: Runtime Exception ITAB_DUPLICATE_KEY behaves differen... in Technology Q&amp;A</title>
    <link>https://community.sap.com/t5/technology-q-a/runtime-exception-itab-duplicate-key-behaves-differently-for-corresponding/qaa-p/13771457#M4838296</link>
    <description>&lt;P&gt;Reproduced in 7.52 SP 04.&lt;/P&gt;&lt;P&gt;As far as I can see in the documentation of&amp;nbsp;&lt;A href="https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abencorresponding_variants_s_abexa.htm" target="_self"&gt;CORRESPONDING&lt;/A&gt;, it says that to avoid a duplicate exception (doesn't say what kind of exception it is), DISCARDING DUPLICATES is to be used.&lt;/P&gt;&lt;P&gt;Now, what should happen exactly if a duplicate happens, according to the documentation and is that true if I test in ABAP?&lt;/P&gt;&lt;P&gt;The documentation of CORRESPONDING&amp;nbsp;&lt;SPAN&gt;says that the insertion behaves like MOVE-CORRESPONDING (for itabs).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The&amp;nbsp;documentation of&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapmove-corresponding.htm#!ABAP_VARIANT_2@2@" target="_self"&gt;MOVE-CORRESPONDING&lt;/A&gt;&lt;SPAN&gt; says that "Finally, and if necessary, the table keys and associated table indexes are updated in the target table in accordance with the rules for insertions in internal tables. The corresponding exceptions are raised if uniqueness is violated."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;It's still not very clear what exact rules are managed for "insertions in internal tables", it behaves differently for one line at a time and for one set of lines (LINES OF).&lt;/P&gt;&lt;P&gt;If you use MOVE-CORRESPONDING, you get ITAB_DUPLICATE_KEY.&lt;/P&gt;&lt;P&gt;If you use INSERT one line without using LINES OF, the line is&amp;nbsp;not inserted, SY-SUBRC is set to 4 and the program continues.&lt;/P&gt;&lt;P&gt;If you use INSERT LINES OF, you get ITAB_DUPLICATE_KEY.&lt;/P&gt;&lt;P&gt;The documentation of &lt;A href="https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapinsert_itab_linespec.htm#!ABAP_ALTERNATIVE_3@3@" target="_self"&gt;INSERT, line_spec &amp;gt; LINES OF&lt;/A&gt;&amp;nbsp;seems wrong to me: "If there is a conflict with an existing unique table key, a catchable exception is raised when inserting multiple lines from an internal table."&lt;/P&gt;&lt;P&gt;If you look at the general documentation of&amp;nbsp;&lt;A href="https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapinsert_itab.htm" target="_blank" rel="noopener"&gt;INSERT - itab&lt;/A&gt;, you finally obtain the correct rule lost at the bottom of the page: "Uncatchable Exceptions Cause: When inserting a set of lines, entries with an identical key were produced (the target table is defined by UNIQUE).&amp;nbsp;Runtime error: ITAB_DUPLICATE_KEY".&lt;/P&gt;&lt;P&gt;So, I guess that SAP could improve the documentation and clarify.&lt;/P&gt;&lt;P&gt;My tests:&lt;/P&gt;&lt;LI-CODE lang="abap"&gt;DATA(itab3) = VALUE _hashed_table_type( ).
TRY.
    CASE 6.
      WHEN 1.
        " Runtime error ITAB_DUPLICATE_KEY if duplicate
        itab3 = CORRESPONDING _hashed_table_type( itab1 ).
      WHEN 2.
        " Runtime error ITAB_DUPLICATE_KEY if duplicate
        MOVE-CORRESPONDING itab1 TO itab3.
      WHEN 3.
        " Runtime error ITAB_DUPLICATE_KEY if duplicate
        INSERT LINES OF itab1 INTO TABLE itab3.
      WHEN 4.
        " SY-SUBRC = 4 if duplicate
        INSERT VALUE #( id = 1 name = |Tamit| ) INTO TABLE itab3.
        INSERT VALUE #( id = 1 name = |Tamit| ) INTO TABLE itab3.
      WHEN 5.
        " CX_SY_ITAB_DUPLICATE_KEY if duplicate
        itab3 = VALUE _hashed_table_type( FOR &amp;lt;itab&amp;gt; IN itab1 ( CORRESPONDING #( &amp;lt;itab&amp;gt; ) ) ).
      WHEN 6.
        " Discard duplicate lines
        itab3 = CORRESPONDING _hashed_table_type( itab1 DISCARDING DUPLICATES ).
    ENDCASE.
  CATCH cx_root INTO DATA(error).
ENDTRY.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 25 Jul 2024 08:40:51 GMT</pubDate>
    <dc:creator>Sandra_Rossi</dc:creator>
    <dc:date>2024-07-25T08:40:51Z</dc:date>
    <item>
      <title>Runtime Exception ITAB_DUPLICATE_KEY behaves differently for CORRESPONDING and FOR loop</title>
      <link>https://community.sap.com/t5/technology-q-a/runtime-exception-itab-duplicate-key-behaves-differently-for-corresponding/qaq-p/13770431</link>
      <description>&lt;P&gt;Hi Experts,&lt;/P&gt;&lt;P&gt;I think this is a shortcoming from SAPs side, the runtime exception ITAB_DUPLICATE_KEY behaves differently in case of CORRESPONDING component operator and FOR loop. In case of the former, the runtime exception cannot be caught. However, in case of the later there is an exception class CX_SY_ITAB_DUPLICATE_KEY which can be caught and handled.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Scenario: Moving data from an internal table of type standard to internal table of type hashed.&lt;/P&gt;&lt;P&gt;Consider the following code.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="abap"&gt;TYPES:
  BEGIN OF _structure,
    id   TYPE i,
    name TYPE string,
  END OF _structure,

_standard_table_type TYPE STANDARD TABLE OF _structure WITH EMPTY KEY,
_hashed_table_type   TYPE HASHED TABLE OF _structure WITH UNIQUE KEY id.

DATA(itab1) = VALUE _standard_table_type( 
                ( id = 1 name = |Tamit| ) 
                ( id = 1 name = |Tamit| ) 
                ( id = 2 name = |tamit| ) 
                ( id = 3 name = |Tamit New| ) ).&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now if we create a hashed internal table from the above mentioned standard table using for the exception can be caught as displayed below.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="abap"&gt;TRY.
  DATA(itab2) = VALUE _hashed_table_type( FOR &amp;lt;itab&amp;gt; IN itab1 ( &amp;lt;itab&amp;gt; ) ).
    CATCH cx_sy_itab_duplicate_key INTO DATA(exception1).
      out-&amp;gt;write( |Exception raised via For Loop| ).
      out-&amp;gt;write( exception1-&amp;gt;get_text( ) ).
      out-&amp;gt;write( exception1-&amp;gt;get_longtext( ) ).
ENDTRY.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, we can create the same hashed table using corresponding - component operator as shown below. But in this case the exception cannot be caught.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="abap"&gt;DATA(itab3) = CORRESPONDING _hashed_table_type( itab1 ).&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;The question to the community is that shouldn't in both the case the exception be allowed to be caught as the runtime error is same in both the cases.&lt;/P&gt;&lt;P&gt;&lt;LI-PRODUCT title="SAP NetWeaver Application Server for ABAP" id="01200314690800000234"&gt;&lt;/LI-PRODUCT&gt;&amp;nbsp;&lt;LI-PRODUCT title="SAP S/4HANA Cloud ABAP Environment" id="60907aa9-99e9-4d5d-9103-8b970e9bc0a4"&gt;&lt;/LI-PRODUCT&gt;&amp;nbsp;&lt;LI-PRODUCT title="ABAP Extensibility" id="338571334339306322581424656448659"&gt;&lt;/LI-PRODUCT&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 24 Jul 2024 13:29:31 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/runtime-exception-itab-duplicate-key-behaves-differently-for-corresponding/qaq-p/13770431</guid>
      <dc:creator>tamitdassharma</dc:creator>
      <dc:date>2024-07-24T13:29:31Z</dc:date>
    </item>
    <item>
      <title>Re: Runtime Exception ITAB_DUPLICATE_KEY behaves differently for CORRESPONDING and FOR loop</title>
      <link>https://community.sap.com/t5/technology-q-a/runtime-exception-itab-duplicate-key-behaves-differently-for-corresponding/qaa-p/13771212#M4838275</link>
      <description>Sure, this would be correct, fully acknowledge&lt;span class="lia-unicode-emoji" title=":thumbs_up:"&gt;👍&lt;/span&gt;</description>
      <pubDate>Thu, 25 Jul 2024 06:44:28 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/runtime-exception-itab-duplicate-key-behaves-differently-for-corresponding/qaa-p/13771212#M4838275</guid>
      <dc:creator>MichiFr</dc:creator>
      <dc:date>2024-07-25T06:44:28Z</dc:date>
    </item>
    <item>
      <title>Re: Runtime Exception ITAB_DUPLICATE_KEY behaves differen...</title>
      <link>https://community.sap.com/t5/technology-q-a/runtime-exception-itab-duplicate-key-behaves-differently-for-corresponding/qaa-p/13771457#M4838296</link>
      <description>&lt;P&gt;Reproduced in 7.52 SP 04.&lt;/P&gt;&lt;P&gt;As far as I can see in the documentation of&amp;nbsp;&lt;A href="https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abencorresponding_variants_s_abexa.htm" target="_self"&gt;CORRESPONDING&lt;/A&gt;, it says that to avoid a duplicate exception (doesn't say what kind of exception it is), DISCARDING DUPLICATES is to be used.&lt;/P&gt;&lt;P&gt;Now, what should happen exactly if a duplicate happens, according to the documentation and is that true if I test in ABAP?&lt;/P&gt;&lt;P&gt;The documentation of CORRESPONDING&amp;nbsp;&lt;SPAN&gt;says that the insertion behaves like MOVE-CORRESPONDING (for itabs).&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The&amp;nbsp;documentation of&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapmove-corresponding.htm#!ABAP_VARIANT_2@2@" target="_self"&gt;MOVE-CORRESPONDING&lt;/A&gt;&lt;SPAN&gt; says that "Finally, and if necessary, the table keys and associated table indexes are updated in the target table in accordance with the rules for insertions in internal tables. The corresponding exceptions are raised if uniqueness is violated."&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;It's still not very clear what exact rules are managed for "insertions in internal tables", it behaves differently for one line at a time and for one set of lines (LINES OF).&lt;/P&gt;&lt;P&gt;If you use MOVE-CORRESPONDING, you get ITAB_DUPLICATE_KEY.&lt;/P&gt;&lt;P&gt;If you use INSERT one line without using LINES OF, the line is&amp;nbsp;not inserted, SY-SUBRC is set to 4 and the program continues.&lt;/P&gt;&lt;P&gt;If you use INSERT LINES OF, you get ITAB_DUPLICATE_KEY.&lt;/P&gt;&lt;P&gt;The documentation of &lt;A href="https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapinsert_itab_linespec.htm#!ABAP_ALTERNATIVE_3@3@" target="_self"&gt;INSERT, line_spec &amp;gt; LINES OF&lt;/A&gt;&amp;nbsp;seems wrong to me: "If there is a conflict with an existing unique table key, a catchable exception is raised when inserting multiple lines from an internal table."&lt;/P&gt;&lt;P&gt;If you look at the general documentation of&amp;nbsp;&lt;A href="https://help.sap.com/doc/abapdocu_latest_index_htm/latest/en-US/index.htm?file=abapinsert_itab.htm" target="_blank" rel="noopener"&gt;INSERT - itab&lt;/A&gt;, you finally obtain the correct rule lost at the bottom of the page: "Uncatchable Exceptions Cause: When inserting a set of lines, entries with an identical key were produced (the target table is defined by UNIQUE).&amp;nbsp;Runtime error: ITAB_DUPLICATE_KEY".&lt;/P&gt;&lt;P&gt;So, I guess that SAP could improve the documentation and clarify.&lt;/P&gt;&lt;P&gt;My tests:&lt;/P&gt;&lt;LI-CODE lang="abap"&gt;DATA(itab3) = VALUE _hashed_table_type( ).
TRY.
    CASE 6.
      WHEN 1.
        " Runtime error ITAB_DUPLICATE_KEY if duplicate
        itab3 = CORRESPONDING _hashed_table_type( itab1 ).
      WHEN 2.
        " Runtime error ITAB_DUPLICATE_KEY if duplicate
        MOVE-CORRESPONDING itab1 TO itab3.
      WHEN 3.
        " Runtime error ITAB_DUPLICATE_KEY if duplicate
        INSERT LINES OF itab1 INTO TABLE itab3.
      WHEN 4.
        " SY-SUBRC = 4 if duplicate
        INSERT VALUE #( id = 1 name = |Tamit| ) INTO TABLE itab3.
        INSERT VALUE #( id = 1 name = |Tamit| ) INTO TABLE itab3.
      WHEN 5.
        " CX_SY_ITAB_DUPLICATE_KEY if duplicate
        itab3 = VALUE _hashed_table_type( FOR &amp;lt;itab&amp;gt; IN itab1 ( CORRESPONDING #( &amp;lt;itab&amp;gt; ) ) ).
      WHEN 6.
        " Discard duplicate lines
        itab3 = CORRESPONDING _hashed_table_type( itab1 DISCARDING DUPLICATES ).
    ENDCASE.
  CATCH cx_root INTO DATA(error).
ENDTRY.&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 25 Jul 2024 08:40:51 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/runtime-exception-itab-duplicate-key-behaves-differently-for-corresponding/qaa-p/13771457#M4838296</guid>
      <dc:creator>Sandra_Rossi</dc:creator>
      <dc:date>2024-07-25T08:40:51Z</dc:date>
    </item>
    <item>
      <title>Re: Runtime Exception ITAB_DUPLICATE_KEY behaves differen...</title>
      <link>https://community.sap.com/t5/technology-q-a/runtime-exception-itab-duplicate-key-behaves-differently-for-corresponding/qaa-p/13776573#M4838916</link>
      <description>&lt;P&gt;Indeed, there is a translation error in the documentation of INSERT LINES. The documentation was in German originally, where it read "unbehandelbare Ausnahme". Unfortunately. we are fighting, with lots of these. Corrected for 2411,&lt;/P&gt;</description>
      <pubDate>Tue, 30 Jul 2024 07:16:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/technology-q-a/runtime-exception-itab-duplicate-key-behaves-differently-for-corresponding/qaa-p/13776573#M4838916</guid>
      <dc:creator>retired_member</dc:creator>
      <dc:date>2024-07-30T07:16:18Z</dc:date>
    </item>
  </channel>
</rss>

