With the coding below you can use a file to upload data into any Z table. The program below first deletes the full content first, then upload the content from the file. If you want different behavior, you can adjust the program to your own needs.
The program is to be protected with a proper authorization check. And it is for utility support only for non-productive systems. Do not use on productive systems.
Be careful: the program first deletes ALL the content of the current table. Then it inserts the entries from the file.
Program selection screen:
Coding:
*&--------------------------------------------------------------------*
*& Report Z_UPLOAD_TABLE
*&--------------------------------------------------------------------*
*& Description: Upload the data from a file and fill a Z table
*&--------------------------------------------------------------------*
REPORT z_upload_table.
PARAMETERS:
p_table TYPE dd02l-tabname OBLIGATORY,
p_file TYPE ibipparms-path OBLIGATORY.
DATA: lt_file_data TYPE STANDARD TABLE OF string,
lt_table_data TYPE REF TO data,
lt_fieldcat TYPE lvc_t_fcat,
lt_component TYPE abap_component_tab,
lv_separator TYPE c LENGTH 1 VALUE ','.
DATA: lv_offset TYPE i VALUE 0,
lv_until TYPE i VALUE 0,
lv_field TYPE string.
DATA: lv_filename TYPE string.
FIELD-SYMBOLS: <lt_table_data> TYPE STANDARD TABLE,
<ls_table_data> TYPE any,
<lv_field> TYPE any.
DATA: new_line TYPE REF TO data.
AT SELECTION-SCREEN.
DATA: l_got_state TYPE ddgotstate.
* Validate table name
CALL FUNCTION 'DDIF_TABL_GET'
EXPORTING
name = p_table
IMPORTING
gotstate = l_got_state
EXCEPTIONS
OTHERS = 1.
IF l_got_state <> 'A'.
MESSAGE 'Table does not exist' TYPE 'E'.
ENDIF.
IF p_table+0(1) <> 'Z' AND
p_table+0(1) <> 'Y'.
MESSAGE 'Please use only Z or Y tables.' TYPE 'E'.
ENDIF.
*----------------------------------------------------------------------*
* AT SELECTION-SCREEN ON VALUE-REQUEST
*----------------------------------------------------------------------*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
CALL FUNCTION 'F4_FILENAME'
IMPORTING
file_name = p_file.
START-OF-SELECTION.
* Dynamically create internal table
CREATE DATA lt_table_data TYPE TABLE OF (p_table).
ASSIGN lt_table_data->* TO <lt_table_data>.
CREATE DATA new_line LIKE LINE OF <lt_table_data>.
ASSIGN new_line->* TO <ls_table_data>.
* Generate field catalog for the table
CALL FUNCTION 'LVC_FIELDCATALOG_MERGE'
EXPORTING
i_structure_name = p_table
CHANGING
ct_fieldcat = lt_fieldcat
EXCEPTIONS
OTHERS = 1.
IF sy-subrc <> 0.
MESSAGE 'Error generating field catalog' TYPE 'E'.
ENDIF.
lv_filename = p_file.
* Read file into internal table
CALL FUNCTION 'GUI_UPLOAD'
EXPORTING
filename = lv_filename
filetype = 'ASC'
TABLES
data_tab = lt_file_data
EXCEPTIONS
file_open_error = 1
file_read_error = 2
no_batch = 3
gui_refuse_filetransfer = 4
OTHERS = 5.
IF sy-subrc <> 0.
MESSAGE 'Error uploading file' TYPE 'E'.
ENDIF.
* Delete all entries from the target table
DELETE FROM (p_table).
IF sy-subrc = 0.
MESSAGE 'All entries deleted from table' TYPE 'I'.
ENDIF.
* Parse and insert data into the table
DESCRIBE TABLE lt_file_data LINES DATA(lv_idx).
DELETE lt_file_data INDEX lv_idx.
LOOP AT lt_file_data INTO DATA(ls_line) FROM 4.
CLEAR <ls_table_data>.
LOOP AT lt_fieldcat ASSIGNING FIELD-SYMBOL(<fs_fieldcat>).
IF <fs_fieldcat>-fieldname = 'MANDT'.
ASSIGN COMPONENT <fs_fieldcat>-fieldname OF STRUCTURE <ls_table_data> TO <lv_field>.
IF sy-subrc = 0.
<lv_field> = sy-mandt.
ENDIF.
ELSE.
CLEAR lv_offset.
DO strlen( ls_line ) TIMES.
DATA(lv_index) = sy-index.
DATA(lv_single) = substring( val = ls_line off = lv_index - 1 len = 1 ).
IF lv_index = 1 AND lv_single = '|'.
lv_offset = lv_offset + 1.
ELSEIF lv_single = '|'. "New field
lv_until = lv_index - lv_offset - 1.
lv_field = ls_line+lv_offset(lv_until).
lv_offset = lv_offset + lv_until + 1.
ASSIGN COMPONENT <fs_fieldcat>-fieldname OF STRUCTURE <ls_table_data> TO <lv_field>.
IF sy-subrc = 0.
<lv_field> = lv_field.
ENDIF.
ls_line = ls_line+lv_offset.
EXIT.
ENDIF.
ENDDO.
ENDIF.
ENDLOOP.
APPEND <ls_table_data> TO <lt_table_data>.
ENDLOOP.
* Insert data into the database table
INSERT (p_table) FROM TABLE <lt_table_data>.
IF sy-subrc = 0.
MESSAGE 'Data successfully inserted into table' TYPE 'S'.
ELSE.
MESSAGE 'Error inserting data into table' TYPE 'E'.
ENDIF.
