简体   繁体   English

为什么 INSERT 到内部表失败?

[英]Why INSERT into internal table fails?

I have a Problem with ABAP.我有 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.问题是行INSERT reservation_wa INTO TABLE reservation_tab. which does not run correctly and I always get sy-subrc <> 0 .它运行不正确,我总是得到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?我怎样才能找到一条详细的错误信息,这条 SQL 语句出了什么问题?

This snipplet tests for not equal 0 :此 snipplet 测试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.纯文本:如果插入成功(返回码==0),则报错。 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.我不知道您的确切要求,但您似乎混淆了代码中的 if/else 分支。

In ABAP, sy-subrc == 0 always means success.在 ABAP 中, sy-subrc == 0总是意味着成功。 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.您可以通过将光标放在指令上并使用 F1 键来检查指令的可能返回值。 this will launch the help/documentation.这将启动帮助/文档。

Regards问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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