2015 Jun 03 10:55 AM
Hi,
In the program, I have populated an internal table itab1, with 11 fields as," key1, F1, F2,....F10 ".
Now, I am looping inside another table, say itab2. I want to check each of the above 10 fields, inside the loop, against another table itab3,
as below.
Loop at itab2.
read table itab1 with key key1 = itab2-key1.
if sy-subrc= 0.
read table itab3 with key F1 = itab1-F1.
if -sy-subrc = 0.
write : 'success F1'
endif.
read table itab3 with key F2 = itab1-F2.
if -sy-subrc = 0.
write : 'success F2'
endif.
:
:
read table itab3 with key F10 = itab1-F10.
if -sy-subrc = 0.
write : 'success F10'
endif.
endloop.
My question is, instead of writing 10 times(where we check F1, F2,----F10), can we enhance the above logic? where we can achive this with lesser statements and avoid repetition?
Regards
SR
2015 Jun 03 4:41 PM
Hi Swastik,
You could write a subroutine importing field name& value and changing subrc.
data: go_descr TYPE REF TO cl_abap_tabledescr.
field-symbols: <fs> type abap_keydescr,
<fs2> type any.
LOOP AT itab2.
read table itab1 into wa_itab1 with key key1 = itab2-key1.
if sy-subrc= 0.
go_descr ?= cl_abap_typedescr=>describe_by_data( itab1 ).
LOOP AT go_descr->key ASSIGNING <fs>.
ASSIGN COMPONENTS <fs>-name of structure wa_itab1 into <fs2>
PERFORM check_fields using <fs>-name itab1changing gv_subrc.
ENDLOOP.
endif.
ENDLOOP.
FORM f_check_fields using field_name type char10 field_value type char10 changing subrc type i.
read table itab3 with key (field_name)= field_value.
subrc = sy-subrc.
ENDFORM.
Maybe have little bug.
regards,
Archer
Hi Swastik,
You could write a subroutine importing field name& value and changing subrc.
data: go_descr TYPE REF TO cl_abap_tabledescr.
field-symbols: <fs> type abap_keydescr,
<fs2> type any.
LOOP AT itab2.
read table itab1 into wa_itab1 with key key1 = itab2-key1.
if sy-subrc= 0.
go_descr ?= cl_abap_typedescr=>describe_by_data( itab1 ).
LOOP AT go_descr->key ASSIGNING <fs>.
ASSIGN COMPONENTS <fs>-name of structure wa_itab1 into <fs2>
PERFORM check_fields using <fs>-name itab1changing gv_subrc.
ENDLOOP.
endif.
ENDLOOP.
FORM f_check_fields using field_name type char10 field_value type char10 changing subrc type i.
read table itab3 with key (field_name)= field_value.
subrc = sy-subrc.
ENDFORM.
Maybe have little bug.
regards,
Archer
2015 Jun 03 4:41 PM
Hi Swastik,
You could write a subroutine importing field name& value and changing subrc.
data: go_descr TYPE REF TO cl_abap_tabledescr.
field-symbols: <fs> type abap_keydescr,
<fs2> type any.
LOOP AT itab2.
read table itab1 into wa_itab1 with key key1 = itab2-key1.
if sy-subrc= 0.
go_descr ?= cl_abap_typedescr=>describe_by_data( itab1 ).
LOOP AT go_descr->key ASSIGNING <fs>.
ASSIGN COMPONENTS <fs>-name of structure wa_itab1 into <fs2>
PERFORM check_fields using <fs>-name itab1changing gv_subrc.
ENDLOOP.
endif.
ENDLOOP.
FORM f_check_fields using field_name type char10 field_value type char10 changing subrc type i.
read table itab3 with key (field_name)= field_value.
subrc = sy-subrc.
ENDFORM.
Maybe have little bug.
regards,
Archer
2015 Jun 03 7:57 PM
Hi Swastik,
you can try with:
DATA: lv_comp(2) TYPE n,
lv_fieldname(30) TYPE c.
FIELD-SYMBOLS: <fs>.
LOOP AT itab2.
READ TABLE itab1 WITH KEY key1 = itab2-key1.
IF sy-subrc = 0.
lv_comp = 1.
DO 10 TIMES.
CONCATENATE 'F' lv_comp INTO lv_fieldname.
ADD 1 TO lv_comp.
ASSIGN COMPONENT lv_comp OF STRUCTURE itab1 TO <fs>.
READ TABLE itab3 WITH KEY (lv_fieldname) = <fs>.
IF sy-subrc = 0.
WRITE:/ 'Success', lv_fieldname.
ENDIF.
ENDDO.
ENDIF.
ENDLOOP.
Regards
Oliver
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |