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 field in select query

Former Member
0 Kudos

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

1 ACCEPTED SOLUTION

Former Member
0 Kudos

Hi Asik,

I m selecting all fields now as suggestd by u so in one record i have hsl01 to hsl12.

I have to calculate balance into e_glt0_bal-bal .This balance is summation of e_glt0_bal-hsl01 till e_glt0_bal-hsl12 (depending on monat).

I m using logic in process then

my code now :

data: w_monat_bal type string .

DO p_monat TIMES.

MOVE c_01 TO w_count .

CONCATENATE 'e_glt0_bal-hsl'

w_count

INTO w_monat_bal.

ASSIGN w_monat_bal TO <fs_monat_bal>.

e_glt0_bal-bal = e_glt0_bal-bal + <fs_monat_bal>.

w_count = w_count + c_01.

ENDDO.

But <fs_monat_bal> can not be added.

Giving exception :

Unable to interpret "e_glt0_bal-hsl01" as a number.

If I do <fs_monat_bal> type GLT0-hslvt instead to type any

Then assigning from w_monat_bal is not possible.

8 REPLIES 8

Sm1tje
Active Contributor
0 Kudos

try INTO CORRESPONDING FIELDS OF TABLE t_glto_with_saknr

former_member181995
Active Contributor
0 Kudos

Sangeeta,

seems very much strange and scattred code:

data t_fieldname TYPE STANDARD TABLE OF y_fieldname .

SELECT (t_fieldname)"check t_fieldname what is it?its a internal table as you defined (its not a field of GLT0 table)
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.

naimesh_patel
Active Contributor
0 Kudos

I don't know what is the data type for W_COUNT in your program.

Try this way:


data: w_count(2) type c.

DO p_monat TIMES.
clear w_count .
MOVE sy-index TO w_count .
unpack w_count to w_count.
CONCATENATE 'HSL' " c_hsl
w_count
INTO e_fieldname-fieldname.
APPEND e_fieldname TO t_fieldname.
ENDDO.

Regards,

Naimesh Patel

asik_shameem
Active Contributor
0 Kudos

Hi

Dot add your logic in SELECT query. I wud suggest you to add the logic in the process.

SELECT all the periods fields HSL01, HSL02,..,HSL12 into Internal table t_glto_with_saknr in SELECT Query.

While processing the record, do your logic.

FIELD-SYMBOLS: <fs_field> TYPE ANY.

LOOP AT t_glto_with_saknr.
  CONCATENATE 'GLTO_WTH_SAKNR-HSL' p_period into gv_field.
  ASSIGN gv_field TO <fs_field>.
  
  " Now <fs_field> will have the value of particular period which is mentioned in screen
  " << your code Here >>

ENDLOOP.

Former Member
0 Kudos

Hi Asik,

I m selecting all fields now as suggestd by u so in one record i have hsl01 to hsl12.

I have to calculate balance into e_glt0_bal-bal .This balance is summation of e_glt0_bal-hsl01 till e_glt0_bal-hsl12 (depending on monat).

I m using logic in process then

my code now :

data: w_monat_bal type string .

DO p_monat TIMES.

MOVE c_01 TO w_count .

CONCATENATE 'e_glt0_bal-hsl'

w_count

INTO w_monat_bal.

ASSIGN w_monat_bal TO <fs_monat_bal>.

e_glt0_bal-bal = e_glt0_bal-bal + <fs_monat_bal>.

w_count = w_count + c_01.

ENDDO.

But <fs_monat_bal> can not be added.

Giving exception :

Unable to interpret "e_glt0_bal-hsl01" as a number.

If I do <fs_monat_bal> type GLT0-hslvt instead to type any

Then assigning from w_monat_bal is not possible.

0 Kudos

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.

Former Member
0 Kudos

Hi Sangeetha,

Try to select hsl01 to hsl12 for all the records, based on the period consider the field.

if period is 01 then consider hsl01 for that perticular record.

You can have CASE... ENDCASE to identify the period & according to period u can calculate the balance.

see the sample code below

CASE w_monat.

WHEN '01'.

apply the summation logic here.

WHEN '02'.

ENDCASE.

I hope you are clear with the above explanation

Regards,

Sujatha

Edited by: Sujatha Reddy on Sep 8, 2008 4:46 PM

Former Member
0 Kudos

The easy way would be to use the DO ... VARYING construction.

Rob