<?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 more dynamic select statements with same target itab in Application Development and Automation Discussions</title>
    <link>https://community.sap.com/t5/application-development-and-automation-discussions/more-dynamic-select-statements-with-same-target-itab/m-p/572211#M22326</link>
    <description>&lt;P&gt;Hi everyone!&lt;/P&gt;
  &lt;P&gt;I want to read different tables for material data (MARA, MARC, MARD etc.) dynamically, because the selected fields, tables and the where condition are just known at runtime.&lt;/P&gt;
  &lt;P&gt;The result should be in one target for all different source tables.&lt;/P&gt;
  &lt;P&gt;non-dynamic it could be something like that:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;SELECT matnr matkl
          FROM mara
          INTO (rt_material-matnr, rt_material-matkl)
          WHERE matkl EQ '123'.


SELECT werks lgort
          FROM mard
          INTO (rt_material-werks, rt_material-lgort)
          FOR ALL ENTRIES IN rt_material
          WHERE matnr EQ rt_material-matnr
            AND werks EQ '01'.
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;The dynamic select I have already. But I don't know how to make the target field list.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;SELECT (&amp;lt;fs_fields&amp;gt;-sfields)
          FROM (&amp;lt;fs_fields&amp;gt;-tabname)
          INTO CORRESPONDING FIELDS OF TABLE rt_material
          WHERE (lv_where).&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;I have a loop at the different tables, I want to select from, where I call my dynamic select. But each dynamic select deletes the result from the earlier select. I hope you understand me.&lt;/P&gt;
  &lt;P&gt;Following is my whole code for this.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;types:
    BEGIN OF ty_sel_fields,
        tabname TYPE tabname,
        sfields TYPE edpline_t,
      END OF ty_sel_fields .
  types:
    tt_sel_fields TYPE TABLE OF ty_sel_fields .

types:
    BEGIN OF ty_conditions,
      tabname TYPE tabname,
      value   TYPE string,
    END OF ty_conditions .
  types:
    tt_conditions TYPE TABLE OF ty_conditions WITH KEY tabname.


METHOD read_material_dynamic.

    DATA: lt_sel_fields TYPE tt_sel_fields,
          lt_where_cond TYPE tt_conditions,
          lv_where      TYPE string.

    FIELD-SYMBOLS: &amp;lt;fs_conditions&amp;gt; TYPE ty_conditions,
                   &amp;lt;fs_fields&amp;gt;     TYPE ty_sel_fields.

    " convert select-options
    CALL METHOD me-&amp;gt;conv_dynamic_conditions
      EXPORTING
        it_conditions = it_conditions
      RECEIVING
        rt_where_cond = lt_where_cond.

    " zu selektierende Felder je Tabelle
    LOOP AT it_sel_fields ASSIGNING &amp;lt;fs_fields&amp;gt;.

      " Where-Bedingung zur Tabelle
      READ TABLE it_where_cond WITH TABLE KEY tabname = &amp;lt;fs_fields&amp;gt;-tabname ASSIGNING &amp;lt;fs_conditions&amp;gt;.

      lv_where = &amp;lt;fs_conditions&amp;gt;-value.

IF &amp;lt;fs_fields&amp;gt;-tabname EQ 'MARA'.
        " MARA is first table for select
        SELECT (&amp;lt;fs_fields&amp;gt;-sfields)
          FROM (&amp;lt;fs_fields&amp;gt;-tabname)
          INTO CORRESPONDING FIELDS OF TABLE rt_material
          WHERE (lv_where).

      ELSE.

        SELECT (&amp;lt;fs_fields&amp;gt;-sfields)
          FROM (&amp;lt;fs_fields&amp;gt;-tabname)
          INTO CORRESPONDING FIELDS OF TABLE rt_material
          FOR ALL ENTRIES IN rt_material
          WHERE matnr EQ rt_material-matnr
            AND (lv_where).

      ENDIF.

    ENDLOOP.

  ENDMETHOD.



  METHOD conv_dynamic_conditions.

    DATA: lt_where      TYPE rsds_twhere,
          ls_where_cond TYPE ty_conditions.

    FIELD-SYMBOLS: &amp;lt;fs_where&amp;gt;     TYPE rsds_where,
                   &amp;lt;fs_where_tab&amp;gt; TYPE rsdswhere.

    CALL FUNCTION 'FREE_SELECTIONS_RANGE_2_WHERE'
      EXPORTING
        field_ranges  = it_conditions
      IMPORTING
        where_clauses = lt_where.

    LOOP AT lt_where ASSIGNING &amp;lt;fs_where&amp;gt;.
      CLEAR ls_where_cond.
      ls_where_cond-tabname = &amp;lt;fs_where&amp;gt;-tablename.

      LOOP AT &amp;lt;fs_where&amp;gt;-where_tab ASSIGNING &amp;lt;fs_where_tab&amp;gt;.
        CONCATENATE ls_where_cond-value &amp;lt;fs_where_tab&amp;gt; INTO ls_where_cond-value SEPARATED BY space.
      ENDLOOP.

      APPEND ls_where_cond TO rt_where_cond.

    ENDLOOP.

  ENDMETHOD.&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;I hope you understand my problem and someone can help me.&lt;/P&gt;
  &lt;P&gt;Best Regards&lt;/P&gt;
  &lt;P&gt;Carina&lt;/P&gt;</description>
    <pubDate>Thu, 19 Oct 2017 12:55:09 GMT</pubDate>
    <dc:creator>Former Member</dc:creator>
    <dc:date>2017-10-19T12:55:09Z</dc:date>
    <item>
      <title>more dynamic select statements with same target itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/more-dynamic-select-statements-with-same-target-itab/m-p/572211#M22326</link>
      <description>&lt;P&gt;Hi everyone!&lt;/P&gt;
  &lt;P&gt;I want to read different tables for material data (MARA, MARC, MARD etc.) dynamically, because the selected fields, tables and the where condition are just known at runtime.&lt;/P&gt;
  &lt;P&gt;The result should be in one target for all different source tables.&lt;/P&gt;
  &lt;P&gt;non-dynamic it could be something like that:&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;SELECT matnr matkl
          FROM mara
          INTO (rt_material-matnr, rt_material-matkl)
          WHERE matkl EQ '123'.


