Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic columns in ABAP queries

Former Member
0 Kudos

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.

1 ACCEPTED SOLUTION

former_member784222
Active Participant
0 Kudos

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.

5 REPLIES 5

Former Member
0 Kudos

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

<a href="http://">http://help.sap.com/saphelp_nw04/helpdata/en/d2/cb416c455611d189710000e8322d00/frameset.htm</a>

Girish

former_member784222
Active Participant
0 Kudos

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 Kudos

hi s. chandra

I saw your forums posted long back .there you have mentioned that

' we can restrict the number of columns by dynamically building field list and target list as internal table' . can you provide the code for that.

actually i have a urgent requirement on dynamic concept.

i want to get value from one table by compared with the other table.

for this two table(actual table and compare table ) is required. as well as actual field for which result is required from actual table and compared fields from actual table

my requirement is like, i have created a maintenance table. there customer has to maintain table name, field name and compared fields from target table . and also they have to maintain for compared table and compared fields in the same table with which query will compare.

how can i write a query based on this.

thanks & regards

ajit

0 Kudos

Hi,

Here we go:



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

DATA: BEGIN OF field_list OCCURS 0,
        field(200),
      END OF field_list.

DATA: BEGIN OF query_tab OCCURS 0,
        query(200),
      END OF query_tab.


* building field list.

field_list-field = 'BUKRS'.
APPEND field_list.

field_list-field = 'BUTXT'.
APPEND field_list.


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>.


* query table
CONCATENATE '''' '0001' '''' INTO query_tab-query.
CONCATENATE 'BUKRS >= ' query_tab-query  'AND' INTO query_tab-query SEPARATED BY space.
APPEND query_tab.
CLEAR: query_tab.

CONCATENATE '''' 'B%' '''' INTO query_tab-query.
CONCATENATE 'ORT01 LIKE ' query_tab-query  '.' INTO query_tab-query SEPARATED BY space.
APPEND query_tab.
CLEAR: query_tab.






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

You need to be careful while building the query table. Ranges are not allowed and things like that. Please refer documentation.

Thanks and regards,

S. Chandra Mouli.

0 Kudos

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