ABAPers normally doesn’t have much information about the Field Groups as they are rarely used in the normal course of programming. These are more ABAP-HR relevant, still a must for an ABAP progrmmer.
A field group does not reserve storage space for the fields but contains pointers to existing fields.> Internal take up memory. Depending on how much memory your system has . > Once you have declared the possible record types as field groups and defined their structure you can fill the extract dataset using the following statements: EXTRACT. When the first EXTRACT statement occurs in a program the system creates the extract dataset and adds the first extract record to it. In each subsequent EXTRACT statement the new extract record is added to the dataset EXTRACT HEADER. When you extract the data the record is filled with the current values of the corresponding fields. As soon as the system has processed the first EXTRACT statement for a field group the structure of the corresponding extract record in the extract dataset is fixed. You can no longer insert new fields into the field groups and HEADER. If you try to modify one of the field groups afterwards and use it in another EXTRACT statement a runtime error occurs. By processing EXTRACT statements several times using different field groups you fill the extract dataset with records of different length and structure. Since you can modify field groups dynamically up to their first usage in an EXTRACT statement extract datasets provide the advantage that you need not determine the structure at the beginning of the program.
Field group works in conjuction with
INSERT f1 f2 INTO fg
SORT BY fg
LOOP … ENDLOOP
INSERT f1 f2 INTO fg
The insert statement is used to create a field group dynamically by inserting the field into it. Only global data fields can be inserted and not local data fields eg : in form modules.
This will combine all the fields in the fieldgroup and write them to a sequential dataset as a single record.
SORT BY fg
Sorting of sequential dataset by field group.
LOOP AND ENDLOOP
When to Use Field Groups
Because field-groups write their data to paging space (rather than storing it in memory), they are appropriate only for processing lists with lots (like 50,000 or more) of records. If you expect your programs to be handling tens of thousands of records, you should:
analyze the expected size of your lists. For instance, if your system has 512M of main memory, you may decide that you don’t want any report to use more than 15M of memory for its lists. In that program, you may have a list:
begin of mylist occurs XXX,
dat1(100) type c,
dat2(50) type c,
dat3(10) type c,
end of list.
Then each record takes up approximately 160 bytes; so every 6 records take up approximately 1K. For this list structure, it would take about 90,000 records to use up 15M RAM. Decide the maximum amount of memory you want your program to use decide whether to use field-groups or something else (like internal tables). If you expect the size of your list to be greater than the amount of memory you want your program to use, then use field-groups (actually, if you use internal tables, and the number of records exceeds the number of records in your OCCURS statement, the system just writes those extra records to the paging space. So is there really any difference between just using an internal table with an OCCURS 0 statement– which would write the entire table to paging space– and using field-groups? According to Gareth M. de Bruyn and Robert Lyfareff in Introduction to ABAP/4 Programming for SAP, field-groups are stored more efficiently, and have better performance. They recommend field-groups for lists of 100,000 or more records).
The sample code that explains this concept best is
*& Report ZSPFLI *
REPORT ZSPFLI LINE-SIZE 132 LINE-COUNT 65(3)
NO STANDARD PAGE HEADING.
TABLES:SPFLI,SCARR, SFLIGHT, SBOOK.
SELECT-OPTIONS: MYCARRID FOR SPFLI-CARRID.
FIELD-GROUPS: HEADER, SPFLI_FG, SFLIGHT_FG, SBOOK_FG.
SELECT * FROM SPFLI WHERE CARRID IN MYCARRID.
SELECT SINGLE * FROM SCARR WHERE CARRID = SPFLI-CARRID.
SELECT * FROM SFLIGHT
WHERE CARRID = SPFLI-CARRID AND CONNID = SPFLI-CONNID.
SELECT * FROM SBOOK
WHERE CARRID = SFLIGHT-CARRID AND
CONNID = SFLIGHT-CONNID AND FLDATE = SFLIGHT-FLDATE.
FORMAT COLOR COL_HEADING.
WRITE: / SCARR-CARRNAME,
SPFLI-AIRPFROM, SPFLI-CITYTO, SPFLI-AIRPTO, SPFLI-DEPTIME.
FORMAT COLOR OFF.
WRITE: /15 SFLIGHT-FLDATE, SFLIGHT-PRICE, SFLIGHT-SEATSMAX,
WRITE: /30 SBOOK-BOOKID, SBOOK-CUSTOMID,
*& END OF REPORT *