Skip to Content
author's profile photo Former Member
Former Member

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.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on May 29, 2007 at 04:57 AM

    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.

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 29, 2007 at 04:53 AM

    please go through the link there it was give from the field selection screen ....how to set it and the sap list variant .....etc

    http://help.sap.com/saphelp_nw04/helpdata/en/d2/cb416c455611d189710000e8322d00/frameset.htm>

    Girish

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.