Hi All
I have prepared this document to be able to help ABAPers to find some easy fixed while they are using READ statement during coding ..
I myself faced some issues and saw a lot of open posts for the same hence thought a ready document might come handy .
Read statement and some Limitations
***************************************************************************************
Limitation 1 : Read table does not make use of SY-SUBRC NE 0 condition .
Here we are trying to read a table LT_PO_ITEM which does not have matching entries in IT_ITEM ..
Example
Case 1 : READ TABLE LT_PO_ITEMS INTO LS_FINAL WITH KEY GUID NE IT_ITEM-GUID . " This is wrong
Case 2 : READ TABLE LT_PO_ITEMS INTO LS_FINAL WITH KEY GUID EQ IT_ITEM-GUID . "This is right
Explanation :Lets look as to why case 1 is wrong .IN using Read statement SAP does not allow NE to be used . I don't know what's the purpose of doing that is .. probably because there will be a lot of entries which are NE and hence you cannot pass it into the structure.. LS_FINAL ..
Workaround/ Solution for Case 1
LOOP AT LT_PO_ITEMS INTO LT_FINAL . " I am still comparing LT_PO_ITEM but by each case now
READ TABLE IT_ITEM INTO WA_ITEM WITH KEY GUID = LT_FINAL " Checking LT_PO_ITEM with IT_ITEM
IF SY-SUBRC EQ 0 . " If it matches the entry
DELETE LT_PO_ITEMS Index Lv_index . " DELETE
- ENDIF.
- ELSE.
MOVE CORRESPONDING LT_PO_ITEMS INTO LT_FINAL2 . "If does not matches add to table LT_FINAL2
Now in LT_FINAL2 you can have all the data which is NOT matching (EXCLUSIVE of entry in ) the initial Table in LT_PO_ITEMS. Hence we achieved it ..
***************************************************************************************
Limitation 2 READ ENTRIES OF ONE Internal TABLE INTO ANOTHER internal table NOT POSSIBLE ..
Example
READ TABLE LT_CRMJEST INTO LT_CRMJEST1 WITH KEY OBJNR = ls_requirments-parent
" If LT_FINAL Is a Table this is not a correct statement . Why ? It will give you the below error.
Explanation : From an internal table you cannot select into an internal table ONLY INTO a structure is allowed .. So how do we select multiple entries from an internal table based on some condition which is not a primary key so obviously it will have multiple entries .
SOLUTION
LOOP AT lt_crmjest1 INTO LS_crmjest1 WHERE objnr = ls_requirements-item.
IF sy-subrc = 0 .
APPEND ls_crmjest1 TO LT_crmjest2.
ENDIF.
- ENDLOOP.
Now from 1 internal table lt_crmjest1 your entries have moved to the 2nd internal table lt_crmjest2.
Now put your logic on conditions.
READ TABLE Lt_CRMJEST2 INTO LS_crmjest2 with key STAT = 'I1113' INACT = ABAP_FALSE .
" We put condition on LT_CRMJEST2 now since it will have the desired entries ..Note here we can use read statement as its from an internal table to a structure .
Hope this helps a bit
Regards
Vinita