Vous êtes sur la page 1sur 48

ABAP program is used frequently in BW in various places.

Most of the times, a ABAP program fits in the following places in the BW data flow: 1. In Transformation 1.a. Start Routine 1.b. End Routine 1.c. Field Routine 1.d. Expert Routine 2.Customer exits These exits are 3. User exits written in the CMOD 4. Enhancement of a data source 5. Creation of a new data source via a functional module 6. Filters in DTP Level and Info package 7. Routines for info objects 8.In APDs to format the output file or for any other purpose Well look into the basics of the ABAP and have a look at the following areas: 1.data declaration of different types (basic data types ,table ,work area ) 2. LOOP statements 3. IF-ELSE decision control statements 4. use of work area 5. reading from table 6. Different types of statements like TYPES, DATA, CONSTANTS, PARAMETERS, TABLES ,use of SELECTION SCREEN (mainly in report program needed) Control statements and loops IF ... ELSEIF ... ELSE ... ENDIF CASE ... ENDCASE CHECK DO ... ENDDO WHILE ... ENDWHILE LOOP ... ENDLOOP 7.Operational statements MOVE, ADD, SUBTRACT, DIVIDE String operation : SEARCH, REPLACE, CONCATENATE, CONDENSE Open SQL : SELECT, INSERT, UPDATE, DELETE, MODIFY Operations on Internal table : READ TABLE, INSERT, UPDATE, DELETE, MODIFY SORT, DELETE ADJACENT DUPLICATES, REFRESH

Copyright 2013-2018, Saurabh Ojha

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.

Copyright 2013-2018, Saurabh Ojha

TABLE

Change table

WORK AREA

Read table

Copyright 2013-2018, Saurabh Ojha

o You can consider internal tables like a multi dimensional array in SAP ABAP. Its a temporary table created and filled by pro gram execution, ends with Program completion. Internal table contents are stored in RAM of application server. There are 3 types of internal tables:

Standard Internal table. Sorted Internal table. Hashed Internal table.


Standard internal tables can be accessed via index or key. Index means row number(s). Non unique entries can be present in the Internal table. Linear search by default is applied when searching/accessing via key. Standard table declaration: DATA: it_ekpo type standard table of ty_ekpo. Sorted internal table contents are sorted and contents can be accessed via index or key, Binary search is employed when searching via key. Non unique entries can be present. Sorted Table Declaration DATA: it_ekpo type sorted table of ty_ekpo with unique key ebeln. Hashed internal table contents are always unique. Table can be accessed only via key. Hash algorithm is used to organize the contents . Hashed Table Declaration DATA: it_ekpo type hashed table of ty_ekpo with unique key ebeln.

Copyright 2013-2018, Saurabh Ojha

Creating Internal Tables

1. By Using the Type Statement: Well create an Internal table Itab using the statement TYPES: Types: Begin of Temp, Column 1 type C, Column 2 type C, End of Temp. This actually creates a structure Temp. We can now create a internal table using this structure: Data: Itab type standard table of Temp, Wa_itab type Temp.

ITAB is created as internal table and WA_ITAB is created as work area.


Copyright 2013-2018, Saurabh Ojha

2. By referring to another table:


Internal table can also be created by referring to another standard table, z table or another internal table.

The syntax is : DATA<f><type>[with header line] Eg: DATA itab LIKE _t_s_ty _t_s_ty is an internal table, which has been referred here. This creates a new internal table of type _t_s_ty. 3. By referring to another structure:
Internal table can also be created by referring to another structure.

DATA <f> LIKE <struct> occurs n [with header line] Eg: DATA itab TYPE sline. This creates a internal table like the structure Sline. If the reference after the TYPE statement is of an structure or a transparent table, a work area is created and not an internal table. IMP: The keyword TYPE is used to refer to already existing data type. The keyword LIKE is used to copy the properties of already existing data object.

TYPE usually refers to the data types created using the TYPES statement.
Copyright 2013-2018, Saurabh Ojha

Populating internal tables Append data line by line : Using the append statement, we can append one row from another work area Or we can add one initial line to the internal table

SYNTAX: APPEND [<WA> TO/ INITIAL LINE TO]<itable>


The system variable SY-TABIX contains the index of the appended line.

Initial Line adds a line to the internal table maintaining the correct data type. The Character data type is filled with space while the integers are filled with 0.

Append wa_itab to itab OR Append initial line to itab.

Copyright 2013-2018, Saurabh Ojha

Using the INSERT Statement


The insert statement is used to add a new line to an internal table. The line is added at the Mentioned index. The use of index is optional.

INSERT LINES OF it_tab1 INTO it_tab2 index 1. *INSERT LINES OF is usually used to copy the contents of one internal table to another. This will insert the records at the position mentioned via index. Existing records will be shifted Accordingly.

INSERT i_tab1 into i_tab2

In the above statement, the index has not been mentioned explicitly. In such case the, the line Number is automatically decided based on the table type:

1. Standard tables: The line is appended to the end of the internal table. This has same affect As the APPEND statement. 2. Sorted tables: The new line is inserted automatically at the right place according to the key of the table. 3. Hashed Tables: The new records are added according to the hash administration of the new Table.
Copyright 2013-2018, Saurabh Ojha

Using the COLLECT statement

COLLECT is another statement that is used to add records to an internal table. It is generally used when inserting records into a table with unique key. SYNTAX : COLLECT [<wa>INTO]<table> In the COLLECT statement, A new row is added to the target table. However, if the key Already exists, then the numerical values are added and the characteristics are left Unchanged. MOVE Statement MOVE statement can also be used to copy the content of one internal table to another

MOVE <i_tab1> to <i_tab2> OR i_tab1 = i_tab2 OR i_tab1[] = i_tab2[]


Copyright 2013-2018, Saurabh Ojha

Reading Internal Tables

Using LOOP-ENDLOOP: SYNTAX: LOOP at i_tab into wa_i_tab When the LOOP ENDLOOP statement is used, the internal table is read line by line. The value of the SY-SUBRC is set to 0 even if only one line is read.

Using READ Statement: SYNTAX: READ TABLE [INTO Workarea] [INDEX | WITH KEY] In the READ statement, both the INDEX and the KEY can be mentioned, but both cannot Be mentioned at the same time. When the INDEX is mentioned, the record at that index number is read and the SY-SUBRC is Set to 0.
Copyright 2013-2018, Saurabh Ojha

DELETING INTERNAL TABLES

1. Deleting within LOOP: The simplest way to delete the internal table is by using the DELETE statement. This is usually works in a loop and deletes the current line. Where clause can also be added to delete selected line. SYNTAX: DELETE <i_table>. 2. Deleting Lines Using the Index SYNTAX: DELETE <i_table> INDEX <IDX>

Copyright 2013-2018, Saurabh Ojha

DESCRIBE Statement This statement determines some properties of the internal table and assigns them to the Variables. We can also determine the table type, number of rows currently filled and the Initial memory requirement.

SYNTAX: DESCRIBE TABLE itab [KIND knd] [LINES lin] [OCCURS n].
If an addition is not specified, the DESCRIBE TABLE statement sets system fields sy-tfill and sy-tleng only.

Variable for Kind: sydes_kind-standard, sydes_kind-sorted, and sydes_kind-hashed.

Copyright 2013-2018, Saurabh Ojha

Copyright 2013-2018, Saurabh Ojha

START ROUTINE
INTERFACE METHODS start_routine IMPORTING request type rsrequest datapackid type rsdatapid segid type rsbk_segid EXPORTING monitor type rstr_ty_t_monitors CHANGING SOURCE_PACKAGE type _ty_t_SC_1 RAISING cx_rsrout_abort cx_rsbk_errorcount. REQUEST Request ID of load in progress DATAPACKID data package ID (i.e. 1, 2, 3) MONITOR Messaging mechanism for Transformations, place informational and error messages inside the structure, it will then be displayed from within the monitor. SOURCE_PACKAGE (formerly DATA_PACKAGE) Contains all of the data being passed into the Transformation from the specified source CX_RSROUT_ABORT Class Exception when raised will cause the transformation process to halt.

Copyright 2013-2018, Saurabh Ojha

The start routine is run for each data package at the start of the transformation. The start routine has a table in the format of the source structure as input and output parameters. It is used to perform preliminary calculations and store these in a global data structure or in a table. This structure or table can be accessed from other routines. You can modify or delete data in the data package.

Copyright 2013-2018, Saurabh Ojha

Field Routines Field routine is written for a particular characteristic or key figure and it runs for every Record that is loaded from source to target and acts as a rule for that particular info object In the transformation.

Copyright 2013-2018, Saurabh Ojha

