0

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?

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
gurehbgui
  • 14,236
  • 32
  • 106
  • 178
  • 2
    `METHOD` and `LEAVE PROGRAM` in the same example? Please throw away this book ASAP. ;-) – vwegert Jan 16 '14 at 16:26
  • 1
    Please add the data type declarations to the sample, especially the structure and table types. – vwegert Jan 16 '14 at 16:27
  • 2
    You still have the check on sy-subrc the wrong way round after the insert. You're reporting an error when the insert actually worked. – Esti Jan 16 '14 at 20:33
  • You are right, the error was because I copied wrong from my book. – gurehbgui Jan 17 '14 at 10:05
  • @vwegert Might be that the `METHOD` comes from a local class in a report, that is why there is `LEAVE PROGRAM` in the example. – Jagger Jan 17 '14 at 12:34

2 Answers2

2

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.

knut
  • 27,320
  • 6
  • 84
  • 112
  • This was the error, thank you. But a other question: what if it would still fail and I would get a sy-subrc <> 0. How to find the reason of the error? – gurehbgui Jan 17 '14 at 10:06
  • You can check the SAP-help to get the retun codes (in effect: 0 = ok, 4 = no insert because the same primary key or a unique secondary index. 2= problem with LOB handle structure). What else problems do you expect? I think other problems will end with an exception. – knut Jan 17 '14 at 15:30
1

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

PATRY Guillaume
  • 4,287
  • 1
  • 32
  • 41
  • sorry, made a mistake in my posting. I mean sy-subrc is always not 0. I edited my question. – gurehbgui Jan 16 '14 at 15:40
  • what kind of table is reservation_tab ? standard, sorted or hashed ? what is the key ? an insert should be OK except if the key is already present. – PATRY Guillaume Jan 16 '14 at 16:06
  • Its of type: TYPES t_reservation_tab TYPE HASHED TABLE OF zreservations WITH UNIQUE KEY reservation_id. – gurehbgui Jan 17 '14 at 09:58