You are on page 1of 40

REPORT ZGDEMO_ITAB_OPERATION

*DECLARE the required Data type

TYPES : BEGIN OF TY_T001,

BUKRS TYPE BUKRS, "Company Code

BUTXT TYPE BUTXT, "Company Name

ORT01 TYPE ORT01, "City

LAND1 TYPE LAND1, "Country Key

END OF TY_T001.

*DECLARE Internal Table From the above structure

DATA IT_T001 TYPE TABLE OF TY_T001.

DATA WA_T001 TYPE TY_T001.

*Fetch Data from T001 into IT_T011

SELECT BUKRS,

BUTXT,

ORT01,

LAND1

INTO TABLE IT_T001

FROM T001

WHERE LAND1 = 'DE'.

*SORT

SORT IT_T001 DESCENDING BY BURKS

WRITE : / 'LIST OF GERMAN COMPANIES IN DESCENDING ORDER'.

*Display Data from IT_T001

LOOP AT IT_T001 INTO WA_T001.


WRITE : / SY-TABIX,

WA_T001-BUKRS,

WA_T001-BUTXT,

WA_T001-ORT01,

WA_T001-LAND1.

ENDLOOP.

*ADD SOME MORE RECORDS

CLEAR WA_T001. "Clear the contents

WA_T001-BURKS = 'F001'.

WA_T001-BUTXT = 'FREE SAP TUTORIALS'.

WA_T001-ORT01 = 'INDIA'

APPEND WA_T001 TO IT_T001.

*INSERT

CLEAR WA_T001. "Clear the Contents

WA_T001-BURKS = 'S001'.

WA_T001-BUTXT = 'SAP TUTORIALS'.

WA_T001-ORT01 = 'INDIA'

INSERT WA_T001 INTO IT_T001 INDEX 1.

SKIP 2.

WRITE : / 'DATA AFTER APPEND AND INSERT' COLOR 1.

ULINE.

*Display data from IT_T001

LOOP AT IT_T001 INTO WA_T001.

WRITE : / SY-TABIX,

WA_T001-BURKS,

WA_T001-BUTXT,
WA_T001-ORT01,

WA_T001-LAND1.

ENDLOOP.

*DELETE

DELETE IT_T001 FROM 4 TO 5.

SKIP 2.

WRITE : / 'DATA AFTER DELETE' COLOR 1.

ULINE.

*Display data from IT_T001

LOOP AT IT_T001 INTO WA_T001

WRITE : / SY-TABIX,

WA_T001-BURKS,

WA_T001-BUTXT,

WA_T001-ORT01,

WA_T001-LAND1.

ENDLOOP.

*MODIFY

*Set country key to 'IN' for both company codes 'F001' and 'S001'.

CLEAR WA_T001.

WA_T001-LAND1 = 'IN'.

MODIFY IT_T001 FROM WA_T001 TARNSPORTING LAND1

WHERE BURKS = 'F001' OR BURKS = 'S001'.

IF SY-SUBRC = 0.

SKIP 2.

WRITE : / 'DATA AFTER MODIFY' COLOR 1.


ULINE.

*Display data from IT_T001

LOOP AT IT_T001 INTO WA_T001.

WRITE : / SY-TABIX,

WA_T001-BURKS,

WA_T001-BUTXT,

WA_T001-ORT01,

WA_T001-LAND1.

ENDLOOP.

ENDIF.

*READ

SORT IT_T001 BY BURKS ASCENDING.

CLEAR WA_T001.

READ TABLE IT_T001 INTO WA_T001 WITH KEY BURKS = '0001'

BINARY SEARCH.

SKIP 2.

WRITE : / 'DATA AFTER READ' COLOR 1.

ULINE.

*Display data from IT_T001

WRITE : / SY-TABIX,

WA_T001-BURKS,

WA_T001-BUTXT,

WA_T001-ORT01,

WA_T001-LAND1.

*DELETE ADJANCENT DUPLICATES

*SINCE WE DON'T HAVE DUPLICATES ON BUKRS, WE PREPARE DUPLICATES


DO 3 TIMES.

APPEND LINES OF IT_T001 TO IT_T001.

ENDDO.

SORT IT_T001 BY BURKS ASCENDING.

SKIP 2.

WRITE : / 'DATA AFTER ADDING DUPLICATE RECORDS' COLOR 1.

ULINE.

LOOP AT IT_T001 INTO WA_T001.

*Display data from IT_T001

WRITE : / SY-TABIX,

