Tip

ABAP program for loading PC file into table

ZTABLE_LOAD program can be used to load data into any table in SAP. Program only works in 4.6x version. Program extensively uses Dynammic language concepts.


REPORT ztable_load .

*This program loads data into any table specified on selection screen.
*File should be PC file with TAB delimiter and the order of the
*fields in the file should match with TABLE fields order.




TABLES: dd03l.  "Table Fields

DATA:  dref TYPE REF TO data.  "Reference Variable
DATA : fields_itab LIKE dd03l OCCURS 0 WITH HEADER LINE.
DATA : data_itab(1000) TYPE c OCCURS 0 WITH HEADER LINE.
FIELD-SYMBOLS: <fs_table_wa>,  "Points to Table work area
               <fs_field>.     "Points to Table Field

PARAMETER : p_table LIKE dd03l-tabname OBLIGATORY.
PARAMETER : p_file LIKE ibipgettxt-tdfilename OBLIGATORY.

DATA : v_hex TYPE x VALUE '09'. "Tab delimiter

DATA: i_tmp(100) OCCURS 0 WITH HEADER LINE.  "Split internal table


AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.

  CALL FUNCTION 'WS_FILENAME_GET'
       EXPORTING
            mask             = ',*.*,*.*.'
       IMPORTING
            filename         = p_file
       EXCEPTIONS
            inv_winsys       = 1
            no_batch         = 2
            selection_cancel = 3
            selection_error  = 4
            OTHERS           = 5.

AT SELECTION-SCREEN.
  REFRESH fields_itab.
  SELECT        * FROM  dd03l
         WHERE  tabname     = p_table
        

    Requires Free Membership to View

AND as4local = 'A'. IF dd03l-fieldname NE '.INCLUDE '. MOVE dd03l TO fields_itab. APPEND fields_itab. ENDIF. ENDSELECT. IF sy-subrc NE 0. MESSAGE e005(zb). ENDIF. ********************** MAIN PROCESSING BLOCK *************************** START-OF-SELECTION. PERFORM f_open_file. PERFORM f_dynamic_table_load. *---------------------------------------------------------------------* * FORM f_open_file * *---------------------------------------------------------------------* * ........ * *---------------------------------------------------------------------* FORM f_open_file. REFRESH data_itab. CALL FUNCTION 'WS_UPLOAD' EXPORTING filename = p_file filetype = 'ASC' TABLES data_tab = data_itab EXCEPTIONS conversion_error = 1 file_open_error = 2 file_read_error = 3 invalid_table_width = 4 invalid_type = 5 no_batch = 6 unknown_error = 7 OTHERS = 8. IF sy-subrc NE 0. WRITE : ' Error in opening file : ' , p_file. STOP. ENDIF. ENDFORM. *---------------------------------------------------------------------* * FORM f_dynamic_table_load * *---------------------------------------------------------------------* * ........ * *---------------------------------------------------------------------* FORM f_dynamic_table_load. *---Create a new data object of table specified in parameter (p_table) *---Reference is stored in dref variable. dref now contains a pointer *---to newly created object. CREATE DATA dref TYPE (p_table). *--Asssign the reference to field-symbol ASSIGN dref->* TO <fs_table_wa>. LOOP AT data_itab. REFRESH i_tmp. *--Split each input row data into tokens with Tab delimiter SPLIT data_itab AT v_hex INTO TABLE i_tmp. CLEAR <fs_table_wa>. *---Assign each field of table to field-symbol <fs1> DO. ASSIGN COMPONENT sy-index OF STRUCTURE <fs_table_wa> TO <fs_field> . IF sy-subrc NE 0. EXIT. ENDIF. READ TABLE i_tmp INDEX sy-index. IF sy-subrc EQ 0. <fs_field> = i_tmp. ENDIF. ENDDO. *--Now <fs> will have all the data - Modify the table MODIFY (p_table) FROM <fs_table_wa>. ENDLOOP. clear sy-tfill. describe table data_itab lines sy-tfill. Write: /'Total number of records updated in Table ', p_table no-gap, ' are : ', sy-tfill. ENDFORM. " LOAD_TABLES

This was first published in January 2002

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.