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: 

how to create a dynamic field sql?

former_member625844
Participant
0 Kudos

I want to query data from faglflext table by given accounting period. In this table each month is a column. like HSL01,HSL02,HSL03....I wonder is it possible to dynamcie create a SQL like select HSL01,HESL02,...from select-options. Such as if I enter accounting period as 3 to 5, then the SQL is select HSL03,HSL04,HSL05...like. I wonder can EXEC be used for that? Thx.

1 REPLY 1

mateuszadamus
Active Contributor

Hi loki_luo15

Something like this?

PARAMETERS: p_prstr TYPE i.
PARAMETERS: p_prend TYPE i.

DATA:
  lv_period TYPE n LENGTH 2,
  lt_fields TYPE TABLE OF string.

lv_period = p_prstr.
WHILE lv_period <= p_prend.
  APPEND |HSL{ lv_period }| TO lt_fields.
  lv_period = lv_period + 1.
ENDWHILE.

SELECT (lt_fields)
  FROM faglflext
  INTO TABLE lt_results

You could also retrieve all HSL fields, for all months and then read the data only from fields for which the period was specified on the selection screen.

Regards,

Mateusz