Uploading and Downloading Programs - SAP ABAP(Flat file to Database Table and Vice-Versa)


Uploading Program
Uploading programs are used to upload the data from Flat files/Input files into SAP.
Step1:
Prepare an input file as below to upload into SAP

Step2:
Write an ABAP Program in SE38 and
-> Declare Local Structure (declared as per the available input file fields), Internal table and Work area.
-> Make use of Function Module: GUI_UPLOAD to upload the data from Input file to Internal table.
Example:
  CALL FUNCTION 'GUI_UPLOAD'
    
EXPORTING
      FILENAME            
L_PATH    " Input File Path
      FILETYPE            
'ASC'
      HAS_FIELD_SEPARATOR 
'X'
    
TABLES
      DATA_TAB            
IT.       " Internal table to hold the File data
  
IF SY-SUBRC <> 0.
* Implement suitable error handling here
  
ENDIF.

Now, the data will be available in Internal table IT.

By using Internal Table data, we can do following activities:
-> we can upload the Internal table data to DATABASE TABLES by Using DML Operations.
-> we can show the data as program's Output.

Example Program 1:
*&-------------------------------------------------------------------------------
*& Example to upload the Flatfile data to show it as a report Output
*&-------------------------------------------------------------------------------
File Data:

*&Code:
REPORT ZTEST_PROG1.
*& Structure
TYPES:BEGIN OF TY_DATA,
        FNAME1
(10TYPE N,
        FNAME2
(10TYPE C,
        FNAME3     
TYPE CHAR10,
      
END OF TY_DATA.

*& Declare Internal table and Work area
DATAIT_DATA TYPE TABLE OF TY_DATA,
      WA_DATA 
TYPE TY_DATA.

CALL FUNCTION 'GUI_UPLOAD'
  
EXPORTING
    FILENAME            
'C:\Users\nagaraju.adhikari\Desktop\Input_file.txt'
    FILETYPE            
'ASC'
    HAS_FIELD_SEPARATOR 
'X'
  
TABLES
    DATA_TAB            
IT_DATA.

IF SY-SUBRC 0.
* Implement suitable error handling here
  
DELETE IT_DATA INDEX 1.    " Removing the Header from First Line
ENDIF.

LOOP AT IT_DATA INTO WA_DATA.
  
WRITE:/ WA_DATA-FNAME1,
          WA_DATA
-FNAME2,
          WA_DATA
-FNAME3.
ENDLOOP.

Save-> Check->Activate-> Execute.
Report Output:



Example Program2:
*&------------------------------------------------------------------
*& Example to upload the Flat file data Into SAP Database table
*&------------------------------------------------------------------
Input File Data:


Table Structure:


*& Code
REPORT ZTEST_PROG1.
*& Structure
TYPES:BEGIN OF TY_DATA,
        PATIENTID    
TYPE ZPATIENT_DATA-PATIENTID,
        PATIENTNAME  
TYPE ZPATIENT_DATA-PATIENTNAME,
        HOSPITALNAME 
TYPE ZPATIENT_DATA-HOSPITALNAME,
      
END OF TY_DATA.

*& Declare Internal table and Work area
DATAIT_DATA TYPE TABLE OF TY_DATA,
      WA_DATA 
TYPE TY_DATA.
CALL FUNCTION 'GUI_UPLOAD'
  
EXPORTING
    FILENAME            
'C:\Users\nagaraju.adhikari\Desktop\Input_file.txt'
    FILETYPE            
'ASC'
    HAS_FIELD_SEPARATOR 
'X'
  
TABLES
    DATA_TAB            
IT_DATA.

IF SY-SUBRC 0.
* Implement suitable error handling here
  
DELETE IT_DATA INDEX 1.    " Removing the Header from First Line
ENDIF.

*& If you wanted to process record by record you can use the below syntax:
LOOP AT IT_DATA INTO WA_DATA.
  
INSERT ZPATIENT_DATA FROM WA_DATA.
  
CLEAR WA_DATA.
ENDLOOP.

*& or

*& If you wantto process all records at a time use the below syntax:
*& Syntax to insert the multiple records from internal table to DB table
  
INSERT ZPATIENT_DATA FROM TABLE it_DATA.

Save-> Check->Activate-> Execute.

Open Transaction: SE11 and Enter  Database Table name execute to check uploaded data in Database table  from the above program.



Downloading Program

Download programs are used to download the data from SAP DB to Flat files/Input files.

Step1:
Take the requirement from the functional consultant for what data we need to download from SAP into the flat files.

Step2:
  • Write an ABAP Program and
  • Declare Local Structure (declared as per the input file fields), Internal table and Work area.
  • Write a SELECT Query to fetch the data from Database table to Internal Table.
  • Use, Function Module: GUI_DOWNLOAD to download the data from Internal table to output file path.

Usage of FM GUI_DOWNLOAD to download data:
  CALL FUNCTION 'GUI_DOWNLOAD'
    
EXPORTING
      FILENAME            
L_PATH " Output File Path
      FILETYPE            'ASC'
    WRITE_FIELD_SEPARATOR 
'X'
    
TABLES
      DATA_TAB   =
 IT_DATA. " Internal table to hold the Table data
  
IF SY-SUBRC <> 0.
* Implement suitable error handling here
  
ENDIF.

Example Program:

Requirement:
Write a Program to download the customer data from Table: KNA1 into text file.

Program:
REPORT ZDOWNLOAD_CUST_DATA.
*&--------------
*& Declarations
*&--------------
*&structure
TYPESBEGIN OF TY_CUST,
         KUNNR 
TYPE KNA1-KUNNR,
         LAND1 
TYPE KNA1-LAND1,
         NAME1 
TYPE KNA1-NAME1,
         ORT01 
TYPE KNA1-ORT01,
       
END OF TY_CUST.

*& it & wa
DATAIT_CUST TYPE TABLE OF  TY_CUST,
      WA_CUST 
TYPE TY_CUST.

*&--------------
*& Select Query
*&--------------
*& Selecting Few fields of Database table into internal table
*& using below syntax
SELECT KUNNR
       LAND1
       NAME1
       ORT01 
FROM KNA1
       
INTO TABLE IT_CUST
            
WHERE LAND1 'US'.

*&--------------
*& Download Data to PC from SAP
*&--------------
*& Use FM :GUI_DOWNLOAD
CALL FUNCTION 'GUI_DOWNLOAD'
  
EXPORTING
    FILENAME              
'C:\Users\nagaraju.adhikari\Desktop\CUST.txt'
    FILETYPE              
'ASC'
    WRITE_FIELD_SEPARATOR 
'X'
  
TABLES
    DATA_TAB              
IT_CUST.

IF SY-SUBRC 0.
  
MESSAGE 'Data downloaded successfully' TYPE 'I'.
ENDIF.

SAVE->CHECK->ACTIVATE.
Output:


A text file will be downloaded as below to the given path




Comments

Popular posts from this blog

SAP ABAP-Internal Table Operations and Example Program

HR ABAP Interview Questions and Answers

BDC (Batch Data Communication) - SAP ABAP