ABAP program for loading PC file into table

ZTABLE_LOAD program that can be used to load data into any table in SAP.

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
         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

Dig deeper on SAP ABAP

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

0 comments

Oldest 

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:

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close