Sie sind auf Seite 1von 28

Beginners Guide to ABAP in BW

Presented by Karen Dawson

InfoSession
The objective of this InfoSession is to enable participants to gain basic knowledge required to develop ABAP code in BI.

Agenda
What is covered in todays session
Understand basic principles. Using the editor - see function module Z_GET_SPARK_VERSION_ATTRIBUTES Table types Transformation (Start, End, Expert Routines) Performance issues Tips and Tricks Useful Transactions

Not covered todays session


ABAP in the Extraction Process ABAP in InfoPackage Routine OOPS ABAP.

Not covered here .


Not covered in this InfoSession: ABAP in the Extraction Process ABAP in InfoPackage Routine OOPS ABAP.

ABAP in BI
Different Routines available in SAP BI
Start Routine End Routine Expert Routine Characteristic /Key Figure Routine Customer Exit Variable (Bex) InfoPackage (not covered in this presentation) Process Chain ABAP (not covered in this presentation)

Global Data / Data Dictionary


To work with data at runtime, you need to store it in the program at runtime and address it from your program. The system needs to know the type of the data (for example, character string, whole number, table consisting of name, currency amount, and date, and so on). Between *$*$ begin of global ... and *$*$ end of global ... you can define global data declarations 'CLASS-DATA' that are then available in all routines.
Use the following statement *$*$ begin of global - insert your declaration only below this line *-* ... "insert your code here TYPES: BEGIN OF t_cust, /bic/zcnf_cust TYPE /bic/oizcnf_cust, /bic/zcustl_06 TYPE /bic/oizcustl_06, END OF t_cust. * * DATA: l_t_custattr TYPE HASHED TABLE OF t_cust WITH UNIQUE KEY /bic/zcnf_cust. *$*$ end of global - insert your declaration only before this line *-*

Structure definition A structure is a user-defined sequence of data types. It fully defines the data object. You can either access the entire data object, or its individual components. You need to define your own structures, either in the ABAP program in which you want to use it, or in the ABAP Dictionary.

Go to Data Dictionary data type

Internal Tables in ABAP


Internal Tables
Internal tables consists of a series of lines that all have the same data type. Internal tables are characterised by: Their line type. The line type of an internal table can be any ABAP data type - an elementary type, a structure or an internal table. A key. The key of an internal table is used to identify its entries. It is made up of the elementary fields in the line. The key may be unique or non-unique. The access type. The access method determines how ABAP will access individual table entries. There are three access types, namely unsorted tables, sorted index tables and hash tables

Choosing a Table Type


Standard tables This is the most appropriate type if you are going to address the individual table entries using the index. Index access is the quickest possible access. You should fill a standard table by appending lines (ABAP APPEND statement), and read, modify and delete entries by specifying the index (INDEX option with the relevant ABAP command). The access time for a standard table increases in a linear relationship with the number of table entries. If you need key access, standard tables are particularly useful if you can fill and process the table in separate steps. For example, you could fill the table by appending entries, and then sort it. If you use the binary search option with key access, the response time is logarithmically proportional to the number of table entries.

Sorted tables
This is the most appropriate type if you need a table which is sorted as you fill it. You fill sorted tables using the INSERT statement. Entries are inserted according to the sort sequence defined through the table key. Any illegal entries are recognized as soon as you try to add them to the table. The response time for key access is logarithmically proportional to the number of table entries, since the system always uses a binary search. Sorted tables are particularly useful for partially sequential processing in a LOOP if you specify the beginning of the table key in the WHERE condition.

Hashed tables
This is the most appropriate type for any table where the main operation is key access. You cannot access a hashed table using its index. The response time for key access remains constant, regardless of the number of table entries. Like database tables, hashed tables always have a unique key. Hashed tables are useful if you want to construct and use an internal table which resembles a database table or for processing large amounts of data.

