12-BDC & File Handling
12-BDC & File Handling
Objective:
presentation
server.
Batch Data Communication
Definition :
Batch Data Communication (BDC) is the process of transferring data
from one SAP System to another SAP system or from a non-SAP
system to SAP System.
Features :
BDC is an automatic procedure.
This method is used to transfer large amount of data that is available
in electronic medium.
BDC can be used primarily when installing the SAP system and when
transferring data from a legacy system (external system).
BDC uses normal transaction codes to transfer data.
Batch Data Communication
Types of BDC :
CALL TRANSACTION
Batch Data Communication
Features:
Asynchronous processing.
Synchronous Processing in database update
Transfer data for more than one transaction.
Batch input processing log will be generated.
During processing, no transaction is started until the previous
transaction has been written to the database.
Batch Data Communication
Sequential
file
Batch
Batch input
input program
program
Queue file
Batch
Batch input
input function
function
SAP
SAP R/3
R/3
Flight
Customer connections Reservation
data data
Batch Data Communication
Steps involved in Classical Batch Input :
BDC_OPEN_GROUP
BDC_OPEN_GROUP
Client
Client CLIENT
CLIENT
Session
Sessionname
name GROUP
GROUP
Lock date
Lock date HOLD
HOLD
Delete
Deletesession
sessionafter
after KEEP
KEEP
processing
processing
BDC
BDCuser
user name
name USER
USER
BDC_INSERT
BDC_INSERT
Transaction
Transactioncode
code TCODE
TCODE
BDC
BDCtable
table DYNPROTAB
DYNPROTAB
BDC_CLOSE_GROUP
BDC_CLOSE_GROUP
Batch Data Communication
BDC Table
Program
Program Screen
Screen Start
Start Field
Field name
name Field
Field contents
contents
<program
<program name>
name> <number
<number1>
1> xx
<field
<field 11>
11> <value
<value 11>
11>
<field
<field 12>
12> <value
<value 12>
12>
...
... ...
...
<program
<program name>
name> <number
<number2>
2> xx
<field
<field 21>
21> <value
<value 21>
21>
<field
<field 22>
22> <value
<value 22>
22>
...
... ...
...
...
... ...
...
Batch Data Communication
Recording Step 1:
Starting screen of the recording (Transaction code SHDB)
Click “New Recording” button to new recording.
Batch Data Communication
Recording Step 2: Enter the recording name and the transaction for
which you want the recording and click “Start Recording” Button.
Batch Data Communication
Recording Step 4: After entering all data for the transaction (here MK01),
the recording overview screen will be displayed. Then save the
recording. And click the “Program” button.
Batch Data Communication
ENDLOOP.
PERFORM CLOSE_GROUP. “ Closing the BDC session
Batch Data Communication
Sample BDC Program (Continues):
FORM OPEN_GROUP.
CALL FUNCTION 'BDC_OPEN_GROUP'
EXPORTING
CLIENT = SY-MANDT
GROUP = 'SESSION1'
USER = SY-UNAME
KEEP = 'X'.
IF SY-SUBRC <> 0.
WRITE 'ERROR IN OPEN_GROUP'.
ENDIF.
ENDFORM.
FORM CLOSE_GROUP.
CALL FUNCTION 'BDC_CLOSE_GROUP'.
ENDFORM. " CLOSE_GROUP
Batch Data Communication
Foreground Processing:
First screen of the transaction MK01(Vendor Creation)
Batch Data Communication
Features:
Synchronous processing. The system performs a database commit
immediately before and after the CALL TRANSACTION USING
statement.
Updating the database can be either synchronous or asynchronous.
The program specifies the update type.
Transfer data for a single transaction.
Transfers data for a sequence of dialog screens.
No batch input processing log is generated.
Batch Data Communication
CALL TRANSACTION
CALL TRANSACTION <transaction code>
<transaction code>
USING
USING <BDC
<BDC table>
table>
MODE <display
MODE <display
mode>mode>
UPDATE
UPDATE <update
<update mode>mode>
MESSAGES INTO
MESSAGES INTO <messtab>
<messtab>
<display
<display mode>:
<display mode>:
mode>:
AA Display
Display allall
EE Display only
Display only if there
if there are errors
are errors
NN Display nothing
Display nothing
<update
<update mode>:
<update mode>:
mode>:
SS Do notcontinue
Do not continue processing
processing until until
update has
update has finished
finished (synchronous)
(synchronous)
AA Continue processing
Continue processing immediately
immediately
Batch Data Communication
Session
Batch input CALL
CALL TRANSACTION
TRANSACTION
Return
Return code
code No
No Yes
Yes
Database Update
Error logging Synchronous
Yes Asynchronous/Synchronous
No
Processing
Processing Time-delayed
Time-delayed Immediately
Immediately
Will not be
Error Log Will be created created
Batch Data Communication
Exercise:
M BOH X X Material1 KG
Batch Data Communication
Solution:
REPORT ZREPORT1.
DATA: I_BDCDATA LIKE BDCDATA OCCURS 0 WITH HEADER LINE.
DATA: BEGIN OF RECORD OCCURS 0,
MBRSH_001(001),
MTART_002(004),
KZSEL_01_003(001),
KZSEL_02_004(001),
MAKTX_005(040),
MEINS_006(003),
MAKTX_007(040),
END OF RECORD.
START-OF-SELECTION.
*Uploading data from the local file C:\Vendor1.txt
CALL FUNCTION 'WS_UPLOAD'
EXPORTING
FILENAME = 'C:\MARA.TXT '
FILETYPE = 'DAT'
TABLES
DATA_TAB = RECORD.
Batch Data Communication
IF SY-SUBRC <> 0.
WRITE 'ERROR IN UPLOAD'.
ENDIF.
PERFORM OPEN_GROUP.
LOOP AT RECORD.
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '0060'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'RMMG1-MTART'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'/00'.
PERFORM BDC_FIELD USING 'RMMG1-MBRSH'
RECORD-MBRSH_001.
PERFORM BDC_FIELD USING 'RMMG1-MTART'
RECORD-MTART_002.
PERFORM BDC_DYNPRO USING 'SAPLMGMM' '0070'.
PERFORM BDC_FIELD USING 'BDC_CURSOR'
'MSICHTAUSW-DYTXT(02)'.
PERFORM BDC_FIELD USING 'BDC_OKCODE'
'=ENTR'.
Batch Data Communication
OPEN DATASET
CLOSE DATASET
DELETE DATASET
READ DATASET
TRANSFER
File Handling in SAP
OPEN DATASET
Opens the specified file. If you do not use any additions, the file is
opened for reading in binary mode. It returns SY-SUBRC = 0 if the file
is opened successfully. Otherwise SY-SUBRC = 8.
Syntax
OPEN DATASET <dsn> [Additions].
Additions:
1. FOR INPUT ( Default )
2. FOR OUTPUT
3. FOR APPENDING
4. IN BINARY MODE
5. IN TEXT MODE
6. AT POSITION p
7. TYPE ctrl
8. MESSAGE mess
9. FILTER f
File Handling in SAP
This statement tries to open the file in 'append' mode. If the file is
already open, the system moves to the end of the file. When you open
a file using FOR APPENDING, attempting to read the file sets SY-
SUBRC to 4. The system display the end of the file.
Note :
The contents of the file are not structured in lines in the READ
DATASET or TRANSFER operations. Instead, they are input or output
as a stream. You do not have to specify the IN BINARY MODE
addition explicitly.
Use this addition to specify the explicit starting position p in the file
(calculated in bytes from the start of the file). The next read or write
operation will start at this position. You cannot position before the
beginning of the file. Do not use this addition with the IN TEXT MODE
addition, since the physical representation of a text file depends
heavily on the underlying operating system.
If you use OPEN ... FOR OUTPUT AT POSITION ..., the contents of the
file are destroyed if the file already existed. To avoid this, use OPEN
... FOR INPUT AT POSITION ... instead.
Note
OPEN ... AT POSITION p does not work for file positions where p >= 2
giga bytes.
File Handling in SAP
CLOSE DATASET
Syntax
DELETE DATASET
READ DATASET
Used to read a record from a file.
Syntax
READ DATASET dsn INTO f.
Addition : LENGTH len.
The actual length of the data objet read is placed in the field len after
the read access. len must be defined as a variable. A syntax error will
occur if you define it as a constant. The following example displays 9.
Example
DATA: len TYPE i,
text(30) TYPE c VALUE 'Beethoven',
dir(30) TYPE c VALUE '/usr/test.dat'.
OPEN DATASET dir IN TEXT MODE.
TRANSFER text TO dir. CLOSE DATASET dir.
OPEN DATASET dir IN TEXT MODE.
READ DATASET dir INTO text LENGTH len.
CLOSE DATASET dir. WRITE / len.
File Handling in SAP
TRANSFER statement
Used to write a record into a file.
Syntax
TRANSFER f TO dsn.
Transfers the data object f to a sequential file whose name is
specified in dsn. dsn can be a field or a literal. You must already have
opened the file. . If the specified file is not already open, TRANSFER
attempts to open the file FOR OUTPUT IN BINARY MODE. If this is not
possible, a runtime error occurs.f can be a field, a string, or a
structure.
BIN_FILESIZE
File Length for binary files. A length of zero or the length which is
larger than the number of bytes in the internal table (width * number
of lines) causes an exception.
CODEPAGE
Only for download in DOS
FILENAME
The name of the file that is to be generated on the presentation
server( if necessary with predefined path name). If the path doesn’t
exist or the file cannot be opened, an exception will be raised.
File Handling in SAP
FILETYPE
The target format of the file. Valid values are:
‘ASC’ : ASCII format, the table is stored with rows.
‘DAT’: ASCII format as in 'ASC', additional column separation
with TABs.
‘BIN’ : Binary format (specification of BIN_FILESIZE required)
‘DBF’ : Stored as Dbase file (always with DOS code page).
‘IBM’ : ASCII format as in 'ASC' with IBM code page conversion
(DOS)
MODE
Writing mode ( ‘A’ = Append , empty = Overwrite)
FILELENGTH
The length of the generated file is returned.
File Handling in SAP
TABLE PARAMETER
DATA_TAB
The source internal table whose contents are downloaded into a
file.
EXCEPTIONS
The exceptions for the function module WS_DOWNLOAD are
CODEPAGE
Only for download in DOS.
FILENAME
Name of the file
FILETYPE
The source file type. Valid values are:
‘BIN’ : Binary files.
‘ASC’: ASCII files, text files with end-of-line markers.
‘DAT’: The file is loaded line by line into the transferred table.
Tabs in the file mean a change of field.
File Handling in SAP
Summary