Register Login

Upload Text File To Z Table

Updated May 18, 2018

 

REPORT ZF_UTIL_TABLE_UPLOAD
      no standard page heading
      line-size 132
      line-count 65
      message-id zf_0.
************************************************************************
*    Program Name      :    ZF_UTIL_TABLE_UPLOAD
*    Title            :    General Utility to upload data into tables
*    Analyst          :      Kakali
*    Developer       :    Kakali
*    Date            :    10/29/2003
*    Description      :    General Utility to upload data into tables
*  Special Instruction :
* This program loads data into any table specified on selection screen.
* File should be app server file with TAB delimiter and the order of the
* fields in the file should match with TABLE fields order.
************************************************************************

************************************************************************
* MODIFICATION LOG
*-----------------------------------------------------------------------
* Sl. Date        Correction No.  Changed by      Description
*-----------------------------------------------------------------------
* 1.  08-22-2002  DC1K925297    Kakali  Initial development
************************************************************************
* Tables
************************************************************************
TABLES: dd03l.  "Table Fields
************************************************************************
*Data Declarations
************************************************************************
DATA:  dref TYPE REF TO data.  "Reference Variable
data:  v_title  type sy-lisel.          " For title1
DATA : v_hex TYPE x VALUE '09'. "Tab delimiter
************************************************************************
*Field Symbols
************************************************************************
FIELD-SYMBOLS: <fs1>,  "Points to Table work area
              <fs2>.    "Points to Table Field


************************************************************************
*Internal tables
************************************************************************
DATA:  i_tmp(100) OCCURS 0 WITH HEADER LINE.  "Split internal table
DATA : i_fields LIKE dd03l OCCURS 0 WITH HEADER LINE.
DATA : i_data(1000) TYPE c OCCURS 0 WITH HEADER LINE.

************************************************************************
*Selection Screen
************************************************************************
PARAMETER : p_table LIKE dd03l-tabname OBLIGATORY.
PARAMETERS: p_file TYPE rlgrap-filename
              DEFAULT '/usr/sap/shared/'
                                                          OBLIGATORY .
*-----------------------------------------------------------------------
* INCLUDES
*-----------------------------------------------------------------------

*-----------------------------------------------------------------------
*                  At selection-screen
*-----------------------------------------------------------------------
AT SELECTION-SCREEN.

  PERFORM validate_app_filename.

  perform validate_table.
************************************************************************
*Start-of-selection
************************************************************************
START-OF-SELECTION.

  PERFORM open_file.
  PERFORM dynamic_table_load.


*---------------------------------------------------------------------*
*      FORM open_file                                              *
*---------------------------------------------------------------------*
*      ........                                                      *
*---------------------------------------------------------------------*
FORM open_file.

  REFRESH i_data.
  OPEN DATASET P_file FOR INPUT IN TEXT MODE.
  IF SY-SUBRC <> 0.
    MESSAGE I000 WITH 'No file found'.

    STOP.
  ELSE.
    DO.
      READ DATASET P_FILE INTO I_DATA.
      IF SY-SUBRC <> 0.
        EXIT.
      ELSE.
        APPEND I_DATA.
        CLEAR I_DATA.
      ENDIF.
    ENDDO.
  ENDIF.

ENDFORM.

*---------------------------------------------------------------------*
*      FORM dynamic_table_load                                    *
*---------------------------------------------------------------------*
*      ........                                                      *
*---------------------------------------------------------------------*
FORM 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 <fs1>.

  LOOP AT i_data.
    REFRESH i_tmp.
*--Split each input row data into tokens with Tab delimiter
    SPLIT i_data AT v_hex INTO TABLE i_tmp.
*    CLEAR .
*---Assign each field of table to field-symbol
    DO.
      ASSIGN COMPONENT sy-index OF STRUCTURE  <fs1> TO <fs2>.
      IF sy-subrc NE 0.
        EXIT.
      ENDIF.
      READ TABLE i_tmp  INDEX sy-index.
      IF sy-subrc EQ 0.
        <fs2>  = i_tmp.
      ENDIF.
    ENDDO.
*--Now  will have all the data - Modify the table
    MODIFY (p_table) FROM <fs1>.
  ENDLOOP.

  clear sy-tfill.
  describe table i_data lines sy-tfill.
  Write: /'Total number of records loaded from file ',
        p_table no-gap, ' are : ', sy-tfill.

ENDFORM.                    " LOAD_TABLES

*&---------------------------------------------------------------------*
*&      Form  validate_table
*&---------------------------------------------------------------------*
*      text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <--  p2        text
*----------------------------------------------------------------------*
FORM validate_table.
  if p_table+0(1) <> 'Y' and
    p_table+0(1) <> 'Z'.
    message e000(ZF_0) with
      'Only custom tables allowed with this utility'.
  endif.
ENDFORM.                    " validate_table

*&---------------------------------------------------------------------*
*&      Form  validate_app_filename
*&---------------------------------------------------------------------*
*      This subroutine is used to validate the application file name
*      entered on the selection screen
*----------------------------------------------------------------------*
form validate_app_filename.

  data : v_len type i.          " To get the length of the file name



  v_len = strlen( p_file ).

  if p_file ca space.
    if sy-fdpos < v_len.
      message e000(zf_0) with
        'File name should not contain spaces'.
    endif.
  endif.

  v_len = v_len - 1.

  if p_file+v_len(2) = '/ '.
    message e000(zf_0) with
      'File name cannot be spaces'.
  endif.

*-- If filename contains a back slash
  if p_file ca ''. "File name should not contain ''.
    message e000(zf_0) with
      'File name should not contain '.
  endif.
*-- If filename contains only a forward slash
  if p_file+0(1) = '/' and p_file+1(127) = space.
    message e000(zf_0) with
      'File name should not contain only /'.
  endif.
endform.                    " validate_app_filename


×