Former Member

# what is the difference between SUM and COLLECT

give some examples...

Thanks

10|10000 characters needed characters exceeded

• Former Member
Posted on Sep 28, 2007 at 08:37 AM

Hi,

SUM.

Effect

When processing an internal table in a block starting with LOOP and concluded by ENDLOOP , SUM calculates the control totals of all fields of type I , F and P (see also ABAP/4 number types ) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).

You can use the SUM statement both at the end and the beginning of a control group (see also AT FIRST/LAST ).

Example

Display the table T with sub-totals:

DATA: BEGIN OF T OCCURS 100,

CODE(4),

SALES TYPE P,

DISCOUNT TYPE P,

END OF T.

...

LOOP AT T.

AT FIRST.

SUM.

WRITE: /4 'Grand Total:',

20 T-SALES, 40 T-DISCOUNT.

ULINE. SKIP.

ENDAT.

WRITE: / T-CODE,

20 T-SALES, 40 T-DISCOUNT.

AT END OF CODE.

SUM.

WRITE: / T-CODE, 10 'Total:',

20 T-SALES, 40 T-DISCOUNT.

SKIP.

ENDAT.

ENDLOOP.

Notes

When you use SUM in a LOOP with an explicitly specified output area, this output area must be compatible with the line type of the internal table.

When using LOOP to process a sorted extract (see SORT ), the control total of f at the end of the group appears in the field SUM(f) - - if f is type I , F or P .

Note

Runtime errors

SUM_OVERFLOW : Value too large when calculatng totals in internal table, field too small.

SUM_NO_INTERNAL_TABLE : The SUM statement was used outside a LOOP on an internal table.

SUM_ON_FOREIGN_INTERNAL_TABLE : The SUM statement was used in a LOOP belonging to another ABAP/4 program.

COLLECT

Basic form

COLLECT [wa INTO] itab.

... SORTED BY f

Effect

COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .

If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.

If, besides its default key fields, the internal table contains number fields (see also ABAP/4 number types ), the contents of these number fields are added together if the internal table already contains an entry with the same key fields.

If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.

If you specify wa INTO , the entry to be processed is taken from the explicitly specified work area wa . If not, it comes from the header line of the internal table itab .

After COLLECT , the system field SY-TABIX contains the index of the - existing or new - table entry with default key fields which match those of the entry to be processed.

Notes

COLLECT can create unique or compressed datasets and should be used precisely for this purpose. If uniqueness or compression are unimportant, or two values with identical default key field values could not possibly occur in your particular task, you should use APPEND instead. However, for a unique or compressed dataset which is also efficient, COLLECT is the statement to use.

If you process a table with COLLECT , you should also use COLLECT to fill it. Only by doing this can you guarantee that

the internal table will actually be unique or compressed, as described above and

COLLECT will run very efficiently.

If you use COLLECT with an explicitly specified work area, it must be compatible with the line type of the internal table.

Example

Compressed sales figures for each company

DATA: BEGIN OF COMPANIES OCCURS 10,

NAME(20),

SALES TYPE I,

END OF COMPANIES.

COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 10.

COLLECT COMPANIES.

COMPANIES-NAME = 'Tiger'. COMPANIES-SALES = 20.

COLLECT COMPANIES.

COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 30.

COLLECT COMPANIES.

The table COMPANIES now has the following appearance:

NAME SALES

Duck 40

Tiger 20

... SORTED BY f

Effect

COLLECT ... SORTED BY f is obsolete and should no longer be used. Use APPEND ... SORTED BY f which has the same meaning.

Note

Performance

The cost of a COLLECT in terms of performance increases with the width of the default key needed in the search for table entries and the number of numeric fields with values which have to be added up, if an entry is found in the internal table to match the default key fields.

If no such entry is found, the cost is reduced to that required to append a new entry to the end of the table.

A COLLECT statement used on a table which is 100 bytes wide and has a key which is 60 bytes wide and seven numeric fields is about approx. 50 msn (standardized microseconds).

Note

Runtime errors

COLLECT_OVERFLOW : Overflow in integer field when calculating totals.