METHODS compute_0FAX_NUM IMPORTING request type rsrequest datapackid type rsdatapid SOURCE_FIELDS type _ty_s_SC_1 segid type RSBK_SEGID record type RSARECORD EXPORTING RESULT type _ty_s_TG_1-FAX_NUM monitor type rstr_ty_t_monitor RAISING cx_rsrout_abort cx_rsrout_skip_record cx_rsrout_skip_val cx_rsbk_errorcount. METHODS invert_0FAX_NUM IMPORTING i_th_fields_outbound TYPE rstran_t_field_inv i_r_selset_outbound TYPE REF TO cl_rsmds_set i_is_main_selection TYPE rs_bool i_r_selset_outbound_complete TYPE REF TO cl_rsmds_set i_r_universe_inbound TYPE REF TO cl_rsmds_universe CHANGING c_th_fields_inbound TYPE rstran_t_field_inv c_r_selset_inbound TYPE REF TO cl_rsmds_set c_exact TYPE rs_bool. ENDCLASS. "routine DEFINITION Copyright 2013-2018, Saurabh Ojha

END Routine
An end routine is a routine with a table in the target structure format as input and output parameters. You can use an end routine to post process data after transformation on a package-by-package basis. For example, you can delete records that are not to be updated, or perform data checks.

Copyright 2013-2018, Saurabh Ojha

METHODS new_record__end_routine IMPORTING source_segid type rstran_segid source_record type sytabix EXPORTING record_new type sytabix. METHODS end_routine IMPORTING request type rsrequest datapackid type rsdatapid segid type rsbk_segid EXPORTING monitor type rstr_ty_t_monitors CHANGING RESULT_PACKAGE type _ty_t_TG_1 RAISING cx_rsrout_abort cx_rsbk_errorcount. METHODS inverse_end_routine IMPORTING i_th_fields_outbound TYPE rstran_t_field_inv I_R_SELSET_OUTBOUND TYPE REF TO CL_RSMDS_SET i_is_main_selection TYPE rs_bool i_r_selset_outbound_complete TYPE REF TO cl_rsmds_set i_r_universe_inbound TYPE REF TO CL_RSMDS_UNIVERSE CHANGING c_th_fields_inbound TYPE rstran_t_field_inv c_r_selset_inbound TYPE REF TO CL_RSMDS_SET c_exact TYPE rs_bool. ENDCLASS. "routine DEFINITION Copyright 2013-2018, Saurabh Ojha

Raising Error EXPORTING monitor

type rstr_ty_t_monitors

Copyright 2013-2018, Saurabh Ojha

Structure of RSMONITOR

Copyright 2013-2018, Saurabh Ojha

Creation of custom Message


Go to T-CODE SE91 and create message class. A screenshot is given below:

Copyright 2013-2018, Saurabh Ojha

Here the short text for the message is entered along with the message number

Copyright 2013-2018, Saurabh Ojha

IF SOURCE_FIELDS-/BIC/ZME_CUST = 'C001'. RESULT = 'D01'. ELSEIF SOURCE_FIELDS-/BIC/ZME_CUST = 'C002'. RESULT = 'D02'. ELSE. MONITOR_REC-MSGID = 'ZCL_MSG'. MONITOR_REC-MSGTY = 'E'. MONITOR_REC-MSGNO = '000'. MONITOR_REC-MSGV1 = 'ERROR, D/C Indicator'. MONITOR_REC-MSGV2 = SOURCE_FIELDS/BIC/ZME_CUST. MONITOR_REC-MSGV3 = SOURCE_FIELDS/BIC/ZME_ADDR. append monitor_rec to MONITOR. ENDIF.

Copyright 2013-2018, Saurabh Ojha

ABAP code in Infopackage to create or Automate filters

The SAP BW gives us the flexibility of writing ABAP code in the Infopackage. This code is used to create filter based on certain characteristic This can also be used to automate certain filter criteria like 0CALMONTH. Variables can also be used to create filters for the Infopackage. In the DATA SELECTION Tab of the infopackage, you have to select the variable for which you are going to create the Variable:

Once the Variable is selected, click on the select button that appears on the right edge( It has Been encircled in red color).

Copyright 2013-2018, Saurabh Ojha

Once, you click on the select button, youll get two options to select from: ABAP Variable And OLAP Variable. Both of these will be taken here. 1. ABAP variable:- Select 6 from the option to create a ABAP variable as shown in the following Screenshot. Youll get a pop up to provide a name to the code. It does not appear any where.

Once, you enter the name of the routine and click on the editor, the Editor opens up, where You can write the code for the variables.

Copyright 2013-2018, Saurabh Ojha

Copyright 2013-2018, Saurabh Ojha

