Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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
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
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
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.
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
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
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.
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,
END OF ty_student.
*--------------------------------------------------------------* *Data Declaration *--------------------------------------------------------------* DATA: gwa_student TYPE ty_student. DATA: it TYPE TABLE OF ty_student.
= 1. = 'JOHN'. = 'London'.
22 | P a g e
Nagesh NVS
gwa_student-age = 20.
23 | P a g e
Nagesh NVS
WRITE:/ 'SY-TLENG : ', sy-tleng.
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:/ '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:/ '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.
*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,
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.
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.
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.
*--------------------------------------------------------------* *Data Types *--------------------------------------------------------------* TYPES: BEGIN OF ty_student, id(5) name(10) TYPE n, TYPE c,
END OF ty_student.
*--------------------------------------------------------------* *Data Declaration *--------------------------------------------------------------* DATA: gwa_student TYPE ty_student. DATA: it TYPE TABLE OF ty_student.
29 | P a g e
Nagesh NVS
gwa_student-id gwa_student-name gwa_student-place gwa_student-age = 2. = 'JIM'. = 'New York'. = 21.
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.
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.
*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
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.
33 | P a g e
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 **
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.
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 **
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.
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.
WHILE ENDWHILE Conditional Loop WHILE can be used to execute a certain lines of codes as long as the condition is true.
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.
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.
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.
39 | P a g e
DATA: a TYPE i VALUE 10. " We can assign a value in the declaration
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.
Nagesh NVS WRITE:/ a, 'Greater Than', 4. ELSEIF a > 3. WRITE:/ a, 'Greater Than', 3. ELSE. WRITE:/ a, 'Less Than', 3. ENDIF.
Output
CASE a. WHEN 3. WRITE:/ a, 'Equals', 3. WHEN 4. WRITE:/ a, 'Equals', 4. WHEN OTHERS. WRITE:/ 'Not Found'. ENDCASE.
Output
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 '-'.
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
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.
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
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: 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
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.
48 | P a g e
Nagesh NVS
EXCEPTIONS id_illegal_name = 1 OTHERS = 2.
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
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
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
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
gwa_employee-id
= 6.
59 | P a g e
Nagesh NVS
60 | P a g e
DATA: gwa_employee TYPE zemployee. gwa_employee-id gwa_employee-name gwa_employee-place gwa_employee-phone = 6. = 'JOSEPH'. = 'FRANKFURT'. = '7897897890'.
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
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
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
64 | P a g e
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
Since there was no entry with the key 6, a new entry was added to the table.
Since there was an entry with the key 6, the values in the existing record were modified.
66 | P a g e
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
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.
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
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
Error message in report programs will be displayed in the status bar and when the user press enter, the program terminates.
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.
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.
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.