Skip to Content
0
Former Member
Dec 21, 2006 at 07:28 PM

Search for records logic doesn't seem to work

12 Views

Hi,

I am not sure why my logic finds a record in table itab_kna1 but then also add records to the table error: How would I code the logic to loop through table itab_data and do a lookup to itab_kna1 to see if there is a records. If not found then enter in the error table itab_error. It's entering some records found in the error table and the results table.

Thanks

SELECT SORTL KUNNR KTOKD

INTO CORRESPONDING FIELDS OF TABLE ITAB_KNA1

FROM KNA1

WHERE KTOKD = 'ZC01'.

SORT: ITAB_KNA1 BY SORTL,

ITAB_DATA BY CUST_NO.

LOOP AT ITAB_DATA.

READ TABLE ITAB_KNA1 WITH KEY SORTL = ITAB_DATA-CUST_NO

BINARY SEARCH.

IF SY-SUBRC = 0.

ITAB_RESULTS-KUNNR = ITAB_KNA1-KUNNR.

ITAB_RESULTS-CUST_NO = ITAB_KNA1-SORTL.

ITAB_RESULTS-SYEAR = ITAB_DATA-SALES_YEAR.

ITAB_RESULTS-CALMONTH = ITAB_DATA-SALES_MONTH.

APPEND ITAB_RESULTS.

ELSE.

ITAB_ERROR-KUNNR = ITAB_KNA1-KUNNR.

ITAB_ERROR-CUST_NO = ITAB_KNA1-SORTL.

ITAB_ERROR-SYEAR = ITAB_DATA-SALES_YEAR.

ITAB_ERROR-CALMONTH = ITAB_DATA-SALES_MONTH.

APPEND ITAB_ERROR.

ENDIF.

ENDLOOP.