Search code examples
selectabapopensql

How to correctly check that the table contains both values in ABAP?


I have a code in which I use the check_adr method to check if the selected address is in the table. The following logic occurs inside the check_adr method:

  1. Obtaining the value of the addrnum_dd, addrnum_pbd fields from the zfslscase_agr table, where case_guid of the contract card = case_guid in the ZFSLTCASE_AGR table.
  2. Selecting the ADDRNUMBER value from the ADRC table, where (ADDRNUMBER is equal to zfslscase_agr-addrnum_dd OR ADDRNUMBER is equal to zfslscase_agr-addrnum_pbd) And the start of the action (begda) is less than or equal to the current date And the end of the action (endda) is greater than or equal to the current date.
  3. IF the entered value is in the zfslscase_agr-addrnum_dd fields, zfslscase_agr-addrnum_pbd is not found in the ADRC table, then lv_chk = X ELSE Iv_chk = empty.

Here's how I've currently implemented it. The problem is that lt_addr will not be empty since 1 line for lv_addrnum_dd will be selected there, that is, the check does not work correctly. How to check for the existence of both values ​​if they are filled? I need to implement with a read table check for each non-empty field. How can I do that?

SELECT SINGLE addrnum_dd addrnum_pbd
    INTO (lv_addrnum_dd, lv_addrnum_pbd)
    FROM zfsltcase_agr
    WHERE case_guid = iv_case_guid.

  IF sy-subrc = 0.
    IF lv_addrnum_dd IS NOT INITIAL AND lv_addrnum_pbd IS NOT INITIAL.
      SELECT addrnumber
        INTO TABLE lt_addr
        FROM adrc
        WHERE ( addrnumber = lv_addrnum_dd OR addrnumber = lv_addrnum_pbd )
          AND date_from <= sy-datum
          AND date_to >= sy-datum.

      IF lines( lt_addr ) = 0.
        lv_chk = abap_true.
      ELSE.
        lv_chk = abap_false.
      ENDIF.
    ELSE.
      lv_chk = abap_false.
    ENDIF.
  ENDIF.

Solution

  • As you wrote, you will be able to check this with a READ command and not have to write 2 separate SELECTs. In addition, the line_exists() function may also come in handy, if it is already available to you. If not already, you may want to check the sy-subrc value after READ TABLE to see if it finds the specified field.

    The problem would be that if the table contains the key value lv_addrnum_dd and does not contain the key path lv_addrnum_pbd, then lv_chk should be true. If I am correct, the following code should help you:

    SELECT addrnumber
        INTO TABLE lt_addr
        FROM adrc
        WHERE ( addrnumber = lv_addrnum_dd OR addrnumber = lv_addrnum_pbd )
          AND date_from <= sy-datum
          AND date_to >= sy-datum.
    IF line_exists( lt_addr[ addrnumber = lv_addrnum_dd ] ) 
    AND NOT line_exists( lt_addr[ addrnumber = lv_addrnum_pbd ] ) .
       lv_chk = abap_true.
    ENDIF.
    

    I hope I have understood the problem you outlined correctly and I was able to help.