1. Trang chủ
  2. » Công Nghệ Thông Tin

BC ABAP Programming PHẦN 3 pps

153 368 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 153
Dung lượng 7,76 MB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Inserting Lines Using the IndexInserting Lines Using the Index The INSERT statement allows you not only to insert lines in any type of internal table, but alsoallows you to change them u

Trang 1

Appending Table Lines

general rule, where internal tables can be extended dynamically If you add more lines thanspecified, the last line is discarded This is useful for creating ranked lists of limited length (forexample "Top Ten") You can use the APPEND statement to generate ranked lists containing up

to 100 entries When dealing with larger lists, it is advisable to sort [Page 272] tables normallyfor performance reasons

Examples

DATA: BEGIN OF WA,

COL1 TYPE C, COL2 TYPE I, END OF WA.

DATA ITAB LIKE TABLE OF WA.

DO 3 TIMES.

APPEND INITIAL LINE TO ITAB.

WA-COL1 = SY-INDEX WA-COL2 = SY-INDEX ** 2.

APPEND WA TO ITAB.

ENDDO.

LOOP AT ITAB INTO WA.

WRITE: / WA-COL1, WA-COL2.

DATA: BEGIN OF LINE2,

FIELD1(1) TYPE C, FIELD2 LIKE TAB1, END OF LINE2,

TAB2 LIKE TABLE OF LINE2.

LINE1-COL1 = 'abc' LINE1-COL2 = '12' LINE1-COL3 = 3.

APPEND LINE1 TO TAB1.

Trang 2

Appending Table Lines

LINE1-COL1 = 'def' LINE1-COL2 = '34' LINE1-COL3 = 5.

APPEND LINE1 TO TAB1.

LINE2-FIELD1 = 'A' LINE2-FIELD2 = TAB1.

APPEND LINE2 TO TAB2.

REFRESH TAB1.

LINE1-COL1 = 'ghi' LINE1-COL2 = '56' LINE1-COL3 = 7.

APPEND LINE1 TO TAB1.

LINE1-COL1 = 'jkl' LINE1-COL2 = '78' LINE1-COL3 = 9.

APPEND LINE1 TO TAB1.

LINE2-FIELD1 = 'B' LINE2-FIELD2 = TAB1.

APPEND LINE2 TO TAB2.

LOOP AT TAB2 INTO LINE2.

WRITE: / LINE2-FIELD1.

LOOP AT LINE2-FIELD2 INTO LINE1.

WRITE: / LINE1-COL1, LINE1-COL2, LINE1-COL3.

The example creates two internal tables TAB1 and TAB2 TAB2 has a deep

structure because the second component of LINE2 has the data type of internal tableTAB1 LINE1 is filled and appended to TAB1 Then, LINE2 is filled and appended toTAB2 After clearing TAB1 with the REFRESH statement, the same procedure isrepeated

DATA: BEGIN OF LINE,

COL1 TYPE C, COL2 TYPE I, END OF LINE.

DATA: ITAB LIKE TABLE OF LINE,

JTAB LIKE ITAB.

DO 3 TIMES.

LINE-COL1 = SY-INDEX LINE-COL2 = SY-INDEX ** 2.

APPEND LINE TO ITAB.

LINE-COL1 = SY-INDEX LINE-COL2 = SY-INDEX ** 3.

APPEND LINE TO JTAB.

ENDDO.

APPEND LINES OF JTAB FROM 2 TO 3 TO ITAB.

Trang 3

Appending Table Lines

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2.

This example creates two internal tables of the same type, ITAB and JTAB In the

DO loop, ITAB is filled with a list of square numbers, and JTAB with a list of cubenumbers Then, the last two lines of JTAB are appended to ITAB

DATA: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, COL3 TYPE I, END OF LINE.

DATA ITAB LIKE TABLE OF LINE INITIAL SIZE 2.

LINE-COL1 = 1 LINE-COL2 = 2 LINE-COL3 = 3.

APPEND LINE TO ITAB SORTED BY COL2.

LINE-COL1 = 4 LINE-COL2 = 5 LINE-COL3 = 6.

APPEND LINE TO ITAB SORTED BY COL2.

LINE-COL1 = 7 LINE-COL2 = 8 LINE-COL3 = 9.

APPEND LINE TO ITAB SORTED BY COL2.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL2.

ENDLOOP.

The output is:

85The program inserts three lines into the internal table ITAB using the APPENDstatement and the SORTED BY addition The line with the smallest value for thefield COL2 is deleted from the table, since the number of lines that can be appended

