Nisha Nisha
Nisha Nisha

Reputation: 11

Insert functions inserts an empty record into database table in ABAP SAP

Basically I'm trying to insert a record from a form into a database table. Not sure why it adds in empty record.

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

It inserts an empty record as shown above

enter image description here

^User input record is added into the database as "Course Created" message pops up.

Upvotes: 0

Views: 1713

Answers (1)

Oguz
Oguz

Reputation: 1926

You did not append the values to the internal table. And also, you do not need an insert statement, if you are using modify statement. Modify will update if there are records with that key fields, if not it will insert.

it should be like:

*-------------------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.

Upvotes: 3

Related Questions