Dynamic Read Table With Key and Value
Autor: Rafael Riso | Publicado: 2010-11-23T08:27:00-08:00

IntroductionThe concepts of dynamic programming are part of the unique features that ABAP offers for business programming. Exploiting the features of dynamic programming allows you to write powerful and flexible applications.REPORT ZADN_SAMPLE.*----------------------------------------------------------------------** -> CLASS lc_abnsample DEFINITION*----------------------------------------------------------------------*CLASS lc_abnsample DEFINITION.  PUBLIC SECTION.    TYPES : BEGIN OF ty_table_line,              id    TYPE char02,              name  TYPE char20,              phone TYPE char15,            END OF  ty_table_line.    METHODS : constructor.    METHODS : get_item_by_key IMPORTING p_key   TYPE char12                                        p_value TYPE any                              EXPORTING p_item  TYPE any.  PRIVATE SECTION.    TYPES : ty_table TYPE STANDARD TABLE OF ty_table_line.    DATA : it_table TYPE ty_table,           st_table TYPE LINE OF ty_table.    METHODS : load_table_data.ENDCLASS.                    "lc_abnsample DEFINITION*----------------------------------------------------------------------** -> CLASS lc_abnsample DEFINITION*----------------------------------------------------------------------*CLASS lc_abnsample IMPLEMENTATION.  METHOD constructor.    me->load_table_data( ).  ENDMETHOD.                    "constructor  METHOD load_table_data.    CLEAR : me->st_table.    me->st_table-id    = 'AA'.    me->st_table-name  = 'NAME AA'.    me->st_table-phone = 'PHONE AA'.    APPEND me->st_table TO me->it_table.    CLEAR : me->st_table.    me->st_table-id    = 'BB'.    me->st_table-name  = 'NAME BB'.    me->st_table-phone = 'PHONE BB'.    APPEND me->st_table TO me->it_table.    CLEAR : me->st_table.    me->st_table-id    = 'CC'.    me->st_table-name  = 'NAME CC'.    me->st_table-phone = 'PHONE CC'.    APPEND me->st_table TO me->it_table.    CLEAR : me->st_table.  ENDMETHOD.                    "load_table_data  METHOD get_item_by_key.    READ TABLE me->it_table WITH KEY (p_key) = p_value INTO me->st_table.    p_item = me->st_table.    CLEAR : me->st_table.  ENDMETHOD.                    "get_item_by_keyENDCLASS.                    "lc_abnsample DEFINITION*************************************************************************************************************************************************                                                                      **                           PROGRAM FLOW LOGIC                         **                                                                      *************************************************************************PARAMETERS : pa_key   TYPE char12 DEFAULT 'ID', " Use : ID, NAME or PHONE             pa_value TYPE char12 DEFAULT 'AA'. " For ID Use :                                                " AA, BB or CC                                                " For NAME Use :                                                " NAME AA, NAME BB or NAME CC                                                " For PHONE Use :                                                " PHONE AA, PHONE BB or PHONE CCSTART-OF-SELECTION.  DATA : olc_abnsample TYPE REF TO lc_abnsample,         o_data        TYPE REF TO data.  FIELD-SYMBOLS :   TYPE ANY,                   TYPE ANY.  CREATE OBJECT olc_abnsample.  CREATE DATA o_data TYPE lc_abnsample=>ty_table_line.  IF olc_abnsample IS BOUND.    ASSIGN o_data->* TO .    olc_abnsample->get_item_by_key( EXPORTING p_key   = pa_key                                              p_value = pa_value                                    IMPORTING p_item  =  ).    IF  IS ASSIGNED AND  IS NOT INITIAL.      DO.        ASSIGN COMPONENT sy-index OF STRUCTURE  TO .        IF sy-subrc NE 0.          EXIT.        ELSE.          WRITE / : .        ENDIF.      ENDDO.    ELSE.      WRITE / : 'Filter not found in table'.    ENDIF.    UNASSIGN : .  ENDIF.