Internal Table – COLLECT Statement
July 7, 2011 Leave a comment
The COLLECT statement is use to sum up the field by categorize other fields as the different category.
For example, we have the following data.
| CODE | TYPE | VALUE |
| A0001 | K01 | 10 |
| A0002 | K01 | 20 |
| A0003 | K02 | 30 |
| A0003 | K02 | 5 |
| A0001 | K01 | 7 |
After we use COLLECT statement, we will get the following result.
| CODE | TYPE | VALUE |
| A0001 | K01 | 17 |
| A0002 | K01 | 20 |
| A0003 | K02 | 35 |
Example 1: CODE for 3 fields
REPORT ZBC_ITCOLLECT_3FLD. * Define the IT Line TYPES: BEGIN OF iLine, code(10) TYPE c, type(10) TYPE c, value TYPE i, END OF iLine. * Internal Table * Work area with Header Line DATA: it_code TYPE STANDARD TABLE OF iLine INITIAL SIZE 10 WITH HEADER LINE, wa_code TYPE iLine, wa_code_tmp TYPE iLine, sMsg TYPE string. PERFORM WRITE_TXT USING 'Section 1: Insert 5 records into the internal table.'. * COLLECT 1 it_code-code = 'A0001'. it_code-type = 'K01'. it_code-value = 10. COLLECT it_code. * COLLECT 2 it_code-code = 'A0002'. it_code-type = 'K01'. it_code-value = 20. COLLECT it_code. * COLLECT 3 it_code-code = 'A0003'. it_code-type = 'K02'. it_code-value = 30. COLLECT it_code. * COLLECT 4 it_code-code = 'A0003'. it_code-type = 'K02'. it_code-value = 5. COLLECT it_code. * COLLECT 5 it_code-code = 'A0001'. it_code-type = 'K01'. it_code-value = 7. COLLECT it_code. PERFORM WRITE_IT. FORM WRITE_IT. LOOP AT it_code INTO wa_code_tmp. WRITE:/ wa_code_tmp-code, wa_code_tmp-type, wa_code_tmp-value. ENDLOOP. SKIP. ENDFORM. FORM WRITE_TXT USING sMsg. SKIP 2. WRITE: sMsg. ULINE /1(70). ENDFORM.
Result
Section 1: Insert 5 records into the internal table. ---------------------------------------------------------------------- A0001 K01 17 A0002 K01 20 A0003 K02 35
Example 2: Code for 2 fields and collect with empty statement.
REPORT ZBC_ITCOLLECT. * Define the IT Line TYPES: BEGIN OF iLine, code(10) TYPE c, section TYPE i, END OF iLine. * Internal Table * Work area with Header Line DATA: it_code TYPE STANDARD TABLE OF iLine INITIAL SIZE 10 WITH HEADER LINE, wa_code TYPE iLine, wa_code_tmp TYPE iLine, sMsg TYPE string. PERFORM WRITE_TXT USING 'Section 1: Insert 5 records into the internal table.'. DO 5 TIMES. it_code-code = SY-INDEX. it_code-section = SY-INDEX ** 2. COLLECT it_code. ENDDO. PERFORM WRITE_IT. PERFORM WRITE_TXT USING 'Section 2: Now add in another 5 records into the internal table and write out.'. DO 5 TIMES. it_code-code = SY-INDEX. it_code-section = SY-INDEX ** 3. WRITE:/ it_code-code, it_code-section. COLLECT it_code. ENDDO. PERFORM WRITE_TXT USING 'Section 3: This is the output for COLLECT.'. PERFORM WRITE_IT. sMsg = 'Section 4: Perform COLLECT in LOOP of 5'. PERFORM WRITE_TXT USING sMsg. DO 5 TIMES. COLLECT it_code. PERFORM WRITE_TXTH2 USING SY-INDEX. PERFORM WRITE_IT. ENDDO. FORM WRITE_IT. LOOP AT it_code INTO wa_code_tmp. WRITE:/ wa_code_tmp-code, wa_code_tmp-section. ENDLOOP. SKIP. ENDFORM. FORM WRITE_TXT USING sMsg. SKIP 2. WRITE: sMsg. ULINE /1(70). ENDFORM. FORM WRITE_TXTH2 USING sMsg. SKIP 1. WRITE: 9 'COLLECT', sMsg. ULINE /1(30). ENDFORM.
Result
Section 1: Insert 5 records into the internal table. ---------------------------------------------------------------------- 1 1 2 4 3 9 4 16 5 25 Section 2: Now add in another 5 records into the internal table and write out. ---------------------------------------------------------------------- 1 1 2 8 3 27 4 64 5 125 Section 3: This is the output for COLLECT. ---------------------------------------------------------------------- 1 2 2 12 3 36 4 80 5 150 Section 4: Perform COLLECT in LOOP of 5 ---------------------------------------------------------------------- COLLECT 1 ------------------------------ 1 2 2 12 3 36 4 80 5 275 COLLECT 2 ------------------------------ 1 2 2 12 3 36 4 80 5 400 COLLECT 3 ------------------------------ 1 2 2 12 3 36 4 80 5 525 COLLECT 4 ------------------------------ 1 2 2 12 3 36 4 80 5 650 COLLECT 5 ------------------------------ 1 2 2 12 3 36 4 80 5 775
In the second example, you can see that, the additional FORM is created. It’s just like a sub function, that we can call it from other part of our program, to reduce our code.
To declare:
FORM function_name [USING variable/structure,…]. … ENDFORM.
To call it:
PERFORM function_name [USING variable/structure,…].