SELECT werks lgort
          FROM mard
          INTO (rt_material-werks, rt_material-lgort)
          FOR ALL ENTRIES IN rt_material
          WHERE matnr EQ rt_material-matnr
            AND werks EQ '01'.
&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;The dynamic select I have already. But I don't know how to make the target field list.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;SELECT (&amp;lt;fs_fields&amp;gt;-sfields)
          FROM (&amp;lt;fs_fields&amp;gt;-tabname)
          INTO CORRESPONDING FIELDS OF TABLE rt_material
          WHERE (lv_where).&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;I have a loop at the different tables, I want to select from, where I call my dynamic select. But each dynamic select deletes the result from the earlier select. I hope you understand me.&lt;/P&gt;
  &lt;P&gt;Following is my whole code for this.&lt;/P&gt; 
  &lt;PRE&gt;&lt;CODE&gt;types:
    BEGIN OF ty_sel_fields,
        tabname TYPE tabname,
        sfields TYPE edpline_t,
      END OF ty_sel_fields .
  types:
    tt_sel_fields TYPE TABLE OF ty_sel_fields .

types:
    BEGIN OF ty_conditions,
      tabname TYPE tabname,
      value   TYPE string,
    END OF ty_conditions .
  types:
    tt_conditions TYPE TABLE OF ty_conditions WITH KEY tabname.


METHOD read_material_dynamic.

    DATA: lt_sel_fields TYPE tt_sel_fields,
          lt_where_cond TYPE tt_conditions,
          lv_where      TYPE string.

    FIELD-SYMBOLS: &amp;lt;fs_conditions&amp;gt; TYPE ty_conditions,
                   &amp;lt;fs_fields&amp;gt;     TYPE ty_sel_fields.

    " convert select-options
    CALL METHOD me-&amp;gt;conv_dynamic_conditions
      EXPORTING
        it_conditions = it_conditions
      RECEIVING
        rt_where_cond = lt_where_cond.

    " zu selektierende Felder je Tabelle
    LOOP AT it_sel_fields ASSIGNING &amp;lt;fs_fields&amp;gt;.

      " Where-Bedingung zur Tabelle
      READ TABLE it_where_cond WITH TABLE KEY tabname = &amp;lt;fs_fields&amp;gt;-tabname ASSIGNING &amp;lt;fs_conditions&amp;gt;.

      lv_where = &amp;lt;fs_conditions&amp;gt;-value.

