Вы находитесь на странице: 1из 24

0

Files in the R/3 System

Overview

Sequential Files
Local Sequential Files

SAP AG

(C) SAP AG BC420 3-1


0.2

Objectives

At the conclusion of this unit, you will be able to:


Explain where and how the files are stored in the R/3
System.
Outline how to process sequential files correctly.

Define two function modules that you can use to


write local files to the presentation server and read
from them.

SAP AG

(C) SAP AG BC420 3-2


0.3

Files in the R/3 System

Overview

Sequential Files

Local Sequential Files

SAP AG

(C) SAP AG BC420 3-3


0.4

R/3 Client / Server Architecture

Presentation

Application

Database

SAP AG

If you distribute the R/3 System according to the client / server method, the components of the R/3
System extend over a three-level computer hierarchy.
Presentation server:
The R/3 programs offered at this level implement the platform-specific input and output functions of
the R/3 System. The SAP GUI (Graphical User Interface).
Application server:
The R/3 programs offered at this level implement the whole business logic of the R/3 application.
You can select or combine several application servers according to your performance profile for
particular tasks. Several frontends (workstation, PC) can also be connected to each application
server.
Database server:
The database server is installed on a central host. The R/3 update service also runs on this host.
Several application servers can be connected to the database server.

(C) SAP AG BC420 3-4


0.5

Transferring Files
Local seq.
files
Presentation

Application ABAP TRANSFER Seq.


files
READ
Data transfer:
BI, CT, DI DATASET

Database

SAP AG

The runtime environment is implemented on the application server, which executes the ABAP
programs. ABAP supports the file transfer (data transfer) technique to the application server and the
frontend hosts.
The interface to the file system on the application server is implemented in the form of ABAP
language elements.
You can process sequential files using the statements READ DATASET (read) and TRANSFER
(write).

You implement the file interface on the application server using function modules (function modules
are a special type of subroutine that are stored in a library).
The data is transferred from the application server to the database server using batch input (BI), call
transaction (CT) or direct input (DI). You use a sequential file as the data source. This method
preserves the consistency of the data in the R/3 System.

(C) SAP AG BC420 3-5


0.6

Files in the R/3 System

Overview

Sequential Files

Local Sequential Files

SAP AG

(C) SAP AG BC420 3-6


0.7

Processing Files

Open Process Close


file file file

SAP AG

You open a sequential file before you write to or read from it.
The file is closed once it has been processed.
The program calls the file by its absolute file name. The absolute file name consists of the exact path
and the actual (relative) file name, such as '/tmp/file01' (note that file names are case-sensitive) or
'\\hstrnn0\sapmnt\trans' for Windows NT.
The format of file names depends largely on the operating system. You can access portable programs
by using the function module FILE_GET_NAME. This function module returns the physical name
for a given logical file name. You can find the function module description in the Function Builder
(the function module library).

(C) SAP AG BC420 3-7


0.8

Opening Files
Open Process Close
file file file

OPEN DATASET <filename>


[FOR {INPUT | OUTPUT | APPENDING}].

REPORT xxx.
:
OPEN DATASET newfile FOR APPENDING.
IF sy-subrc NE 0. EXIT. ENDIF.
:

Default: FOR INPUT R

SAP AG

To open a file, use the statement OPEN DATASET <file name>.


If this is successful, SY-SUBRC is set to 0. If not, it is set to 8. Any errors that occur are ignored.
To avoid confusion, SAP recommends that you open a file explicitly. OPEN DATASET also has the
advantage that it avoids an immediate termination if an error occurs as the file is opened (SY-
SUBRC = 8).
Possible options:
FOR INPUT
Opens an existing file for reading. If the file is already open, the cursor is positioned at the beginning
of the file. You do not have to specify FOR INPUT explicitly.
FOR OUTPUT
Opens the file for writing. If the file already exists, its contents are deleted. If the file does not exist,
it is created.
FOR APPENDING
Opens the file for writing at the end of the file. If the file does not exist, it is created. If the file is
already open, the cursor is positioned at the end.

(C) SAP AG BC420 3-8


0.9

Opening Files - Options


Open Process Close
file file file

OPEN DATASET <filename>


[FOR {INPUT | OUTPUT | APPENDING}]
[IN {BINARY MODE | TEXT MODE}]
[MESSAGE <field>].

REPORT xxx.
:
OPEN DATASET newfile FOR APPENDING MESSAGE text.
IF sy-subrc NE 0. EXIT. ENDIF.
:

Default: BINARY MODE R

SAP AG

You can choose to open a file in binary or text mode.


