插入函数将空记录插入ABAP SAP中的数据库表

时间:2018-01-26 17:40:12

标签: database sap abap

基本上我试图将表单中的记录插入到数据库表中。不确定为什么它会添加空记录。

Here's my codes:
*--------------Screen 9003------------------------------------------------------------*

MODULE status_9003 OUTPUT.
  SET PF-STATUS 'PF_PO_INN'.
  SET TITLEBAR 'PO_TITLE1'.

ENDMODULE.


module user_command_9003 input.

  "IF sy-ucomm = 'EXE'.
  IF sy-ucomm = 'CREATE'.
    PERFORM ADD_COURSE.
  ELSEIF SY-UCOMM = 'BACK'.
    PERFORM CLEAR_INPUTS.
    LEAVE TO SCREEN 9001.
  ENDIF.
ENDMODULE.
*-------------------Create course--------------------------------------*
FORM ADD_COURSE.
  DATA  ITAB_ZCMS_COURSES_HD LIKE TABLE OF ZCMS_COURSES WITH HEADER LINE.

  ITAB_ZCMS_COURSES_HD-COURSE_ID = ZCMS_COURSES-COURSE_ID.
  ITAB_ZCMS_COURSES_HD-COURSE_CONTENT = ZCMS_COURSES-COURSE_CONTENT .
  ITAB_ZCMS_COURSES_HD-MUSIC_GENRE = ZCMS_COURSES-MUSIC_GENRE.
  ITAB_ZCMS_COURSES_HD-OPTIONS = ZCMS_COURSES-OPTIONS.
  ITAB_ZCMS_COURSES_HD-COURSE_NAME = ZCMS_COURSES-COURSE_NAME.


  INSERT ZCMS_COURSES FROM ITAB_ZCMS_COURSES_HD.

  MODIFY zcms_courses FROM TABLE ITAB_ZCMS_COURSES_HD.

  SELECT * INTO TABLE  ITAB_ZCMS_COURSES_HD FROM zcms_courses.

  LOOP AT ITAB_ZCMS_COURSES_HD.
    WRITE AT: /1(2) sy-tabix,
    6   ITAB_ZCMS_COURSES_HD-COURSE_NAME,
    20  ITAB_ZCMS_COURSES_HD-COURSE_CONTENT,
    30  ITAB_ZCMS_COURSES_HD-OPTIONS,
    46  ITAB_ZCMS_COURSES_HD-MUSIC_GENRE.
  ENDLOOP.


  "To show that record is added
  MESSAGE 'Course Created.' TYPE 'S'.
ENDFORM.

enter image description here

它插入一个空记录,如上所示

enter image description here

^用户输入记录作为" Course Created"添加到数据库中。消息弹出。

1 个答案:

答案 0 :(得分:3)

您没有将值附加到内部表。而且,如果您使用的是modify语句,则不需要insert语句。如果有包含该关键字段的记录,则修改将更新,否则将插入。

应该是这样的:

*-------------------Create course--------------------------------------*
FORM ADD_COURSE.
  DATA  ITAB_ZCMS_COURSES_HD LIKE TABLE OF ZCMS_COURSES WITH HEADER LINE.
  " filling the header(work area) of the itab
  ITAB_ZCMS_COURSES_HD-COURSE_ID = ZCMS_COURSES-COURSE_ID.
  ITAB_ZCMS_COURSES_HD-COURSE_CONTENT = ZCMS_COURSES-COURSE_CONTENT .
  ITAB_ZCMS_COURSES_HD-MUSIC_GENRE = ZCMS_COURSES-MUSIC_GENRE.
  ITAB_ZCMS_COURSES_HD-OPTIONS = ZCMS_COURSES-OPTIONS.
  ITAB_ZCMS_COURSES_HD-COURSE_NAME = ZCMS_COURSES-COURSE_NAME.
  " Now you filled the itab.
  APPEND ITAB_ZCMS_COURSES_HD. 

  " You don't need insert statement modify will update/insert operations. 
  "INSERT ZCMS_COURSES FROM ITAB_ZCMS_COURSES_HD.

  MODIFY zcms_courses FROM TABLE ITAB_ZCMS_COURSES_HD.

  SELECT * INTO TABLE  ITAB_ZCMS_COURSES_HD FROM zcms_courses.

  LOOP AT ITAB_ZCMS_COURSES_HD.
    WRITE AT: /1(2) sy-tabix,
    6   ITAB_ZCMS_COURSES_HD-COURSE_NAME,
    20  ITAB_ZCMS_COURSES_HD-COURSE_CONTENT,
    30  ITAB_ZCMS_COURSES_HD-OPTIONS,
    46  ITAB_ZCMS_COURSES_HD-MUSIC_GENRE.
  ENDLOOP.


  "To show that record is added
  MESSAGE 'Course Created.' TYPE 'S'.
ENDFORM.