<?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: Insert into a DB table based on condition in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194839#M1981172</link>
    <description>&lt;P&gt;Thank you very much!&lt;/P&gt;</description>
    <pubDate>Fri, 22 May 2020 11:22:06 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2020-05-22T11:22:06Z</dc:date>
    <item>
      <title>Insert into a DB table based on condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194834#M1981167</link>
      <description>&lt;P&gt;Hi!&lt;BR /&gt;I have a selection screen that insrts into a DB table. it has two options- enter an entry with start time, and then enter an entry with end time. is it possible to create a logic that when someone creats an entry with end time, it will insert to the same table row of the start time, just the end time?&lt;BR /&gt;I need it to insert into the &lt;STRONG&gt;last &lt;/STRONG&gt;entry in X table where y=y (one of the fields needs to be eq on both entries).&lt;BR /&gt;and then define which fields will be inserted where.&lt;/P&gt;
  &lt;P&gt;maybe someone has an idea how to achive that?&lt;/P&gt;
  &lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 04:38:18 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194834#M1981167</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-05-21T04:38:18Z</dc:date>
    </item>
    <item>
      <title>Re: Insert into a DB table based on condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194835#M1981168</link>
      <description>&lt;P&gt;In a relational database &lt;STRONG&gt;there is no concept of order&lt;/STRONG&gt;. Therefore, unless you build it in, there is no "last entry".&lt;/P&gt;&lt;P&gt;It would be helpful if you give some example of what you're trying to achieve, i.e. what's in the table before, what action you take and what you want in the table afterwards.&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 05:24:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194835#M1981168</guid>
      <dc:creator>matt</dc:creator>
      <dc:date>2020-05-21T05:24:49Z</dc:date>
    </item>
    <item>
      <title>Re: Insert into a DB table based on condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194836#M1981169</link>
      <description>&lt;P&gt;&lt;SPAN class="mention-scrubbed"&gt;userc&lt;/SPAN&gt;,&lt;/P&gt;&lt;P&gt;I can understand that you are trying to log the changes but could not get the complete business requirement, if you could help us an example it will be better to suggest a solution.&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 05:30:57 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194836#M1981169</guid>
      <dc:creator>former_member1716</dc:creator>
      <dc:date>2020-05-21T05:30:57Z</dc:date>
    </item>
    <item>
      <title>Re: Insert into a DB table based on condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194837#M1981170</link>
      <description>&lt;P&gt;Hi  &lt;SPAN class="mention-scrubbed"&gt;userc&lt;/SPAN&gt; &lt;/P&gt;&lt;P&gt;Maybe something like this?&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;PARAMETERS: p_y TYPE char10 OBLIGATORY.
PARAMETERS: p_start TYPE dats.
PARAMETERS: p_end TYPE dats.

START-OF-SELECTION.
  PERFORM run_report.
  COMMIT WORK AND WAIT.

FORM run_report.
  IF p_start IS INITIAL AND p_end IS INITIAL.
    WRITE: / 'Please provide Start or End date'.
    RETURN. " nothing to do
  ENDIF.

  SELECT *
    FROM ztable
    WHERE y = @p_y
    ORDER BY record_count DESC " I assume there is some record counting column
    INTO @DATA(ls_ztable)
    UP TO 1 ROWS.
  IF sy-subrc = 0.
    IF p_start IS NOT INITIAL.
      IF ls_ztable-end_date IS INITIAL.
        WRITE: / 'You should provide End date for the last record'.
        RETURN.
      ELSE.
        " last record is closed
        PERFORM create_record USING ls_ztable-record_count.
      ENDIF.

      RETURN.
    ENDIF.

    IF p_end IS NOT INITIAL.
      IF ls_ztable-end_date IS NOT INITIAL.
        WRITE: / 'Last record already closed. Create new one'.
        RETURN.
      ELSE.
        ls_ztable-end_date = p_end.
        MODIFY ztable FROM ls_ztable.
      ENDIF.
    ENDIF.
  ELSE.
    " no last record found
    " create new but only if Start date provided
    IF p_start IS NOT INITIAL.
      PERFORM create_record USING 0.
    ELSE.
      WRITE: / 'No last record found'.
    ENDIF.
  ENDIF.
ENDFORM.

FORM create_record USING iv_record_count TYPE i.
  DATA:
    ls_ztable TYPE ztable.

  CHECK p_start IS NOT INITIAL.

  ls_ztable-y = p_y.
  ls_ztable-record_count = iv_record_count + 1.
  ls_ztable-start_date = p_start.
  INSERT INTO ztable FROM ls_ztable.
ENDFORM.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;My assumption is that there is some record counting key column in the table.&lt;/P&gt;&lt;P&gt;It my be a simple integer column, a numc or a date one. You have to have a look and modify the code respecitvely.&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;&lt;P&gt;Mateusz&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 06:54:20 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194837#M1981170</guid>
      <dc:creator>MateuszAdamus</dc:creator>
      <dc:date>2020-05-21T06:54:20Z</dc:date>
    </item>
    <item>
      <title>Re: Insert into a DB table based on condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194838#M1981171</link>
      <description>&lt;P&gt;There is a very simple way to do that within a single OpenSQL statement since ABAP Release 7.52 (also in S/4HANA OP 1709).&lt;/P&gt;&lt;P&gt;Since 7.50, you can use a subquery as a data source for an INSERT statement in OpenSQL:&lt;BR /&gt;&lt;A href="https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abennews-750-open_sql.htm#!ABAP_MODIFICATION_8@8@"&gt;https://help.sap.com/doc/abapdocu_750_index_htm/7.50/en-US/abennews-750-open_sql.htm#!ABAP_MODIFICATION_8@8@&lt;/A&gt; &lt;BR /&gt;Since 7.52 you can use the ORDER BY clause and the UP TO addition within the subquery: &lt;A href="https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abennews-752-open_sql.htm#!ABAP_MODIFICATION_6@6@"&gt;https://help.sap.com/doc/abapdocu_752_index_htm/7.52/en-US/abennews-752-open_sql.htm#!ABAP_MODIFICATION_6@6@&lt;/A&gt;&lt;/P&gt;&lt;P&gt;These two changes altogether allow exactly what you are looking for -- without transferring the records from database to application server and transferring them back.&lt;/P&gt;</description>
      <pubDate>Thu, 21 May 2020 20:39:38 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194838#M1981171</guid>
      <dc:creator>gasparerdelyi</dc:creator>
      <dc:date>2020-05-21T20:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Insert into a DB table based on condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194839#M1981172</link>
      <description>&lt;P&gt;Thank you very much!&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 11:22:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194839#M1981172</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-05-22T11:22:06Z</dc:date>
    </item>
    <item>
      <title>Re: Insert into a DB table based on condition</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194840#M1981173</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 11:22:35 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/insert-into-a-db-table-based-on-condition/m-p/12194840#M1981173</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2020-05-22T11:22:35Z</dc:date>
    </item>
  </channel>
</rss>