IN BINARY MODE
The data is not interpreted by the read and write operations READ DATASET and TRANSFER. The
data areas specified for these key words are input or output directly. You do not have to specify IN
BINARY MODE explicitly.
IN TEXT MODE
If you open a file with this option, the system assumes that the file has a line structure. Each time
READ DATASET or TRANSFER is executed, one line is always input or output and the data is
always processed up to the end-of-line selection. If the data area is too large for the line that is read,
the remaining area is padded with blanks. If it is too small, the end of the line is lost.
The following additional options also exist for the OPEN statement:
MESSAGE <field>
If an error occurs when opening the file, the relevant operating system message is stored in the field
<field>.
Opening and closing files explicitly aids readability. Also, the advantage of using OPEN DATASET
is that it avoids a program termination if an error occurs when opening the file (SY - SUBRC = 8).
See the online documentation for additional options.

(C) SAP AG BC420 3-9


0.10

Binary Mode and Text Mode


Binary mode Text mode
ABAP ABC ABAP ABC
program program
AB AB
ABCD ABCD

TRANSFER TRANSFER

ABCABABCD A BC
AB
ABCD
READ DATASET READ DATASET
ABAP ABC ABAP ABC
program program AB
ABA
BCD ABC R

SAP AG

The example above illustrates the difference between binary mode and text mode:
Three fields of different lengths are transferred at any one time. The data is then read into three
fields of the same length.
In text mode, the operating system specific at end of line character is set after every data record.
Blanks at the end of a data record are not suppressed in text mode.

(C) SAP AG BC420 3-10


0.11

Transferring Data Records


Open Process Close
file file file

TRANSFER <field> to <file name> [LENGTH <len>].

REPORT xxx.
DATA rec LIKE BBKPF.
:
SELECT ...
TRANSFER rec TO demo_file.
ENDSELECT.

One record at a time


R

SAP AG

Each TRANSFER statement transfers one data record to a sequential file.


Before TRANSFER, you place the data record into a field or a structure.
Possible structures are field strings or table work areas.
The execution of the TRANSFER statement depends on the mode:
Binary mode: Writes the length of the field or structure.
Text mode: Writes a line.
If the specified file is not open, the TRANSFER statement tries to open the file <file name> FOR
OUTPUT (IN BINARY MODE) or using the additions of the last OPEN DATASET statement for
this file. If this is not successful, a runtime error occurs.
Errors which occur during the TRANSFER statement result in program termination.
The additional parameter LENGTH <length> of the TRANSFER statement allows you to specify a
length in bytes (in the format TRANSFER <structure> TO <file name> LENGTH <length>). In this
case, the exact number of characters specified in <length> is transferred. If the structure is shorter,
the record is padded (with blanks in text mode and hexadecimal zeros in binary mode). If the
structure is longer, the record is truncated.

(C) SAP AG BC420 3-11


0.12

Reading Data Records


Open Process Close
file file file

READ DATASET <file name> INTO <field> [LENGTH <len>].

REPORT xxx.
DATA rec LIKE BBKPF.
:
DO.
READ DATASET demo_file into rec.
IF sy-subrc NE 0. EXIT. ENDIF.
ENDDO.

One data record at a time


R

SAP AG

Each READ DATASET statement reads one record from a sequential file into a field or structure.
Possible structures are field strings or table work areas.
The execution of the READ DATASET statement depends on the mode:

- Binary mode: Reads the length of the structure.


- Text mode: Reads a line.
If the specified file is not open, READ DATASET tries to open the file (IN BINARY MODE FOR
INPUT or with the options of the last OPEN DATASET statement for this file).
If the end of the file is reached, SY-SUBRC is set to 4. Otherwise it is set to 0. If the file cannot be
opened, SY-SUBRC contains the value 8. Errors result in program termination.
READ DATASET, like TRANSFER, does not perform conversions implicitly. The data is read in as it
was written.
The READ DATASET statement together with the additional parameter LENGTH <length> allow you
to specify the length of the imported file record.
In binary mode you see the length of the read structure.
In text mode you see the length of the record.

(C) SAP AG BC420 3-12


0.13

Closing / Deleting a Sequential File


Open Process Close
file file file

CLOSE DATASET <file name>.

DELETE DATASET <file name>.

SAP AG

You use the CLOSE DATASET <file name> statement to close a sequential file explicitly As with
OPEN DATASET, errors are ignored.
In a program, all opened files are implicitly closed each time the screen changes, and then reopened
exactly as they were before when processing resumes.
At the end of a program, all files are closed.
Opening and closing files explicitly aids readability. Also, the advantage of using OPEN DATASET
is that it avoids a program termination if an error occurs when opening the file (SY-SUBRC = 8).
A file is not closed implicitly when READ DATASET reaches the end of a file
You can use the DELETE DATASET <file name> to delete a sequential file explicitly. If this is
successful, SY-SUBRC is set to 0.
You can display the current state of the file in debugging.