is fixed through the INITIAL SIZE 2 addition in the DATA statement

Trang 4

Inserting Lines Using the Index

Inserting Lines Using the Index

The INSERT statement allows you not only to insert lines in any type of internal table, but alsoallows you to change them using a line index You can insert either a single line or a group oflines into index tables using the index

Inserting a Single Line

To insert a line into an index table, use the statement:

INSERT <line> INTO <itab> [INDEX <idx>].

<line> is either a work area that is convertible to the line type, or the expression INITIAL LINE Ifyou use <wa>, the system adds a new line to the internal table <itab> and fills it with the contents

of the work area INITIAL LINE inserts a blank line containing the correct initial value for eachfield of the structure

If you use the INDEX option, the new line is inserted before the line which has the index <idx>.After the insertion, the new entry has the index <idx> and the index of the following lines isincremented by 1 If the table contains <idx> -1 lines, the new line is added at the end of thetable If the table has less than <idx> - 1 lines, the new line cannot be inserted, and SY-SUBRC

is set to 4 When the system successfully adds a line to the table, SY-SUBRC is set to 0

Without the INDEX addition, you can only use the above statement within a LOOP Then, thenew line is inserted before the current line (<idx> is implicitly set to SY-TABIX)

Appending lines to standard tables and sorted tables with a non-unique key works regardless ofwhether lines with the same key already exist in the table Duplicate entries may occur A runtimeerror occurs if you attempt to add a duplicate entry to a sorted table with a unique key Equally, aruntime error occurs if you violate the sort order of a sorted table by appending to it

Inserting Several Lines

To add several lines to an internal table, use the statement:

INSERT LINES OF <itab1> INTO <itab2> [INDEX <idx>].

The system inserts the lines of table <itab1> one by one into <itab2> using the same rules as forsingle lines ITAB1 can be any type of table The line type of ITAB1 must be convertible into theline type of ITAB2

When you append an index table to another index table, you can specify the lines to be

Trang 5

Inserting Lines Using the Index

Examples

DATA: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, END OF LINE.

DATA ITAB LIKE TABLE OF LINE.

INSERT LINE INTO ITAB INDEX 2.

INSERT INITIAL LINE INTO ITAB INDEX 1.

LOOP AT ITAB INTO LINE.

WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.

DATA: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, END OF LINE.

DATA ITAB LIKE TABLE OF LINE.

LOOP AT ITAB INTO LINE.

LINE-COL1 = 3 * SY-TABIX LINE-COL2 = 5 * SY-TABIX.

INSERT LINE INTO ITAB.

ENDLOOP.

Trang 6

Inserting Lines Using the Index

LOOP AT ITAB INTO LINE.

WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.

DATA: ITAB LIKE TABLE OF LINE,

JTAB LIKE ITAB.

DO 3 TIMES.

LINE-COL1 = SY-INDEX LINE-COL2 = SY-INDEX ** 2.

APPEND LINE TO ITAB.

LINE-COL1 = SY-INDEX LINE-COL2 = SY-INDEX ** 3.

APPEND LINE TO JTAB.

ENDDO.

INSERT LINES OF ITAB INTO JTAB INDEX 1.

LOOP AT JTAB INTO LINE.

WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.

Trang 7

Reading Lines Using the Index

Reading Lines Using the Index

You can use the READ statement to read lines in tables using their index To read a single line of

an index table, use the statement:

READ TABLE <itab> INDEX <idx> <result>.

The system reads the line with the index <idx> from the table <itab> This is quicker than

searching using the key [Page 288] The <result> part can specify a further processing option forthe line that is retrieved

If an entry with the specified index was found, the system field SUBRC is set to 0 and TABIX contains the index of that line Otherwise, SY-SUBRC is set to a value other than 0

SY-If <idx> is less than or equal to 0, a runtime error occurs SY-If <idx> is greater than the number oflines in the table, SY-SUBRC is set to 4

Example

DATA: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.

FIELD-SYMBOLS <FS> LIKE LINE OF ITAB.

READ TABLE ITAB ASSIGNING <FS> INDEX 7.

WRITE: SY-SUBRC, SY-TABIX.

WRITE: / <FS>-COL1, <FS>-COL2.

The output is:

The example creates a sorted table ITAB and fills it with 20 lines The line with index

7 is read and assigned to the field symbol <FS>

Trang 8

Binary Search in Standard Tables

Binary Search in Standard Tables

If you read entries [Page 288] from standard tables using a key other than the default key, youcan use a binary search instead of the normal linear search To do this, include the additionBINARY SEARCH in the corresponding READ statements