COLLECT_OVERFLOW_TYPE_P : Overflow in type P field when calculating totals.

Related APPEND , WRITE ... TO , MODIFY , INSERT

Thanks,

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 28, 2007 at 08:39 AM
10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 28, 2007 at 08:39 AM

hi

SUM means .

ex :

data : a type i,

b type i,

c type i.

a = 5.

b = 5.

sum a b to c.

output:

c= 10.

COLLECT :

data : begin of wa_jesus,

v_no type i,

v_m1 type i,

v_m2 type i,

v_m3 type i,

v_tot type i,

end of wa_jesus.

data : it_jesus like hashed table of wa_jesus with header line with unique key v_no.

it_jesus-v_no = 1.

it_jesus-v_m1 = 100.

it_jesus-v_m2 = 100.

it_jesus-v_m3 = 100.

collect it_jesus.

it_jesus-v_no = 1.

it_jesus-v_m1 = 100.

it_jesus-v_m2 = 100.

it_jesus-v_m3 = 100.

collect it_jesus.

it_jesus-v_no = 1.

it_jesus-v_m1 = 100.

it_jesus-v_m2 = 100.

it_jesus-v_m3 = 100.

collect it_jesus.

loop at it_jesus.

write : / it_jesus-v_no,

it_jesus-v_m1,

it_jesus-v_m2,

it_jesus-v_m3.

endloop.

output.

1 300 300 300

COLLECT is used for samuation of all the fields in depent of v_no field..

i hope u can clear..

thanks

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 28, 2007 at 08:40 AM

Hi,

Sum:

You can only use this statement within a LOOP. If you use SUM in an AT - ENDAT block, the system calculates totals for the numeric fields of all lines in the current line group and writes them to the corresponding fields in the work area. If you use the SUM statement outside an AT - ENDAT block (single entry processing), the system calculates totals for the numeric fields of all lines of the internal table in each loop pass and writes them to the corresponding fields of the work area. It therefore only makes sense to use the SUM statement in AT...ENDAT blocks. If the table contains a nested table, you cannot use the SUM statement. Neither can you use it if you are using a field symbol instead of a work area in the LOOP statement.

Collect:

If an entry with the same key already exists, the COLLECT statement does not append a new line, but adds the contents of the numeric fields in the work area to the contents of the numeric fields in the existing entry.

You can also go through this:

SUM:

When processing an internal table in a block starting with LOOP and concluded by ENDLOOP , SUM calculates the control totals of all fields of type I , F and P (see also ABAP/4 number types ) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).

When you use SUM in a LOOP with an explicitly specified output area, this output area must be compatible with the line type of the internal table.When using LOOP to process a sorted extract (see SORT ), the control total

of f at the end of the group appears in the field SUM(f) - - if f is type I , F or P .

COLLECT:

COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .

If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.

If, besides its default key fields, the internal table contains number fields,the contents of these number fields are added together if the internal table already contains an entry with the same key fields.

If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.

If you specify wa INTO , the entry to be processed is taken from the explicitly specified work area wa . If not, it comes from the header line of the internal table itab .

After COLLECT , the system field SY-TABIX contains the index of the - existing or new - table entry with default key fields which match those of the entry to be processed.

COLLECT can create unique or compressed datasets and should be used precisely for this purpose. If uniqueness or compression are unimportant, or two values with identical default key field values could not possibly occur in your particular task, you should use APPEND instead. However, for a unique or compressed dataset which is also efficient, COLLECT is the statement to use.

If you process a table with COLLECT , you should also use COLLECT to fill it. Only by doing this can you guarantee that the internal table will actually be unique or compressed, as described above and COLLECT will run very efficiently.

If you use COLLECT with an explicitly specified work area, it must be compatible with the line type of the internal table.

******************************************************************

SUM.

Effect

When processing an internal table in a block starting with LOOP and concluded by ENDLOOP, SUM calculates the control totals of all fields of type I, F and P (see also ABAP number types) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).

You can use the SUM statement both at the end and the beginning of a control group (see also AT FIRST/LAST).

Example

Display the table T with sub-totals:

TYPES: BEGIN OF T_TYPE,

CODE(4),

SALES TYPE P,