Structures
Use the following statement *----------------------------------------------------------------------* * Method start_routine *----------------------------------------------------------------------* * Calculation of source package via start routine *----------------------------------------------------------------------* * <-> source package *----------------------------------------------------------------------* METHOD start_routine. *=== Segments === FIELD-SYMBOLS: <SOURCE_FIELDS> DATA: MONITOR_REC

You use structures in ABAP programs to group work areas that logically belong together. Since the individual elements within a structure can be of any type, and can also themselves be structures or internal tables, the possible uses of structures are very wide-ranging. For example, you can use a structure with elementary data types to display lines from a database table within a program. You can also use structures containing aggregated elements to include all of the attributes of a screen or control in a single data object.

TYPE _ty_s_SC_1.

TYPE rstmonitor.

* * *

SELECT /bic/zcnf_cust /bic/zcustl_03 /bic/zcustl_04 /bic/zcustl_05 /bic/zcustl_06 FROM /bic/mzcnf_cust INTO TABLE l_t_custattr WHERE objvers = 'A' AND dateto = '99991231' AND soursystem = 'RU'. *-*

*$*$ end of routine - insert your code only before this line ENDMETHOD. "start_routine

Where ABAP is used


Subroutines
You call subroutines from ABAP programs using the PERFORM statement. Subroutines are introduced with the FORM statement and concluded with the ENDFORM statement. You can define subroutines in any ABAP program. You can either call a subroutine that is part of the same program or an external subroutine, that is, one that belongs to a different program. If you call an internal subroutine, you can use global data to pass values between the main program and the subroutine. When you call an external subroutine, you must pass actual parameters from the main program to formal parameters in the subroutine.

Function Modules
Function modules are external functions with a defined interface. You call function modules from ABAP programs using the CALL FUNCTION statement. Function modules are introduced with the FUNCTION statement and concluded with the ENDFUNCTION statement. You can only create function groups within special ABAP programs called function groups using the Function Builder. This means that you can only call them externally from other ABAP programs. Unlike subroutines, you always pass data to function modules using an explicit parameter interface.

Methods
Methods describe the functions of classes in ABAP Objects. Like function modules, they have a defined interface. You call methods from ABAP programs using the CALL METHOD statement. Methods are introduced with the METHOD statement and concluded with the ENDMETHOD statement. Methods can only be defined in the implementation parts of classes. You can either do this globally in the Class Builder, or locally within ABAP programs. Methods can work with the attributes of their class and with data that you pass to them using their explicit parameter interface.

Date Calculations in ABAP


DATA: DAYSOLD TYPE P, DOB TYPE D, TODAY LIKE SY-DATUM. DOB = 19621230. TODAY = SY-DATUM. DAYSOLD = TODAY - DOB. WRITE: You are, DAYSOLD, days old.. You are 12410 days old. Date fields store values as YYYYMMDD.

Working with Field Symbols in ABAP


DATA: NUM TYPE I VALUE 12. FIELD-SYMBOLS: <F1>, <F2> TYPE I, <F3> LIKE NUM. ASSIGN: NUM TO <F1>, NUM TO <F2>, NUM TO <F3>. WRITE: / Line 1:, NUM, <F1>, <F2>, <F3>. <F1> = 32. WRITE: / Line 2:, NUM, <F1>, <F2>, <F3>. Line 1: 12 Line 2: 32 12 32 12 32 12 32

A field symbol is a pointer that assumes a fields address, not a fields value.
The ASSIGN statement associates a field to a field symbol at runtime (i.e., when the program is executed).

Data Dictionary
A data dictionary is a centralized storage location for information about the data that is stored in a database. This information is often called metadata (data about data). SAPs data dictionary is called the ABAP Dictionary. A data dictionary provides answers to questions such as: What data is contained in the database? What are the attributes of this data: name, length, format, etc.? What relationships exist among different data objects?

How to decide on Start, End, etc


How to decide whether to use Start Routines, End Routines, Individual Update or Expert Routines Start Routine
Generally Start routines are used for Filtering records or fetching global data to be used in Characteristic routine. Start Routines are preferred when any transformation is needed in the data at package Level