READ TABLE <itab> WITH KEY = <f> <result> BINARY SEARCH.

DATA ITAB LIKE STANDARD TABLE OF LINE.

SORT ITAB BY COL2.

READ TABLE ITAB WITH KEY COL2 = 16 INTO LINE BINARY SEARCH WRITE: 'SY-SUBRC =', SY-SUBRC.

The output is:

SY-SUBRC = 0

The program fills a standard table with a list of square numbers and sorts them intoascending order by field COL2 The READ statement uses a binary search to lookfor and find the line in the table where COL2 has the value 16

Trang 9

Finding Character Strings in Internal Tables

Finding Character Strings in Internal Tables

To find a string in a line of an index table, use the following statement:

SEARCH <itab> FOR <str> <options>.

The statement searches the internal table <itab> for the character string <str> If the search issuccessful, SY-SUBRC is set to 0, and SY-TABIX is set to the index of the table line in which thestring was found SY-FDPOS contains the offset position of the string in the table line

Otherwise, SY-SUBRC is set to 4

The statement treats all table lines as type C fields, regardless of their actual line type There is

no conversion The search string <str> can have the same form as for a normal string search[Page 171] in a field

The different options (<options>) for the search in an internal table <itab> are:

• ABBREVIATED

Field <c> is searched for a word containing the string in <str> The characters can beseparated by other characters The first letter of the word and the string <str> must bethe same

If the search string is found, all the characters in the search string (and all the characters

in between when using ABBREVIATED) are converted to upper case

This statement only works with index tables There is no corresponding statement for hashedtables

Example

DATA: BEGIN OF LINE,

INDEX TYPE I, TEXT(8) TYPE C, END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY INDEX DATA NUM(2) TYPE N.

DO 10 TIMES.

LINE-INDEX = SY-INDEX.

NUM = SY-INDEX.

CONCATENATE 'string' NUM INTO LINE-TEXT.

APPEND LINE TO ITAB.

ENDDO.

Trang 10

Finding Character Strings in Internal Tables

SEARCH ITAB FOR 'string05' AND MARK.