DISCOUNT TYPE P,

END OF T_TYPE.

DATA: T TYPE STANDARD TABLE OF T_TYPE WITH NON-UNIQUE

DEFAULT KEY INITIAL SIZE 100,

WA_T TYPE T_TYPE.

...

LOOP AT T INTO WA_T.

AT FIRST.

SUM.

WRITE: /4 'Grand Total:',

20 WA_T-SALES, 40 WA_T-DISCOUNT.

ULINE. SKIP.

ENDAT.

WRITE: / WA_T-CODE,

20 WA_T-SALES, 40 WA_T-DISCOUNT.

AT END OF CODE.

SUM.

WRITE: / WA_T-CODE, 10 'Total:',

20 WA_T-SALES, 40 WA_T-DISCOUNT.

SKIP.

ENDAT.

ENDLOOP.

Example

Summarized sales figures by company:

COLLECT

Effect

COLLECT allows you to create unique or summarized datasets. The system first tries to find a table entry corresponding to the table key. (See also Defining Keys for Internal Tables). The key values are taken either from the header line of the internal table itab, or from the explicitly-specified work area wa. The line type of itab must be flat - that is, it cannot itself contain any internal tables. All the components that do not belong to the key must be numeric types ( ABAP Numeric Types).

If the system finds an entry, the numeric fields that are not part of the table key (see ABAPNumeric Types) are added to the sum total of the existing entries. If it does not find an entry, the system creates a new entry instead.

The way in which the system finds the entries depends on the kind of the internal table:

TYPES: BEGIN OF COMPANY,

NAME(20) TYPE C,

SALES TYPE I,

END OF COMPANY.

DATA: COMP TYPE COMPANY,

COMPTAB TYPE HASHED TABLE OF COMPANY

WITH UNIQUE KEY NAME.

COMP-NAME = 'Duck'. COMP-SALES = 10. COLLECT COMP INTO COMPTAB.

COMP-NAME = 'Tiger'. COMP-SALES = 20. COLLECT COMP INTO COMPTAB.

COMP-NAME = 'Duck'. COMP-SALES = 30. COLLECT COMP INTO COMPTAB.

Table COMPTAB now has the following contents:

NAME | SALES

-

Duck | 40

Tiger | 20

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 28, 2007 at 08:43 AM

hi

good

SUM

Basic form

SUM.

Effect

When processing an internal table in a block starting with LOOP and concluded by ENDLOOP , SUM calculates the control totals of all fields of type I , F and P (see also ABAP/4 number types ) and places them in the LOOP output area (header line of the internal table or an explicitly specified work area).

You can use the SUM statement both at the end and the beginning of a control group (see also AT FIRST/LAST ).

Example

Display the table T with sub-totals:

DATA: BEGIN OF T OCCURS 100,

CODE(4),

SALES TYPE P,

DISCOUNT TYPE P,

END OF T.

...

LOOP AT T.

AT FIRST.

SUM.

WRITE: /4 'Grand Total:',

20 T-SALES, 40 T-DISCOUNT.

ULINE. SKIP.

ENDAT.

WRITE: / T-CODE,

20 T-SALES, 40 T-DISCOUNT.

AT END OF CODE.

SUM.

WRITE: / T-CODE, 10 'Total:',

20 T-SALES, 40 T-DISCOUNT.

SKIP.

ENDAT.

ENDLOOP.

Notes

When you use SUM in a LOOP with an explicitly specified output area, this output area must be compatible with the line type of the internal table.

When using LOOP to process a sorted extract (see SORT ), the control total of f at the end of the group appears in the field SUM(f) - - if f is type I , F or P .

Note

Runtime errors

SUM_OVERFLOW : Value too large when calculatng totals in internal table, field too small.

SUM_NO_INTERNAL_TABLE : The SUM statement was used outside a LOOP on an internal table.

SUM_ON_FOREIGN_INTERNAL_TABLE : The SUM statement was used in a LOOP belonging to another ABAP/4 program.

COLLECT

Basic form

COLLECT [wa INTO] itab.

... SORTED BY f

Effect

COLLECT is used to create unique or compressed datsets. The key fields are the default key fields of the internal table itab .

If you use only COLLECT to fill an internal table, COLLECT makes sure that the internal table does not contain two entries with the same default key fields.

If, besides its default key fields, the internal table contains number fields (see also ABAP/4 number types ), the contents of these number fields are added together if the internal table already contains an entry with the same key fields.

If the default key of an internal table processed with COLLECT is blank, all the values are added up in the first table line.

If you specify wa INTO , the entry to be processed is taken from the explicitly specified work area wa . If not, it comes from the header line of the internal table itab .

After COLLECT , the system field SY-TABIX contains the index of the - existing or new - table entry with default key fields which match those of the entry to be processed.

Notes

COLLECT can create unique or compressed datasets and should be used precisely for this purpose. If uniqueness or compression are unimportant, or two values with identical default key field values could not possibly occur in your particular task, you should use APPEND instead. However, for a unique or compressed dataset which is also efficient, COLLECT is the statement to use.

If you process a table with COLLECT , you should also use COLLECT to fill it. Only by doing this can you guarantee that

the internal table will actually be unique or compressed, as described above and

COLLECT will run very efficiently.

If you use COLLECT with an explicitly specified work area, it must be compatible with the line type of the internal table.

Example

Compressed sales figures for each company

DATA: BEGIN OF COMPANIES OCCURS 10,

NAME(20),

SALES TYPE I,

END OF COMPANIES.

COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 10.

COLLECT COMPANIES.

COMPANIES-NAME = 'Tiger'. COMPANIES-SALES = 20.

COLLECT COMPANIES.

COMPANIES-NAME = 'Duck'. COMPANIES-SALES = 30.

COLLECT COMPANIES.

The table COMPANIES now has the following appearance:

NAME SALES

Duck 40

Tiger 20

... SORTED BY f

Effect

COLLECT ... SORTED BY f is obsolete and should no longer be used. Use APPEND ... SORTED BY f which has the same meaning.

Note

Performance

The cost of a COLLECT in terms of performance increases with the width of the default key needed in the search for table entries and the number of numeric fields with values which have to be added up, if an entry is found in the internal table to match the default key fields.

If no such entry is found, the cost is reduced to that required to append a new entry to the end of the table.

A COLLECT statement used on a table which is 100 bytes wide and has a key which is 60 bytes wide and seven numeric fields is about approx. 50 msn (standardized microseconds).

Note

Runtime errors

COLLECT_OVERFLOW : Overflow in integer field when calculating totals.

COLLECT_OVERFLOW_TYPE_P : Overflow in type P field when calculating totals.

Related APPEND , WRITE ... TO , MODIFY , INSERT

reward point if helpful.

thanks

mrutyun^

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 28, 2007 at 09:26 AM

Hi execute the following example and check out that how the sum and collect works for the same intenal table entries.

data: begin of itab occurs 0,

num1 type i,

num2(5) type c,

end of itab.

itab-num1 = '10'.

itab-num2 = 'CAR'.

append itab.

itab-num1 = '20'.

itab-num2 = 'HOME'.

append itab.

itab-num1 = '10'.

itab-num2 = 'CAR'.

append itab.

write:/ 'SUM'.

loop at itab.

sum.

write:/ itab-num1,itab-num2.

endloop.

free itab.

itab-num1 = '10'.

itab-num2 = 'CAR'.

collect itab.

itab-num1 = '30'.

itab-num2 = 'HOME'.

collect itab.

itab-num1 = '10'.

itab-num2 = 'CAR'.

collect itab.

write:/ 'COLLECT'.

loop at itab.

write:/ itab-num1,itab-num2.

endloop.

10|10000 characters needed characters exceeded
• Former Member
Posted on Sep 28, 2007 at 09:41 AM

hI

<b>SUM</b>

• ASSIGNMENT – LOOPING MANY ITABs – Inefficient.

TABLES: VBAK.

DATA: BEGIN OF I_SALES OCCURS 0,

V_VBELN LIKE VBAK-VBELN,

V_ERDAT LIKE VBAK-ERDAT,

V_ERNAM LIKE VBAK-ERNAM,

V_BUKRS_VF LIKE VBAK-BUKRS_VF,

END OF I_SALES.

DATA: BEGIN OF I_TITLE OCCURS 0,

V_BUKRS LIKE T001-BUKRS,

V_BUTXT LIKE T001-BUTXT,

V_ORT01 LIKE T001-ORT01,

END OF I_TITLE.

DATA: BEGIN OF I_MAIN OCCURS 0,

V_VBELN LIKE VBAP-VBELN,

V_ERDAT LIKE VBAK-ERDAT,

V_ERNAM LIKE VBAK-ERNAM,

V_POSNR LIKE VBAP-POSNR,

V_MATNR LIKE VBAP-MATNR,

V_KWMENG LIKE VBAP-KWMENG,

V_NETWR LIKE VBAP-NETWR,

END OF I_MAIN.

DATA: SUM TYPE I.

SUM = 0.

SELECT-OPTIONS S_VBELN FOR VBAK-VBELN.

START-OF-SELECTION.

SELECT VBELN ERDAT ERNAM BUKRS_VF FROM VBAK INTO TABLE I_SALES WHERE

VBELN IN S_VBELN.

IF NOT I_SALES[] IS INITIAL.

SELECT BUKRS BUTXT ORT01 ADRNR FROM T001 INTO TABLE I_TITLE FOR ALL

ENTRIES IN I_SALES WHERE BUKRS EQ I_SALES-V_BUKRS_VF.

SELECT VBELN ERDAT ERNAM POSNR MATNR KWMENG NETWR FROM VBAP INTO TABLE

I_MAIN FOR ALL ENTRIES IN I_SALES WHERE VBELN EQ I_SALES-V_VBELN.

ENDIF.

CALL FUNCTION 'OPEN_FORM'

EXPORTING

• APPLICATION = 'TX'

• ARCHIVE_INDEX =

• ARCHIVE_PARAMS =

• DEVICE = 'PRINTER'

DIALOG = 'X'

FORM = 'ZDANFORM3 '

LANGUAGE = SY-LANGU

• OPTIONS =

• MAIL_SENDER =

• MAIL_RECIPIENT =

• MAIL_APPL_OBJECT =

• RAW_DATA_INTERFACE = '*'

• IMPORTING

• LANGUAGE =

• NEW_ARCHIVE_PARAMS =

• RESULT =

• EXCEPTIONS

• CANCELED = 1

• DEVICE = 2

• FORM = 3

• OPTIONS = 4

• UNCLOSED = 5

• MAIL_OPTIONS = 6

• ARCHIVE_ERROR = 7

• INVALID_FAX_NUMBER = 8

• MORE_PARAMS_NEEDED_IN_BATCH = 9

• SPOOL_ERROR = 10

• CODEPAGE = 11

• OTHERS = 12

.

IF SY-SUBRC <> 0.

• MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

• WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

SORT I_MAIN BY V_VBELN.

LOOP AT I_MAIN.

AT NEW V_VBELN.

CALL FUNCTION 'START_FORM'

EXPORTING

• ARCHIVE_INDEX =

FORM = 'ZDANFORM3 '

LANGUAGE = SY-LANGU

STARTPAGE = 'FIRST'

PROGRAM = SY-REPID

• MAIL_APPL_OBJECT =

• IMPORTING

• LANGUAGE =

• EXCEPTIONS

• FORM = 1

• FORMAT = 2

• UNENDED = 3

• UNOPENED = 4

• UNUSED = 5

• SPOOL_ERROR = 6

• CODEPAGE = 7

• OTHERS = 8

.

IF SY-SUBRC <> 0.

• MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

• WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

ENDAT.

SUM = SUM + I_MAIN-V_NETWR.

• CODE TO POPULATE TITLE WINDOW.

LOOP AT I_TITLE.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

ELEMENT = 'TITLE'

• FUNCTION = 'SET'

• TYPE = 'BODY'

WINDOW = 'TITLE'

• IMPORTING

• PENDING_LINES =

• EXCEPTIONS

• ELEMENT = 1

• FUNCTION = 2

• TYPE = 3

• UNOPENED = 4

• UNSTARTED = 5

• WINDOW = 6

• BAD_PAGEFORMAT_FOR_PRINT = 7

• SPOOL_ERROR = 8

• CODEPAGE = 9

• OTHERS = 10

.

IF SY-SUBRC <> 0.

• MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

• WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

ENDLOOP.

• CODE TO POPULATE SALES WINDOW.

*LOOP AT I_SALES.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

ELEMENT = 'SALES'

• FUNCTION = 'SET'

• TYPE = 'BODY'

WINDOW = 'SALES'

• IMPORTING

• PENDING_LINES =

• EXCEPTIONS

• ELEMENT = 1

• FUNCTION = 2

• TYPE = 3

• UNOPENED = 4

• UNSTARTED = 5

• WINDOW = 6

• BAD_PAGEFORMAT_FOR_PRINT = 7

• SPOOL_ERROR = 8

• CODEPAGE = 9

• OTHERS = 10

.

IF SY-SUBRC <> 0.

• MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

• WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

*ENDLOOP.

• CODE TO POPULATE THE MAIN WINDOW i.e CUST ADDR.

LOOP AT I_TITLE.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

• FUNCTION = 'SET'

• TYPE = 'BODY'

• IMPORTING

• PENDING_LINES =

• EXCEPTIONS

• ELEMENT = 1

• FUNCTION = 2

• TYPE = 3

• UNOPENED = 4

• UNSTARTED = 5

• WINDOW = 6

• BAD_PAGEFORMAT_FOR_PRINT = 7

• SPOOL_ERROR = 8

• CODEPAGE = 9

• OTHERS = 10

.

IF SY-SUBRC <> 0.

• MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

• WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

ENDLOOP.

*CODE FOR POPULATING SALES DOCU DETAILS.

CALL FUNCTION 'WRITE_FORM'

EXPORTING

ELEMENT = 'ELE'

• FUNCTION = 'SET'

• TYPE = 'BODY'

WINDOW = 'MAIN'

• IMPORTING

• PENDING_LINES =

• EXCEPTIONS

• ELEMENT = 1

• FUNCTION = 2

• TYPE = 3

• UNOPENED = 4

• UNSTARTED = 5

• WINDOW = 6

• BAD_PAGEFORMAT_FOR_PRINT = 7

• SPOOL_ERROR = 8

• CODEPAGE = 9

• OTHERS = 10

.

IF SY-SUBRC <> 0.

• MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

• WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

AT END OF V_VBELN.

CALL FUNCTION 'END_FORM'

• IMPORTING

• RESULT =

• EXCEPTIONS

• UNOPENED = 1

• BAD_PAGEFORMAT_FOR_PRINT = 2

• SPOOL_ERROR = 3

• CODEPAGE = 4

• OTHERS = 5

.

IF SY-SUBRC <> 0.

• MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

• WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

ENDAT.

ENDLOOP.

CALL FUNCTION 'CLOSE_FORM'

• IMPORTING

• RESULT =

• RDI_RESULT =

• TABLES

• OTFDATA =

• EXCEPTIONS

• UNOPENED = 1

• BAD_PAGEFORMAT_FOR_PRINT = 2

• SEND_ERROR = 3

• SPOOL_ERROR = 4

• CODEPAGE = 5

• OTHERS = 6

.

IF SY-SUBRC <> 0.

• MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO

• WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.

ENDIF.

<u>SUM</u>

SUM.

The statement SUM can only be specified within a loop starting with LOOP, and is only considered within a AT-ENDAT control structure. Prerequisites for using the statement SUM include using the addition INTO in the LOOP statement, and that the specified work area wa is compatible with the row type of the internal table. In addition, SUM cannot be used when the row type of the internal table itab contains components that are tables.

The statement SUM calculates the component total with the numeric data type ( i, p, f) of all rows in the current control level and assigns these to the components of the work area wa. In the control levels FIRST, LAST , and outside of an AT-ENDAT control structure, the system calculates the sum of numeric components of all rows in the internal table.

Example

Control level processing for creating a list. At the end of line groups, the total of reserved places is calculated and issued.

DATA: sflight_tab TYPE SORTED TABLE OF sflight

