Registration

Dear SAP Community Member,
In order to fully benefit from what the SAP Community has to offer, please register at:
http://scn.sap.com
Thank you,
The SAP Community team.
Skip to end of metadata
Go to start of metadata

How to use a dynamic read on an internal table in ABAP when the number of keys are not known until runtime?

This feature is available in SAP Netweaver 7.3

Use cases:-In case we want to compare and see the existence of 1 internal table record in another internal table this method is very useful.

Here is a sample code which explains how to use the read statement with the following example:-

In this example we take input as any database table name as parameter.

REPORT zdynamic_read_test.

PARAMETERS:p_tab1 TYPE dfies-tabname,       "database table name 1
 p_tab2 TYPE dfies-tabname.       "database table name 2

DATA: BEGIN OF ls_data,
key1  TYPE char10,
key2  TYPE char10,
value TYPE char10,
END OF ls_data,

DATA: lt_key_fields_range TYPE RANGE OF name_komp,
lt_key_fields       TYPE TABLE OF name_komp,
ls_key_data        TYPE REF TO data,
lo_data               TYPE REF TO data,
lo_t_data1          TYPE REF TO data,
lo_t_data2          TYPE REF TO data,
lo_s_data           TYPE REF TO data,
lo_key_data        TYPE REF TO data.

FIELD-SYMBOLS: <ft_data>         TYPE STANDARD TABLE,
<ft_tcurc1>       TYPE STANDARD TABLE,
<ft_tcurc>         TYPE ANY TABLE,
<ft_ztcurc>       TYPE ANY TABLE,
<ft_ztcurc1>     TYPE ANY TABLE,
<fs_data_key> TYPE data.

DATA: lr_data TYPE REF TO data.

" create a reference variable with structure of DB table
CREATE DATA lr_data TYPE (p_tab1) .

" assign the data reference to a field symbol
ASSIGN lr_data->* TO FIELD-SYMBOL(<fs_struc1>).

" Get the list of fields from the database table name
DATA(lo_struct_descr1) = CAST cl_abap_structdescr( cl_abap_structdescr=>describe_by_data( <fs_struc1>  ) ).
DATA(lt_fieldlist1) = lo_struct_descr1->get_ddic_field_list( ).

" Get a list of key fields from the list of all fields
LOOP AT lt_fieldlist1 ASSIGNING FIELD-SYMBOL(<fs_fld>) WHERE keyflag = abap_true.
APPEND INITIAL LINE TO lt_key_fields ASSIGNING FIELD-SYMBOL(<fs_fld1>).
<fs_fld1> = <fs_fld>-fieldname.
ENDLOOP.

" create a reference variable which is of structure table
CREATE DATA lo_data TYPE TABLE OF (p_tab1).
ASSIGN lo_data->* TO  <ft_tcurc>.

" fetch data dynamically from the given database table name(e.g:if its TCURC fetch it from DB)
SELECT * FROM (p_tab1)
INTO TABLE <ft_tcurc>.

" Repeat the same set of steps for database table 2
CREATE DATA lo_data TYPE TABLE OF (p_tab2).
ASSIGN lo_data->* TO  <ft_ztcurc>.

SELECT * FROM (p_tab2)
INTO TABLE <ft_ztcurc>.

CREATE DATA lr_data TYPE (p_tab2) .
ASSIGN lr_data->* TO FIELD-SYMBOL(<fs_struc2>).

DATA(lo_struct_descr2) = CAST cl_abap_structdescr( cl_abap_structdescr=>describe_by_data( <fs_struc2>  ) ).
DATA(lt_fieldlist2) = lo_struct_descr2->get_ddic_field_list( ).

* From the list of all components get the key components
DATA(lt_components) = lo_struct_descr1->get_components( ).
DATA(lt_key_compnents) = lt_components.
* Build Range Table for Key Field
lt_key_fields_range = VALUE #( FOR <fs_key_fields> IN lt_key_fields ( low = <fs_key_fields> sign = 'I' option = 'EQ' ) ).
DELETE lt_key_compnents WHERE NOT name IN lt_key_fields_range.
DELETE lt_components WHERE name IN lt_key_fields_range.

" Create an internal table with all the fields in the DB along with another field which points to the key field
TRY.
DATA(lo_key_fields_struct) = cl_abap_structdescr=>create( lt_key_compnents ).
INSERT INITIAL LINE INTO lt_components ASSIGNING FIELD-SYMBOL(<fs_components>) INDEX 1.
<fs_components>-name = 'KEY'.
<fs_components>-as_include = abap_true.
<fs_components>-type = lo_key_fields_struct.
DATA(lo_table_data_struct) = cl_abap_structdescr=>create( lt_components ).
DATA(lo_table_data_table) = cl_abap_tabledescr=>create( lo_table_data_struct ).
CATCH cx_sy_struct_creation.  "
ENDTRY.

" build internal table 1 <ft_tcurc1> which has the key field record
CREATE DATA lo_t_data1 TYPE HANDLE lo_table_data_table.
ASSIGN lo_t_data1->* TO <ft_tcurc1>.
<ft_tcurc1> = <ft_tcurc>.

" build internal table 2 <ft_tcurc2> which has the key field record
CREATE DATA lo_t_data2 TYPE HANDLE lo_table_data_table.
ASSIGN lo_t_data2->* TO <ft_ztcurc1>.
<ft_ztcurc1> = <ft_ztcurc>.

" Loop into first ITAB and compare second ITAB using READ
LOOP AT <ft_tcurc1> ASSIGNING FIELD-SYMBOL(<fs_tcurc>).

ASSIGN COMPONENT 'KEY' OF STRUCTURE <fs_tcurc> TO FIELD-SYMBOL(<fs_key_value>).

" dynamic read statement with KEY-VALID for any number of keys
READ TABLE <ft_ztcurc1> ASSIGNING

FIELD-SYMBOL(<fs_data>) WITH KEY ('KEY') = <fs_key_value>.

IF sy-subrc = 0.
WRITE:/ 'success'.
ELSE.
WRITE:/ 'fail'.
ENDIF.

ENDLOOP.

  • No labels