WA_T001-BURKS,

WA_T001-BUTXT,

WA_T001-ORT01,

WA_T001-LAND1.

ENDLOOP.

*Delete adjacent duplicates

DELETE ADJACENT DUPLICATED FROM IT_T001 COMPARING BUKRS.

SKIP 2.

WRITE : / 'DATA AFTER ADJACENT DUPLICATES' COLOR 1.

ULINE.

LOOP AT IT_T001 INTO WA_T001.

*Display data from IT_T001

WRITE : / SY-TABIX,

WA_T001-BUKRS,

WA_T001-BUTXT,
WA_T001-ORT01,

WA_T001-LAND1.

ENDLOOP.

Steps for Declaring Internal table

Categories: SAP

1. Declare the structure of Internal Table i.e. A User Defined Data type with the required
fields, According tp the fileds required from the Corresponding database tables(s).

TYPES : BEGIN OF <TY>

F1 TYPE <Ref. Datatype> ,

F2 TYPE <Ref. Datatype> ,

F3 TYPE <Ref. Datatype> ,

END OF <TY>.

NOTE: TYPES is to define user defined Datatype.

1. Declare Internal Table i.e. Array of above structure.

DATA <ITAB> TYPE TABLE OF <TY>.

EXAMPLE: Declare an ITAB to maintain data from table T001 (Company Code Data)

NOTE: While Declaring <ITAB>, refer the corresponding database table i.e. T001 here.

*Declare the required datatypes

TYPES : BEGIN OF TY_T001,

BUKRS TYPE BUKRS, “Company Code

BUTXT TYPE BUTXT, “Comapny Name

ORT01 TYPE ORT01, “City

LAND1 TYPE LAND1, “Country Key


END OF TY_T001.

*Declare Internal Table from above structure

DATA IT_T001 TYPE TABLE OF TY_T001.

Syntax to select the Data from Database table into Internal table:

SELECT <F1>,

<F2>,

<F3>,

……..

INTO TABLE <ITAB>

FROM <DBT>

WHERE <Condition if Any>.

NOTE: Make sure that the structure (order) of the fields in the SELECT and Internal table should
be same, because the content if 1st filed in SELECT is transferred to the 1st filed of <ITAB> and
similarly 2nd , 3rd etc.

EXAMPLE: Display the List of German Companies’s Details

(Company Code, name, city, Country) from T001.

*Declare the Required Datatype

TYPES : BEGIN OF TY_T001,

BUKRS TYPE BUKRS, “Company Code

BUTXT TYPE BUTXT, “Company Name

ORT01 TYPE ORT01, City

LAND1 TYPE LAND1, “Country Key

END OF TY_T001.

*Declare Internal table from the above structure

DATA IT_T001 TYPE TABEL OF TY_T001.


*Fetch data from T001 into IT_T001

SELECT BUKRS,

BUTXT,

ORT01,

LAND1

INTO TABLE IT_T001

FROM T001

WHERE LAND1 = 'DE'. “Germany Code

NOTE: After the SELECT data from database table is transferred to Internal table.

*Display data in Internal Table

*Since data in Internal table is Sorted record by record, Accessing/Displaying Data from Internal
table is also record by recoord. So that there is a special loop which starts with the 1st record and
ends with the second record of <ITAB> by default..

*Keep the Internal table in a LOOP, LOOP points to the first record by default and collect it into
<WA> and process (Print) the data from <WA>.

Eaxmple using VBAK table.

REPORT Z9AMPROGRAM52.

tables vbak.
select-options : so_vbeln for vbak-vbeln.

types : begin of ty_vbak,


erdat type vbak-erdat,
erzet type vbak-erzet,
ernam type vbak-ernam,
end of ty_vbak.

data : lt_vbak type standard table of ty_vbak,


ls_vbak type ty_vbak.

select erdat erzet ernam


from vbak
into table lt_vbak
where vbeln in so_vbeln.
if sy-subrc eq 0.
write :/ 'No of records :',sy-dbcnt.
loop at lt_vbak into ls_vbak.
write :/ ls_vbak-erdat,
ls_vbak-erzet,
ls_vbak-ernam.
endloop.
else.
write :/ 'No data'.
endif.

ABAP Statement Key words and Data Types in SAP

Categories: SAP

ABAP/4 – Advanced Business Application Programming Language.

4 – 4th Generation Language.

It is Not Case Sensitive.

Is Event Driven Programming Language.

