简体   繁体   中英

Why INSERT into internal table fails?

I have a Problem with ABAP. I have the following code from a book:

METHOD make_reservation.
  DATA: license_plate     TYPE zcars-license_plate,
        reservation_wa    LIKE LINE OF reservation_tab,
        reservation_num   TYPE i,
        mess              TYPE string.

  reservation_num = lines( reservation_tab ).

  SELECT license_plate FROM zcars INTO (license_plate) WHERE category = category.

    LOOP AT reservation_tab
      TRANSPORTING NO FIELDS
      WHERE license_plate = license_plate
      AND NOT ( date_from > date_to OR date_to < date_from ).

    ENDLOOP.

    IF sy-subrc <> 0.
      reservation_wa-reservation_id   = reservation_num + 1.
      reservation_wa-customer_id      = customer.
      reservation_wa-license_plate    = license_plate.
      reservation_wa-date_from        = date_from.
      reservation_wa-date_to          = date_to.

      INSERT reservation_wa INTO TABLE reservation_tab.
      IF sy-subrc <> 0.
        CONCATENATE license_plate ' reserved!' INTO mess.
        MESSAGE mess TYPE 'I'.
      ELSE.
        MESSAGE 'internal error!' TYPE 'I' DISPLAY LIKE 'E'.
        LEAVE PROGRAM.
      ENDIF.

      RETURN.
    ENDIF.
  ENDSELECT.

  RAISE EXCEPTION TYPE zcx_no_car_available.

ENDMETHOD.

the problem is the line INSERT reservation_wa INTO TABLE reservation_tab. which does not run correctly and I always get sy-subrc <> 0 . This results to the message "internal error!"

now my question: I tired to debug it, but I cant find the reason why this statement does not insert the data. How can I find out a detailed error message what went wrong with this SQL statement?

This snipplet tests for not equal 0 :

      INSERT reservation_wa INTO TABLE reservation_tab.
      IF sy-subrc <> 0.
        CONCATENATE license_plate ' reserved!' INTO mess.
        MESSAGE mess TYPE 'I'.
      ELSE.
        MESSAGE 'internal error!' TYPE 'I' DISPLAY LIKE 'E'.
        LEAVE PROGRAM.
      ENDIF.

I always prefer to test for equality (that's easier to read). The similar coding for your code:

      INSERT reservation_wa INTO TABLE reservation_tab.
      IF sy-subrc = 0.
        MESSAGE 'internal error!' TYPE 'I' DISPLAY LIKE 'E'.
        LEAVE PROGRAM.
      ELSE.
        CONCATENATE license_plate ' reserved!' INTO mess.
        MESSAGE mess TYPE 'I'.
      ENDIF

In plain text: If the insert is successful (return code == 0), then report an error. If the insert is not successful, then inform about the correct reservation.

I don't know your exact requirement, but it seems you mix up the if/else branch in your code.

In ABAP, sy-subrc == 0 always means success. Your code seems to have a problem since the success is associated with any other value.

If you put a breakpoint in your code just before the insert, you'll be able to check that the insertion was a success.

You can check the possible return values of an instruction by putting the cursor on it and using the F1 key. this will launch the help/documentation.

Regards

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM