Sei sulla pagina 1di 71

Nagesh NVS A data dictionary is a central source of information for the data in a information management system.

Its main function is to support the creation and management of data definitions (or metadata). What is Data dictionary used for ? Management of data definitions Provision of information for evaluations Support for software development Support for documentation Ensuring that data definitions are flexible and up-to-date

Objects in the ABAP Dictionary resided on three levels that support their re-usability. These levels are: Tables and structures Data elements Domains Lets look into them in detail -`

1|Page

Nagesh NVS

Domains
Describes the technical characteristics of a table field Specifies a value range which describes allowed data values for the fields Fields referring to the same domain (via the data elements assigned to them) are changed when a change is made to the domain Ensures consistency Ex. Purchasing document number (EBELN)

Data Elements
Describes the role played by a field in a technical context Fields of same semantic meaning can refer to the same data element Contains the field information Ex. Purchasing document number (EBELN)

2|Page

Nagesh NVS

Tables
Represent the Database Tables where data actually resides. Tables can be defined independently of the database in the ABAP Dictionary. The fields of the table are defined with their (database-independent) SAP ABAP data types and lengths.

3|Page

Nagesh NVS

Structures
Are record declarations that do NOT correspond to a Database Table. Just like user-defined data type. Defined like a table and can then be addressed from ABAP programs. Structures contain data only during the runtime of a program.

4|Page

Nagesh NVS

Aggregated Objects of ABAP Dictionary


Aggregated means consisting of several components. In the ABAP Dictionary, aggregated objects are objects which come from several different transparent tables. Views Search Help Lock Objects Lets look into them in detail

Views
Views in SAP _ ABAP are used to summarize data which is distributed among several tables The data of a view is not actually physically stored. The data of a view is instead derived from one or more other tables It is tailored to the needs of a specific application

5|Page

Nagesh NVS

Search Help
A Search help is a tool to help you search for data records in the system An efficient and user-friendly search assists users where the key of a record is unknown

6|Page

Nagesh NVS

Lock Objects
Simultaneous accessing of the same data record by two users in the SAP system is synchronized by a lock mechanism. Locks are set and released by calling certain function modules. These function modules are generated automatically from the definition of so-called lock objects in the ABAP/4 Dictionary Function modules : Enqueue_<obj name> to lock the table

7|Page

Nagesh NVS dequeue_<obj name> to release the lock

Important Transactions SE11 : Data Dictionary Initial Screen (SE12 Display only) SE13 : ABAP Dictionary : Technical Settings SE14 : Database Utility SE15 : Repository Information System SE16 : Data Browser SE17 : General table Display SE55 : Table View Maintenance SM30 : Table Maintenance

8|Page

Nagesh NVS ABAP Fundamentals Introduction to ABAP Data Dictionary Modularity in ABAP: Macro,Include,Subroutines,Function Modules & Groups Lets Dive into Database Native and Open SQL SAP Internal Tables ABAP Table Controls Lets CODE! ABAP Report Programming ABAP Dialog Programming Subscreens Process on Value & Process on Help ALV ABAP List Viewer Programming Forms and Scripts All About SAP Scripts Smart Forms Time for Some Exits Customer and User Exits BADI Lets Transfer Some Data! SAP BDC Batch Data Communication What is EDI ,ALE and iDOC? IDOC:Definition, Architecture, Implementation All About BAPI RFC

9|Page

Nagesh NVS

10 | P a g e

Nagesh NVS Modularity in ABAP: Macro,Include,Subroutines,Function Modules & Groups When you modularize source code, you place a sequence of ABAP statements in a module. Then, instead of placing all of the statements in your main program, you just call the module.When the program is generated, the source code in the modularization unit is treated as though it were actually physically present in the main program. Need of Modularization Improve the structure of the program. Easy to read the code Easy to maintain the code Avoid redundancy and promotes code reuse Various Modularization Techniques Use of Macros Use of include files Subroutines Function Modules Lets look into each of them in detail : SAP- ABAP Macro If you want to reuse the same set of statements more than once in a program, you can include them in a macro. You can only use a macro within the program in which it is defined, and it can only be called in lines of the program following its definition. Macros can be useful for long calculations or complex WRITE statements. Syntax DEFINE <macro_name> Macro Statements END-OF-DEFINITION Macros can use Parameters &N where N = 1,2,3 Example:DATA: number1 TYPE I VALUE 1. DEFINE increment. ADD 1 to &1. 11 | P a g e

Nagesh NVS WRITE &1. END-OF-DEFINITION. Increment number1. WRITE number1. Output: 2 Include Programs Include Programs are solely for modularizing source code, and have no parameter interface. Include programs allow you to use the same source code in different programs. They can be useful if you have lengthy data declarations that you want to use in different programs. Syntax Include <include program Name> Points to Note Include programs cannot call themselves. Include programs must contain complete statements. Example: INCLUDE ZILX0004. WRITE: / User, SY-UNAME,/ Date, SY-DATUM. ================================ PROGRAM ZRPM0001. INCLUDE ZILX0004. Subroutines Subroutines are procedures that you can define in any ABAP program and also call from any program. Subroutines are normally called internally, that is, they contain sections of code or algorithms that are used frequently locally. If you want a function to be reusable throughout the system, use a function module. SyntaxFORM <Subroutine> [<pass>]. <Statement block>. ENDFORM. <Subroutine> = Name of the subroutine 12 | P a g e

Nagesh NVS <pass> = Parameters being passed Types of Subroutines Internal Subroutine defined in same program being called. Can access all the data objects declared in the main ABAP/4 program. External Subroutine defined outside the program being called. Need to use the <pass> option or declare data objects in common parts of memory. Calling a Subroutine Internal Subroutines PERFORM <subroutine> [<pass>] <subroutine> = Name of the subroutine <pass> = Parameters being passed Data declared in main program is automatically available. External Subroutines PERFORM <subroutine>(<Program>) [<pass>]. PERFORM <subroutine> (<Program>) [<pass>] [IF FOUND]. PERFORM (<subroutine>) IN PROGRAM (<Program>) [<pass>] [IF FOUND]. PERFORM <index> OF <subroutine1> <subroutine2> <subroutine3> [<pass>]. Points to Note Nested calls are allowed in subroutines (i.e. PERFORM within a FORM ENDFORM ). Recursive calls are also possible. To define local data, use the DATA statement after FORM . Each time you enter the subroutine, the data is recreated (with an initial value) and released at the end (from the stack). To define global data used within a subroutine, use the LOCAL statement after FORM . The values are saved when you enter the subroutine and then released at the end (from the stack)

Function Modules 13 | P a g e

Nagesh NVS Function Modules are general purpose ABAP/4 routines that anyone can use. Infact , there are a large number of standard function Modules available. Function Modules are organized into Function Groups: Collections of logically related functions. A Function module always belongs to a Function Group. SyntaxFUNCTION <function module> <Statements> ENDFUNCTION. Important information Associated with Function Module Administration Import/Changing/Export parameters. Table Parameters/Exceptions. Documentation Source code L<fgrp>U01 . <fgrp> is the Function Group Global Data L<fgrp>TOP .Global data for the function group- Accessible across function modules in the function group. Main Program SAPL<fgrp> . Contains the list of all the include files for that function group Call a Function Module To call a function module, use the CALL FUNCTION statement: CALL FUNCTION <module> [EXPORTING f1 = a 1.... f n = a n] [IMPORTING f1 = a 1.... f n = a n] [CHANGING f1 = a 1.... f n = a n] [TABLES 1.... f n = a n] [EXCEPTIONS e1 = r 1.... e n = r n [ERROR_MESSAGE = r E] [OTHERS = ro]]. Function Groups Function groups are containers for function modules. Infact, there are a large number of standard Function Groups. All of the function modules in a function group can access the global data of the group. Like executable programs (type 1) and module pools (type M), function groups can contain screens, selection screens, and lists. Points to Note Function Groups cannot be executed. The name of a function group can be up to 26 characters long. f1 = a

14 | P a g e

Nagesh NVS When you create a function group or function module, the main program and include programs are generated automatically. Function groups encapsulate data.

How to create a Function Group Goto Transaction SE80. Select Program in the DropDown. Write the name of the Function Group That you want to create. Generally User made Function groups start with Z. e.g. <Z_FUNCTION_GROUP_NAME> . Hit Enter Key. Note that The TOP Include is create by default if the user checks the option of creating a TOP include. How to create a Function Module Create a function Group (say ZCAL). Create a function module, set the attributes like (Function group, Application, Short Text and Process Type) and Save. Include file LZCALU01 will have source code of first function module. Include file LZCALTOP will have global data. Main program SAPLZCAL contains Global data Include file LZCALTOP Function modules include file LZCALUXX User defined Include files LZCALF.., LZCALO.. and LZCALI.. Define interface parameters and Exceptions Write the source code Activate Function Module Testing the Function Module Single Test & Debugging Documenting and Releasing a Function Module

15 | P a g e

Nagesh NVS

What is an Internal Table?


Internal tables are used to obtain data from a fixed structure for dynamic use in ABAP. Each line in the internal table has the same field structure. The main use for internal tables is for storing and formatting data from a database table within a program.

What is a Work Area ?


Work areas are single rows of data. They should have the same format as any of the internal tables. It is used to process the data in an internal table one line at a time.

Difference Between Internal Table and a Work Area ?


A picture says a thousand words

Types of Internal Tables


There are two types of internal tables. Internal tables with HEADER line Internal tables without HEADER line. Internal Tables with Header Line Here the system automatically creates the work area. The work area has the same data type as internal table. This work area is called the HEADER line. It is here that all the changes or any of the action on the contents of the table are done. As a result of this, records can be directly inserted into the table or accessed from the internal table directly. Internal Tables without Header Line : 16 | P a g e

Nagesh NVS Here there is no work area associated with the table. Work area is to be explicitly specified when we need to access such tables. Hence these tables cannot be accessed directly.

Creating Internal Tables


There are many ways to create an Internal Table. Lets look at them one by one1.By Using the Type Statement Let us now create a Internal table itab using the TYPE statement. The syntax is Types : begin of line, column1 type I, column2 type I, end of line. Example: TYPES : begin of line, empno type I, empname(20) type c , end of line. The TYPES statement creates a structure line as defined. To actually create an Internal Table itab use the following commandData itab type line occurs 10. An internal table itab is created with the structure of line.Besides declaring the structure of an internal table, the OCCURS clause also defines how many table entries are maintained in main storage(in this case 10). Extra records are written out to paging area and can effect performance 2.By referring to another Table You can create an internal table by referring to an existing table. The existing table could be a standard SAP table, a Z table or another internal table. SyntaxData <f> <type> [with header line]. Example17 | P a g e

Nagesh NVS DATA itab TYPE line OCCURS 10 with header line. Here an internal table itab is created of the type line with a header line. Please note with header line is optional 3.By referring to existing Structure SyntaxData <f> LIKE <struct> occurs n [with header line]. ExampleDATA itab LIKE sline OCCURS 10. Here a table itab is created having a structure same as that of sline 4.By creating a new Structure Let us now create an internal table with a structure of our own. Here the table is created with an Header line, by default. Syntax Data : Begin of <f> occurs <n>, <component declaration>, , End of <f>. Example Data : Begin of itab occurs 10, column1 type I, column2(4) type C, column3 like mara-ernam, End of itab. Internal table itab is created

Populating Internal Tables


Now that we have successfully created some internal tables, let us see how do we populate them with some records. There are various methods available to populate tables 1.Append Data line by line The first method available is the use of the APPEND statement. 18 | P a g e

Nagesh NVS Using the APPEND statement we can either add one line from another work area to the internal table or we can add one initial line to the internal table.. Syntax APPEND [<wa> TO / INITIAL LINE TO] <itable>. Here work area <wa> or the Initial Line is appended to the internal table <itable>. The system variable SY-TABIX contains the index of the appended line. Example: Data: Begin of itab occurs 10, col1 type C, col2 type I, end of itab. Append initial line to itab. Results : 0 Initial lines adds a line initialized with the correct value for its type to the table. Here , col1 is an integer and col2 is a character. Then APPEND initial line , adds a line initialized with respect to the data type of the columns, i.e. 0 for Col1 and space for Col2. 2.Using COLLECT statement COLLECT is another form of statement used for populating the internal tables. Generally COLLECT is used while inserting lines into an internal table with unique standard key. SyntaxCOLLECT [<wa> INTO] <itable>. Incase of tables with Header line, INTO option is omitted. Suppose there is already an entry having a key same as the one you are trying to append, then a new line is not added to the table, but the numeric fields of both the entries are added and only one entry corresponding to the key is present. Value of SY-TABIX is changed to the row of the original entry. Else COLLECT acts similar to APPEND and SY-TABIX contains the index of the processed line. 3.Using INSERT statement INSERT statement adds a line/work area to the internal table. You can specify the position at which the new line is to be added by using the INDEX clause with the INSERT statement. Syntax INSERT [<wa> INTO / INITIAL LINE INTO] <itable> [index <idx>]. Here, the work area <wa> or INITIAL LINE is inserted into internal table <itable> at index <idx>. 19 | P a g e

Nagesh NVS

Copying Internal Tables


The contents of one internal table can be copied to another by using the APPEND LINES or INSERT LINES statement. A more simpler way is to usetany of the following syntaxs. MOVE <itab1> To <itab2>. OR <itab1> = <itab2>. These copy the contents of ITAB1 to ITAB2. Incase of internal tables with header line we have to use [] inorder to distinguish from work area. So, to copy contents of internal tables with header line the syntax becomes, itab1[] = itab2[].

Reading Internal Tables


We are now familiar with the creation of internal tables and populating them with data. We will now see how do we actually use the data or retrieve the data from the internal tables. 1.Using Loop -Endloop One of the ways of accessing or reading the internal table is by using LOOP-ENDLOOP. Syntax LOOP AT <itable> [INTO <wa>] .. ENDLOOP. Here when you say LOOP AT ITABLE, then the internal table ITABLE is read line by line. You can access the values of the columns for that line during any part of the LOOP-ENDLOOP structure. The value of the SY-SUBRC is set to 0, even if only one record is read. 2.Using READ The other method of reading the internal table is by using the READ statement. SyntaxREAD TABLE <itable> [INTO <wa>] INDEX <idx>. This statement reads the current line or line as specified by index <idx>. The value of SY-TABIX is the index of the line read. If an entry with the specified index is found, then SY-SUBRC is set to 0. If the specified index is less than 0, then run-time error occurs. If the specified index exceeds table size then SY-SUBRC is set to 4.

Deleting Internal Tables


20 | P a g e

Nagesh NVS There are many ways for deleting lines from an internal table. 1.Deleting lines in a loop. This is the simplest way for deleting lines. Sytax DELETE <ITABLE>. This statement works only within a loop. It deletes the current line. You can delete the lines in a loop conditionally by adding the WHERE clause. 2.Deleting lines using the index. This is used to delete a line from internal table at any know index. Syntax DELETE <ITABLE> INDEX <IDX>. The line with the index <IDX> is deleted. The index of the following line is decremented by 1.

21 | P a g e

Nagesh NVS
DESCRIBE TABLE is the statement to get the attributes like number of lines, line width of each row etc. of the internal table. DESCRIBE TABLE statement also fills the system fields SY-TFILL (Current no. of lines in internal table), SY-TLENG (line width of internal table) etc.

DESCRIBE TABLE <internal table> [LINES <lines>].


SORT is the statement to sort an ABAP internal table. We can specify the direction of the sort using the additions ASCENDING and DESCENDING. The default is ascending.

SORT <internal table> [ASCENDING|DESCENDING]


We can also delete the adjacent duplicates from an internal table by using the following statement.

DELETE ADJACENT DUPLICATE ENTRIES FROM <internal table> [COMPARING <f1> <f2> ... |ALL FIELDS].
COMPARING ALL FIELDS is the default. If we do not specify the COMPARING addition, then the system compares all the fields of both the lines. If we specify fields in the COMPARING clause, then the system compares only the fields specified after COMPARING of both the lines. If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.

*--------------------------------------------------------------* *Data Types *--------------------------------------------------------------* TYPES: BEGIN OF ty_student, id(5) name(10) TYPE n, TYPE c,

place(10) TYPE c, age TYPE i,

END OF ty_student.

*--------------------------------------------------------------* *Data Declaration *--------------------------------------------------------------* DATA: gwa_student TYPE ty_student. DATA: it TYPE TABLE OF ty_student.

DATA: gv_lines TYPE i.

gwa_student-id gwa_student-name gwa_student-place

= 1. = 'JOHN'. = 'London'.

22 | P a g e

Nagesh NVS
gwa_student-age = 20.

APPEND gwa_student TO it.

gwa_student-id gwa_student-name gwa_student-place gwa_student-age

= 2. = 'JIM'. = 'New York'. = 21.

APPEND gwa_student TO it.

gwa_student-id gwa_student-name gwa_student-place gwa_student-age

= 3. = 'JACK'. = 'Bangalore'. = 20.

APPEND gwa_student TO it.

gwa_student-id gwa_student-name gwa_student-place gwa_student-age

= 4. = 'ROB'. = 'Bangalore'. = 22.

APPEND gwa_student TO it.

gwa_student-id gwa_student-name gwa_student-place gwa_student-age

= 2. = 'JIM'. = 'New York'. = 21.

APPEND gwa_student TO it.

DESCRIBE TABLE it LINES gv_lines. WRITE:/ 'No. of lines in IT : ', gv_lines.

WRITE:/ 'SY-TFILL : ', sy-tfill.

23 | P a g e

Nagesh NVS
WRITE:/ 'SY-TLENG : ', sy-tleng.

WRITE:/ 'Values in IT before SORT' COLOR 4.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5, 37 'Age' COLOR 5. LOOP AT it INTO gwa_student. WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place, gwa_student-age. ENDLOOP.

WRITE:/ 'Values in IT after SORT' COLOR 4.

*SORT by name SORT it BY name DESCENDING.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5, 37 'Age' COLOR 5. LOOP AT it INTO gwa_student. WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place, gwa_student-age. ENDLOOP.

WRITE:/ 'Values in IT after deleting duplicates' COLOR 4.

*Delete duplicates SORT it. DELETE ADJACENT DUPLICATES FROM it.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5, 37 'Age' COLOR 5.

24 | P a g e

Nagesh NVS
LOOP AT it INTO gwa_student. WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place, gwa_student-age. ENDLOOP.

WRITE:/ 'Values in IT after deleting duplicates comparing place' COLOR 4.

*Delete duplicates comparing only place SORT it BY place. DELETE ADJACENT DUPLICATES FROM it COMPARING place.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5, 37 'Age' COLOR 5. LOOP AT it INTO gwa_student. WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place, gwa_student-age. ENDLOOP.
Output

25 | P a g e

Nagesh NVS

We can exit out of LOOP/ENDLOOP processing using EXIT, CONTINUE and CHECK similar to all other LOOPS. We can also initialize the internal table using FREE, CLEAR and REFRESH statements. CLEAR and REFRESH just initializes the internal table where as FREE initializes the internal table and releases the memory space.

*--------------------------------------------------------------* *Data Types *--------------------------------------------------------------* TYPES: BEGIN OF ty_student, id(5) name(10) TYPE n, TYPE c,

place(10) TYPE c, age TYPE i,

END OF ty_student.

*--------------------------------------------------------------*

26 | P a g e

Nagesh NVS
*Data Declaration *--------------------------------------------------------------* DATA: gwa_student TYPE ty_student. DATA: it TYPE TABLE OF ty_student.

DATA: gv_lines TYPE i.

gwa_student-id gwa_student-name gwa_student-place gwa_student-age

= 1. = 'JOHN'. = 'London'. = 20.

APPEND gwa_student TO it.

gwa_student-id gwa_student-name gwa_student-place gwa_student-age

= 2. = 'JIM'. = 'New York'. = 21.

APPEND gwa_student TO it.

WRITE:/ 'Values in IT before initializing' COLOR 4.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5, 37 'Age' COLOR 5. LOOP AT it INTO gwa_student. WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place, gwa_student-age. ENDLOOP.

*Initialize IT CLEAR it.

SKIP.

27 | P a g e

Nagesh NVS
WRITE:/ 'Values in IT before initializing' COLOR 4. WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5, 37 'Age' COLOR 5. LOOP AT it INTO gwa_student. WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place, gwa_student-age. ENDLOOP. *If no records are processed inside LOOP, then SY-SUBRC <> 0 IF sy-subrc <> 0. WRITE:/ 'No records found.'. ENDIF.

SKIP. *We can also use IS INITIAL to check any records found in IT IF it IS INITIAL. WRITE:/ 'No records found in IT.'. ENDIF.
Output

28 | P a g e

Nagesh NVS
DELETE is the statement to delete one or more lines from an ABAP Internal Table. Use the INDEX addition to delete a single line. If we use the INDEX addition and the operation is successful, SY-SUBRC will be set to zero, the line with the corresponding index in the internal table will be deleted and the indexes of the subsequent lines will be reduced by one.

DELETE <internal table> [INDEX <index>].


We can also use the above DELETE statement without INDEX addition inside LOOP. Inside LOOP if we do not specify the INDEX, then the current loop line will be deleted. We can use the WHERE clause to delete single or multiple lines. All the lines that meet the logical condition will be deleted. If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4.

DELETE <internal table> [FROM <n1>] [TO <n2>] [WHERE <condition>].


With WHERE clause we can also specify the lines between certain indices that we want to delete by specifying indexes in FROM and TO additions.

*--------------------------------------------------------------* *Data Types *--------------------------------------------------------------* TYPES: BEGIN OF ty_student, id(5) name(10) TYPE n, TYPE c,

place(10) TYPE c, age TYPE i,

END OF ty_student.

*--------------------------------------------------------------* *Data Declaration *--------------------------------------------------------------* DATA: gwa_student TYPE ty_student. DATA: it TYPE TABLE OF ty_student.

gwa_student-id gwa_student-name gwa_student-place gwa_student-age

= 1. = 'JOHN'. = 'London'. = 20.

INSERT gwa_student INTO TABLE it.

29 | P a g e

Nagesh NVS
gwa_student-id gwa_student-name gwa_student-place gwa_student-age = 2. = 'JIM'. = 'New York'. = 21.

INSERT gwa_student INTO TABLE it.

gwa_student-id gwa_student-name gwa_student-place gwa_student-age

= 3. = 'JACK'. = 'Bangalore'. = 20.

INSERT gwa_student INTO TABLE it.

gwa_student-id gwa_student-name gwa_student-place gwa_student-age

= 4. = 'ROB'. = 'Bangalore'. = 22.

INSERT gwa_student INTO TABLE it.

WRITE:/ 'Values in IT before DELETE' COLOR 4.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5, 37 'Age' COLOR 5. LOOP AT it INTO gwa_student. WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place, gwa_student-age. ENDLOOP.

SKIP. WRITE:/ 'Values in IT after DELETE' COLOR 4.

*Delete second line from IT

30 | P a g e

Nagesh NVS
DELETE it INDEX 2.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5, 37 'Age' COLOR 5. LOOP AT it INTO gwa_student. WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place, gwa_student-age. ENDLOOP.

SKIP. WRITE:/ 'Values in IT after DELETE using WHERE Clause' COLOR 4.

*Delete entries from IT where place is Bangalore DELETE it WHERE place = 'Bangalore'.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5, 37 'Age' COLOR 5. LOOP AT it INTO gwa_student. WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place, gwa_student-age. ENDLOOP.
Output

31 | P a g e

Nagesh NVS

32 | P a g e

Nagesh NVS Control Break Processing in ABAP Internal Tables


Control break processing is used to execute a piece of code whenever a specific condition in the data is detected during the processing of internal table loop. The following control break statements are available with in LOOP and ENDLOOP. AT FIRST / ENDAT AT LAST / ENDAT AT NEW / ENDAT AT END OF / ENDAT SUM ON CHANGE OF / ENDON The code between AT NEW and ENDAT is executed only during the first loop pass. So it is used to write the headers or some other initialization processing. The code between AT LAST and ENDAT is executed only during the last loop pass. So it is used to write the totals or some report footers. ** *Data Declaration **

DATA: gwa_spfli TYPE spfli. DATA: gt_spfli TYPE TABLE OF spfli.


*UP TO 5 ROWS addition selects only 5 rows from table SPFLI

SELECT * UP TO 5 ROWS FROM spfli INTO TABLE gt_spfli.

LOOP AT gt_spfli INTO gwa_spfli.


AT FIRST.

WRITE:/ 'Start of Loop'. WRITE:/ 'Flight Details'. WRITE:/ 'Airline Code' COLOR 5,14 'Connection No.' COLOR 5, 29 'Departure City' COLOR 5, 44 'Arival City' COLOR 5. ULINE.
ENDAT.

WRITE:/ gwa_spfli-carrid,14 gwa_spfli-connid, 29 gwa_spfli-cityfrom,44 gwa_spfli-cityto.


AT LAST.

33 | P a g e

Nagesh NVS ULINE. WRITE:/ 'End of Loop'.


ENDAT.

ENDLOOP.
Output

Between AT FIRST and ENDAT the work area will not contain any data. The default key fields are filled with asterisks(*) and the numeric fields are filled with zeros. The ENDAT restores the contents to the values they had prior to entering the AT FIRST. Changes to the work area within AT FIRST and ENDAT are lost. The same applies for AT LAST and ENDAT. ** *Data Declaration **

DATA: gwa_spfli TYPE spfli. DATA: gt_spfli TYPE TABLE OF spfli.


*UP TO 5 ROWS addition selects only 5 rows from table SPFLI

SELECT * UP TO 5 ROWS FROM spfli INTO TABLE gt_spfli.

LOOP AT gt_spfli INTO gwa_spfli. AT FIRST. WRITE:/ 'Flight Details'. WRITE:/ 'Airline Code' COLOR 5,14 'Connection No.' COLOR 5, 29 'Departure City' COLOR 5, 44 'Arival City' COLOR 5, 58 'Distance' COLOR 5.
WRITE:/ gwa_spfli-carrid,14 gwa_spfli-connid, 29 gwa_spfli-cityfrom,44 gwa_spfli-cityto, 58 gwa_spfli-distance.

ULINE. 34 | P a g e

Nagesh NVS ENDAT. WRITE:/ gwa_spfli-carrid,14 gwa_spfli-connid, 29 gwa_spfli-cityfrom,44 gwa_spfli-cityto, 58 gwa_spfli-distance. AT LAST. ULINE.
WRITE:/ gwa_spfli-carrid,14 gwa_spfli-connid, 29 gwa_spfli-cityfrom,44 gwa_spfli-cityto, 58 gwa_spfli-distance.

WRITE:/ 'End of Loop'. ENDAT. ENDLOOP.


Output

AT NEW and ENDAT is used to detect a change in the value of the field between the loop passes. The field that is specified in AT NEW is called control level. The code between AT NEW and ENDAT will be executed during the first loop pass and every time the value of the control level changes or any other field left to the control level changes. Between AT NEW and ENDAT all the fields in the work area that are right to the control level are filled with zeros and asterisks. Similarly The code between AT END OF and ENDAT will be executed during the last loop pass and every time the value of the control level changes or any other field left to the control level changes. ** *Data Declaration **

DATA: gwa_spfli TYPE spfli. DATA: gt_spfli TYPE TABLE OF spfli.

SELECT * UP TO 5 ROWS FROM spfli INTO TABLE gt_spfli.

35 | P a g e

Nagesh NVS LOOP AT gt_spfli INTO gwa_spfli. AT FIRST. WRITE:/ 'Flight Details'. WRITE:/ 'Airline Code' COLOR 5,14 'Connection No.' COLOR 5, 29 'Departure City' COLOR 5, 44 'Arival City' COLOR 5, 58 'Distance' COLOR 5. ULINE. ENDAT.
AT NEW carrid. WRITE:/ gwa_spfli-carrid, : New Airline. ULINE. ENDAT.

WRITE:/14 gwa_spfli-connid, 29 gwa_spfli-cityfrom,44 gwa_spfli-cityto, 58 gwa_spfli-distance.


AT END OF carrid. ULINE. WRITE:/ End of Airline : , gwa_spfli -carrid. ULINE. ENDAT.

AT LAST. WRITE:/ 'End of Loop'. ENDAT. ENDLOOP.


Output

36 | P a g e

Nagesh NVS

37 | P a g e

Nagesh NVS
DO ENDDO Unconditional Loop DO can be used to execute a certain lines of codes specific number of times.

DO 5 TIMES. WRITE sy-index. ENDDO.


Output

" SY-INDEX (system variable) - Current loop pass

WHILE ENDWHILE Conditional Loop WHILE can be used to execute a certain lines of codes as long as the condition is true.

WHILE sy-index < 3. WRITE sy-index. ENDWHILE.


Output

CONTINUE Terminate a loop pass unconditionally. After continue the control directly goes to the end statement of the current loop pass ignoring the remaining statements in the current loop pass, starts the next loop pass.

DO 5 TIMES. IF sy-index = 2. CONTINUE. ENDIF. WRITE sy-index. ENDDO.


Output

CHECK Terminate a loop pass conditionally. If the condition is false, the control directly goes to the end statement of the current loop pass ignoring the remaining statements in the current loop pass, starts the next loop pass.

DO 5 TIMES. CHECK sy-index < 3.

38 | P a g e

Nagesh NVS
WRITE sy-index. ENDDO.
Output

EXIT Terminate an entire loop pass unconditionally. After EXIT statement the control goes to the next statement after the end of loop statement.

DO 10 TIMES. IF sy-index = 2. EXIT. ENDIF. WRITE sy-index. ENDDO.


Output

39 | P a g e

Nagesh NVS Control Statements


To control the flow of the ABAP program use the following statements. IF Branching Conditionally IF statement The code between IF and ENDIF is executed only if the condition is true.

DATA: a TYPE i VALUE 10. " We can assign a value in the declaration

IF a > 5. WRITE:/ 'Condition True'. ENDIF.


Output

IF-ELSE statement The code between IF and ELSE is executed if the condition is true, the code between ELSE and ENDIF is executed if the condition is False.

DATA: a TYPE i VALUE 1.

IF a > 5. WRITE:/ 'Condition True'. ELSE. WRITE:/ 'Condition False'. ENDIF.


Output

IF-ELSEIF statement Used to check multiple conditions.

DATA: a TYPE i VALUE 2.

IF a > 5. WRITE:/ a, 'Greater Than', 5. ELSEIF a > 4. 40 | P a g e

Nagesh NVS WRITE:/ a, 'Greater Than', 4. ELSEIF a > 3. WRITE:/ a, 'Greater Than', 3. ELSE. WRITE:/ a, 'Less Than', 3. ENDIF.
Output

CASE-ENDCASE Branching based on the content of the variable.

DATA: a TYPE i VALUE 4.

CASE a. WHEN 3. WRITE:/ a, 'Equals', 3. WHEN 4. WRITE:/ a, 'Equals', 4. WHEN OTHERS. WRITE:/ 'Not Found'. ENDCASE.
Output

When no condition is met, OTHERS will be executed. OTHERS is not mandatory.

41 | P a g e

Nagesh NVS

String Operations
CONCATENATE Combines 2 or more strings into one string.

DATA: s1(10) VALUE 'Hello', s2(10) VALUE 'ABAP', s3(10) VALUE 'World', result1(30), result2(30). CONCATENATE s1 s2 s3 INTO result1. CONCATENATE s1 s2 s3 INTO result2 SEPARATED BY '-'.

WRITE / result1. WRITE / result2.


Output

If the the concatenated string fits in the result string, then the system variable sy-subrc is set to 0. If the result has to be truncated then sy-subrc is set to 4. SPLIT Splits a string into 2 or more smaller strings.

DATA: s1(10), s2(10), s3(10), source(20) VALUE 'abc-def-ghi'. SPLIT source AT '-' INTO s1 s2 s3. WRITE:/ 'S1 - ', s1. WRITE:/ 'S2 - ', s2. WRITE:/ 'S3 - ', s3.
Output

42 | P a g e

Nagesh NVS
If all target fields are long enough and no target fields has to be truncated then sy-subrc is set to 0, else set to 4. SEARCH Searches for a sub string in main string. If found then sy-subrc is set to 0, else set to 4.

DATA: string(30) VALUE 'SAP ABAP Development', str(10) VALUE 'ABAP'. SEARCH string FOR str. IF sy-subrc = 0. WRITE:/ 'Found'. ELSE. WRITE:/ 'Not found'. ENDIF.
Output

REPLACE Replaces the sub string with another sub string specified, in the main string. If replaced successfully then sy-subrc is set to 0, else set to 4.

DATA: string(30) VALUE 'SAP ABAP Development', str(10) VALUE 'World'. REPLACE 'Development' WITH str INTO string. WRITE:/ string.
Output

43 | P a g e

Nagesh NVS Selection screen push button


We can create pushbuttons on ABAP selection screen using the statement SELECTION-SCREEN PUSHBUTTON. The event that gets triggered when the pushbutton is pressed is handled in the AT SELECTION-SCREEN event.

TABLES sscrfields. *--------------------------------------------------------------* *Selection-Screen *--------------------------------------------------------------* SELECTION-SCREEN: PUSHBUTTON /2(40) button1 USER-COMMAND but1, PUSHBUTTON /2(40) button2 USER-COMMAND but2. *--------------------------------------------------------------* *At Selection-Screen *--------------------------------------------------------------* AT SELECTION-SCREEN. CASE sscrfields. WHEN 'BUT1'. MESSAGE 'Button 1 was clicked' TYPE 'I'. WHEN 'BUT2'. MESSAGE 'Button 2 was clicked' TYPE 'I'. ENDCASE. *--------------------------------------------------------------* *Initialization *--------------------------------------------------------------* INITIALIZATION. button1 = 'Button 1'. button2 = 'Button 2'.
Selection Screen Output

44 | P a g e

Nagesh NVS
If Button 1 is clicked then we get the following popup.

Even we can add icons to the pushbuttons on selection screen.

TYPE-POOLS: icon. TABLES sscrfields. *--------------------------------------------------------------* *Selection-Screen *--------------------------------------------------------------* SELECTION-SCREEN: PUSHBUTTON /2(40) button1 USER-COMMAND but1, PUSHBUTTON /2(40) button2 USER-COMMAND but2. *--------------------------------------------------------------* *At Selection-Screen *--------------------------------------------------------------* AT SELECTION-SCREEN. CASE sscrfields. WHEN 'BUT1'. MESSAGE 'Button 1 was clicked' TYPE 'I'. WHEN 'BUT2'. MESSAGE 'Button 2 was clicked' TYPE 'I'. ENDCASE. *--------------------------------------------------------------* *Initialization *--------------------------------------------------------------* INITIALIZATION. button1 = 'Button 1'.

45 | P a g e

Nagesh NVS
button2 = 'Button 2'.

CALL FUNCTION 'ICON_CREATE' EXPORTING name text info IMPORTING RESULT = button1 EXCEPTIONS OTHERS = 0. = icon_okay = 'Continue' = 'Click to Continue'

CALL FUNCTION 'ICON_CREATE' EXPORTING name text info IMPORTING RESULT = button2 EXCEPTIONS OTHERS = 0.
Selection Screen Output

= icon_cancel = 'Exit' = 'Click to Exit'

46 | P a g e

Nagesh NVS
Dropdown list is a user interface element which displays a list of values from which a user can select one value. Follow the below steps to create a dropdown list in SAP ABAP selection screen. First build the list in the INITIALIZATION event. Capture the value selected by the user from the list in the AT SELECTION-SCREEN event. Use the selected value for further processing.

TYPE-POOLS: vrm.

DATA: gt_list DATA: gwa_list DATA: gt_values gwa_values

TYPE vrm_values. TYPE vrm_value. TYPE TABLE OF dynpread, TYPE dynpread.

DATA: gv_selected_value(10) TYPE c. *--------------------------------------------------------------* *Selection-Screen *--------------------------------------------------------------* PARAMETERS: list TYPE c AS LISTBOX VISIBLE LENGTH 20. *--------------------------------------------------------------* *At Selection Screen *--------------------------------------------------------------* AT SELECTION-SCREEN ON list. CLEAR: gwa_values, gt_values. REFRESH gt_values. gwa_values-fieldname = 'LIST'. APPEND gwa_values TO gt_values. CALL FUNCTION 'DYNP_VALUES_READ' EXPORTING dyname dynumb = sy-cprog = sy-dynnr

translate_to_upper = 'X' TABLES

47 | P a g e

Nagesh NVS
dynpfields = gt_values.

READ TABLE gt_values INDEX 1 INTO gwa_values. IF sy-subrc = 0 AND gwa_values-fieldvalue IS NOT INITIAL. READ TABLE gt_list INTO gwa_list WITH KEY key = gwa_values-fieldvalue. IF sy-subrc = 0. gv_selected_value = gwa_list-text. ENDIF. ENDIF. *--------------------------------------------------------------* *Initialization *--------------------------------------------------------------* INITIALIZATION. gwa_list-key = '1'. gwa_list-text = 'Product'. APPEND gwa_list TO gt_list. gwa_list-key = '2'. gwa_list-text = 'Collection'. APPEND gwa_list TO gt_list. gwa_list-key = '3'. gwa_list-text = 'Color'. APPEND gwa_list TO gt_list. gwa_list-key = '4'. gwa_list-text = 'Count'. APPEND gwa_list TO gt_list.

CALL FUNCTION 'VRM_SET_VALUES' EXPORTING id values = 'LIST' = gt_list

48 | P a g e

Nagesh NVS
EXCEPTIONS id_illegal_name = 1 OTHERS = 2.

*--------------------------------------------------------------* *Start of Selection *--------------------------------------------------------------* START-OF-SELECTION. WRITE:/ gv_selected_value.


Selection Screen Output

49 | P a g e

Nagesh NVS Native and Open SQL in ABAP The goal of this tutorial is not to teach you SQL or database concepts but to introduce you to the SQL diversity in ABAP In ABAP/4 programming language, there are two types of SQL being used. NATIVE SQL OPEN SQL. Open SQL allows you to access the database tables declared in the ABAP dictionary regardless of the database platform that the R/3 system is using. Native SQL allows you to use database-specific SQL statements in an ABAP/4 program. This means that you can use database tables that are not administered by ABAP dictionary, and therefore integrate data that is not part of the R/3 system. Open SQL consists of a set of ABAP statements that perform operations on the central database in the R/3 system. The results of the operations and any error messages are independent of the database system in use. Open SQL thus provides a uniform syntax and semantics for all of the database systems supported by SAP. ABAP programs that only use Open SQL statements will work in any R/3 system, regardless of the database system in use. Open SQL statements can only work with database tables that have been been created in the ABAP dictionary. Basic Open SQL Commands SELECT INSERT UPDATE MODIFY DELETE OPEN CURSOR, FETCH, CLOSE CURSOR Example TABLES SBOOK. DATA C TYPE CURSOR, WA LIKE SBOOK. OPEN CURSOR C FOR SELECT * FROM SBOOK WHERE CARRID = LH AND CONNID = 0400 AND FLDATE = 19950228 ORDER BY PRIMARY KEY.

50 | P a g e

Nagesh NVS DO. FETCH NEXT CURSOR C INTO WA. IF SY-SUBRC <> 0. CLOSE CURSOR C. EXIT. ENDIF. WRITE: / WA-BOOKID, WA-CUSTOMID, WA-CUSTTYPE, WA-SMOKER, WA-LUGGWEIGHT, WA-WUNIT, WA-INVOICE. ENDDO.

Output the passenger list for the Lufthansa flight 0400 on 28-02.1995: Open SQL Return Codes All Open SQL statements fill the following two system fields with return codes. SY-SUBRC After every Open SQL statement, the system field SY-SUBRC contains the value 0 if the operation was successful, a value other than 0 if not. SY-DBCNT After an Open SQL statement, the system field SY-DBCNT contains the number of database lines processed.

51 | P a g e

Nagesh NVS

Native SQL As already mentioned, Native SQL allows you to use database-specific SQL statements in an ABAP program.To use Native SQL statement, you must precede it with the EXEC SQL statement, and follow it with the ENDEXEC statement. Syntax EXEC SQL [PERFORMING <form>]. <Native SQL statement> ENDEXEC. There is no period after Native SQL statements. Furthermore, using inverted commas () or an asterisk (*) at the beginning of a line in a native SQL statement does not introduce a comment as it would in normal ABAP syntax. You need to know whether table and field names are case-sensitive in your chosen database. In Native SQL statements, the data is transported between the database table and the ABAP program using host variables. These are declared in the ABAP program, and preceded in the Native SQL statement by a colon (:). You can use elementary structures as host variables. Exceptionally, structures in an INTO clause are treated as though all of their fields were listed individually. As in Open SQL, after the ENDEXEC statement, SY-DBCNT contains the number of lines processed. In nearly all cases, SY-SUBRC contains the value 0 after the ENDEXEC statement. Open SQL Performance Rules To improve the performance of the SQL and in turn of the ABAP program, one should take care of the following rulesKeep the Result Set Small Using the where clause If only one record is required from the database, use SELECT SINGLE whenever possible . Minimize the Amount of Data Transferred Restrict the number of lines If only certain fields are required from a table, use the SELECT <field1> <field2> INTO statement Restrict no of columns Use aggregate functions Minimize the Number of Data Transfers Avoid nested select loops An alternative option is to use the SELECT .. FOR ALL ENTRIES statement. This statement can often be a lot more efficient than performing a large number of SELECT or SELECT SINGLE statements during a LOOP of an internal table. 52 | P a g e

Nagesh NVS Use dictionary views Use Joins in the FROM clause Use subqueries in the where clause Minimize the Search Overhead Use index fields in the where clause When accessing databases, always ensure that the correct index is being used . Reduce the Database Load Buffering Logical databases Avoid repeated database access Using Internal Tables to Buffer Records To avoid executing the same SELECT multiple times (and therefore have duplicate selects), an internal table of type HASHED can be used to improve performance.

53 | P a g e

Nagesh NVS

What is Open SQL?


Open SQL is a set of ABAP statements that performs operations like reads, modifies or deletes data in the SAP database. Open SQL is independent of the database system, so the syntax of the open SQL is uniform for all the databases supported by SAP. All open SQL statements are passed to the database interface. The DB interface converts the open SQL to native SQL and passes it on to the database. List of Open SQL statements OPEN SQL SELECT INSERT UPDATE MODIFY DESCRIPTION Reads data from database Inserts lines to database Changes the contents of lines in database Inserts lines into database or changes the contents of existing lines Deletes lines from database

DELETE

All Open SQL statements fill the following two system fields: SY-SUBRC After every Open SQL statement, the system field SY-SUBRC contains the value 0 if the operation was successful, a value other than 0 if not. SY-DBCNT After an open SQL statement, the system field SY-DBCNT contains the number of database lines processed.

54 | P a g e

Nagesh NVS Reading Data using Open SQL


SELECT is the open SQL statement to read the data from the database. The general syntax for SELECT statement is as follows. SELECT <result> INTO <target> FROM <source> [WHERE <condition>] CLAUSE SELECT <result> DESCRIPTION Specifies which columns you want to read, whether one line or many lines needs to selected, and whether duplicate entries are allowed Determines the target area into which the selected data is to be placed Specifies the database table from which the data is to be selected specifies which lines are to be read by specifying conditions for the selection

INTO <target> FROM <source> WHERE <condition>

DATA: gwa_employee TYPE zemployee.

WRITE:/1 'Emp ID' color 5,9 'Name' color 5,17 'Place' color 5, 27 'Phone' color 5,39 'Dept' color 5.

SELECT * FROM zemployee INTO gwa_employee. WRITE:/1 gwa_employee-id,9 gwa_employee-name, 17 gwa_employee-place,27 gwa_employee-phone, 39 gwa_employee-dept_id. ENDSELECT.
In the above code, GWA_EMPLOYEE is the work area to hold one record of table ZEMPLOYEE at a time. SELECT * specifies all the rows and columns are read from the database. SELECT ENDSELECT works in a loop, so the code between SELECT and ENDSELECT will be executed for each record found in the database table. WRITE statements are used to output the values in the list. If the SELECT statement returns any record then the value of the system variable SY-SUBRC is set to zero else a non zero value will be set.

55 | P a g e

Nagesh NVS
After the SELECT statement is executed, the value of the system variable SY-DBCNT contains the number of records read from the database. The value of SY-DBCNT is zero if no records are read from the database. Table ZEMPLOYEE Entries

Report Output

Selective Reading using Open SQL


In Reading Data using Open SQL we have read all the rows from the database. What if we want to read only certain records that match a certain criteria? Then we need to use the where clause of the SELECT statement. Let us write a program to read only the employees with department ID 2.

DATA: gwa_employee TYPE zemployee.

WRITE:/1 'Emp ID' COLOR 5,9 'Name' COLOR 5,17 'Place' COLOR 5, 27 'Phone' COLOR 5,39 'Dept' COLOR 5.

SELECT * FROM zemployee INTO gwa_employee WHERE dept_id = 2. WRITE:/1 gwa_employee-id,9 gwa_employee-name, 17 gwa_employee-place,27 gwa_employee-phone, 39 gwa_employee-dept_id. ENDSELECT.
Report Output

56 | P a g e

Nagesh NVS

What if we want to select only certain columns from the database table instead of all the columns? Then we need to specify the field list(field names) in the SELECT statement instead of specifying *.

SELECT id phone dept_id FROM zemployee INTO CORRESPONDING FIELDS OF gwa_employee WHERE dept_id = 2. WRITE:/1 gwa_employee-id,9 gwa_employee-name, 17 gwa_employee-place,27 gwa_employee-phone, 39 gwa_employee-dept_id. ENDSELECT.
Report Output

Only columns ID, PHONE and DEPT_ID were read from the database. To select a single record from the database use SELECT SINGLE instead of SELECT statement. SELECT SINGLE picks the first record found in the database that satisfies the condition in WHERE clause. SELECT SINGLE does not work in loop, so no ENDSELECT is required.

SELECT SINGLE * FROM zemployee INTO gwa_employee WHERE dept_id = 2. WRITE:/1 gwa_employee-id,9 gwa_employee-name, 17 gwa_employee-place,27 gwa_employee-phone, 39 gwa_employee-dept_id.
Report Output

57 | P a g e

Nagesh NVS

58 | P a g e

Nagesh NVS Inserting Values using SAP Open SQL


INSERT is the open SQL statement to add values to the database table. First declare a work area as the line structure of database table and populate the work area with the desired values. Then add the values in the work area to the database table using INSERT statement. The syntax for the INSERT statement is as follows. INSERT <database table> FROM <work area> or INSERT INTO <database table> VALUES <work area> If the database table does not already contain a line with the same primary key as specified in the work area, the operation is completed successfully and SY-SUBRC is set to 0. Otherwise, the line is not inserted, and SY-SUBRC is set to 4.

DATA: gwa_employee TYPE zemployee.

gwa_employee-id

= 6.

gwa_employee-name = 'MARY'. gwa_employee-place = 'FRANKFURT'. gwa_employee-phone = '7897897890'. gwa_employee-dept_id = 5.

INSERT zemployee FROM gwa_employee.


EMPLOYEE table entries before INSERT

EMPLOYEE table entries after INSERT

59 | P a g e

Nagesh NVS

60 | P a g e

Nagesh NVS Changing Values using SAP Open SQL


UPDATE is the open SQL statement to change the values in the database table. First declare a work area as the line structure of database table and populate the work area with the desired values for a specific key in the database table. Then update the values for the specified key in the database table using UPDATE statement. The syntax for the UPDATE statement is as follows. UPDATE <database table> FROM <work area> If the database table contains a line with the same primary key as specified in the work area, the operation is completed successfully and SY-SUBRC is set to 0. Otherwise, the line is not inserted, and SY-SUBRC is set to 4.

DATA: gwa_employee TYPE zemployee. gwa_employee-id gwa_employee-name gwa_employee-place gwa_employee-phone = 6. = 'JOSEPH'. = 'FRANKFURT'. = '7897897890'.

gwa_employee-dept_id = 5. UPDATE zemployee FROM gwa_employee.


EMPLOYEE table entries before UPDATE

EMPLOYEE table entries after UPDATE

We can also change certain columns in the database table using the following syntax UPDATE <target> SET <set1> <set 2> [WHERE <condition>]. The WHERE clause determines the lines that are changed. If we do not specify a WHERE clause, all lines will be changed.

61 | P a g e

Nagesh NVS
UPDATE zemployee SET place = 'MUMBAI' WHERE dept_id = 2.
EMPLOYEE table entries after UPDATE

62 | P a g e

Nagesh NVS Deleting Entries using SAP Open SQL


DELETE is the open SQL statement to delete entries from database table. First declare a work area as the line structure of database table and populate the work area with the specific key that we want to delete from the database table. Then delete the entries from the database table using DELETE statement. The syntax for the DELETE statement is as follows. DELETE <database table> FROM <work area> If the database table contains a line with the same primary key as specified in the work area, the operation is completed successfully and SY-SUBRC is set to 0. Otherwise, the line is not deleted, and SY-SUBRC is set to 4.

DATA: gwa_employee TYPE zemployee. gwa_employee-id = 6.

gwa_employee-name = 'JOSEPH'. gwa_employee-place = 'FRANKFURT'. gwa_employee-phone = '7897897890'. gwa_employee-dept_id = 5. DELETE zemployee FROM gwa_employee.
EMPLOYEE table entries before DELETE

EMPLOYEE table entries after DELETE

We can also multiple lines from the table using the WHERE clause in the DELETE statement. DELETE FROM <database table> WHERE <condition>

63 | P a g e

Nagesh NVS DELETE FROM zemployee WHERE dept_id = 2.


EMPLOYEE table entries after DELETE

64 | P a g e

Nagesh NVS Inserting or Changing Values using SAP Open SQL


MODIFY is the open SQL statement to insert or change entries in the database table. If the database table contains no line with the same primary key as the line to be inserted, MODIFY works like INSERT, that is, the line is added. If the database already contains a line with the same primary key as the line to be inserted, MODIFY works like UPDATE, that is, the line is changed. The syntax for the MODIFY statement is as follows. MODIFY <database table> FROM <work area> If the database table does not already contain a line with the same primary key as specified in the work area, a new line is inserted. If the database table does already contain a line with the same primary key as specified in the work area, the existing line is overwritten. SY-SUBRC is always set to 0.

DATA: gwa_employee TYPE zemployee. gwa_employee-id = 6.

gwa_employee-name = 'JOSEPH'. gwa_employee-place = 'FRANKFURT'. gwa_employee-phone = '7897897890'. gwa_employee-dept_id = 5. MODIFY zemployee FROM gwa_employee.
ZEMPLOYEE table entries before MODIFY

ZEMPLOYEE table entries after MODIFY

Since there was no entry with the key 6, a new entry was added to the table.

DATA: gwa_employee TYPE zemployee. gwa_employee-id = 6.

gwa_employee-name = 'JOHNNY'. gwa_employee-place = 'LONDON'. 65 | P a g e

Nagesh NVS gwa_employee-phone = '7897897890'. gwa_employee-dept_id = 3. MODIFY zemployee FROM gwa_employee.

Since there was an entry with the key 6, the values in the existing record were modified.

66 | P a g e

Nagesh NVS Messages in ABAP SAP Message Class


We know that we can use MESSAGE statement to issue a message in ABAP. What if we want to issue the same message in more than one program? Do we need to hard code the same message text or maintain the same text symbols in all the programs? The answer is NO. Instead of maintaining the same message text in all the programs maintain the message in a Message class and use it in all the programs. What is a Message Class? Message Class is a like a container which holds a number of different messages. Each message in the message class is identified with unique message number. So when you call a message in a ABAP program, you need to specify the message class and message number. How to create a Message Class? First go to t-code SE91 i.e. Message Maintenance, enter the name of the message class and click on create button.

Maintain the required message texts with message numbers. Then save the entries and assign it to proper development class and transport request. Once the message class is saved we can use it in our ABAP programs.

67 | P a g e

Nagesh NVS

Messages can be issued as follows.

MESSAGE s000(ztest).
Output

In the above code, the message number, message class and message type are specified in the MESSAGE statement. We can also specify the message class in the REPORT statement as shown below, so that we can skip the message class in the MESSAGE statements of the program.

REPORT zmessages
MESSAGE-ID ztest.

MESSAGE s000.
We can also maintain placeholders for variables in messages.

68 | P a g e

Nagesh NVS
In the above message & is the placeholder. At runtime the placeholders (&) will be replaced by the variable values specified in the MESSAGE statement.

REPORT zmessages MESSAGE-ID ztest. MESSAGE s001


WITH XYZ 1000. Output

The values XYZ and 1000 replaces the placeholders in the actual message.

Messages are usually used to tell the user what is going on. The following types of messages are available in ABAP. A Termination The message appears in a dialog box, and the program terminates. When the user has confirmed the message, control returns to the next-highest area menu. Depending on the program context, an error dialog appears or the program terminates. The message appears in a dialog box. Once the user has confirmed the message, the program continues immediately after the MESSAGE statement. The program continues normally after the MESSAGE statement, and the message is displayed in the status bar of the next screen. Depending on the program context, an error dialog appears or the program terminates. No message is displayed, and the program terminates with a short dump. Program terminations with a short dump normally only occur when a runtime error occurs.

Error

Status

Error

Warning

Exit

The syntax for issuing a message is as follows.

MESSAGE <message> TYPE <message type>.


We can issue a status message as follows. Status message will be displayed in the status bar. After the message is displayed the program continues after the MESSAGE statement.

MESSAGE 'This is a status message' TYPE 'S'.

Information message will be displayed in a dialog box. Once the user has confirmed the message, the program continues immediately after the MESSAGE statement.

69 | P a g e

Nagesh NVS MESSAGE 'This is an information message' TYPE 'I'.

Error message in report programs will be displayed in the status bar and when the user press enter, the program terminates.

MESSAGE 'This is an error message' TYPE 'E'.

Warning message behaves similar to error message in report programs. Exit Message No message is displayed, and the program terminates with a short dump. Short dumps can be viewed in t-code ST22.

MESSAGE 'This produces short dump' TYPE 'X'.

Termination Message appears in a dialog box, and the program terminates. When the user has confirmed the message, control returns to the next-highest area menu.

MESSAGE 'This is termination message' TYPE 'A'.

70 | P a g e

Nagesh NVS

Instead of hardcode the message text in the program we can maintain the message text in text symbols. In order to maintain text symbols use the menu path Goto->Text Elements->Text Symbols in ABAP editor.

In the text symbols screen we can maintain the messages with a 3 character identifier.

Then use the following message statement.

MESSAGE text-001 TYPE 'I'. 71 | P a g e

Potrebbero piacerti anche