This overview describing applications programming in the R/3 System. All applications
programs, along with parts of the R/3 Basis system, are written in the ABAP Workbench
using ABAP, SAP’s programming Language. The individual components of application
programs are stored in a special section of the database called the R/3 Repository. The R/3
Repository serves as a central store for all of the development objects in the R/3 System. The
following sections of this documentation cover the basics and characteristics of application
programming.

A - Advanced

B - Business

A - Application(s)

P - Programming

Program :

A Program Is Group Of Meaningful Instructions.

An Instruction is Group of
Keywords + Variables + Operators + Data Types

Keywords :

Syntax : Each ABAP statement Should begin with a keyword and ends with a period.

Since Each Statement Should Start with a Keyword. It is Difficult to give the exact no. of
Keywords So that Keywords are Divided into Different Types Depends On the Functionality Of
the Keywords.

Types Of Keywords :

Declarative Key words : To Declare Variables.

TYPES, DATA, TABLES

Syntax for Variables : DATA < Var. Name> TYPE <Data Types>.

Database Keywords : To Work With Database Operations Such as

SELECT - To Select Data

INSERT - To Insert Data

UPDATE - To Change Data

DELETE - To Delete Data etc.

Control Keywords :

Statements are used to control the flow of an ABAP program within a processing block
according to certain conditions.

Ex :

IF, ELSEIF, ENDIF.

DO-ENDDO, WHILE – ENDWHILE.

Definition Keywords are used to define Re-usable Modules (Blocks)

Ex :
FORM - ENDFORM

FUINCTION - ENDFUNCTION

MODULE - ENDMODULE

Calling Keywords :

Are used to call Re-usable Modules (Blocks) that are already defined.

PERFORM to Call FORM

CALL FUNCTION to Call FUNCTION

MODULE to Call MODULE

Operational Key Words :

To Process the data that you have defined using declarative statements.

Ex : WRITE, MOVE, ADD

Event Keywords :

Statements containing these keywords are used to define event blocks.

Ex : TOP-OF-PAGE To Print the Same Heading On the TOP Of Every Page.

END-OF-PAGE To Print the Same FOOTER for every Page on the Output List.

Date Types and Objects (Variables)

The physical units with which ABAP statements work at routine are called internal program data
objects. The contents of a data occupy memory space in the program. ABAP statements access
these contents by addressing the name of the data object. Each ABAP data objects has a set of
technical attributes which are fully defined at all times when an ABAP program is running. The
technical attributes of a data object are : Data Type, Field Length and Number of Decimal
Places.

ABAP Contains the following Pre-defined Data Types :


Non-Numeric Data Types :

Character String (C)

Numeric Character String (N)

Date (D)

Time (T)

Numeric Types :

Integer (I)

Floating-point number (F)

Packed Number (P)

The Field Length for data types D, F, I and T is fixed. The field length determines the number of
bytes that the data object occupies in memory. In types C, N, X and P, the length is not part of
the type definition. Instead, you define it when you declare the data object in your
program.

Data Type P is particularly useful for exact calculations in a business context. When you define
an object with type P, you also specify a number of decimal places.

You can also define your own elementary data types in ABAP using the TYPES statements. You
base these on the predefined data types. This determines all of the technical attributes of the new
data type. For example, you could define a data type P_2 with two decimal places on the
predefined data type P. You could then use the new type in your data declarations.

Predefine Elementary ABAP Types : All field lengths are specified in bytes.

Data Types Initial Field Length Valid Field Length Initial Value Meaning
Numerica Types

I 4 4 0 Integer (Whole Number)


F 8 8 0 Floating Point Number
P 8 1 0 Packed Number

16
Character (Non-Numeric) Types
C 1 1 ‘………..’ Text Field

6553 (Alphanumeric Characters)

5
D 8 8 ’00000000′ Date Field

(Format : YYYYMMDD)
N 1 1 ’0 … 0′ Numeric Text Fields

6553 (Numeric Characters)

5
T 6 6 ’000000′ Time Field

(Format : HHMMSS)

Note : Data Type N is not a numeric type. Type N objects can only contain number characters
(0…….9) but are not represented internally as numbers. Typical type N fields are account
numbers and zip codes.

Integers – Type 1

The value range of type 1 numbers is -2**31 to 2**31-1 and includes only whole numbers. Non-
integer results of arithmetic operations (e.g. fractions) are rounded, not truncated.

You can use type 1 data for counters, number of items, indexes, time periods, and so on.

Packed Numbers – Type P

