Skip to Content
-2
Jul 11, 2020 at 05:20 PM

DATA FROM LOCAL FILE TO INTERNAL TABLE AND TO DATABASE TABLE

275 Views

HI EVERYONE. I am working on this demo report where there is a local file in .txt format with sales order header data. i have to take the data and add to the internal table later the data should be updated in the ztable in which data already exists. i am attaching my code  where i managed to move the data from local file into internal table. and need help to update the same data into ztable. the fields in the ztable is same as i created in the structure.please do the needful. thank you.


REPORT ztst1 LINE-SIZE 500.



TYPES : BEGIN OF ty_headr,
          zzsdoc  TYPE CHAR15, "zordh_t-zzsdoc,
          zzdcat  TYPE CHAR15 ,"zordh_t-zzdcat,
          zzdtyp  TYPE CHAR15 ,"-zzdtyp,
          zzsorg  TYPE CHAR15 ,"-zzsorg,
          zzdist  TYPE CHAR15 ,"zordh_t-zzdist,
          zzdivi  TYPE CHAR15 ,"zordh_t-zzdivi,
          zzcust  TYPE CHAR15 ,"zordh_t-zzcust,
          zzcpur  TYPE CHAR15 ,"zordh_t-zzcpur,
          zzdcur  TYPE CHAR15 ,"zordh_t-zzdcur,
          zznamt  TYPE CHAR15 ,"zordh_t-zznamt,
          zzerdat TYPE CHAR15 ,"zordh_t-zzerdat,
        END OF ty_headr.

TYPES : BEGIN OF ty_item,
          zzsdoc TYPE CHAR15,"zordi_t-zzsdoc,
          zzsitm TYPE CHAR15,"zordi_t-zzsitm,
          zzmatr TYPE CHAR15,"zordi_t-zzmatr,
          zzmdes TYPE CHAR15,"zordi_t-zzmdes,
          zzoqty TYPE CHAR15,"zordi_t-zzoqty,
          zzunit TYPE CHAR15,"zordi_t-zzunit,
          zznamt TYPE CHAR15,"zordi_t-zznamt,
        END OF ty_item.

DATA : gt_headr TYPE STANDARD TABLE OF ty_headr,
       gt_item  TYPE STANDARD TABLE OF ty_item,
       wt_headr TYPE ty_headr,
       wt_item  TYPE ty_item,
       gt_file TYPE FILETABLE,
       gs_file TYPE FILE_TABLE,
       gv_rc   TYPE i.


parameters : p_path type string.

skip 2.

parameters : r1 radiobutton group grp1 default 'X' user-command actn,
             r2 radiobutton group grp1.



AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_path .



CALL METHOD cl_gui_frontend_services=>file_open_dialog
*  EXPORTING
*    window_title            =
*    default_extension       =
*    default_filename        =
*    file_filter             =
*    with_encoding           =
*    initial_directory       =
*    multiselection          =
  CHANGING
    file_table              =  gt_file
    rc                      =  gv_rc
*    user_action             =
*    file_encoding           =
*  EXCEPTIONS
*    file_open_dialog_failed = 1
*    cntl_error              = 2
*    error_no_gui            = 3
*    not_supported_by_gui    = 4
*    others                  = 5
        .
IF sy-subrc <> 0.
* Implement suitable error handling here

ELSE.

read TABLE gt_file into gs_file INDEX 1.
IF sy-subrc = 0.
p_path = gs_file-filename.
ENDIF.

ENDIF.


START-OF-SELECTION.

case 'X'.
  when r1.

CALL METHOD cl_gui_frontend_services=>gui_upload
  EXPORTING
     filename                = p_path
     filetype                = 'DAT'  "ASC
*    has_field_separator     = SPACE
*    header_length           = 0
*    read_by_line            = 'X'
*    dat_mode                = SPACE
*    codepage                = SPACE
*    ignore_cerr             = ABAP_TRUE
*    replacement             = '#'
*    virus_scan_profile      =
*  IMPORTING
*    filelength              =
*    header                  =
  CHANGING
    data_tab                = gt_headr
*    isscanperformed         = SPACE
*  EXCEPTIONS
*    file_open_error         = 1
*    file_read_error         = 2
*    no_batch                = 3
*    gui_refuse_filetransfer = 4
*    invalid_type            = 5
*    no_authority            = 6
*    unknown_error           = 7
*    bad_data_format         = 8
*    header_not_allowed      = 9
*    separator_not_allowed   = 10
*    header_too_long         = 11
*    unknown_dp_error        = 12
*    access_denied           = 13
*    dp_out_of_memory        = 14
*    disk_full               = 15
*    dp_timeout              = 16
*    not_supported_by_gui    = 17
*    error_no_gui            = 18
*    others                  = 19
        .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.


LOOP AT gt_headr INTO wt_headr.

WRITE : / wt_headr-zzsdoc,wt_headr-zzdcat,wt_headr-zzdtyp,wt_headr-zzsorg,wt_headr-zzdist,wt_headr-zzdivi,wt_headr-zzcust,wt_headr-zzcpur,wt_headr-zzdcur,wt_headr-zznamt,wt_headr-zzerdat.

ENDLOOP.

when r2.
CALL FUNCTION 'GUI_UPLOAD'
  EXPORTING
    filename                      = p_path
  FILETYPE                      = 'DAT'
*   HAS_FIELD_SEPARATOR           = ' '
*   HEADER_LENGTH                 = 0
*   READ_BY_LINE                  = 'X'
*   DAT_MODE                      = ' '
*   CODEPAGE                      = ' '
*   IGNORE_CERR                   = ABAP_TRUE
*   REPLACEMENT                   = '#'
*   CHECK_BOM                     = ' '
*   VIRUS_SCAN_PROFILE            =
*   NO_AUTH_CHECK                 = ' '
* IMPORTING
*   FILELENGTH                    =
*   HEADER                        =
  tables
    data_tab                      = GT_ITEM
* CHANGING
*   ISSCANPERFORMED               = ' '
* EXCEPTIONS
*   FILE_OPEN_ERROR               = 1
*   FILE_READ_ERROR               = 2
*   NO_BATCH                      = 3
*   GUI_REFUSE_FILETRANSFER       = 4
*   INVALID_TYPE                  = 5
*   NO_AUTHORITY                  = 6
*   UNKNOWN_ERROR                 = 7
*   BAD_DATA_FORMAT               = 8
*   HEADER_NOT_ALLOWED            = 9
*   SEPARATOR_NOT_ALLOWED         = 10
*   HEADER_TOO_LONG               = 11
*   UNKNOWN_DP_ERROR              = 12
*   ACCESS_DENIED                 = 13
*   DP_OUT_OF_MEMORY              = 14
*   DISK_FULL                     = 15
*   DP_TIMEOUT                    = 16
*   OTHERS                        = 17
          .
IF sy-subrc <> 0.
* Implement suitable error handling here
ENDIF.
*
*

LOOP AT gt_item INTO wt_item.

WRITE : / wt_item-zzsdoc,wt_item-zzsitm,wt_item-zzmatr,wt_item-zzmdes,wt_item-zzoqty,wt_item-zzunit,wt_item-zznamt.

ENDLOOP.
endcase.