cancel
Showing results for 
Search instead for 
Did you mean: 

Dynamic range table creation using field symbol

Former Member
0 Kudos

Hello Team,

I am looking for how to create dynamic range tables at runtime using field symbol.

Suppose i have a table i.e. GT_SELECT which contains all field names , low, high, sign option .My requirement is I have to loop through the GT_SELECT table and form the dynamic range table for each unique field name.

Please suggest if anyone having any idea on this.

thanks

RP

Accepted Solutions (0)

Answers (3)

Answers (3)

matt
Active Contributor

The easiest way is use a generic range structure. There are several available in standard SAP - you just need to search for them. Alternatively, you can create your own based on, e.g. RSLOW date element - or a STRING or some other type long enough to hold all your values.

DATA: 
  range1 TYPE RANGE OF rslow,
  range2 TYPE RANGE OF rslow,
...
  rangeN TYPE RANGE OF rslow.

DATA where_clause TYPE string.
DATA count TYPE n LENGTH 1.
SORT gt_select BY fname.
LOOP AT gt_select INTO DATA(select_line).
  FIELD-SYMBOLS <range> TYPE STANDARD TABLE.
  ASSIGN (|range{ count }|) TO <range>.
  DATA range_line TYPE rslow.
  MOVE-CORRESPONDING select_line TO range_line.
  INSERT range_line INTO TABLE <range>.
  AT END OF fname.
    ADD 1 TO count.
    IF where_clause IS NOT INITIAL.
      where_clause = where_clause && |AND { select_line-fname } IN range{ count }|.
    ELSE.
      where_clause = |{ select_line-fname } IN range{ count }|.
    ENDIF.
  ENDAT.
ENDLOOP.

I've written this blind, so it may need some adjustment, but I think you can see the basic concepts.

Former Member
0 Kudos
Hello Matthew,

Thank you for your answer.I have also tried logic for the above requirement.I think I have achieved in this way.

Please suggest necessary comments.

 TYPE-POOLS: abap.
DATA:

  gr_structdescr    TYPE REF TO cl_abap_structdescr,

  gr_tabledescr     TYPE REF TO cl_abap_tabledescr,

  gr_datadescr      TYPE REF TO cl_abap_datadescr,

  gr_typedescr      TYPE REF TO cl_abap_typedescr,

  gt_components     TYPE abap_component_tab,

  gw_component      TYPE LINE OF abap_component_tab,

  gr_wa             TYPE REF TO data,

  gr_tab            TYPE REF TO data.

FIELD-SYMBOLS: <fs_wa> TYPE any,

               <sign>  TYPE any.

FIELD-SYMBOLS: <range_1> TYPE table,

               <range_2> TYPE table,

               <range_3>  TYPE table,

               <range_4>      TYPE table .

LOOP AT gt_select INTO wa_select.
* determine components of structure -> GT_COMPONENTS

  MOVE 'SIGN' TO gw_component-name.

  gw_component-type ?= cl_abap_elemdescr=>get_c( p_length = 1 ).

  INSERT gw_component INTO TABLE gt_components.
  MOVE 'OPTION' TO gw_component-name.
  gw_component-type ?= cl_abap_elemdescr=>get_c( p_length = 2 ).

  INSERT gw_component INTO TABLE gt_components.

 MOVE 'LOW' TO gw_component-name.

  gw_component-type ?= cl_abap_elemdescr=>describe_by_name( wa_select-fieldnm ).

  INSERT gw_component INTO TABLE gt_components.

  MOVE 'HIGH' TO gw_component-name.

  gw_component-type ?= cl_abap_elemdescr=>describe_by_name( wa_select-fieldnm ).

  INSERT gw_component INTO TABLE gt_components.

* get structure descriptor -> GR_STRUCTDESCR

  gr_structdescr ?= cl_abap_structdescr=>create( gt_components ).

* create work area of structure GR_STRUCTDESCR -> GR_WA

  CREATE DATA gr_wa TYPE HANDLE gr_structdescr.

  ASSIGN gr_wa->* TO <fs_wa>.

*Move the values to the field symbol work area

MOVE-CORRESPONDING wa_select TO <fs_wa>.

  gr_datadescr ?= gr_structdescr.

  gr_tabledescr ?= cl_abap_tabledescr=>create( gr_datadescr ).

* Create dynmaic internal table for each range

  CREATE DATA gr_tab TYPE HANDLE gr_tabledescr.

  ASSIGN gr_tab->*        TO <range_1>.

    ASSIGN gr_tab->*      TO <range_2>.

      ASSIGN gr_tab->*    TO <range_3>.

        ASSIGN gr_tab->*  TO <range_4>.

CASE  wa_select-fieldnm .

  WHEN 'field1'.

  APPEND <fs_wa> TO <range_1>.

  WHEN 'field2'.

 APPEND <fs_wa>  TO <range_2>.

* -

* -

* -

* *Like his so on for multiple fields.

 ENDCASE.

REFRESH : gt_components.

UNASSIGN : <fs_wa>.
  ENDLOOP.

<br>

thanks again

RP

0 Kudos

Ich hatte gerade auch eine ähnliche Aufgabe für den Aufbau einer dynamische Range für eine Prüfung. Mein Ansatz war allerdings ohne LOOP.

Hier mein Vorschlag.

DATA:
lv_datenelement TYPE string,
lr_range TYPE REF TO data.
FIELD-SYMBOLS:
**LOOP AT gt_select INTO DATA(ls_select).
lv_datenelement = ls_select-tabname && `-` && ls_select-fieldname.

TYPES: tyt_range LIKE RANGE OF lv_datenelement.
FIELD-SYMBOLS: <lt_range> TYPE tyt_range.

CREATE DATA lr_range TYPE tyt_range.
ASSIGN lr_range->* TO <lt_range>.
APPEND INITIAL LINE TO <lt_range> ASSIGNING FIELD-SYMBOL(<ls_range>).

<ls_range>-sign = ls_select-low.
<ls_range>-option = ls_select-option
<ls_range>-low = ls_select-low.

<ls_range>-high = ls_select-high.

IF ls_select-check_value in <lt_range>.

* Handling Check

ENDIF.

UNASSIGN <lt_range>.

**ENDLOOP.

DoanManhQuynh
Active Contributor

This question is 3 years ago man...

Sandra_Rossi
Active Contributor
0 Kudos

... and most importantly:

  1. it doesn't even answer the question (whose main assumption is the presence of LOOP AT gt_select, you just remove it)
  2. your code is misleading, because it creates ranges of type "string", not ranges of type defined in variable tabname-fieldname (you can check that in debug). The only solution is the one posted by the OP based on the RTTC classes.
matt
Active Contributor
0 Kudos

If it's character based field, then the type doesn't matter much and a range of string will work fine.

Sandra_Rossi
Active Contributor

matthew.billingham of course, but what about numeric fields. Anyway, I was saying that the following part of the code is misleading, probably the goal of the initialization of LV_DATENELEMENT was to make the TYPES declaration "dynamic", but of course it can't work:

lv_datenelement = ls_select-tabname && `-` && ls_select-fieldname.
TYPES: tyt_range LIKE RANGE OF lv_datenelement. CREATE DATA lr_range TYPE tyt_range.
Former Member
0 Kudos

Dynamic internal table building concept itself can be used and you can create ranges dynamically. Check that, how dynamic internal table can created. Many posts are available.