Application Development Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 

DATA FROM LOCAL FILE TO INTERNAL TABLE AND TO DATABASE TABLE

0 Kudos
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.
1 ACCEPTED SOLUTION

maulik
Contributor

Karon, your wt_header should be type compatible with Z_HEADER_TABLE.

See below.

data: wt_header1 type z_header_table


loop at gt_headr into wt_header.

  move-corresponding wt_header to wt_header1.


  insert into z_header_table value wt_header1.
  
  check sy-subrc is initial.
  
  loop at gt_item into wt_item where zzdoc = wt_header-zzdoc.
     insert into z_detail_table values wt_item.
     if sy-subrc = 0.
        commit work.
     endif.
  endloop.
  
endloop.
7 REPLIES 7

maulik
Contributor

Something on these lines should work.

loop at gt_headr into wt_header.
  insert into z_header_table value wt_header.
  
  check sy-subrc is initial.
  
  loop at gt_item into wt_item where zzdoc = wt_header-zzdoc.
     insert into z_detail_table values wt_item.
     if sy-subrc = 0.
        commit work.
     endif.
  endloop.
  
endloop.

0 Kudos

The type of the database table and work area (or internal table) "WT_HEADR" are not Unicode convertible. convertible.

i am getting this error. will you please guide me where exactly i should use your code. since i am using radiobuttons.

0 Kudos

can you please derive a simple example code for the above requirement or please modify my code. since the first coloumn will be header data in the internal tab. how can i move the data's to ztable .

Sandra_Rossi
Active Contributor

What is your issue with transferring to internal table compatible with lines of ZTABLE, and with INSERT of this internal table into database table?

maulik
Contributor

Karon, your wt_header should be type compatible with Z_HEADER_TABLE.

See below.

data: wt_header1 type z_header_table


loop at gt_headr into wt_header.

  move-corresponding wt_header to wt_header1.


  insert into z_header_table value wt_header1.
  
  check sy-subrc is initial.
  
  loop at gt_item into wt_item where zzdoc = wt_header-zzdoc.
     insert into z_detail_table values wt_item.
     if sy-subrc = 0.
        commit work.
     endif.
  endloop.
  
endloop.

Abinathsiva
Active Contributor

Hi karoncdas_31

Both option of Modify and Insert has been provided already, one more option you have is update statement, usually this is not required for custom table updates Insert and modify should work.

update ztable
set <field in ztable> = <value> where <condition>.

maulik
Contributor
0 Kudos

karoncdas_31 you can use this approach.

Step 1: Define a selection-scree variable to decide at run-time if you have a header in the file or not.

parameters: p_head type c default 'X'.

Step 2: add this snippet of code inside your loop to bypass the first row in the internal table processing.

Loop at table_name
   if p_head = abap_true.
     check sy-tabix gt 1.
   endif.
endloop.