The routine that has been written to calculate the SAP division. The logic of the calculation Is: If the current month is an even month, the division is automatically taken as 40.However, If the month is odd, nothing is done and division is not restricted.

The values are passed to the various fields of the table l_t_range . This table is assigned the Structure of RSSDLRANGE. The structure has the following fields:

Copyright 2013-2018, Saurabh Ojha

Copyright 2013-2018, Saurabh Ojha

Copyright 2013-2018, Saurabh Ojha

Copyright 2013-2018, Saurabh Ojha

Copyright 2013-2018, Saurabh Ojha

Copyright 2013-2018, Saurabh Ojha

Copyright 2013-2018, Saurabh Ojha

The selection of the Cal month can be seen in the following screenshot:

Copyright 2013-2018, Saurabh Ojha

Copyright 2013-2018, Saurabh Ojha

Copyright 2013-2018, Saurabh Ojha

*----------- End of type definitions -------------------------------FORM compute_data_transformation USING it_source TYPE yt_source_fields ir_context TYPE REF TO if_rsan_rt_routine_context EXPORTING et_target TYPE yt_target_fields . *--------- Begin of transformation code ----------------------------DATA: ls_source TYPE y_source_fields, ls_target TYPE y_target_fields, GRVAL TYPE STRING, GSVAL TYPE STRING, SFQTY TYPE STRING, PDQTY TYPE STRING, GSQTY TYPE STRING, GSQTY_Q TYPE /BIC/OIZVKRBQTY, GSQTY_V TYPE /BIC/OIZVKGSVAL, RBQTY TYPE STRING. LOOP AT it_source INTO ls_source. CLEAR: RBQTY,GRVAL,PDQTY,SFQTY. ls_target-year = ls_source-calmonth(4). ls_target-month = ls_source-calmonth+2. if ls_source-ZVKRBQTY < '0.00' . RBQTY = ls_source-ZVKRBQTY . SHIFT RBQTY right deleting trailing '-'. SHIFT RBQTY LEFT DELETING LEADING ' '. concatenate '-' RBQTY into ls_target-ZKRBQTY. else. ls_target-ZKRBQTY = ls_source-ZVKRBQTY . SHIFT ls_target-ZKRBQTY LEFT DELETING LEADING ' '. endif. if ls_source-ZVKGRVAL < '0.00' . GRVAL = ls_source-ZVKGRVAL . SHIFT GRVAL right deleting trailing '-'. SHIFT GRVAL LEFT DELETING LEADING ' '. concatenate '-' GRVAL into ls_target-ZKGRVAL. else. ls_target-ZKGRVAL = ls_source-ZVKGRVAL . SHIFT ls_target-ZKGRVAL LEFT DELETING LEADING ' '. endif. " Pending order qty if ls_source-ZVKPNQTY < '0.00' . PDQTY = ls_source-ZVKPNQTY . SHIFT PDQTY right deleting trailing '-'. SHIFT PDQTY LEFT DELETING LEADING ' '. concatenate '-' PDQTY into ls_target-ZKPNQTY. else. ls_target-ZKPNQTY = ls_source-ZVKPNQTY . SHIFT ls_target-ZKPNQTY LEFT DELETING LEADING ' '. endif.

Copyright 2013-2018, Saurabh Ojha

" Sales Free qty


if ls_source-ZVKSFQTY < '0.00' . SFQTY = ls_source-ZVKSFQTY . SHIFT SFQTY right deleting trailing '-'. SHIFT SFQTY LEFT DELETING LEADING ' '. concatenate '-' SFQTY into ls_target-ZKSFQTY. else. ls_target-ZKSFQTY = ls_source-ZVKSFQTY . SHIFT ls_target-ZKSFQTY LEFT DELETING LEADING ' '. endif. ls_target-ZKGSQTY = ls_source-ZVKSBQTY + ls_source-ZVKRBQTY + ls_source-ZVKCLQTY. SHIFT ls_target-ZKGSQTY LEFT DELETING LEADING ' '. GSQTY_Q = ls_target-ZKGSQTY. if GSQTY_Q < 0. ls_target-ZKGSQTY = ls_target-ZKGSQTY * -1. SHIFT ls_target-ZKGSQTY LEFT DELETING LEADING ' '. concatenate '-' ls_target-ZKGSQTY into ls_target-ZKGSQTY. endif. ls_target-ZKGSVAL = ls_source-ZVKGSVAL + ls_source-ZVKGRVAL + ls_source-ZVKGDNVAL + ls_source-ZVKGCNVAL + ls_source-ZVKCLMB + ls_source-ZVKCLAD + ls_source-ZVKCLPD. SHIFT ls_target-ZKGSVAL LEFT DELETING LEADING ' '. GSQTY_V = ls_target-ZKGSVAL. if GSQTY_V < 0 . ls_target-ZKGSVAL = ls_target-ZKGSVAL * -1. SHIFT ls_target-ZKGSVAL LEFT DELETING LEADING ' '. concatenate '-' ls_target-ZKGSVAL into ls_target-ZKGSVAL. endif. MOVE-CORRESPONDING ls_source TO ls_target. APPEND ls_target TO et_target. ENDLOOP.

