Dynamic Data Selection – Dynamic SQL

Hi Techies!

There will be instances where you need to read tables dynamically. For a example when you work with access sequences in SAP, you will need to read data using this method.

In the below example, program is defined in such a way that the table which it needs to fetch data from is determined only at run-time.

User have the option of reading data from either EKKO (Purchasing Document Header) or EKPO (Purchasing Document Item) tables. Selection parameters which can be used to read each table is different. Selection parameters Purchasing document number and company code can read can filter entries in EKKO, where Purchasing document, line item and material can filter the result of EKPO.

Selection Screen.

For option EKKO.

For Option EKPO.

 

Selection Screen Definition:

Enable and disable selection screen parameters at PBO.

 

Logic to determine the table and where conditions at run-time.

 

Fetch data dynamically.

SAP, SAP ABAP, Dynamic data fetching, Dynamic SQL, dynamic select, access sequence, PBO, PAI, hide screen fields, modify screen, Isuru Fernando.

TECH GADGETS I USE EVERY DAY

These are some of the tech gadgets I use every day. If you make a purchase through these links I will earn a small comission at absolutely no extra cost to you.

11 thoughts on “Dynamic Data Selection – Dynamic SQL

  1. Amit says:

    You can also make fields part dynamic too

    data : lv_fields type string.
    concatenate 'EBELN' 'BUKRS' etc etc into lv_fields.

    Select (lv_fields)
    from table
    where condition.

  2. edwin ermac says:

    Hi, I have been doing several dynamic selection, but I do not know if you could help me but this problem is quite strange.

    I am doing a dynamic selection of TIME Master data from an INFOPROVIDER.

    The code is actually redundant, but both contains the same entry.
    *Select dynamically the YEAR.MONTH figures under TIME
    SELECT (lv_fld1) INTO TABLE lt_time FROM (lv_tab)
    WHERE (lv_where) .
    IF sy-subrc IS INITIAL.
    APPEND LINES OF lt_time TO tab_time.
    ELSE.
    SELECT /b28/s_lqd4j2k FROM /b28/plqd4j2k INTO TABLE lt_time
    WHERE /b28/s_tilevel = 'MONTH' AND /b28/s_year = year. "(lv_where).
    IF sy-subrc IS INITIAL.
    APPEND LINES OF lt_time TO tab_time.
    ENDIF.
    ENDIF.

    The hardcoded figures, seem retrieving but the dynamic one is not.

    I checked on my code and the entry on the fields, they are seem correct. Is there anything that I miss?

    Thanks for this useful blog. Hope anyone can help.

  3. Johny Jackson says:

    Thanks for your marvelous posting! I quite enjoyed reading it, you
    might be a great author.I will make sure to
    bookmark your blog and will eventually come back at some point.
    I want to encourage continue your great writing, have a nice morning!

  4. Suraj Grewal says:

    Hi Isuru ,
    I am working on a requirement where I have to perform a select based on reading the fields from a table and performing some filter conditions which is again maintained in a z table . This operation I am unable to perform in ABAP as it is throwing dump since number of filters are very large so I am going with AMDP where I am able to apply filter with APPLY_FILTER method and group by also can be done by CE_AGGREGATION but CE_AGGREGATION takes the field values hardcoded which I need to pass dynamically 🙂 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

Get the latest tips and tricks on SAP!