Introduction

We use Database tables frequently for look-up in our ABAP logic. Maintaining records in table is a manual process by updating table record by record . Available program for such updates are static in nature .

Following approach allow you to create a generic program from updating any database table.

How To Use :

  • Go to TCODE SE38 .
  • Create a program Z_FLATFILE_UPLOAD as shown Below

Untitled.jpg

Untitled.jpg

  • Copy Paste the below code

*&———————————————————————*
*& Z_FLATFILE_UPLOAD.
*& TITLE         : Program to upload a flat file from path provided
*& CREATED BY    : Jaya Tiwari
*&———————————————————————*

REPORT z_flatfile_upload.
*———————————————————————-*
* data declarations
*———————————————————————-*
TYPES: BEGIN OF lty_ttab,
rec
TYPE STRING,
END OF lty_ttab.

DATA: lt_itab TYPE STANDARD TABLE OF lty_ttab WITH HEADER LINE.
DATA: lv_file_str TYPE string,
lv_column
TYPE n LENGTH 4.
*Dynamic Table and Work area
FIELD-SYMBOLS :
<lfs_table> 
TYPE ANY TABLE ,
<lfs_watable>
.

DATA : lv_tabname TYPE dd02ltabname . ” DataBase Table Name
DATA : lt_tble TYPE REF TO data ,
lt_line
TYPE REF TO data .
“Structure for Reading Field Names of DataBase Table
TYPES : BEGIN OF lty_dd03vv,
tabname 
TYPE tabname ,
fieldname 
TYPE fieldname ,
position  TYPE tabfdpos ,
END OF lty_dd03vv .
“Internal Table and work area for Reading Field Names of DataBase Table
DATA : lt_dd03vv TYPE STANDARD TABLE OF lty_dd03vv WITH HEADER LINE ,
lwa_dd03vv
LIKE LINE OF lt_dd03vv .
DATA : lv_fieldname  TYPE fieldname ,
lv_counter
(2) TYPE n VALUE 0.

FIELD-SYMBOLS: <lfs_field_from>.
*———————————————————————-*
* selection screen design
*———————————————————————-*
SELECTION-SCREEN BEGIN OF BLOCK bi WITH FRAME TITLE  txt_t1 .
SELECTION-SCREEN BEGIN OF LINE .
SELECTION-SCREEN COMMENT (25) txt_file .
PARAMETERS: lp_file TYPE localfile.
SELECTION-SCREEN END OF LINE .

SELECTION-SCREEN BEGIN OF LINE .
SELECTION-SCREEN COMMENT (25) txt_tabl .
PARAMETERS: lp_tble LIKE lv_tabname.
SELECTION-SCREEN END OF LINE .
SELECTION-SCREEN SKIP 1 .

SELECTION-SCREEN BEGIN OF LINE .
PARAMETERS : cb_hdr AS CHECKBOX  DEFAULT ‘X’ MODIF ID m1.
SELECTION-SCREEN COMMENT (70) txt_warn MODIF ID m1.
SELECTION-SCREEN END OF LINE .

SELECTION-SCREEN END OF BLOCK bi.
*———————————————————————-*
* At selection screen for field
*———————————————————————-*
INITIALIZATION .
txt_file
= ‘File Path  ‘.
txt_t1 
= ‘File Upload Details ‘.
txt_tabl
= ‘Target table Name ‘.
txt_warn
= ‘Ignore Header Line in file Uploading .’.
*———————————————————————-*
* Read File Path
*———————————————————————-*
AT SELECTION-SCREEN ON VALUE-REQUEST FOR lp_file.
CALL FUNCTION ‘KD_GET_FILENAME_ON_F4’
EXPORTING
static    = ‘X’
CHANGING
file_name
= lp_file.
*———————————————————————-*
* Validation
*———————————————————————-*
AT SELECTION-SCREEN ON BLOCK bi .
IF  lp_tble IS INITIAL.
MESSAGE ‘Target Table Name Cannot be Empty.’ TYPE ‘E’.
ENDIF.