Individual Routine
Individual Routines can be used to read data from the global Internal Table which is populated in the start routines or to implement any simple formula or a calculation

End Routine
End Routines are Target Specific. Any fields which you wish to populate which are not present in the souce but present in the target, can be filled using End Routines. End routine alone can replace start routines as well as individual routines in some cases.

Expert Routine
The expert routine completely replaces the graphical modelling process and enables you to convert each data package from the source into the target structure in a single routine.

Transformation
The transformation contains a total of five abap exits
1. 2. 3. 4. 5. Exit for determining key figures and data fields Exit for determining characteristics and key fields Start Routine End Rountine Expert Routing

Start Routine
Start Routine is available with Transformation. It is triggered before Transformation. Generally Start routines are used for Filtering records or fetching global data to be used in Characteristic routine. Click on Start Routine button to create Start Routine. SOURCE_PACKAGE contains contents of source

The start routine is run for each data package at the start of the update. The start routine has only these parameters: MONITOR, MONITOR_RECNO, DATA_PACKAGE, RECORD_ALL, SOURCE_SYSTEM and ABORT. DATA_PACKAGE refers to the entire data package. The start routine does not have a return value. It is used to perform preliminary calculations and store these in a global data structure or in a table. You can access this structure or table from other routines.

Start Routine Example


Use the following statement *$*$ begin of routine - insert your code only below this line * fill the internal tables "MONITOR" and/or "MONITOR_RECNO", * to make monitor entries TABLES: /bic/AZOLAMTOT00. DATA : lt_ZOLAMTOT like /bic/aZOLAMTOT00 occurs 0 with header line. SELECT * from /bic/aZOLAMTOT00 into table lt_ZOLAMTOT for all entries in data_package where /bic/aZOLAMTOT00-/bi0/company and /bic/aZOLAMTOT00-/bi0/chrt_accts and /bic/aZOLAMTOT00-/bi0/gl_account and /bic/aZOLAMTOT00-/bi0/ref_key3 AND /bic/aZOLAMTOT00-ztotal 0.

= data_package-/bi0/company = data_package-/bi0/chrt_accts = data_package-/bi0/gl_account = data_package-/bi0/ref_key3

sort lt_zolamtot by /bi0/company /bi0/chrt_accts /bi0/gl_account /bi0/ref_key3. DELETE ADJACENT DUPLICATES FROM lt_zolamtot comparing /bi0/company /bi0/chrt_accts /bi0/gl_account /bi0/ref_key3. loop at data_package. read table lt_zolamtot with key /bi0/company =data_package-/bi0/company /bi0/chrt_accts=data_package-/bi0/chrt_accts /bi0/gl_account=data_package-/bi0/gl_account /bi0/ref_key3=data_package-/bi0/ref_key3 BINARY SEARCH. if sy-subrc ne 0. delete data_package. else. data_package-/bic/ztotal = lt_zolamtot-/bic/ztotal. data_package-/bi0/loc_currcy = lt_zolamtot-/bi0/loc_currcy. endif. endloop. * if abort is not equal zero, the update process will be canceled ABORT = 0.

Characteristic Routine
Characteristic Routine is inside the Transformation. This routine will be triggered inside the Transformation based on the characteristic or key figure.

Characteristic routine is executed after Start routine and before the End Routine. All global objects declared in Start Routine are available in Characteristic Routine.

End Routine
End Routine is available with Transformation. It is triggered after Transformation. Generally End user is used for updating data based on existing data. The End Routine clearly mirrors the start routine, the only difference in the code is that the name SOURCE_PACKAGE is RESULT_PACKAGE. Click on End Routine button to create End Routine. RESULT_PACKAGE contains process data i.e. processed via start routine and transformation. RESULT_PACKAGE has same structure as that of target Object.

Expert Routine
Using an Expert Routine has advantages for system performance. If you need a Start Routine and an End Routine then use an Expert Routine instead.

