SAP ABAP Transferring Internal Table Contents to a file on the Application Server
In SAP ABAP you can transfer the contents of the internal table to a file. This is required when you wish to extract data from SAP tables into a flat file. This file can then be used to transfer data to another SAP or NON SAP system. This can also be used for an interfacing when you wish to transfer data on a daily or a weekly basis from R/3 to NON R/3 or SAP. The command used to create a file on the application server is as follows.
OPEN DATASET
Please see example code below.
REPORT ZEX_DATATOFILE .
*&---------------------------------------------------------------------* *& ABAPLOVERS: Data Transfer *&---------------------------------------------------------------------*
* Parameters to enter the path PARAMETERS FILENAME(128) DEFAULT '/usr/tmp/testfile.dat' LOWER CASE. * Table Declaration TABLES VBAK.
* Data Declaration DATA D_MSG_TEXT(50).
* Get data for file transfer DATA INT_VBAK LIKE VBAK OCCURS 100 WITH HEADER LINE. SELECT * FROM VBAK INTO TABLE INT_VBAK. SORT INT_VBAK BY VBELN. LOOP AT INT_VBAK. WRITE: / INT_VBAK-VBELN, INT_VBAK-KUNNR. ENDLOOP.
* Opening the File OPEN DATASET FILENAME FOR OUTPUT IN TEXT MODE MESSAGE D_MSG_TEXT. IF SY-SUBRC NE 0. WRITE: 'File cannot be opened. Reason:', D_MSG_TEXT. EXIT. ENDIF. * Transferring Data LOOP AT INT_VBAK. TRANSFER INT_VBAK-VBELN TO FILENAME. ENDLOOP. * Closing the File CLOSE DATASET FILENAME.
Always use Pretty Printer and Extended Program Check before releasing the code.
Do not leave unused code in the program. Comment the code thoroughly. Align the comments and the Code. Follow the SAP Standards and SAP Best Practices guidelines. It’s a good practice to take a dump of the code on your local drive.