*———————————————————————-*
* Start of selection
*———————————————————————-*

START-OF-SELECTION.
lv_file_str
= lp_file.

*FM to read file content from path provided
CALL FUNCTION ‘GUI_UPLOAD’
EXPORTING
filename               
= lv_file_str
TABLES
data_tab               
= lt_itab
EXCEPTIONS
file_open_error       
= 1
file_read_error       
= 2
no_batch               
= 3
gui_refuse_filetransfer
= 4
invalid_type           
= 5
no_authority           
= 6
unknown_error         
= 7
bad_data_format       
= 8
header_not_allowed     
= 9
separator_not_allowed 
= 10
header_too_long       
= 11
unknown_dp_error       
= 12
access_denied         
= 13
dp_out_of_memory       
= 14
disk_full             
= 15
dp_timeout             
= 16
OTHERS                  = 17.

IF sysubrc <> 0.
MESSAGE ‘File could not be uploaded’ TYPE ‘E’.
ENDIF.

*———————————————————————-*
* Dynamic Internal Table and Work Area creation
*———————————————————————-*

IF lp_tble IS NOT INITIAL.

lv_tabname = lp_tble.
“Check If table exist in Data Dictionary and no of columns in table

SELECT COUNT(*)
FROM dd03vv
INTO  lv_counter
WHERE tabname = lv_tabname
AND as4local = ‘A’ .

IF sysubrc = 0 AND lv_counter GT 0 .

“Dynamic Internal Table Defination
CREATE DATA lt_tble TYPE TABLE OF (lp_tble).
ASSIGN lt_tble->* TO <lfs_table> .

“Dynamic Work Area Defination
CREATE DATA lt_line LIKE LINE OF <lfs_table>.
ASSIGN lt_line->* TO <lfs_watable> .

SELECT
tabname
fieldname
position
FROM dd03vv
INTO TABLE lt_dd03vv
WHERE tabname = lv_tabname
AND as4local = ‘A’ .

SORT lt_dd03vv BY  position .
*———————————————————————-*
* Assign Data to Internal Table via Work Area
*———————————————————————-*
LOOP AT lt_itab.
” Ignore header Row if Checkbox is checked
IF cb_hdr = ‘X’.

cb_hdr = .
ELSE.
“Read Field Name from Internal Table one by one
DO  lv_counter TIMES.
READ TABLE lt_dd03vv INTO lwa_dd03vv
WITH KEY position = syindex BINARY SEARCH .
lv_fieldname
lwa_dd03vvfieldname .

“Assign Data to each field
ASSIGN COMPONENT lv_fieldname OF STRUCTURE
<lfs_watable>
TO <lfs_field_from>.

ENDDO.

“Insert row into Dynamic Internal table
INSERT <lfs_watable> INTO TABLE  <lfs_table> .
ENDIF.
ENDLOOP.
*———————————————————————-*
***——- Modify Database tabe with Internal table ——-***
*   If record already exist it will be update ,
*   else new entry will get created
*———————————————————————-*

MODIFY (lp_tble) FROM TABLE  <lfs_table>.
IF sysubrc = 0.
WRITE : ‘Table ‘,lp_tble ,‘Successfully Updated’.
ENDIF.
ELSE .

MESSAGE ‘Table not found in Data Dictionary .’ TYPE ‘E’.

ENDIF.

ENDIF.
*———————————————————————-*
* End Of Program
*———————————————————————-*

  • Save and Activate your code .
  • Execute Program  ,Following selection screen will appear .Provide file path and table name as input .Also uncheck check box if there is no header row.

Untitled.jpg

  • Test Data :

ztest1

ztest2

ztest3

A

DD

ADADSADA

B

DD

adadada

C

DD

sccada

D

DD

adadada

E

DD

12121313

F

DD

12121

G

DD

  1. 445.67

H

DD

saas

I

DD

rwrw

J

DD