Expert Routine
Expert Routine
To create an Expert routine go to Edit menu and select Expert Routine. Expert Routine will trigger without any transformation Rule. All existing Rules will be deleted once you develop Expert Routine. This is used generally for customizing rules.

Source_Package
SOURCE_PACKAGE has same structure as that of Source of the Transformation. RESULT_PACKAGE has same structure as that of target Object. You can manipulate values from SOURCE_PACKAGE and append them in RESULT_PACKAGE.

End Routine Example


Click below to view an example End Routine

End Routine Sample

Performance tips
ABAP for a BI Consultant is usually limited to implementing the logic and making it work out. BI Consultants are rarely concerned about the performance while implementing the Business Logic in Routines. The consequences of these are faced by the Support Team who dont understand why the Data load is taking so much time in Production. So for a successful BI Implementation it is mandatory that we also consider the performance aspect while writing ABAP Logic. Performance does matter when it comes to loading large volumes of data in the Production System. This document gives you an overview of the best practices which should be followed while writing an ABAP Code considering the performance.

Performance tips
Deleting a Record
Use the following statement Delete source_package where F1 = X. Dont use this Loop at source_package assigning <source_fields>. If <source_fields>-F1 = X. Delete source_package. Endif. Endloop.

Sometimes you might come across a requirement when you want to delete records for multiple values. Eg. I would like to delete all records from source_package where F1 = ABCD and EFGH. In that case you can use the SELECT-OPTIONS type. Use the following statement DATA : TEMP type F1. i.e. the field for which we are creating SELECT-OPTIONS SELECT-OPTIONS : s_F1 for temp. DATA : wa like line of s_F1. wa-sign = 'I'. wa-option = 'EQ'. wa-low = 'ABCD'. append wa to s_F1. wa-sign = 'I'. wa-option = 'EQ'. wa-low = 'EFGH'. append wa to s_F1. Delete source_package where F1 in s_f1.

Range tables provide an easier method to either add or remove values we want.

Sample Code # 1
Sample Code - Splitting 60+ char string into multiple IO Consider an example of character string of 120 Characters fetched via Datasource. As BI has character limit of 60 for an InfoObject, we need to split this string into two InfoObjects. Following is the characteristic code routine for these InfoObjects. Code for InfoObject 1 Characteristic routine: *---Read first 60 characters from the string and assign to Result MOVE SOURCE_FIELDS-STRING+0(60) TO RESULT. Code for InfoObject 2 Characteristic routine: *---Read 60-119 characters from the string and assign to Result MOVE SOURCE_FIELDS-STRING+60(60) TO RESULT.

Sample Code #2
Sample code - Remove Special Characters
Most data loading related errors in BI are due to some special character in the Datasource string. Following code is helpful in avoiding such errors by removing special characters from the string.
Use the following statement* Assign source field to Result RESULT = SOURCE_FIELDS-ZZLIFEX. *Convert string into Upper case TRANSLATE RESULT TO UPPER CASE. *Conditional loop on String (i.e. Result) till special characters are found in string WHILE RESULT CN '%&()"''*+,-./:;<=>?_#~@!$^[]{}0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ '. *Replace special character found with none i.e. remove special character * sy-fdpos -> Found location for search in byte-type and character-type data objects. RESULT+SY-FDPOS(1) = ' '. *Remove blank spaces from string by condensing it. CONDENSE RESULT. ENDWHILE. *If first character of string is a space or special character # then remove it. IF RESULT(1) = '#'. RESULT(1) = ' '. CONDENSE RESULT. ENDIF.

Note: Instead of above code you can also use SF_SPECIALCHAR_DELETE Function module to remove these special characters.

Useful Transactions
SE38 SE37 SM50 SM04 SM12 SE11 SE15 SE16 SM66 BAPI

Useful ABAP Programs & Function Modules in SAP BI http://wiki.sdn.sap.com/wiki/pages/viewpage.action?pageId=35458

Questions?
This presentation should provide the participant with a basic knowledge required to develop efficient ABAP code in BW.

Das könnte Ihnen auch gefallen