WRITE: / '''string05'' found at line', (1) SY-TABIX,

'with offset', (1) SY-FDPOS.

SKIP.

READ TABLE ITAB INTO LINE INDEX SY-TABIX.

WRITE: / LINE-INDEX, LINE-TEXT.

The output is:

'string05' found at line 5 with offset 4

5 STRING05The offset of the string found in the table is determined by the width of the first tablecolumn, which has type I and length 4 The option AND MARK changes the tablecontents in the corresponding line

Trang 11

Changing Table Lines Using the Index

Changing Table Lines Using the Index

You can use the MODIFY statement to change lines in tables using their index There is also aspecial variant of the WRITE TO statement that you can use to modify standard tables

Changing Single Lines with MODIFY

To change a line using its index, use the following statement:

MODIFY <itab> FROM <wa> [INDEX <idx>] [TRANSPORTING <f 1 > <f 2 > ].

The work area <wa> specified in the FROM addition replaces the existing line in <itab> Thework area must be convertible into the line type of the internal table

If you use the INDEX option, the contents of the work area overwrites the contents of the line withindex <idx> If the operation is successful, SY-SUBRC is set to 0 If the internal table containsfewer lines than <idx>, no line is changed and SY-SUBRC is set to 4

Without the INDEX addition, you can only use the above statement within a LOOP In this case,you change the current loop line <idx> is implicitly set to SY-TABIX

When you change lines in sorted tables, remember that you must not change the contents of keyfields, and that a runtime error occurs if you try to replace the contents of a key field with anothervalue However, you can assign the same value

The TRANSPORTING addition allows you to specify the fields that you want to change explicitly

in a list See also Changing Table Entries [Page 293] If you change a sorted table, you may onlyspecify non-key fields

Changing Lines Using WRITE TO

You can change lines of standard tables using the following statement:

WRITE <f> TO <itab> INDEX <idx>.

This variant of the WRITE TO [Page 149] statement converts the contents of field <f> to type Cand then transfers the resulting character string into the line with index <idx> If the operation issuccessful, SY-SUBRC is set to 0 If the internal table contains fewer lines than <idx>, no line ischanged and SY-SUBRC is set to 4

The data type of <f> must be convertible into a character field; if it is not, a syntax or runtimeerror occurs The line is always interpreted as a character string, regardless of its actual linetype You can process components [Page 197] in the same way as in the normal WRITE TOstatement You should only use this statement for structured line types if you want to change asingle character whose exact position you already know Another possibility is to use internaltables whose structure is made up of a single character field Tables of this kind are often used

in dynamic programming [Page 517]

Examples

DATA: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, END OF LINE.

Trang 12

Changing Table Lines Using the Index

DATA ITAB LIKE TABLE OF LINE.

LOOP AT ITAB INTO LINE.

WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.

DATA NAME(4) VALUE 'COL2'.

DATA: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.

MODIFY ITAB FROM LINE INDEX 3.

LOOP AT ITAB INTO LINE.

WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.

ENDLOOP.

The output is:

Trang 13

Changing Table Lines Using the Index

DATA CODE LIKE TABLE OF TEXT.

TEXT = 'This is the first line.'.

APPEND TEXT TO CODE.

TEXT = 'This is the second line It is ugly.'.

APPEND TEXT TO CODE.

TEXT = 'This is the third and final line.'.

APPEND TEXT TO CODE.

WRITE 'nice.' TO CODE+31 INDEX 2.

LOOP AT CODE INTO TEXT.

WRITE / TEXT.

ENDLOOP.

This produces the following output:

This is the first line

This is the second line It is nice

This is the third and final line

Here, an internal table CODE is defined with an elementary type C field which is 72characters long After filling the table with three lines, the second line is changed byusing the WRITE TO statement The word "ugly" is replaced by the word "nice"

Trang 14

Deleting Lines Using the Index

Deleting Lines Using the Index

You can use the DELETE statement to delete one or more lines from tables using their index

Deleting a Single Line

To delete a line using its index, use the following statement:

DELETE <itab> [INDEX <idx>].

If you use the INDEX addition, the system deletes the line with the index <idx> from table <itab>,reduces the index of the subsequent lines by 1, and sets SY-SUBRC to zero Otherwise, if noline with index <idx> exists, SY-SUBRC is set to 4

Without the INDEX addition, you can only use the above statement within a LOOP In this case,you delete the current loop line (<idx> is implicitly set to SY-TABIX)

Deleting Several Lines

To delete more than one line using the index, use the following statement:

DELETE <itab> [FROM <n 1 >] [TO <n 2 >] [WHERE <condition>].

Here, you must specify at least one of the additions The WHERE addition has the same effect

as when you delete entries [Page 296] from any table As well as the WHERE clause, you canspecify the lines that you want to delete by their index using FROM and TO The system deletesall of the lines of <itab> whose index lies between <n1> and <n2> If you do not specify a FROMaddition, the system deletes lines from the first line onwards If you do not specify a TO addition,the system deletes lines up to the last line

If at least one line is deleted, the system sets SY-SUBRC to 0, otherwise to 4

Examples

DATA: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.

DELETE ITAB INDEX: 2, 3, 4.

WRITE: 'SY-SUBRC =’, SY-SUBRC.

SKIP.

LOOP AT ITAB INTO LINE.

WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.

ENDLOOP.

Trang 15

Deleting Lines Using the Index

DATA: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, END OF LINE.

DATA ITAB LIKE TABLE OF LINE.

LOOP AT ITAB INTO LINE.

WRITE: / SY-TABIX, LINE-COL1, LINE-COL2.

DATA ITAB LIKE TABLE OF LINE.

DO 40 TIMES.

LINE-COL1 = SY-INDEX.

Trang 16

Deleting Lines Using the Index

LINE-COL2 = SY-INDEX ** 2.

APPEND LINE TO ITAB.

ENDDO.

DELETE ITAB FROM 3 TO 38 WHERE COL2 > 20.

LOOP AT ITAB INTO LINE.

WRITE: / LINE-COL1, LINE-COL2.

The program deletes all entries from the standard table ITAB with an index between

3 and 39 where the value in COL2 is greater than 20

Trang 17

Specifying the Index in Loops

Specifying the Index in Loops

When you process an internal table in a loop, you can specify the index of an index table torestrict the number of entries that are read:

LOOP AT <itab> <result> [FROM <n 1 >] [TO <n 2 >] <condition>.

Example

DATA: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, END OF LINE.

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY TABLE

LOOP AT ITAB INTO LINE FROM 10 TO 25 WHERE COL2 > 400.

WRITE: / SY-TABIX, LINE-COL2.

The example fills a sorted table ITAB with 30 lines In the loop, only the lines 10 to

25 are read There is also a condition that the contents of COL2 must be more than400

Trang 18

Specifying the Index in Loops

Trang 19

Access Using Field Symbols

Access Using Field Symbols

When you read table entries using READ or in a LOOP, you can assign them to a field symbolusing the addition

ASSIGNING <FS>

The field symbol <FS> points directly to the assigned line in memory Unlike work areas, inwhich the contents of the line are only available indirectly, field symbols allow you to read andchange table entries directly

Remember when you access internal tables using field symbols that you must not change thecontents of the key fields of sorted or hashed tables If you try to assign a new value to a keyfield using a field symbol, a runtime error occurs Note that you cannot use the SUM statementwith field symbols, since the statement is always applied to work areas

Advantages of Field Symbols

When you read from an internal table, there are no overheads for copying the table line to thework area When you change an internal table with the MODIFY statement, you must first fill awork area with values, and then assign them to the internal table If you work with field symbolsinstead, you do not have this overhead This can improve performance if you have large orcomplex internal tables It also makes it easier to process nested internal tables

Overheads of READ

Note that internal overheads arise when you access internal tables using field symbols After aREAD statement with a field symbol, the system has to register the assignment When you delete

a table line to which a field symbol is pointing, the system also has to unassign the field symbol

to prevent it from pointing to an undefined area

When you read individual table lines, it is worth using field symbols with the READ statement fortables with a line width of 1000 bytes or more If you also change the line using the MODIFYstatement, using field symbols is worthwhile from a line width of 100 bytes onwards

Overheads of LOOP

To minimize the overheads incurred by using field symbols in loop processing, the system doesnot register the assignment of each current line to the field symbol Instead, it registers a generalassignment between a line of the table and the field symbol When the loop is finished, the lineprocessed in the last loop pass is assigned to the field symbol

Consequently, it is worth using field symbols in a LOOP when the internal table has as few as 10lines However, it is not possible to reassign the field symbol to another field or unassign italtogether within the loop If you include the statements ASSIGN, UNASSIGN, or the

ASSIGNING addition for the same field symbol within the loop block, a runtime error occurs

Example

DATA: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, END OF LINE.

Trang 20

Access Using Field Symbols

DATA ITAB LIKE SORTED TABLE OF LINE WITH UNIQUE KEY COL1.

FIELD-SYMBOLS <FS> LIKE LINE OF ITAB.

READ TABLE ITAB WITH TABLE KEY COL1 = 3 ASSIGNING <FS>.

DELETE ITAB INDEX 3.

IF <FS> IS ASSIGNED.

WRITE '<FS> is assigned!'.

ENDIF.

LOOP AT ITAB ASSIGNING <FS>.

WRITE: / <FS>-COL1, <FS>-COL2.

it points to the third line of the table

Trang 21

Using Header Lines as Work Areas

Using Header Lines as Work Areas

When you create an internal table object [Page 260] you can also declare a header line with thesame name You can use the header line as a work area when you process the internal table.The ABAP statements that you use with internal tables have short forms that you can use if yourinternal table has a header line These statements automatically assume the header line as animplicit work area The following table shows the statements that you must use for internal tableswithout a header line, and the equivalent statements that you can use for internal tables with aheader line:

Operations without header line Operations with header line

Operations for all Table Types

INSERT <wa> INTO TABLE <itab> INSERT TABLE ITAB.

COLLECT <wa> INTO <itab> COLLECT <itab>.

READ TABLE <itab> INTO <wa> READ TABLE <itab>

MODIFY TABLE <itab> FROM <wa> MODIFY TABLE <itab>

MODIFY <itab> FROM <wa> WHERE MODIFY <itab> WHERE DELETE TABLE <itab> FROM <wa> DELETE TABLE <itab>.

LOOP AT ITAB INTO <wa> LOOP AT ITAB

Operations for Index Tables

APPEND <wa> TO <itab> APPEND <itab>.

INSERT <wa> INTO <itab> INSERT <itab>

MODIFY <itab> FROM <wa> MODIFY <itab>

Using the header line as a work area means that you can use shorter statements; however, theyare not necessarily easier to understand, since you cannot immediately recognize the origin andtarget of the assignment Furthermore, the fact that the table and its header line have the samename can cause confusion in operations with entire internal tables [Page 265] To avoid

confusion, you should use internal tables with differently-named work areas

Example

The following example shows two programs with the same function One uses aheader line, the other does not

With header line:

TYPES: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, END OF LINE.

DATA ITAB TYPE HASHED TABLE OF LINE WITH UNIQUE KEY COL1

WITH HEADER LINE.

Trang 22

Using Header Lines as Work Areas

Without header line:

TYPES: BEGIN OF LINE,

COL1 TYPE I, COL2 TYPE I, END OF LINE.

DATA: ITAB TYPE HASHED TABLE OF LINE WITH UNIQUE KEY COL1,

WA LIKE LINE OF ITAB.

DELETE TABLE ITAB FROM WA.

LOOP AT ITAB INTO WA.

WRITE: / WA-COL1, WA-COL2.

The statements in the program that does not use a header line are easier to

understand As a further measure, you could have a further work area just to specifythe key of the internal table, but to which no other values from the table are

assigned

Trang 23

Using Header Lines as Work Areas

Trang 24

An extract dataset consists of a sequence of records of a pre-defined structure However, thestructure need not be identical for all records In one extract dataset, you can store records ofdifferent length and structure one after the other You need not create an individual dataset foreach different structure you want to store This fact reduces the maintenance effort considerably.

In contrast to internal tables, the system partly compresses extract datasets when storing them.This reduces the storage space required In addition, you need not specify the structure of anextract dataset at the beginning of the program, but you can determine it dynamically during theflow of the program

You can use control level processing with extracts just as you can with internal tables Theinternal administration for extract datasets is optimized so that it is quicker to use an extract forcontrol level processing than an internal table

Procedure for creating an extract:

1 Define the record types that you want to use in your extract by declaring them as fieldgroups The structure is defined by including fields in each field group

Defining an Extract [Page 333]

2 Fill the extract line by line by extracting the required data

Filling an Extract with Data [Page 335]

3 Once you have filled the extract, you can sort it and process it in a loop At this stage,you can no longer change the contents of the extract

Processing Extracts [Page 337]

Trang 25

Defining an Extract

Defining an Extract

To define an extract, you must first declare the individual records and then define their structure

Declaring Extract Records as Field Groups

An extract dataset consists of a sequence of records These records may have different

structures All records with the same structure form a record type You must define each recordtype of an extract dataset as a field group, using the FIELD-GROUPS statement

You can also define a special field group called HEADER:

FIELD-GROUPS HEADER

This group is automatically placed before any other field groups when you fill the extract Thismeans that a record of a field group <fg> always contains the fields of the field group HEADER.When sorting the extract dataset, the system uses these fields as the default sort key

Defining the Structure of a Field Group

To define the structure of a record, use the following statement to add the required fields to afield group:

INSERT <f1> <fn> INTO <fg>

This statement defines the fields of field group <fg> Before you can assign fields to a field group,you must define the field group <fg> using the FIELD-GROUPS statement The fields in the fieldgroup must be global data objects in the ABAP program You cannot assign a local data objectdefined in a procedure [Page 451] to a field group

The INSERT statement, just as the FIELD-GROUPS statement, neither reserves storage spacenor transfers values You use the INSERT statement to create pointers to the fields <fi> in thefield group <fg>, thus defining the structures of the extract records

When you run the program, you can assign fields to a field group up to the point when you usethis field group for the first time to fill an extract record From this point on, the structure of therecord is fixed and may no longer be changed In short, as long as you have not used a fieldgroup yet, you can still extend it dynamically

The special field group HEADER is part of every extract record Consequently, you may notchange HEADER once you have filled the first extract record

A field may occur in several field groups; however, this means unnecessary data redundancywithin the extract dataset You do not need to define the structure of a field group explicitly withINSERT If the field group HEADER is defined, an undefined field group consists implicitly of thefields in HEADER, otherwise, it is empty

Trang 26

Defining an Extract

NODES: SPFLI, SFLIGHT.

FIELD-GROUPS: HEADER, FLIGHT_INFO, FLIGHT_DATE.

INSERT: SPFLI-CARRID SPFLI-CONNID SFLIGHT-FLDATE

INTO HEADER, SPFLI-CITYFROM SPFLI-CITYTO INTO FLIGHT_INFO.

The program is linked to the logical database [Page 1210] F1S The NODES

statement declares the corresponding interface work areas [Page 131]

There are three field groups The INSERT statement assigns fields to two of the fieldgroups

Trang 27

Filling an Extract with Data

Filling an Extract with Data

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 <fg>

When the first EXTRACT statement occurs in a program, the system creates the extract datasetand adds the first extract record to it In each subsequent EXTRACT statement, the new extractrecord is added to the dataset

Each extract record contains exactly those fields that are contained in the field group <fg>, plusthe fields of the field group HEADER (if one exists) The fields from HEADER occur as a sort key

at the beginning of the record If you do not explicitly specify a field group <fg>, the

EXTRACT

statement is a shortened form of the statement

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 <fg>, thestructure of the corresponding extract record in the extract dataset is fixed You can no longerinsert new fields into the field groups <fg> and HEADER If you try to modify one of the fieldgroups 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 extractdataset 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 theadvantage that you need not determine the structure at the beginning of the program

Assume the following program is linked to the logical database [Page 1210] F1S

REPORT DEMO.

NODES: SPFLI, SFLIGHT.

FIELD-GROUPS: HEADER, FLIGHT_INFO, FLIGHT_DATE.

INSERT: SPFLI-CARRID SPFLI-CONNID SFLIGHT-FLDATE

INTO HEADER, SPFLI-CITYFROM SPFLI-CITYTO INTO FLIGHT_INFO.

Trang 28

Filling an Extract with Data

and SPFLI-CITYTO The first three fields belong to the prefixed field group HEADER.The records of the field group FLIGHT_DATE consist only of the three fields of fieldgroup HEADER The following figure shows the structure of the extract dataset:

Trang 29

Reading the Extract [Page 338]

Sorting the Extract [Page 341]

Control Level Processing [Page 344]

Calculating Numbers and Totals [Page 348]

Trang 30

In contrast to internal tables, extract datasets do not require a special work area or field symbol

as an interface Instead, you can process each record of the dataset within the loop using itsoriginal field names

• AT LAST

The system executes the statement block once for the last record of the dataset

You can also use the AT and ENDAT statements for control level processing [Page 344]

Assume the following program is linked to the logical database [Page 1210] F1S

REPORT DEMO.

NODES: SPFLI, SFLIGHT.

FIELD-GROUPS: HEADER, FLIGHT_INFO, FLIGHT_DATE.

Trang 31

Reading an Extract

INSERT: SPFLI-CARRID SPFLI-CONNID SFLIGHT-FLDATE

INTO HEADER, SPFLI-CITYFROM SPFLI-CITYTO INTO FLIGHT_INFO.

The control statement AT <fgi> tells the system to output the fields corresponding toeach of the two record types The WITH FLIGHT_DATE option means that thesystem only displays the records of field group FLIGHT_INFO if at least one record

of field group FLIGHT_DATE follows; that is, if the logical database passed at leastone date for a flight

The beginning of the output list looks like this:

Trang 33

SORT [ASCENDING|DESCENDING] [AS TEXT] [STABLE]

BY <f 1 > [ASCENDING|DESCENDING] [AS TEXT]

<f n > [ASCENDING|DESCENDING] [AS TEXT].

The SORT statement terminates the creation of the extract dataset of a program and, at thesame time, sorts its records Without the BY option, the system sorts the dataset by the keyspecified in the HEADER field group

You can sort an extract dataset as often as you like in a program, using any number of differentkeys The only prerequisite is that all fields by which you want to sort are contained in the

HEADER during the extraction process You must not use the SORT statement between LOOPand ENDLOOP However, you can sort and read the extract dataset in any sequence No furtherEXTRACT statements may occur after the sort statement, otherwise a runtime error occurs.You can define a different sort key by using the BY addition The system then sorts the datasetaccording to the specified components <f1> <fn> These components must either be fields ofthe HEADER field group or field groups containing only fields from the HEADER field group Thenumber of key fields is limited to 50 The sequence of the components <f1> <fn> determinesthe sort order The system uses the options you specify before BY as a default for all fieldsspecified behind BY The options that you specify behind individual fields overwrite for thesefields the options specified before BY

You can define the sort direction using the DESCENDING or ASCENDING additions (ascending

is the default direction) For character strings, you can use the AS TEXT addition to define thesort method This forces an alphabetical sort, as with internal tables [Page 272] If you want tosort an extract dataset alphabetically more than once, you should include an alphabetically-sortable field in the sort key instead of the text field for performance reasons To fill this field, usethe CONVERT statement [Page 169]

If you put AS TEXT before BY, the addition only applies to type C fields in the sort key If youplace AS TEXT after a field, the field must be of type C If you place AS TEXT after a field group,the option only applies to the type C fields within the group

This sorting process is not stable, that is, the old sequence of records with the same sort keymust not necessarily be kept To force a stable sort, use the STABLE addition

If there is not enough main memory available to sort the data, the system writes data to anexternal auxiliary file during the sorting process The name of the file is determined by the SAPprofile parameter DIR_SORTTMP

The SORT statement sorts by all of the fields in the sort key with the contents HEX 00 before all

of the other entries This is significant when you use logical databases [Page 1210] When alogical database has finished reading a hierarchy level, it fills all of the fields at that level with thevalue HEX 00 Equally, if you use a field list in the GET [Page 999] statement (FIELDS addition),the logical database fills all of the fields not in the field list with HEX 00

Each sorting process executed on the extract dataset using the SORT statement defines acontrol level This is required for subsequent control level processing [Page 344]

Trang 34

Sorting an Extract

Assume the following program is linked to the logical database [Page 1210] F1S

REPORT DEMO.

NODES: SPFLI, SFLIGHT.

FIELD-GROUPS: HEADER, FLIGHT_INFO, FLIGHT_DATE.

INSERT: SPFLI-CARRID SPFLI-CONNID SFLIGHT-FLDATE

INTO HEADER, SPFLI-CITYFROM SPFLI-CITYTO INTO FLIGHT_INFO.

Trang 35

Sorting an Extract

It is worth noting that the records with the value HEX 00 in the field FLDATE (undefined characters in the list) are sorted before the remaining records.This is done to preserve the hierarchy of the data from the logical database,

SFLIGHT-independent of the sort sequence

Trang 36

Processing Control Levels

Processing Control Levels

When you sort an extract dataset, control levels are defined in it For general information aboutcontrol levels, refer to Processing Internal Tables in Loops [Page 300] The control level hierarchy

of an extract dataset corresponds to the sequence of the fields in the HEADER field group Aftersorting, you can use the AT statement within a loop to program statement blocks that the systemprocesses only at a control break, that is, when the control level changes

AT NEW <f> | AT END OF <f>

ENDAT

A control break occurs when the value of the field <f> or a superior field in the current record has

a different value from the previous record (AT NEW) or the subsequent record (AT END) Field

<f> must be part of the HEADER field group

If the extract dataset is not sorted, the AT ENDAT block is never executed Furthermore, allextract records with the value HEX 00 in the field <f> are ignored when the control breaks aredetermined

The AT ENDAT blocks in a loop are processed in the order in which they occur This sequenceshould be the same as the sort sequence This sequence must not necessarily be the sequence

of the fields in the HEADER field group, but can also be the one determined in the SORT

INSERT T2 T1 INTO HEADER.

T1 ='AABB' T2 = 1 EXTRACT HEADER.

T1 ='BBCC' T2 = 2 EXTRACT HEADER.

T1 ='AAAA' T2 = 2 EXTRACT HEADER.

T1 ='AABB' T2 = 1 EXTRACT HEADER.

Trang 37

Processing Control Levels

T1 ='AAAA' T2 = 3 EXTRACT HEADER.

T1 ='AABB' T2 = 1 EXTRACT HEADER.

Trang 38

Processing Control Levels

In the loop, the system displays the contents of the dataset and the control breaks itencountered as follows:

Trang 39

Processing Control Levels

Trang 40

Calculating Numbers and Totals

Calculating Numbers and Totals

When you read a sorted extract dataset using LOOP, you can access two

automatically-generated fields CNT(<f>) and SUM(<g>) These fields contain the number of different valuesand the sums of the numeric fields respectively The system fills these fields at the end of acontrol level and after reading the last record of the dataset as follows:

• CNT(<f>)

If <f> is a non-numeric field of the HEADER field group and the system sorted the extractdataset by <f>, CNT(<f>) contains the number of different values <f> assumed within thecontrol level or entire dataset respectively

• SUM(<g>)

If <g> is a numeric field of the extract dataset, SUM (<g>) contains the total of the values

of <g> within the control level or entire dataset respectively

You can access these fields either within the processing blocks following AT END OF or in theprocessing block following AT LAST, after reading the entire dataset If you try to access thefields CNT(<f>) and SUM(<g>) without first sorting the dataset, a runtime error may occur

REPORT DEMO.

DATA: T1(4), T2 TYPE I.

FIELD-GROUPS: HEADER, TEST.

INSERT T2 T1 INTO HEADER.

T1 ='AABB' T2 = 1 EXTRACT TEST.

T1 ='BBCC' T2 = 2 EXTRACT TEST.

T1 ='AAAA' T2 = 2 EXTRACT TEST.

T1 ='AABB' T2 = 1 EXTRACT TEST.

T1 ='BBBB' T2 = 2 EXTRACT TEST.

T1 ='BBCC' T2 = 2 EXTRACT TEST.

T1 ='AAAA' T2 = 1 EXTRACT TEST.

T1 ='BBBB' T2 = 1 EXTRACT TEST.

T1 ='AAAA' T2 = 3 EXTRACT TEST.

T1 ='AABB' T2 = 1 EXTRACT TEST.

Ngày đăng: 09/08/2014, 14:20

TỪ KHÓA LIÊN QUAN

w