IF &amp;lt;fs_fields&amp;gt;-tabname EQ 'MARA'.
        " MARA is first table for select
        SELECT (&amp;lt;fs_fields&amp;gt;-sfields)
          FROM (&amp;lt;fs_fields&amp;gt;-tabname)
          INTO CORRESPONDING FIELDS OF TABLE rt_material
          WHERE (lv_where).

      ELSE.

        SELECT (&amp;lt;fs_fields&amp;gt;-sfields)
          FROM (&amp;lt;fs_fields&amp;gt;-tabname)
          INTO CORRESPONDING FIELDS OF TABLE rt_material
          FOR ALL ENTRIES IN rt_material
          WHERE matnr EQ rt_material-matnr
            AND (lv_where).

      ENDIF.

    ENDLOOP.

  ENDMETHOD.



  METHOD conv_dynamic_conditions.

    DATA: lt_where      TYPE rsds_twhere,
          ls_where_cond TYPE ty_conditions.

    FIELD-SYMBOLS: &amp;lt;fs_where&amp;gt;     TYPE rsds_where,
                   &amp;lt;fs_where_tab&amp;gt; TYPE rsdswhere.

    CALL FUNCTION 'FREE_SELECTIONS_RANGE_2_WHERE'
      EXPORTING
        field_ranges  = it_conditions
      IMPORTING
        where_clauses = lt_where.

    LOOP AT lt_where ASSIGNING &amp;lt;fs_where&amp;gt;.
      CLEAR ls_where_cond.
      ls_where_cond-tabname = &amp;lt;fs_where&amp;gt;-tablename.

      LOOP AT &amp;lt;fs_where&amp;gt;-where_tab ASSIGNING &amp;lt;fs_where_tab&amp;gt;.
        CONCATENATE ls_where_cond-value &amp;lt;fs_where_tab&amp;gt; INTO ls_where_cond-value SEPARATED BY space.
      ENDLOOP.

      APPEND ls_where_cond TO rt_where_cond.

    ENDLOOP.

  ENDMETHOD.&lt;/CODE&gt;&lt;/PRE&gt;
  &lt;P&gt;I hope you understand my problem and someone can help me.&lt;/P&gt;
  &lt;P&gt;Best Regards&lt;/P&gt;
  &lt;P&gt;Carina&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 12:55:09 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/more-dynamic-select-statements-with-same-target-itab/m-p/572211#M22326</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-10-19T12:55:09Z</dc:date>
    </item>
    <item>
      <title>Re: more dynamic select statements with same target itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/more-dynamic-select-statements-with-same-target-itab/m-p/572212#M22327</link>
      <description>&lt;P&gt;Can you create your target with RTTC?&lt;/P&gt;
  &lt;P&gt;See example&lt;/P&gt;
  &lt;P&gt;&lt;A href="https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abendynamic_sql_abexa.htm" target="test_blank"&gt;https://help.sap.com/http.svc/rc/abapdocu_752_index_htm/7.52/en-US/index.htm?file=abendynamic_sql_abexa.htm&lt;/A&gt;&lt;/P&gt;
  &lt;P&gt;But if it is about "But each dynamic select deletes the result from the earlier select. I hope you understand me." : APPENDING instead of INTO?&lt;/P&gt;</description>
      <pubDate>Thu, 19 Oct 2017 17:50:04 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/more-dynamic-select-statements-with-same-target-itab/m-p/572212#M22327</guid>
      <dc:creator>retired_member</dc:creator>
      <dc:date>2017-10-19T17:50:04Z</dc:date>
    </item>
    <item>
      <title>Re: more dynamic select statements with same target itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/more-dynamic-select-statements-with-same-target-itab/m-p/572213#M22328</link>
      <description>&lt;P&gt;Interesting. As Horst said, appending will stop the overwrite, but I guess you also want the relevant fields next to each other in the same row(s). This becomes messy due to 1:n and n:n relationships. &lt;/P&gt;
  &lt;P&gt;The only two options I can see is either doing the SELECTs into individual tables and combining them afterwards, or a big dynamic join. With a join you could include all possible tables and add a dummy condition (matnr = '999999' or something like that) if the table is not relevant. Not sure how well this would work.&lt;/P&gt;
  &lt;P&gt;Personally I'd go for looping over the tables and doing individual SELECTs using dynamic @data(result) declarations, building your final structure out of the results using RTT*, then merging the lot.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2017 06:04:49 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/more-dynamic-select-statements-with-same-target-itab/m-p/572213#M22328</guid>
      <dc:creator>pokrakam</dc:creator>
      <dc:date>2017-10-20T06:04:49Z</dc:date>
    </item>
    <item>
      <title>Re: more dynamic select statements with same target itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/more-dynamic-select-statements-with-same-target-itab/m-p/572214#M22329</link>
      <description>&lt;P&gt;I tried APPENDING already. And you're right: I want the relevant fields next to each other in the same row. So APPENDING is not the right solution.&lt;/P&gt;
  &lt;P&gt;I will try your idea with the SELECTs into individual tables and combining them afterwards. I think this is the best solution. As far as I know, a big JOIN can have a negative effect on the performance...&lt;/P&gt;
  &lt;P&gt;Thank you for your fast answer &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2017 06:30:19 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/more-dynamic-select-statements-with-same-target-itab/m-p/572214#M22329</guid>
      <dc:creator>Former Member</dc:creator>
      <dc:date>2017-10-20T06:30:19Z</dc:date>
    </item>
    <item>
      <title>Re: more dynamic select statements with same target itab</title>
      <link>https://community.sap.com/t5/application-development-and-automation-discussions/more-dynamic-select-statements-with-same-target-itab/m-p/572215#M22330</link>
      <description>&lt;P&gt;i do not know in which release you are and if you can use Horst's suggestion (with a little work you can create the dynamic structure like for the example), but if not, you can always select the table keys (MATNR-WERKS-LGORT), use the APPEND, sort the internal table, LOOP it and use "AT NEW..." statement to build the final one.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Oct 2017 07:06:06 GMT</pubDate>
      <guid>https://community.sap.com/t5/application-development-and-automation-discussions/more-dynamic-select-statements-with-same-target-itab/m-p/572215#M22330</guid>
      <dc:creator>SimoneMilesi</dc:creator>
      <dc:date>2017-10-20T07:06:06Z</dc:date>
    </item>
  </channel>
</rss>

