Skip to Content
avatar image
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 comment
10|10000 characters needed characters exceeded

  • Follow
  • Get RSS Feed

2 Answers

  • Best Answer
    avatar image
    Former Member
    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 comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      hi s chandra

      thanks for giving ideafor the previous query.

      can you find out my query sent on 3rd july 2007. topic name is 'dynamic sql query' . I thing you can give answer for that.

      thanks & regards

      ajit

  • avatar image
    Former Member
    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 comment
    10|10000 characters needed characters exceeded