Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Dynamic columns in ABAP queries

Hi

I want to select column names dynamically from a DDIC table. For example, the normal way is “select <column_name1>, <column_name2> from <tablename>”. But what I need is something like “select variable1, variable2 from <tablename>” where variable1 and variable2 are names of the column like variable1 = ‘<column_name1>’ and variable2 = ‘<column_name2>’. Is this possible?

Santo.

Tags:
Former Member replied

Hi,

Check out this code:

DATA: tabname LIKE dd03l-tabname VALUE 'T001'.

DATA: BEGIN OF fieldtab OCCURS 0,
       text(100),
      END OF fieldtab.

DATA: BEGIN OF destab OCCURS 0,
       text(100),
      END OF destab.


DATA dref TYPE REF TO data.

FIELD-SYMBOLS: <fs> TYPE ANY.
FIELD-SYMBOLS: <fs1> TYPE ANY.

* creates dynamic data
CREATE DATA dref TYPE (tabname).
ASSIGN dref->* TO <fs>.
*


SELECT * INTO CORRESPONDING FIELDS OF <fs> FROM (tabname).
  SKIP.
  DO.
    ASSIGN COMPONENT  sy-index OF  STRUCTURE <fs> TO <fs1>.
    IF sy-subrc = 0.
      WRITE: <fs1>.
    ELSE.
      EXIT.
    ENDIF.
  ENDDO.

ENDSELECT.

We can also restrict the number of columns by dynamically building field list and target list as internal table.

Even the query after where can be dynamically built.

Thanks and regards,

S. Chandra Mouli.

0 View this answer in context

Helpful Answer

by
Not what you were looking for? View more on this topic or Ask a question