Type P data allows digits after the decimal point. The number of decimal places is generic,
and is determined in the program. The value range of type P data depends on its size and
number of digits after the decimal point. The valid size can by any value from 1 to 16 bytes.
Two decimal digits are packed into one byte, while the last byte contains one digit and the sign.
When working with type P data, it is a good idea to set the program attributes Fixed point
arithmetic. Otherwise, type P numbers are treated as integers.
Note : You can use type P data for such values as distances, weights, amounts of money, and so
on.

Floating Point Numbers – Type F

The value ranges of type F numbers is 1×10**-307 to 1×10**308 for positive and negative
numbers, including 0 (zero). The accuracy range is approximately 15 decimals, depending on the
floating point arithmetic of the hardware platform. Since type F data is internally converted to a
binary system, rounding errors can occur. Although the ABAP processor tries to minimize these
effects, you should not use type F data if high accuracy is required. Instead, use type P data.

You use type F fields when you need to cope with very large ranges and rounding errors are not
critical.

Using I and F fields for Calculations is quicker than using P fields.

Operations using I and F fields are very similar to the actual machine code operations, while P
fields requires more support from the software. Nevertheless, you have to use type P data to meet
accuracy or value range requirements.

Character (Non-Numeric) Types :

Of the five non-numeric types, the four types C,D, N and T are characters types. Fields with
these types are known as character field. Each position in one of these fields talks up enough
space for the code of the character. Currently, ABAP only works with single-byte codes such as
ASCII and EBCDI. However, an adaptation to UNICODE is in preparation. Under UNICODE,
each character occupies two or four bytes.

Type of ABAB Internal Tables : SAP ABAB Tables

Categories: SAP

Standard Table

 These are default table. The key of a standard table is always non-unique, so duplicates
are allowed.
 DATA <ITAB> TYPE STANDARD TABLE OF <TY>.
 Record can be accessed through both INDEX and Condition.
READ TABLE <ITAB> INTO <WA> INDEX <N>.

Or

READ TABLE <ITAB> INTO <WA> WITH KEY <Condition>.

 <ITAB> can be sorted


 Accessing/Searching time for the record depends on the No of Records because
Searching is either Liner or Binary.

Sorted Table

Note: Records are always in sorted order.

This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted
tables using INSERT statement. Entries are inserted according to the sort sequence defined
through the table key.

The response time for key access is logarithmically proportional to the number of table entries,
since the system always uses a binary search.

Note: records can be accessed through both INDEX and with KEY(Condition).

Note: Sorted Internal tables cannot be sorted again.

Sorted internal tables are always either UNIQUE/ NON UNIQUE i.e. sorted internal tables
cannot be declared without UNIQUE/NON-UNIQUE keyword.

DATA <ITAB> TYPE SORTED TABLE OF <TY> WITH UNIQUE/NON-UNIQUE KEY


<F1><F2>…

Hashed Table

This is the most appropriate type for any table where the main operation is key access.

Like database tables, hashed tables always have a unique key.

DATA <ITAB> TYPE HASHED TABLE OF <TY> WITH UNIQUE/NON-UNIQUE KEY


<F1><F2>…

You cannot access a hashed table using its INDEX.

The response (Search) time doesn’t depend on the number of records, instead it always remain
constant regardless the number of table entries.
Hashed tables are useful if you want to construct and use an internal table which resembles a
database table or for processing large amounts of data.

Special features of Standard table

Sorted tables, hashed tables are only introduced in Release 4.0 standard tables already existed
several release previously.

Defining a line type, table type and tables without a header line have only been possible since
Release 3.0. For this reason, there are certain features of standard tables that still exist for
compatibility reasons.

Standard table before Release 3.0

Before Release 3.0, internal tables all had header lines and a flat-structured line type. There were
no independent table types. You could only create a table object using the OCCURS addition in
the DATA statement, followed by a declaration of a flat structure:

DATA: BEGIN OF <ITAB> OCCURS <n>,

<F1>

<F2>

……..

<FN>

END OF <ITAB>.

This statement declared an internal table <ITAB> with the line type defined following the
OCCURS addition. Furthermore, all internal tables had header lines.

The number <N> in the OCCURS addition had the same meaning as in the INITIAL SIZE
addition from Release 4.0. Entering ‘0’ had the same effect as omitting the INTIAL SIZE
addition. In this case, the initial size of the table is determined by the system.

Standard tables from Release 3.0

Since Release 3.0, it has been possible to create table types using
TYPES <T> TYPE|LIKE <linetype> OCCURS <n>.

And table object using

DATA <ITAB> TYEP|LIKE <linetype> OCCURS <n> [WITH HEADER LINE].


The effect of the occurs addition is to construct a standard tables with the data type <linetype>.
The line type can be any data type.

The above statement is still possible in Release 4.0, and has the same function as the following
statement:

TYPE <ITAB> TYEP|LIKE [STANDARD] TABLE OF <linetype>.

NOTE: OCCURS <0> allocates the initial memory 8kb and the system keep on allocated by
8kb, whenever it is required.

OCCURS <N> allocates memory for <N> records initially and keep on allocates for <N>
records, whenever it requires.

ABAP Operation on Internal Tables

Categories: SAP

COPYING (Adding Multiple Records):

 Copy at the END of Internal Table

APPEND LINES OF <ITAB1> FROM <N1> TO <N2> TO <ITAB2>.

 Copy From the given Location

INSERT LINES OF <ITAB1> FROM <N1> TO <N2> TO <ITAB2> INDEX <N>.

NOTE: INSERT With NO INDEX Acts as APPEND Only.

FROM <N1> TO <N2> is Optional, If We Ignore it, All the Records are Transferred, Else Only
the records from <N1> TO <N2> are transferred.

ADDING SINGLE RECORD:

 ADD at the END of Internal Table

ADDPEND <WA> TO <ITAB>.

 ADD at the given Location

INSERT <WA> INTO <ITAB> INDEX <N>. ( N>0 )


NOTE: INSERT With NO INDEX Acts as APPEND Only.

NOTE: Fill the Data into <WA> and Transfer to <ITAB>.

Finding No Of Records

DESCRIBE TABLE <ITAB> LINES <V_LINES> ( V_LINES TYPE I).

The no of records from <ITAB> is collected into V_LINES.

SORTING: To arrange the records into Ascending / Descending Groups.

SORT <ITAB> ASCENDING / DESCENDING BY <F1><F2>……

NOTE: Sorting is Asecnding by Default.

NOTE: The default key is made up of the Non-Numeric fields of the table line in the order in
which they occur.

Accessing/ Reading Single Record:

READ TABLE <ITAB> INTO <WA> INDEX <N>.

OR

READ TABLE <ITAB> INTO <WA> WITH KEY <Condition> BINARY SEARCH.

NOTE: Make sure that the Internal Tabel is Sorted to Apply BINARY SEARCH.

Accessing Multiple Records:

LOOP AT >ITAB> INTO <WA> FROM <N1> TO <N2>.

*Process the Data from <WA>

ENDLOOP.

OR

LOOP AT <ITAB> INTO <WA> WHERE <Condition>

*Process the Data From <WA>

ENDLOOP.

DELETING Records:
Single Record: DELETE <ITAB> INDEX <N>.

Multiple Record: DELETE <ITAB> WHERE <Condition>

DELETE <ITAB> FROM <N1> TO <N2>.

DELETING ADJACENT DUPLICATES:

NOTE: Make Sure that the Duplicates should be Adjacent, which can be done through
SORTING.

So that, Sorting the <ITAB> is Mandatory.

NOTE: The Duplication of Records(s) depends on the comparing fileds.

DELETE ADJACENT DUPLICATES FROM <ITAB>

COMPARING <F1><F2>……..

NOTE: Make sure that the <ITAB> is Sorted by all the comparing fields in the same order…

MODIFY:

Either Single/Multiple records modification is always through <WA>.

*Fill the new data into <WA> fields.

MODIFY <ITAB> FROM <WA> TRANSPORTING <F1><F2>…

WHERE <Condition>.

NOTE: The Where condition, decides the no of records to be modified.

How to get records from Database Tables – Use of Select single

Categories: SAP

Now that we have our table created in SAP we want to fetch records from the table and display it
on the LPS (List Processing Screen).This is very simple.

There are no connections to be made with the database, as the table that was created is treated as
a object by SAP. Let’s suppose the name of the table is zemployee – it has three fields as

EMPNO-INT type, ENAME-CHAR type, EMPADD-CHAR type


When we get data from the table we also require something to store that data, that something
would be a structure. So we declare a structure as

data : begin of emp,


x(20) type c,
y(30) type c,
end of emp.

This structure has two fields X & Y for type character which will hold data that we get from
zemployee table.

Please note that variable X & Y can hold just one value at a time. As only one memory location
is allocated for both variables in the structure.

Consider this code

REPORT ZABC.

data : begin of emp,