(C) SAP AG BC420 3-13


0.14

Filling Record Layouts for Transaction FB01 (1)


REPORT sapbc420x_f_fb01_sapstruct.

TABLES: bgr00, bbkpf, bbseg.


:

PARAMETERS:
oldfile(50) DEFAULT '/tmp/demo_old.txt' LOWER CASE,
sapfile(50) DEFAULT '/tmp/demo_fb01.sap' LOWER CASE,
errfile(50) DEFAULT '/tmp/demo_fb01.err' LOWER CASE,
session(20) DEFAULT 'B420SES01' LOWER CASE,
nodata DEFAULT '/' LOWER CASE.

START-OF-SELECTION.
OPEN DATASET: oldfile FOR INPUT IN TEXT MODE MESSAGE text,
sapfile FOR OUTPUT IN TEXT MODE,
errfile FOR OUTPUT IN TEXT MODE.

PERFORM fill_nodata_bbkpf.
PERFORM fill_nodata_bbseg.
R

SAP AG

The above program reads a sequential file (demo_old.txt) that contains legacy data and stores the
data formatted in the SAP record layout in a new sequential file (demo_fb01.sap).
The program then checks the data, converts it and formats it so that it can be processed by the Data
Transfer Workbench using the SAP standard data transfer method.
If the check determines that a data record is incomplete or incorrect, this record is not processed and
is written to a separate error file (demo_fb01.err).
The SAP record layout for this example is based on the structures:
Record 0 BGR00
Record 1 BBKPF
Record 2 BBSEG

(C) SAP AG BC420 3-14


0.15

Filling Record Layouts for Transaction FB01 (2)


PERFORM fill_bgr00.
TRANSFER bgr00 TO sapfile.
DO.
READ DATASET oldfile INTO oldrec.
IF sy-subrc NE 0. EXIT. ENDIF.
IF oldrec-beldat = space.
TRANSFER oldrec TO errfile.
ELSE.
MOVE auxrec_1 TO bbkpf.
PERFORM fill_bbkpf.
MOVE auxrec_2 TO bbseg_a.
PERFORM fill_bbseg_a.
MOVE auxrec_2 TO bbseg_b.
PERFORM fill_bbseg_b.

TRANSFER: bbkpf TO sapfile,


bbseg_a TO sapfile,
bbseg_b TO sapfile.
ENDIF.
ENDDO.
R

CLOSE DATASET: oldfile, sapfile, errfile.


:
SAP AG

The record layout structure must be initialized before it can be filled. Once the structures have been
filled, they are written to the sequential file.

(C) SAP AG BC420 3-15


0.16

Sequential Files - Summary


Define structure

Write Read
Open file

Fill structure Read data record

Transfer
Process data
structure
record?

No
More data End of
Yes records? file?

No Yes
Close file
SAP AG

In a file processing program, you first define the structures required for the data records with the
TABLES or DATA statement. The program uses these structures as internal buffers for the data
records.
You then open the sequential file for reading or writing with the OPEN statement.
When writing to the file, you transfer the filled structures to the file with TRANSFER statements.
When reading the file, you fill the structures set up for the data records with READ DATASET for
further processing in the program.
To close the sequential file, you exit file processing with the CLOSE DATASET statement.

(C) SAP AG BC420 3-16


0.17

Files in the R/3 System

Overview

Sequential Files

Local Sequential Files

SAP AG

(C) SAP AG BC420 3-17


0.18

DOWNLOAD and UPLOAD

CALL FUNCTION CALL FUNCTION


DOWNLOAD UPLOAD

File name:
File type:

Convert Convert
according to according to
file type file type

Frontend
SAP AG

The function module DOWNLOAD downloads the contents of an internal table to a local sequential
file.
The function module UPLOAD uploads the contents of a local sequential file to an internal table.
For the local file, specify the file name (the complete path, e.g. '/tmp/myfile' for a UNIX file and or
'C:\ MYFILE.TXT' for a PC file).
The directory must be known to the presentation server.
The choice of suitable file names is the responsibility of the customer.
The system requests the file name and the file type interactively.
The data is converted according to the file type.

(C) SAP AG BC420 3-18


0.19

Function Module DOWNLOAD


CALL FUNCTION DOWNLOAD
EXPORTING
BIN_FILESIZE = <File length for binary files>
CODEPAGE = <Do not use>
FILENAME = <File name - default value>
FILETYPE = <File type - default value>
ITEM = <Header for file dialog>
MODE = <Write mode>
WK1_N_FORMAT = <Value column format for WK1 file type>
WK1_N_SIZE = <Value column width for WK1 file type>
WK1_T_FORMAT = <Text column format for WK1 file type>
WK1_T_SIZE = <Text column width for WK1 file type>