*---------- End of transformation code -----------------------------ENDFORM.

Copyright 2013-2018, Saurabh Ojha

Copyright 2013-2018, Saurabh Ojha

The above is an example of the routine for an info object. This type of routine is used When we want a particular routine to run at all the times whenever data is loaded to a Particular info object. Once activated, the routine runs irrespective of the place where the data is loaded into the Info object. This saves us the effort of adding the same routine every time we write a Transformation.

Copyright 2013-2018, Saurabh Ojha

Example of a end routine


Scenario: Following is the data that is coming from an excel file:
MATERIAL
100

YEAR
2013

CODE
XX

200

2013

YY

The excel file contains records that has material, year and code. There will be only one row For a particular code, material and year. The requirement is to create and add records for each calendar day in a year for each record In the excel sheet.

Copyright 2013-2018, Saurabh Ojha

How this is achieved:


A data source is created to get the data from the said excel file:

Copyright 2013-2018, Saurabh Ojha

A DSO has been created to get the data from the data source, it is in this DSO That well add the new field 0calday that is to be calculated in the end routine and Populated in the DSO. The DSO has the properties as follows:

The key of the DSO is code, 0calday, material and the calyear.
Copyright 2013-2018, Saurabh Ojha

The transformation between the data source and the DSO:

Copyright 2013-2018, Saurabh Ojha

DATA: LT_PACKAGE type standard table of _ty_s_TG_1 , CT_PACKAGE TYPE STANDARD TABLE OF _ty_s_TG_1, LS_PACKAGE type _ty_s_TG_1, LG_PACKAGE TYPE _ty_s_TG_1. DATA: Z_LASTDAY type sy-datum, z_lastchar(8) type C, Z_FIRSTDAY type sy-datum, z_firstchar(8) type C, Z_NEXTDAY type sy-datum. data: flag type n, HIGH TYPE N, RECORD_NO type rsarecord. LT_PACKAGE[] = RESULT_PACKAGE[]. loop at LT_PACKAGE INTO LS_PACKAGE . CONCATENATE LS_PACKAGECALYEAR '12' '31' INTO z_lastchar. Z_LASTDAY = z_lastchar. CONCATENATE Z_LASTDAY+0(4) '01' '01' INTO z_firstchar. Z_FIRSTDAY = z_firstchar. Z_NEXTDAY = Z_FIRSTDAY. flag = 0. DO 365 TIMES. IF Z_NEXTDAY+6(2) = '01' and flag = 0. Z_NEXTDAY = Z_FIRSTDAY. flag = 1. LS_PACKAGE-CALDAY = Z_NEXTDAY . LS_PACKAGE-RECORD = RECORD_NO + 1. APPEND LS_PACKAGE TO CT_PACKAGE. else. Z_NEXTDAY = Z_NEXTDAY + 1. LS_PACKAGE-CALDAY = Z_NEXTDAY . LS_PACKAGE-RECORD = RECORD_NO + 1. APPEND LS_PACKAGE TO CT_PACKAGE. ENDIF. ENDDO. ENDLOOP. CLEAR RESULT_PACKAGE[]. RESULT_PACKAGE[] = CT_PACKAGE[].

In case a new record is inserted into the target object in an end routine, it is necessary to maintain the record number correctly. In the present code, you can see that the record no has been first initialized to 0 and then incremented with each record.

Copyright 2013-2018, Saurabh Ojha

This is output of the DSO after the data load, It can be seen that a record for each day of the year has been inserted in to the DSO

Copyright 2013-2018, Saurabh Ojha

Field symbols
Field symbols are placeholders or symbolic names for other fields. They do not physically reserve space for a field, but point to its contents. A field symbol cam point to any data object. The data object to which a field symbol points is assigned to it after it has been declared in the program.

Copyright 2013-2018, Saurabh Ojha

Vous aimerez peut-être aussi