Monday, April 28, 2008

SAP ABAP COMPUTE ADD COLLECT and APPEND

SAP ABAP COMPUTE ADD COLLECT and APPEND

COMPUTE The COMPUTE statement is used to assign the result of the mathematical statement to a variable. The use of COMPUTE statement is optional. Please examine the following program.

Data: d_result type i,
          d_num1 type i,
          d_num2 type i.


COMPUTE d_result = d_num1 + d_num2.

is the same as

d_result = d_num1 + d_num2.
 

Note: If you want to add 2 numbers then it can also be done as follows in SAP ABAP. 

add d_num1 to d_num2.
In this case the result is stored in d_num2.
COLLECT To Sum up the entries in the internal table you can use the COLLECT statement. The syntax is as shown below.
COLLECT wa INTO itab.
 

Please note that when using COLLECT you should ensure that all the fields that are not a part of the table key  should be numeric. This means that fields that are not part of the table key should be either f,i or p.

Example

DATA: BEGIN OF d_collect,
        key(5) TYPE c,
        num1(2) TYPE n,
        num2    TYPE i,
      END OF d_collect
.
DATA itab LIKE STANDARD TABLE OF d_collect.
d_collect-key = 'First'.
d_collect-num1 = '20'.
d_collect-num2 = '30'.
 
COLLECT d_collect into itab.
 
d_collect-key = 'First'.
d_collect-num1 = '20'.
d_collect-num2 = '15'.
 
COLLECT d_collect into itab.
 
d_collect-key = 'Second'.
d_collect-num1 = '20'.
d_collect-num2 = '15'.
COLLECT d_collect into itab.
 

The result is as follows. 

After first collect

    First 20 30 >>>>> First 20 30  

After Second Collect

    First 20 15 >>>>> First 20 45  

After Third Collect

    Second 20 15 >>>>> First 20 45  
            Second 20 15  

Please make a not that the first and the third COLLECT statements act as insertion statements and they just append the row to the table.


APPEND

The APPEND statement will just insert lines in the internal table. See the example given below.

Example:

DATA: BEGIN OF d_append,
        key(5) TYPE c,
        num1(2) TYPE n,
        num2    TYPE i,
            END OF d_append
.
 
DATA itab LIKE STANDARD TABLE OF d_append.
 
d_append-key = 'First'.
d_append-num1 = '20'.
d_append-num2 = '30'.
 
APPEND d_append to itab.
 
d_append-key = 'First'.
d_append-num1 = '20'.
d_append-num2 = '15'.
 
APPEND d_append to itab.
 
d_append-key = 'Second'.
d_append-num1 = '20'.
d_append-num2 = '15'.
APPEND d_append to itab.

After first APPEND

    First 20 30 >>>>> First 20 30  

After Second APPEND

    First 20 15 >>>>> First 20 30  
            First 20 15  

After Third Collect

    Second 20 15 >>>>> First 20 30  
            First 20 15  
            Second 20 15  

No comments:

Post a Comment