Skip to Content
0

Dynamic range table creation using field symbol

Dec 27, 2016 at 05:26 AM

555

avatar image

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

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Singaravel Palanivel Dec 27, 2016 at 05:45 AM
0

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.

Share
10 |10000 characters needed characters left characters exceeded
Matthew Billingham
Dec 27, 2016 at 08:15 AM
0

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.

Show 1 Share
10 |10000 characters needed characters left characters exceeded
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