x(20) type c,
y(30) type c,
end of emp.

parameters : p_z type i.

select single ename empaddr


from zemployee
into emp
where empno = p_z.
write :/ emp-x,
emp-y.

Here we are accepting value p_z from user which will be employee number.

Also note that we are using SELECT SINGLE in SQL query. This type SQL query is native to
SAP.

Select single will get only one record from the table and place it into the structure variables X &
Y.

In above code we are using data types which are not generic. To use more generic value for data
types, we can directly access data types of the fields in the table, here how it is done

REPORT ZXYZ.

parameters : p_z type ZEMPLOYEE-empno.


data : begin of emp,
x type zemployee-ename,
y type zemployee-empaddr,
end of emp.

select single ename empaddr


from zemployee
into emp
where empno = p_z.
if sy-subrc eq 0.
write :/ emp-x,
emp-y.
else.
message 'No record' type 'I'.
endif.<strong></strong>

‘sy-subrc‘ is a return code, set by the following ABAP statements.


As a rule, if SY-SUBRC = 0, the statement was executed successfully.

How to use Do loop : with example

Categories: SAP

Do Loop

The statements DO and ENDDO define a control structure, which can contain a closed statement
block statement_block .

Without the addition n TIMES ,the statement block will be repeated until one of the statements
termination of loops terminates the loop. Especially the statement EXIT is intended to
completely terminate loops.

With the addition n TIMES you can limit the amount of loop passes. n expects a data object of
the data type i. The numerical value of n at entry in the loop determines the maximum amount of
passes of the statement block. The control structure does not accept a change of the value n
within the loop. If n contains a value less than or equal to 0, the statement block will not be
executed.

Example

REPORT Z9AMPROGRAM15.

data : x type i value 5,


y type i,
z type i.

do 10 times.
z = x * y.
write :/ x,'*',y,'=',z.
y = y + 1.
enddo.<strong></strong>

How to use CASE statement : with example

Categories: SAP

Case Statement

These statements define a control structure that can contain multiple statement blocks
statement_block1, …, statement_blockn, of which no more than one is executed depending of
the value in the operand operand.

Starting with the first WHEN statement, the content of the operand in operand is compared with
the content of one of the operands operand1, operand2, … from the top down. The statement
block is executed after the first identical instance is found. If no matches are found, the statement
block is executed after the statement WHEN OTHERS.

If the end of the executed statement block is reached or no statement block is executed,
processing continues after ENDCASE.

For the comparison of the contents, the following logical expression can be used:

operand = operand1 [OR operand = operand2


[OR operand = operand3 [...]]]

Notes

● You cannot use a statement between the statement CASE and the first statement WHEN.

● A CASE control structure is somewhat faster than a semantically equivalent IF control


structure.

It is suggested to use case-endcase over if-endif, because performance wise case end-case
performs better than if-endif

REPORT ZABC.

parameters : x type i,
y type i,
ch type i.

data z type i.

case ch.
when 1.
z = x + y.
write :/ 'Sum is ',z.
when 2.
z = x - y.
write :/ 'Difference is ',z.
when 3.
z = x * y.
write :/ 'Product is ',z.
when 4.
z = x / y.
write :/ 'Division is ',z.
when others.
write :/ 'Invalid choice, please enter 1,2,3,4'.
endcase.<strong></strong>

How to use IF statement : with example

Categories: SAP

IF Statement

These statements define a control structure which can contain several statement_block
statement blocks; depending on the logical expressions, maximally one of the blocks is executed.

After IF and ELSEIF, any number of logical expressions can be specified, while the expressions
statement_block represent any kind of statement blocks.

The logical expressions are checked from top to bottom, starting with the IF statement, and the
statement block after the first true logical expression is executed. If none of the logical
expressions is true, then the statement block after the ELSE statement is executed.

If the end of the executed statement block is reached or if no statement block was executed, then
the processing is resumed after ENDIF.

REPORT ZABC.

parameters : x type i default 10,

y type i default 20.

data z type i.

parameters : r1 radiobutton group g1,

r2 radiobutton group g1,


r3 radiobutton group g1 default 'X',

r4 radiobutton group g1.

if r1 = 'X'.

z = x + y.

write :/ 'sum is ',z.

elseif r2 = 'X'.

z = x - y.

if z &gt;= 0.

write :/ 'Difference is ',z.

else.

write :/ 'Difference is -' no-gap,z no-sign

left-justified.

endif.

elseif r3 = 'X'.

z = x * y.