WITH UNIQUE KEY carrid connid fldate,

sflight_wa LIKE LINE OF sflight_tab.

SELECT *

FROM sflight

INTO TABLE sflight_tab.

LOOP AT sflight_tab INTO sflight_wa.

AT NEW connid.

WRITE: / sflight_wa-carrid,

sflight_wa-connid.

ULINE.

ENDAT.

WRITE: / sflight_wa-fldate,

sflight_wa-seatsocc.

AT END OF connid.

SUM.

ULINE.

WRITE: / 'Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

SKIP.

ENDAT.

AT END OF carrid.

SUM.

ULINE.

WRITE: / 'Carrier Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

NEW-PAGE.

ENDAT.

AT LAST.

SUM.

WRITE: / 'Overall Sum',

sflight_wa-seatsocc UNDER sflight_wa-seatsocc.

ENDAT.

ENDLOOP.

<b>COLLECT</b>

• Using Collect Statement

DATA : BEGIN OF ITAB OCCURS 0,

COL1(3) TYPE C,

COL2 TYPE I,

END OF ITAB.

ITAB-COL1 = 'ABC'.

ITAB-COL2 = 50.

COLLECT ITAB.

ITAB-COL1 = 'ABC'.

ITAB-COL2 = 100.

COLLECT ITAB.

ITAB-COL1 = 'ABC'.

ITAB-COL2 = 150.

COLLECT ITAB.

ITAB-COL1 = 'PQR'.

ITAB-COL2 = 100.

COLLECT ITAB.

ITAB-COL1 = 'PQR'.

ITAB-COL2 = 100.

COLLECT ITAB.

ITAB-COL1 = 'XYZ'.

ITAB-COL2 = 200.

COLLECT ITAB.

LOOP AT ITAB.

WRITE : / ITAB-COL1, ITAB-COL2.

ENDLOOP.

SORT ITAB.

LOOP AT ITAB.

AT FIRST.

WRITE: / 'AT NEW STATEMENT'.

ULINE.

ENDAT.

AT NEW COL1.

WRITE: / ' AT FIRST OF :', ITAB-COL1.

ENDAT.

WRITE : / ITAB-COL1, ITAB-COL2.

AT LAST.

ULINE.

ENDAT.

ENDLOOP.

<u>COLLECT</u>

COLLECT wa INTO itab [result].

This statement inserts the contents of a work area wa either as single row into an internal table itab or adds the values of its numeric components to the corresponding values of existing rows with the same key. As of Release 6.10, you can use result to set a reference to the inserted or changed row in form of a field symbol or data reference.

Prerequisite for the use of this statement is that wa is compatible with the row type of itab. The row type must be flat and all components that are not part of the table key must have a numeric data type ( i, p, f).

If the internal table does not already contain a row with an identical key, the COLLECT statement has the same effect as the following form of the INSERT statement:

INSERT wa INTO TABLE itab [result].

A row, whose position depends on the table key and the table type, is inserted and filled with the contents of wa.

If the internal table already contains one or more rows with an identical key, those values of the components of work area wa that are not part of the key, are added to the corresponding components of the uppermost existing row (in the case of index tables, this is the row with the lowest table index).

The COLLECT statement sets sy-tabix to the table index of the inserted or existing row, in the case of standard tables and sorted tables, and to the value 0 in the case of hashed tables.

Outside of classes, you can omit wa INTO if the internal table has an identically-named header line itab. The statement then implicitly uses the header line as the work area.

Example

Compressed insertion of data from the database table sflight into the internal table seats_tab. The rows in which the key components carrid and connid are identical are compressed by adding the number of occupied seats to the numeric component seatsocc.

DATA: BEGIN OF seats,

carrid TYPE sflight-carrid,

connid TYPE sflight-connid,

seatsocc TYPE sflight-seatsocc,

END OF seats.

DATA seats_tab LIKE HASHED TABLE OF seats

WITH UNIQUE KEY carrid connid.

SELECT carrid connid seatsocc

FROM sflight

INTO seats.

COLLECT seats INTO seats_tab.

ENDSELECT.

<b>REWARD IF USEFULL</b>