zsczczc

K

DD

awaqswqw

L

DD

qqqqqqqqqqqqqqqqqqqqqqqqqqq

  • Pass Inputs like  shown in above snapshot and execute .On success following message will appear .

Untitled.jpg

  • Check database table via TCODE SE16  ,It will display loaded records.

Untitled.jpg

  • To Update Table Data change the Data in file and load again .

Untitled.jpg

*** For client specific tables value in column MANDT will automatically gets populated so no need to create a separate column in flat file for the same .

This program only allows you to add new records or update ( based on key ) existing records in a table .Delete functionality is purposefully opted out, as this can accidentally affect important tables .However if someone want to add delete functionality in program, I would suggest to put some authorization constraint as who or which table can use the program to delete data  .A custom table can be created to store authorized users or tables that can use deletion functionality and before deletion this table will be referred by program .

Limitations


  1. There is limitation of data length for internal table reading  flat file of 262143 characters.
  2. Program can be executed for one table at a time .

Appendix :


Loading a Flat file and placing at Application Server

This program will allow you to load your flat file at specific path in application server .

Here we are providing 3 static paths which can be configured once (Path exist in AL11- SAP Directory) and then can be used just by selecting specific radio button .

If User want to use different path other than listed one  ,Other radio button will allow him to pass it manually .

/wp-content/uploads/2013/09/2_333494.jpg

Program Output :

/wp-content/uploads/2013/09/1_333506.jpg

Double Click on output screen to navigate through SAP Directory to see file output .

Sample File Output

/wp-content/uploads/2013/09/1_333506.jpg

Sample Code :

*&—————————————————————————————–*
*& Report:      z_flatfile_load_al11
*& Title :      Program will load a flat file and place it at application server
*&              at specified path
*& Created BY:  Jaya Tiwari
*&—————————————————————————————–*

REPORT z_flatfile_load_al11.
*—————————————————————————————-*
* data declarations
*—————————————————————————————-*

” To Read CSV file data from user’s workstation
TYPES: BEGIN OF lty_ttab,
rec
TYPE string,
END OF lty_ttab.

DATA: lt_itab TYPE STANDARD TABLE OF lty_ttab  WITH HEADER LINE .

DATA: lv_file_str TYPE string ,   “file Path
lv_system
TYPE c LENGTH 15 , “system name
lv_count
TYPE n LENGTH 5 .

DATA : lv_counter(2) TYPE n VALUE 0 ,
lv_tabname
TYPE tabname.

“For Application server file
DATA: lv_msg_text(50),
lv_string
(20000) TYPE c,
lv_filename
TYPE rlgrapfilename.

“For Process chain last execution logs
DATA : lv_status TYPE c LENGTH 70 ,
lv_datum
TYPE c LENGTH  50.
DATA: lv_ans(1) TYPE c.

DATA : path_name(150) TYPE c. “AL11 Path Name

DATA len  LIKE syfdpos.

DATA len1 LIKE syfdpos.
DATA char.
DATA screentype(4).
DATA shift_ind LIKE syindex.

DATA l_delimiter TYPE c.
DATA l_lines     TYPE i.
DATA ls_split    TYPE string.
DATA lt_split    LIKE TABLE OF ls_split.

*———————————————————————-*
* selection screen design
*———————————————————————-*

SELECTION-SCREEN BEGIN OF BLOCK b1 WITH FRAME TITLE  txt_t1 .
SELECTION-SCREEN BEGIN OF LINE .
SELECTION-SCREEN COMMENT (25) txt_file .
PARAMETERS: lp_file TYPE localfile . “file path
SELECTION-SCREEN END OF LINE .

SELECTION-SCREEN END OF BLOCK b1.

SELECTION-SCREEN BEGIN OF BLOCK b2 WITH FRAME TITLE  txt_t2 .