write :/ 'Product is ',z.

elseif r4 = 'X'.

z = x / y.

write :/ 'Division is ',z.

endif.<strong></strong>

How to accept user input in ABAP: use of parameter

Categories: SAP

Parameter

Parameter generates a selection screen for reading the user input.


Example

REPORT Z9AMPROGRAM6.
parameters : x type,
y type.
data z type i.
z = x + y.
write z.

If you want to provide a default value to the parameter then we use the keyword default.

So, default is a keyword used as part of parameter statement

to provide the default values for the selection screen fields.

Example

parameters : x type i default 10 obligatory,


y type i default 20.

This will provide default value to variable x as 10 and y as 20.

‘obligatory’ keyword used as part of parameter statement to

make a selection screen field as mandatory field.

Having lower case values in input fields

Categories: SAP

By default the character field values on selection screen are stored in upper-case. To control the
case sensitivity of value that is being entered we can use lower case keyword as a part of
parameters statement.

REPORT ZABC.

parameters : x(10) type c lower case.

write :/ x.

Various string functions in ABAP


Categories: SAP

While handling string, ABAP has given predefined string functions which can be directly used
with string variable. These functions are :

 strlen
 translate
 concatenate
 replace
 split

STRLEN It returns length of given string


TRANSLATE It converts given string in lower case of uppercase
CONCATENATE It combines multiple string into single string
REPLACE Used for replacing a substring in the source string
SPLIT It split the given string into multiple substring based of de-
limiter

Example :

REPORT ZABC.

* STRLEN ***
parameters x type string.
data k type i.
k = strlen( x ).
write :/ 'length of string is ',k.
***********************************

* Translate **
*parameters x type string lower case.
write :/ x.
translate x to lower case.
write :/ x.
translate x to upper case.
write :/ x.

* Concatenate
data : s1 type string value 'ABC',
s2 type string value 'Systems',
s3 type string.
write :/ s3.
concatenate s1 s2 into s3.
write :/ s3.
concatenate s1 s2 into s3 separated by ' '.
write :/ s3.
s1 = 'abc'.
s2 = 'pqr'.
concatenate s1 s2 into s2.
write :/ s2.

***********************************

** Replace
*data : s1 type string value
* 'ABC Software Systems'.
write :/ s1.
replace 's' with 'k' into s1.
write :/ ' after replacing s with k : ' , s1.
s1 = 'ABC Software Systems'.
replace all occurrences of 's' in s1 with 'k'.
write :/ s1.
s1 = 'ABC Software Systems'.
replace all occurrences of 's' in s1 with 'k'
ignoring case.
write :/ s1.
***********************************
** SPLIT
*data : s1 type string value
* 'ABC Software Systems',
* s2 type string
* s3 type string,
* s4 type string.
*write :/ s2,s3,s4.
split s1 at ' ' into s2 s3.
write :/ s2,
/ s3.
*split s1 at ' ' into s2 s3 s4.</em>
*write :/ s2,
* / s3,
* / s4.
* OFFSET
*data : s1 type string
* value 'ABC Software Systems',
* s2 type string.

*s2 = s1+5(10).
*write :/ s2.

How to Adjust the output : Use of justify

Categories: SAP

By default integers are right justified. And characters are left justified.
To Adjust the output for your screen, you can use left-justified

Here is an example :

REPORT Z9AMPROGRAM3.

data : x type i,
y type i,
z type i.
x = 10.
y = 20.
z = x + y.
write z.
write :/ ‘sum of two numbers is ’,z.
write :/ ‘sum of two numbers is ’,z left-justified.

CHAIN OPERATOR ‘ : ’

Chain operator is used when we are declaring multiple variables by using single data statement
or when we are displaying multiple variable values by using single write statement.

Working with Table Maintenance Generator

By Rakhi Bose, Capgemini

Go to SE11 and enter the Z table name for which the table maintenance generator has to be generated.
Click on Change.
Click on Utilities  Table Maintenance Generator
Enter Authorization group (For e.g. &NC&)
Function group (go to SE80 for creating function group)

