Application Development and Automation 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: 
Read only

Dynamic Query

Former Member
0 Likes
401

  SELECT ryear

    rtcur

    racct

    rbukrs

    SUM( tsl06 ) AS tbal

     SUM( hsl06 AS rbal

    FROM faglflext INTO TABLE lt_bal

WHERE ryear =<input>  AND rbukrs IN <input> GROUP BY ryear rtcur racct rbukrs.



In the above query , how can I dynamically change the field name TSL06 based on the period. It means that if the current fiscal perid 5, dynamically, it has to come as SUM (TSL05) instead of SUM (TSL06). How can I do this ? I need this SUM only for the current fiscal period.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
371

Hi Selva,

You can use the below code for your reference. I have used a connstant, but you can replace that with variable within the DO loop.

CONSTANTS: lv_var TYPE FIELDNAME VALUE 'TSL06',

                        lv_tab TYPE TABNAME VALUE 'FAGLFLEXT'.

ASSIGN LV_VAR TO <lv_field>.

ASSIGN lv_tab TO <lv_tab>.

CONCATENATE 'RYEAR' 'RTCUR' 'RACCT' 'RBUKRS' 'SUM( lv_var )' INTO lv_string SEPARATED BY space.

REPLACE ALL OCCURRENCES OF 'lv_var' IN lv_string WITH <lv_field> .

ASSIGN lv_string to <lv_string> .

SELECT (<lv_string>) FROM (<lv_tab>) INTO CORRESPONDING FIELDS OF TABLE lt_bal UP TO 10 rows.

Hope this will help.

1 REPLY 1
Read only

Former Member
0 Likes
372

Hi Selva,

You can use the below code for your reference. I have used a connstant, but you can replace that with variable within the DO loop.

CONSTANTS: lv_var TYPE FIELDNAME VALUE 'TSL06',

                        lv_tab TYPE TABNAME VALUE 'FAGLFLEXT'.

ASSIGN LV_VAR TO <lv_field>.

ASSIGN lv_tab TO <lv_tab>.

CONCATENATE 'RYEAR' 'RTCUR' 'RACCT' 'RBUKRS' 'SUM( lv_var )' INTO lv_string SEPARATED BY space.

REPLACE ALL OCCURRENCES OF 'lv_var' IN lv_string WITH <lv_field> .

ASSIGN lv_string to <lv_string> .

SELECT (<lv_string>) FROM (<lv_tab>) INTO CORRESPONDING FIELDS OF TABLE lt_bal UP TO 10 rows.

Hope this will help.