Skip to Content

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

Dynamic field in select query

Hi Team,

I have to select dynamic fields from database table GLT0.

This depends on period value given on screen.

If period is 01 then fields will be hsl01.

If period is 02 then fields will be hsl01 and hsl02.

If period is 03 then fields will be hsl01,hsl02 and hsl03 .

so on...till 12..

My code is throwing exception at select query.

"Error in module RSQL of the database interface."

My code :

*" Type declaration to store field name as per given period

TYPES : BEGIN OF y_fieldname ,

fieldname(10) TYPE c ,

END OF y_fieldname .

data t_fieldname TYPE STANDARD TABLE OF y_fieldname .

DO p_monat TIMES.

clear w_count .

MOVE sy-index TO w_count

IF sy-index LE c_9.

CONCATENATE c_hsl

c_0

w_count

INTO e_fieldname-fieldname.

APPEND e_fieldname TO t_fieldname.

ELSE.

CONCATENATE c_hsl

w_count

INTO e_fieldname-fieldname.

APPEND e_fieldname TO t_fieldname.

ENDIF.

ENDDO.

  • Get Local Currency Amounts

  • from table glt0 depending on period

SELECT (t_fieldname)

FROM glt0

INTO TABLE t_glto_with_saknr

FOR ALL ENTRIES IN t_bukrs

WHERE rldnr EQ c_00

AND rrcty EQ c_0

AND rvers EQ c_001

AND bukrs EQ t_bukrs-bukrs

AND ryear EQ p_gjahr

AND racct IN r_saknr.

Please guide me.

Thanking u in advance.

Sangeeta Verma

Tags:
replied

Hi Sangeetha,

Do in the following way.

DATA: n           TYPE i VALUE 12,
      w_count     TYPE i,
      gv_val(2)   TYPE c,
      w_monta_val TYPE i,
      gv_total    TYPE i,
      w_monat_bal TYPE string.


DATA: BEGIN OF itab OCCURS 10,
        hsl01 TYPE i,
        hsl02 TYPE i,
        hsl03 TYPE i,
        hsl04 TYPE i,
        END OF itab.

itab-hsl01 = 10.
itab-hsl02 = 20.
itab-hsl03 = 30.
itab-hsl04 = 40.
APPEND itab.

FIELD-SYMBOLS: <fs_monat_bal>.

w_count = 1.

DO 4 TIMES.

  gv_val = w_count.

  CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
    EXPORTING
      input  = gv_val
    IMPORTING
      output = gv_val.

  CONCATENATE 'itab-hsl' gv_val INTO w_monat_bal.

  ASSIGN (w_monat_bal) TO <fs_monat_bal>.

  gv_total = gv_total + <fs_monat_bal>.

  ADD 1 TO w_count.

ENDDO.

write: gv_total. " Will have 100  ie. 10+20+30+40

BREAK-POINT.

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question