You can select either one step or two step. Assign Screen numbers (To assign Screen numbers click on
the button 'Find Scr no'. It will propose screen no's)

Then Create. Save

Single step: Only overview screen is created i.e. the Table Maintenance Program will have only one
screen where you can add, delete or edit records.

Two step: Two screens namely the overview screen and Single screen are created. The user can see the
key fields in the first screen and can further go on to edit further details.

You can now create TCode for the table maintenance generator. Enter the TCode to be created and click
on Create.
Select “Transaction with parameters (Parameter transaction)”
Click on save.

Now you can create the table entries through the transaction created above.

Creating Database Views

By Pranshu Kukreti, Infosys

Introduction

Database views are used to combine application data often distributed over several tables. The structure
of such views is defined by specifying the tables and fields that are required. Fields which are not
required can be hidden, thereby minimizing the interfaces. A view can be used in ABAP programs for
data selection.
Following demo shows how to create database view for two database tables having foreign key
relationship.

We will be creating a database view for below shown tables YZ14_BANK & YZ14_ACCOUNTS. Note that
YZ14_ACCOUNTS is foreign key table (dependent table) and YZ14_BANK is check table (referenced
table) for field BRANCH_ID. It should be kept in the mind that we can only include transparent tables in
database view.

Steps

1. Go to transaction SE11 -> select radio button 'View' -> enter the name of the view -> press 'Create'.
2. Below pop up screen will be displayed. Select 'Database View' and press button.

3. Below screen gets displayed, enter suitable short description.


4. In 'Tables' enter the name of the base tables which we want to include in our view. In this case we will
be entering tables as YZ14_BANK & YZ14_ACCOUNTS.

5. Next, we need to link the entered tables by specifying the fields in join condition. We can also derive
the join conditions from existing foreign keys between the base tables of the view. To do this, position

the cursor on the table names and click on present at the bottom.
6. Below pop up comes wherein linked tables are present. Select the tables and press 'Copy'.
7. On clicking copy button, join condition will be derived from the base tables. In our case, we have the
below shown conditions.

8. In the 'View Flds' tab, we need to enter all the fields, we want in our view from the database tables.
We can either enter the fields directly or we can copy them from base tables. Later can be achieved
by pressing button present in the tab 'View Flds'. We can also include complete
table in a view by entering * in 'View field' & table name in 'Table'. If fields are inserted or deleted from
this table, similar modification will be automatically made in view structure.

Click here to continue...


9. On pressing 'Table fields' button we get a popup likewise shown below. Choose the table whose fields
we want to include in our view. Another popup will be shown select the fields we want to include and
press 'Copy'.

10. We repeat the above step to include below shown fields from table YZ14_ACCOUNTS.

11. In above steps do not include 'MANDT' & 'BRANCH_ID' twice. While activating the view, it will
lead to an error.
12. As shown, we have the below fields in our view.
13. We can also formulate the selection criteria using 'Selection Conditions' tab likewise coded below.
Here we have entered the condition for account type and status. All the data in the base table
satisfying the below condition will be selected based on other condition present.

14. Now save the view by pressing button present in standard tool bar. Then activate the view with

present in application tool bar. Once view got activated, press to see the contents. Below
screen gets displayed, do not enter any value, just execute it.

15. Because of the condition we have put on acc_typ & status fields, entries in the base table got filtered.
We can see that all the entries have account type as 'CURRENT' & status as 'X'.
Summary
Above steps demonstrate how to create database view from multiple tables having foreign key
relationships.

Comparison of Transparent, Pool and Cluster tables

By YSP, Defiance technologies

Transparent Pool Cluster

Contain a single table. They are used to hold a They are used to hold
Used to store master large number of very data from a few number
data small tables(stores of large tables.(stores
customizing data or system data)
system data)

It has a one-to-one It has a many-to-one It has a many-to-one


relationship with a table relationship with a table relationship with table in
in the database in the database the database

For each transparent It is stored with other Many cluster tables are
table there is one pooled tables in a single stored in a single table
associated table in the table called table pool in in the database called a
database the database table cluster

The database table has The database table has The database table has
the same name, same different name, different different name, different
number of fields and the number of fields and number of fields and
fields have the same fields have different fields have different
names names names

There is only a single Table pools contain Contains less tables


table more tables than table than table pools
clusters

Single table can have Primary key of each Primary key of each
one or more primary key table does not begin table begins with same
with same fields or fields fields or fields

Secondary indexes can Secondary indexes Secondary indexes


be created cannot be created cannot be created

They can be accessed They can be accessed They can be accessed


using open and native using open SQL only using open SQL only
SQL

USE: They are used to USE: They reduce the USE: They would be
hold master data e.g. amount of database used when the tables
Table vendors or table resources needed when have primary key in
of customers. Example many small tables have common and data in
of transaction data is to be opened at the these tables are all
orders placed by same time accesses
customers simultaneously

You might also like