ABAP开发需要养成的习惯—程序修改数据库表

时间:2024-01-12 16:10:56

①此外将内表数据写入数据库,推荐用Modify而不是insert,因为会有些key一样的报dump

loop at it_record.

*          报错
*          insert zzphilos0001 FROM TABLE it_record.

*          不报错,不推荐
*          insert zzphilos0001 FROM it_record.

*          推荐

       modify zzphilos0001 FROM it_record.
 endloop.

不循环内表的话也行

*          报错,key的问题
*        insert zzphilos0001 FROM TABLE it_record.

*          推荐

     modify zztb from table it_record.
          if sy-subrc eq 0.
            commit work.
          else.
            rollback work.
          endif.

②对数据库操作之后要有判断,进行commit或者rollback,BAPI也有对应的commit和rollback

call function 'BAPI_RESERVATION_CHANGE'
      exporting
        reservation               = v_res_no
      tables
        reservationitems_changed  = it_resitems_changed
        reservationitems_changedx = it_resitems_changedx
        return                    = it_return.
    loop at it_return.
      if it_return-type = 'E' or it_return-type = 'A'.
        call function 'BAPI_TRANSACTION_ROLLBACK'.
        message it_return-message type it_return-type.
      else.
        call function 'BAPI_TRANSACTION_COMMIT'
          exporting
            wait = 'X'.
        message s001(00) with 'Change successfully!'.
      endif.
    endloop.
    clear it_return.

③直接对数据库表的更新操作(单个字段)

LOOP AT g_it_alv_stpox ASSIGNING <l_wa_alv>.

UPDATE marc SET zz_bunit = <l_wa_alv>-zz_bunit WHERE werks = <l_wa_alv>-werks
                                                       AND matnr = <l_wa_alv>-idnrk.
      IF sy-subrc EQ 0.
        COMMIT WORK.
        <l_wa_alv>-zz_message  = l_message_s.
      ELSE.
        ROLLBACK WORK.
        <l_wa_alv>-zz_message  = l_message_f.
      ENDIF.

ENDLOOP.