2008 Jul 16 2:30 PM
Hi All,
Please explain me how to upload data from external files like .csv etc to sap tables. I want to add data in one go instead of adding record one by one manually into the table.
2008 Jul 16 2:34 PM
Hi ,
Declare internal table with same structure as database table to be updated.
upload the flat file data into internal table using GUI_UPLOAD.
Insert/Modify Database table with internal table entries.
<points_removed_by_moderator> <= read [the rules|https://www.sdn.sap.com/irj/sdn/wiki?path=/display/home/rulesofEngagement] here!
Thanks,
USR
Edited by: Julius Bussche on Jul 16, 2008 2:51 PM
2008 Jul 16 2:47 PM
Hi raju,
Will you please explain me in more detail. If you can explain me with example will be great. Thanks in advance.
2008 Jul 16 2:53 PM
Hi,
You have u use methods like LSMW, Call transaction, Sessions method or BAPI's to upload data into SAP tables, as they require authority check, and some fields in turn depends on other table fields which are linked through check tables.
Regards,
B Krishna
2008 Jul 16 3:02 PM
Hi,
If it is Ztable then check below sample code.
Here My Ztable has 3 fields.
PARAMETERS: po_file TYPE rlgrap-filename DEFAULT 'E:test.xls'.
TYPES: BEGIN OF t_final,
empno(10) TYPE c,
name(30) TYPE c,
state(25) TYPE c,
END OF t_final.
PARAMETERS: po_file TYPE rlgrap-filename.
DATA: i_tab TYPE STANDARD TABLE OF alsmex_tabline,
i_final TYPE STANDARD TABLE OF t_final,
wa_tab TYPE alsmex_tabline,
wa_final TYPE t_final.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR po_file.
PERFORM open_folder.
START-OF-SELECTION.
CALL FUNCTION 'ALSM_EXCEL_TO_INTERNAL_TABLE'
EXPORTING
filename = po_file
i_begin_col = 1
i_begin_row = 1
i_end_col = 3
i_end_row = 65256
TABLES
intern = i_tab
EXCEPTIONS
inconsistent_parameters = 1
upload_ole = 2
OTHERS = 3.
CHECK NOT i_tab[] IS INITIAL.
LOOP AT i_tab INTO wa_tab.
CASE wa_tab-col.
WHEN 1.
wa_final-empno = wa_tab-value.
WHEN 2.
wa_final-name = wa_tab-value.
WHEN 3.
wa_final-state = wa_tab-value.
ENDCASE.
AT END OF row.
APPEND wa_final TO i_final.
CLEAR wa_final.
ENDAT.
ENDLOOP.
IF NOT i_final[] IS INITIAL.
"Modify if the record already exist with the key else Insert the
"record
MODIFY zempdata FROM TABLE i_final.
CHECK sy-subrc IS INITIAL.
COMMIT WORK.
ENDIF.
FORM open_folder .
DATA: li_file TYPE TABLE OF sdokpath,
lwa_file TYPE sdokpath.
CLEAR: po_file, lwa_file.
REFRESH li_file[].
CALL FUNCTION 'TMP_GUI_FILE_OPEN_DIALOG'
TABLES
file_table = li_file
EXCEPTIONS
cntl_error = 1
OTHERS = 2.
IF sy-subrc IS INITIAL.
READ TABLE li_file INTO lwa_file INDEX 1.
IF sy-subrc IS INITIAL.
po_file = lwa_file-pathname.
ENDIF.
ENDIF.
ENDFORM. " open_folder
If it is standard table then never update directly
Use BAPI/BDC for the same.
Thanks,
Vinod.
Edited by: Vinod Reddy Vemuru on Jul 16, 2008 7:33 PM
2008 Jul 16 4:21 PM
TYPES: BEGIN OF ty_file_data,
matnr TYPE matnr,
mtart TYPE mtart,
maktl TYPE matlk,
END OF ty_file_data,
Data: it_file_data type standard table of ty_file_data
initial size 0,
it_raw TYPE truxs_t_text_data.
PARAMETERS: p_filenm LIKE rlgrap-filename OBLIGATORY.
AT SELECTION-SCREEN ON VALUE-REQUEST FOR
p_filenm.
PERFORM get_file_from_ps.
START-OF-SELECTION.
PERFORM get_data_from_file.
FORM get_file_from_ps.
CALL FUNCTION 'F4_FILENAME'
EXPORTING
program_name = syst-cprog
dynpro_number = syst-dynnr
field_name = ''
IMPORTING
file_name = p_filenm.
ENDFORM. " get_file_from_ps
FORM get_data_validate .
CALL FUNCTION 'TEXT_CONVERT_XLS_TO_SAP'
EXPORTING
i_line_header = 'X'
i_tab_raw_data = it_raw
i_filename = p_filenm
TABLES
i_tab_converted_data = it_file_data
EXCEPTIONS
conversion_failed = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM
*******************
Assuming that the XCL file have Matnr, MTART and MATKL, the above code you will get all the values in the Excel file or CSV file into Internal table it_file_data. Its works for me in ECC 6.