Friday, August 29, 2014

ABAP TIP Field Symlbo Tip

ABAP Field Symlbo Tip


The one of lots of field symbol advantage of is efficiency because field symbol doesn’t occupy memory space and only have a reference to other variable.
When there is a need to improve filed symbol performance with BINARY SEARCH, we can sort field symbol.
In this case, the inter table defined with field symbol should declared as a standard type.

SORT <fs1> BY ('CARRID').

REPORT  zfieldsymbol01.

DATA : gt_scarr TYPE TABLE OF scarr.

FIELD-SYMBOLS : <fs1> TYPE STANDARD TABLE,
                <fs2> TYPE ANY.

SELECT * INTO TABLE gt_scarr FROM scarr.

ASSIGN gt_scarr TO <fs1>.

SORT <fs1> BY ('CARRID').

READ TABLE <fs1> WITH KEY ('CARRID') = 'AA' ASSIGNING <fs2>
                 BINARY SEARCH.

WRITE <fs2>.



Like this case, when field symbol is defined with TYPE ANY, you can sort the internal table like below program.


SORT <itab> BY (otab).


REPORT  zsort.


PARAMETERS p_dbtab TYPE c LENGTH 30.
DATA : GV_COLUMN TYPE C LENGTH 30.

SELECT-OPTIONS columns FOR GV_COLUMN NO INTERVALS.
PARAMETERS : p_ASC RADIOBUTTON GROUP OPT1,
             p_DSC RADIOBUTTON GROUP OPT1.

PARAMETERS p_where type c LENGTH 255.

DATA: otab  TYPE abap_sortorder_tab,
      oline TYPE abap_sortorder,
      dref  TYPE REF TO data.

FIELD-SYMBOLS: <column> LIKE LINE OF columns,
               <itab> TYPE STANDARD TABLE.

TRY.
    CREATE DATA dref TYPE STANDARD TABLE OF (p_dbtab).
    ASSIGN dref->* TO <itab>.
  CATCH cx_sy_create_data_error.
    MESSAGE 'Wrong data type!' TYPE 'I' DISPLAY LIKE 'E'.
    LEAVE PROGRAM.
ENDTRY.

TRY.
    SELECT *
           FROM (p_dbtab)
           INTO TABLE <itab>
           where (p_where).
  CATCH cx_sy_dynamic_osql_semantics.
    MESSAGE 'Wrong database table!' TYPE 'I' DISPLAY LIKE 'E'.
    LEAVE PROGRAM.
ENDTRY.

LOOP AT columns ASSIGNING <column>.
  oline-name = <column>-low.
  IF P_DSC = 'X'.
     oline-descending = P_DSC.
  ENDIF.
  APPEND oline TO otab.
ENDLOOP.

TRY.
    SORT <itab> BY (otab).
  CATCH cx_sy_dyn_table_ill_comp_val.
    MESSAGE 'Wrong column name!' TYPE 'I' DISPLAY LIKE 'E'.
    LEAVE PROGRAM.
ENDTRY.





No comments:

Post a Comment