IMPORTING
ACT_FILENAME = <File name - entered value>
ACT_FILETYPE = <File type - entered value>
FILESIZE = <Number of bytes downloaded>

TABLES
DATA_TAB = <Transfer file>
R

SAP AG

For the function module DOWNLOAD, you need an internal table for the data transfer. You define
this table according to your data structure at the beginning of the program and then fill it with data.
You use the MODE parameter to define the write mode ('A' to extend a file, ' ' to create a new file).
The specification of default values for the file name, file type and a header for the file dialog is
optional.
The IMPORT parameters specify the values actually entered by the user for the file name, file type
and the number of download bytes.
You only specify a value for the export parameter CODEPAGE when performing a DOWNLOAD to
DOS (the only possible value is 'IBM'). Otherwise, this parameter is not used.
All EXPORTING parameters are optional.
If you want to generate a binary file, you must specify the file length. In this case, the transfer table
must consist of a column of type X.

(C) SAP AG BC420 3-19


0.20

File Formats

Internal
Internal table
table (ABAP)
(ABAP) Internal
Internal table
table (ABAP)
(ABAP)

F1 F2 F3 F1 F2 F3 ...
A B C A B C ...
D E F D E F ...
. . . G H I ...
. . . . . .

Format
Format DAT
DAT Format
Format WK1
WK1

Spreadsheet
Spreadsheet Spreadsheet
Spreadsheet

A<tab> B<tab> C<CRLF>


D<tab> E<tab> F<CRLF>
. . . . . .
. . . . . .

SAP AG

With UPLOAD and DOWNLOAD, another format available for conversions apart from ASC
(ASCII) and BIN (binary) is DAT for Excel. With DOWNLOAD, WK1 is also available for Excel
and Lotus.
For the format DAT, table columns are divided by a tabulator character and the table lines by CF
(carriage return) and LF (line feed).
For the format WK1, the data of a table is entered in a WK1-specific spreadsheet.

(C) SAP AG BC420 3-20


0.21

Example Program: DOWNLOAD

REPORT sapbc420seqd_download.

DATA: BEGIN OF itab OCCURS 0,


kunnr LIKE kna1-kunnr,
land1 LIKE kna1-land1,
name1 LIKE kna1-name1,
stras LIKE kna1-stras,
ort01 LIKE kna1-ort01,
pstlz LIKE kna1-pstlz,
END OF itab.

SELECT kunnr land1 name1 stras ort01 pstlz stras


FROM kna1 INTO CORRESPONDING FIELDS OF TABLE itab.

CALL FUNCTION 'DOWNLOAD'


TABLES
data_tab = itab.
:
:

SAP AG

The above example uses a SELECT statement to read customer data from table KNA1 and enters
this data into a previously defined internal table.
The function module DOWNLOAD stores the internal table on the presentation server.

(C) SAP AG BC420 3-21


0.22

Function Module UPLOAD

CALL FUNCTION UPLOAD

EXPORTING
CODEPAGE = <Do not use>
FILENAME = <File name - default value>
FILETYP = <File type - default value>
ITEM = <Header for file dialog>

IMPORTING
FILESIZE = = <File length>

TABLES
DATA_TAB = <Transfer table for file contents>

SAP AG

For the function module UPLOAD, you need an internal table for the data transfer. You define this
table according to your data structure at the beginning of the program.
The specification of default values for the file name, file type and a header for the file dialog is
optional.
You only specify a value for the export parameter CODEPAGE when performing an UPLOAD to
DOS (the only possible value is 'IBM'). Otherwise, this parameter is not used.

(C) SAP AG BC420 3-22


0.23

Example Program: UPLOAD


REPORT sapbc420seqd_upload.

DATA: BEGIN OF itab OCCURS 0,


kunnr LIKE kna1-kunnr,
land1 LIKE kna1-land1,
name1 LIKE kna1-name1,
stras LIKE kna1-stras,
ort01 LIKE kna1-ort01,
pstlz LIKE kna1-pstlz,
END OF itab.

CALL FUNCTION 'UPLOAD'


TABLES
data_tab = itab.

LOOP AT itab.

WRITE: / itab-kunnr, itab-land1, itab-name1(30),


itab-stras(30), itab-pstlz, itab-ort01(30).
ENDLOOP.
R

SAP AG

The above example program uses the function module UPLOAD to read a local sequential file from the
presentation server and enters this data in a previously defined internal table.
The internal table is then output with a loop.

(C) SAP AG BC420 3-23


0.24

Summary

You can use the READ / TRANSFER statements to


read and write sequential files to the application server.
It is useful to open and close sequential files explicitly.

You can use the function modules DOWNLOAD and


UPLOAD to read and write internal tables to the
presentation server as local sequential files.

SAP AG

(C) SAP AG BC420 3-24

Вам также может понравиться