Search this site

Saturday, April 2, 2011

Read and write file in SAP ABAP

1)OPEN DATASET
Opens a file for reading, writing or for appending
• Syntax:
OPEN DATASET [FOR INPUT|OUTPUT|APPENDING]
[IN BINARY|TEXT MODE]
[AT POSITION ]
[MESSAGE ]
[FILTER ].
can be a logical or physical file name

2)– READ DATASET
• Reads a file
• Syntax:
READ DATASET INTO [LENGTH ].
can be a logical or physical file names
• Logical file names can be created in customizing

3)TRANSFER DATASET
• Syntax:
- Writing to a dataset
TRANSFER TO [LENGTH ].
4) Closing a dataset
CLOSE DATASET

Function module

1)Step 1 T-code Se37
2) Step2
3)Step 3 Create interface parameters
4)Step 4
5)Step 5 Create table parameter
6)Step 6 Create exceptions
7)step 7 Source code in function module
Function module - Interface parameters
Import
Contains a list of the formal parameters that are used to
pass data to a function module.
Export
Contains a list of the formal parameters that are used to
receive data from a function module.
Changing
Contains a list of the formal parameters that are used
both to pass data to and receive data from a function
module.
Tables
Specifies the tables that are to be passed to a function
module. Table parameters are always passed by
reference.
Exceptions
Shows how the function module reacts to exceptions.
The function definition is written in the editor (Source code)


Friday, April 1, 2011

Subroutines

1)Subroutines
• Program module that can be called by any program.
• You use subroutines to avoid having to write frequently
used program components more than once. Data can be
passed explicitly from and to subroutines.
• Types of subroutine
– internal subroutines
– external subroutines

2)Defining and calling subroutine

Definition - Syntax
FORM .
.
ENDFORM.

Calling - syntax
a)Internal Subroutines
PERFORM .
b)External Subroutines
PERFORM () [IF FOUND].
- Program name
IF FOUND - If this is specified and there is no
subroutine in the program ,
the system ignores the PERFORM statement.

Include Programs

Include Programs
• If we want to use the same sequence of statements in
several programs, we can code them once in an include
program and call it in the other programs wherever it is
required
• Include programs cannot contain PROGRAM or
REPORT Statements
• Include programs cannot call themselves
• Include programs must contain complete statements
----------------------------------------------------------------
How to create include programs -->Click for details

Using Include Programs

***INCLUDE ZTEST_INC.

WRITE: / 'Program started date:', SY-DATUM.

We can then include this program in any other ABAP program to display a standard list header.

PROGRAM ZTEST.
INCLUDE ZTEST_INC.

-------------------------------

The result:

Program started

date: 04/01/2011

How to create include programs


1)Step 1

2)Step 2: Name for include



3)Step 3: Save




Include Programs

Include Programs
• If we want to use the same sequence of statements in
several programs, we can code them once in an include
program and call it in the other programs wherever it is
required
• Include programs cannot contain PROGRAM or
REPORT Statements
• Include programs cannot call themselves
• Include programs must contain complete statements
----------------------------------------------------------------

Using Include Programs

***INCLUDE ZTEST_INC.

WRITE: / 'Program started date:', SY-DATUM.

We can then include this program in any other ABAP program to display a standard list header.

PROGRAM ZTEST.
INCLUDE ZTEST_INC.

-------------------------------

The result:

Program started

date: 04/01/2011

Macros

Macros
• Callable modules of program code
Defining Macros - Syntax
DEFINE .
ENDDEFINE.
Calling Macros - Syntax
[ ….].
--------------------------------------------------------------------------------------------
Example:

REPORT demo_mod_tech_macros.

DATA: result TYPE i,
n1 TYPE i VALUE 5,
n2 TYPE i VALUE 6.

DEFINE operation.
result = &1 &2 &3.
output &1 &2 &3 result.

END-OF-DEFINITION.

DEFINE output.
write: / 'The result of &1 &2 &3 is', &4.
END-OF-DEFINITION.

operation 4 + 3.
operation 2 ** 7.
operation n2 - n1.

This produces the following output:

The result of 4 + 3 is 7

The result of 2 ** 7 is 128

The result of N2 - N1 is 1

In this example, the two macros operation and output are defined. output is nested in operation. operation is called three times with different parameters. Note how the placeholders &1, &2,... are replaced in the macros.

Abap Tips

  1. Check a string is number or not

Check a string is number or not

Example for code as below is to check whether a string is number with decimal ','
or not :

REPORT Z_TEST.
DATA
: count type i,
LW_CREDIT_TEMP TYPE BSEG-DMBTR.
PARAMETER: p_input TYPE char14.
FIND ALL OCCURRENCES OF ',' IN p_input MATCH COUNT count.
Write: ' occurance of the , = ', count.
IF p_input CO '1234567890, ' and count <= 1.
WRITE: /'Yes, valid', p_input.
write: lw_credit_temp.
Else.
WRITE: /'invalid', p_input.
write: lw_credit_temp.
ENDIF.

-------------------------------------------

1)Input : 4,5
Output: 4,5 is valid
2)Input: e
output: e is invalid