“Monthly
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS: gf_rdbt1 RADIOBUTTON GROUP g1 USER-COMMAND usr DEFAULT ‘X’.
SELECTION-SCREEN COMMENT (70) gf_rdcm1    .
SELECTION-SCREEN END OF LINE.
“One Time
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS: gf_rdbt2 RADIOBUTTON GROUP g1 .
SELECTION-SCREEN COMMENT (70gf_rdcm2   .
SELECTION-SCREEN END OF LINE.
“Weekly
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS: gf_rdbt3 RADIOBUTTON GROUP g1  .
SELECTION-SCREEN COMMENT (70gf_rdcm3    .
SELECTION-SCREEN END OF LINE.

“User Provided Path
SELECTION-SCREEN BEGIN OF LINE.
PARAMETERS: gf_rdbt4 RADIOBUTTON GROUP g1 .
SELECTION-SCREEN COMMENT (23gf_rdcm4   .
PARAMETERS: lp_fil2 LIKE rlgrapfilename MODIF ID rd2 .
SELECTION-SCREEN END OF LINE.

*SELECTION-SCREEN SKIP 1 .

SELECTION-SCREEN END OF BLOCK b2.

*———————————————————————-*
* At selection screen for field
*———————————————————————-*
INITIALIZATION .

txt_file = ‘Input File Path   ‘.
txt_t1  
= ‘User Selected File Path ‘.
txt_t2  
= ‘Application Server Path to Write File  ‘.
gf_rdcm1
= ‘  /Path_1/monthly’.
gf_rdcm2
= ‘  /Path_2/onetime’.
gf_rdcm3
= ‘  /Path_3/weekly’.
gf_rdcm4
= ‘  Other’.

*———————————————————————-*
* Read File Path
*———————————————————————-*
AT SELECTION-SCREEN OUTPUT.

IF  gf_rdbt1 = ‘X’ OR  gf_rdbt2 = ‘X’ OR  gf_rdbt3 = ‘X’.
LOOP AT SCREEN.
IF screengroup1 = ‘RD2’.
screeninput = 0.
MODIFY SCREEN.
ENDIF.
ENDLOOP.
ENDIF.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR lp_file.
CALL FUNCTION ‘KD_GET_FILENAME_ON_F4’
EXPORTING
static    = ‘X’
CHANGING
file_name
= lp_file.

AT LINE-SELECTION.
CALL TRANSACTION ‘AL11’ .

*———————————————————————-*
* Start of selection
*———————————————————————-*

START-OF-SELECTION.
SPLIT lp_file AT ‘.’ INTO lv_file_str lv_filename .
TRANSLATE lv_filename TO UPPER CASE .

*Important Points :

* Program will accept only file with .CSV extension . Please modify the code if you need to load other file types .


IF lv_filename = ‘CSV’ .

CLEAR : lv_filename .
“Get Filename
SPLIT lv_file_str AT ‘\’ INTO TABLE lt_split.
DESCRIBE TABLE lt_split LINES l_lines.
* file name
LOOP AT lt_split INTO ls_split.
IF sytabix EQ l_lines.
lv_filename 
= ls_split.
ENDIF.
ENDLOOP.

lv_file_str = lp_file .
IF lv_file_str IS NOT INITIAL.
*Read CSV File
PERFORM load_file .
*    Create / overwrite a file on app server from internal table
PERFORM write_file .
ELSE.
MESSAGE ‘File Path Needed .’ TYPE ‘W’.
ENDIF.
ELSE.
MESSAGE ‘.CSV File Needed .’ TYPE ‘W’.
ENDIF.

FREE :lv_system ,lt_itab,lv_filename ,lt_split ,l_lines ,lv_file_str.

*&———————————————————————*
*&      Form  load_file
*&———————————————————————*
*      load flat file from user system into internal table
*———————————————————————-*
FORM load_file .

*FM to read file content from path provided
CALL FUNCTION ‘GUI_UPLOAD’
EXPORTING
filename               
= lv_file_str
TABLES
data_tab               
= lt_itab
EXCEPTIONS
file_open_error        
= 1
file_read_error        
= 2
no_batch               
= 3
gui_refuse_filetransfer
= 4
invalid_type           
= 5
no_authority           
= 6
unknown_error          
= 7
bad_data_format        
= 8
header_not_allowed     
= 9
separator_not_allowed  
= 10
header_too_long        
= 11
unknown_dp_error       
= 12
access_denied          
= 13
dp_out_of_memory       
= 14
disk_full              
= 15
dp_timeout             
= 16
OTHERS                  = 17.

IF sysubrc <> 0.
MESSAGE ‘File could not be uploaded’ TYPE ‘E’.
ENDIF.

ENDFORM .                    “load_file

*&———————————————————————*
*&      Form  write_file
*&———————————————————————*
*      write content of file loaded into application server
*———————————————————————-*
FORM write_file .

IF     gf_rdbt1 = ‘X’.
CONCATENATE ‘/Path_1/monthly/’ lv_filename  ‘.CSV’ INTO lv_filename .
ELSEIF gf_rdbt2 = ‘X’.
CONCATENATE ‘/Path_2/onetime/’ lv_filename  ‘.CSV’ INTO lv_filename .
ELSEIF gf_rdbt3 = ‘X’.
CONCATENATE ‘/Path_3/weekly/’ lv_filename  ‘.CSV’ INTO lv_filename .
ELSEIF gf_rdbt4 = ‘X’.
IF lp_fil2 EQ ‘.\’ OR lp_fil2 EQ ‘./’.
CONCATENATE  lp_fil2  lv_filename  ‘.CSV’ INTO lv_filename .
ELSEIF  lp_fil2 CP ‘*/*’.
CONCATENATE  lp_fil2 ‘/’ lv_filename  ‘.CSV’ INTO lv_filename .
ELSEIF lp_fil2 CP ‘*\*’.
CONCATENATE  lp_fil2 ‘\’ lv_filename  ‘.CSV’ INTO lv_filename .
ENDIF .
ENDIF.

CONDENSE lv_filename NOGAPS .
OPEN DATASET lv_filename FOR OUTPUT IN TEXT MODE
ENCODING DEFAULT MESSAGE lv_msg_text.

IF sysubrc NE 0.
WRITE: ‘File’ ,lv_filename ,‘ cannot be opened. Reason:’, lv_msg_text.
EXIT.
ELSE.
*Transfer data to file at application server
LOOP AT lt_itab.
TRANSFER lt_itabrec TO lv_filename.
ENDLOOP.
WRITE : ‘File available at Application Server as: ‘ ,lv_filename  .
CLOSE DATASET lv_filename.
REFRESH :lt_itab .

ENDIF.
ENDFORM .                    “write_file

*———————————————————————-*
* End Of Program
*———————————————————————-*


To report this post you need to login first.

16 Comments

You must be Logged on to comment or reply to a post.

  1. Satendra Mishra

    Really its a good program to upload flat file data in table, then that table we can use in Generic data source in ECC and we can make a generic delta data source to populate only 

    delta records in BI.

    Nice document.

    Regards,

    Satendra Mishra

    (0) 
  2. Prashanth konduru

    Good job Jaya… Nice post and useful program. Thanks for sharing with us.

    I have a doubt: I can see you can edit the entries and update the entries.

    whats the procedure to delete the entries. Do you have any  process to do so?

    (0) 
    1. Jaya Tiwari Post author

      Hi Prashanth ,

      I have not provided that capability here but this program can be easily enhanced for that too .

      That will be a good enhancement . I will update the tool for the same.

      Thanks for the idea .

      Regards,

      Jaya

      **I realized that deletion might not be a good functionality to provide without some safety constraints .I have added details in the article .

      (0) 
  3. Mario Tibollo

    you could use a write-optimized DSO with a file datasource. should give the same result.

    deleting data is indeed something to be careful with, but changing existing data in standard SAP tables is a bad idea as well.

    M.

    (0) 

Leave a Reply