📄 ch11.htm
字号:
so <TT>binary search</TT> would not produce a measurable improvement
in performance. It is only included to illustrate how it should
be used.
</BLOCKQUOTE>
<H5>Using the comparing Addition</H5>
<BLOCKQUOTE>
<TT>comparing</TT> detects differences between the contents of
the work area and a row that was found before it is placed in
the work area. Use it in conjunction with <TT>index</TT> or <TT>with
key</TT>. If, after a row has been found, the work area contents
are the same as the found row, <TT>sy-subrc</TT> will be <TT>0</TT>.
If the contents are different, <TT>sy-subrc</TT> will be <TT>2</TT>
and the contents will be overwritten by the found row. If the
row is not found, <TT>sy-subrc</TT> will be <TT>> 2</TT> and
the contents will remain unchanged.
</BLOCKQUOTE>
<BLOCKQUOTE>
Table 11.4 describes the values for the comparison expression
(<TT><I>cmpexp</I></TT> in the
syntax for <TT>read table</TT>).<BR>
</BLOCKQUOTE>
<P>
<CENTER><B>Table 11.4 Forms for the Comparison Expression
in the Read Table Statement</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><B>Cmpexp</B></CENTER></TD><TD WIDTH=384><CENTER><B>Description</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>f1 f2 ...</TT></TD><TD WIDTH=384>After a row is found, the value of <TT>f1</TT> in the found row is compared with the value of <TT>f1</TT> in the work area. Then the value of <TT>f2</TT> is compared with the value of <TT>f2</TT> in the work area, and so on. If they are all equal, <TT>sy-subrc</TT> is set to <TT>0</TT>. If any are different, <TT>sy-subrc</TT> is set to <TT>2</TT>.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>all fields</TT></TD><TD WIDTH=384>All fields are compared, as described for <TT>f1 f2...</TT>.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>no fields</TT></TD><TD WIDTH=384>No fields are compared. This is the default.
</TD></TR>
</TABLE>
</CENTER>
<P>
<BLOCKQUOTE>
Listing 11.9 provides examples.
</BLOCKQUOTE>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 11.9 The comparing Addition Sets the Value
of sy-subrc to 2 Whenever Fields Differ<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1109.
2 data: begin of it occurs 3,
3 f1(2) type n,
4 f2 type i,
5 f3(2) type c,
6 f4 type p,
7 end of it,
8 wa like it.
9
10 it-f1 = '10'. it-f3 = 'AA'. it-f2 = it-f4 = 1. append it.
11 it-f1 = '20'. it-f3 = 'BB'. it-f2 = it-f4 = 2. append it.
12 it-f1 = '30'. it-f3 = 'CC'. it-f2 = it-f4 = 3. append it.
13
14 read table it index 2 comparing f1.
15 write: / 'sy-subrc =', sy-subrc,
16 / 'sy-tabix =', sy-tabix,
17 / it-f1, it-f2, it-f3, it-f4.
18
19 read table it into wa index 1 comparing f2 f4.
20 write: /,
21 / 'sy-subrc =', sy-subrc,
22 / 'sy-tabix =', sy-tabix,
23 / it-f1, it-f2, it-f3, it-f4,
24 / wa-f1, wa-f2, wa-f3, wa-f4.
25
26 it = wa.
27 read table it with key f3 = 'AA' comparing all fields.
28 write: /,
29 / 'sy-subrc =', sy-subrc,
30 / 'sy-tabix =', sy-tabix,
31 / it-f1, it-f2, it-f3, it-f4,
32 / wa-f1, wa-f2, wa-f3, wa-f4.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<BLOCKQUOTE>
The code in Listing 11.9 produces this output:
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
sy-subrc = 2
sy-tabix = 2
20 2 BB 2
sy-subrc = 2
sy-tabix = 1
20 2 BB 2
10 1 AA 1
sy-subrc = 0
sy-tabix = 1
10 1 AA 1
10 1 AA 1
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>On line 14, <TT>index 2 </TT>locates row 2 in <TT>it</TT>.
Before the row is copied to the header line, the value of <TT>f1</TT>
in the body is compared with the value of <TT>f1</TT> in the header
line. In this case, the header line still contains <TT>3</TT>
(unchanged after appending the last row) and the found row contains
a <TT>2</TT>. They are different, so <TT>sy-subrc</TT> is set
to <TT>2</TT>. The contents of the internal table row are then
copied into the header line and it overlays the existing contents.
<LI>On line 19, row 1 is located. The values of <TT>f2</TT> and
<TT>f4</TT> in row 1 of the body are compared with header line
components of the same name. The header line contains row <TT>2</TT>
(retrieved by statement 14), and so they are different. As a result,
<TT>sy-subrc</TT> is set to <TT>2</TT>.
<LI>Line 26 transfers the contents of <TT>wa</TT> to the header
line.
<LI>On line 27, a row having <TT>f3 = 'AA'</TT> is searched for.
Row 1 matches, as shown by the values of <TT>sy-subrc</TT> and
<TT>sy-tabix</TT> in the output. Row 1 is already in the header
line, therefore all fields match in value and <TT>sy-subrc</TT>
to be set to <TT>0</TT>.
</UL>
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
The read table statement can only be used to read internal tables. It doesn't work with database tables. Use select single instead.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H5>Using the transporting Addition</H5>
<BLOCKQUOTE>
<TT>transporting</TT> affects how fields are moved from the found
row to the work area. If it is specified, only the values of the
specified components are moved to the work area. If a row is not
found, <TT>transporting</TT> does nothing.
</BLOCKQUOTE>
<BLOCKQUOTE>
Table 11.5 describes the values for the transporting expression
(<TT><I>texp</I></TT> in the syntax
for <TT>read table</TT>).<BR>
</BLOCKQUOTE>
<P>
<CENTER><B>Table 11.5 Forms for the Transporting Expression
in the </B><TT><B>READ TABLE</B></TT><B>
Statement<BR>
</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><B>Cmpexp</B></CENTER></TD><TD WIDTH=384><CENTER><B>Description</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>f1 f2 ...</TT></TD><TD WIDTH=384>After a row is found, the value of <TT>f1</TT> in the found row overlays the value of <TT>f1</TT> in the work area. Then the value of <TT>f2</TT> overlays the value of <TT>f2</TT> in the work area, and so on. Only the components named after <TT>transporting</TT> are moved. All other components remain unchanged.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>All fields</TT></TD><TD WIDTH=384>All fields are transported. This is the default, and has the same effect as leaving off the <TT>transporting</TT> addition.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>no fields</TT></TD><TD WIDTH=384>No fields are transported. None of the fields in the work area are changed.
</TD></TR>
</TABLE>
</CENTER>
<P>
<BLOCKQUOTE>
This addition is useful if you only want to test for the existence
of a row in an internal table without disturbing the contents
of the header line. For example, before appending a row, you might
want to determine whether the row already exists in the internal
table. Listing 11.10 provides an example.<BR>
</BLOCKQUOTE>
<HR>
<P>
<B>Listing 11.10 The Most Efficient Way to Insert Rows
into a Sorted Internal Table While Maintaining the Sort Order.</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1110.
2 data: begin of it occurs 3,
3 f1(2) type n,
4 f2 type i,
5 f3(2) type c,
6 f4 type p,
7 end of it.
8
9 it-f1 = '40'. it-f3 = 'DD'. it-f2 = it-f4 = 4. append it.
10 it-f1 = '20'. it-f3 = 'BB'. it-f2 = it-f4 = 2. append it.
11
12 sort it by f1.
13 do 5 times.
14 it-f1 = sy-index * 10.
15 it-f3 = 'XX'.
16 it-f2 = it-f4 = sy-index.
17 read table it
18 with key f1 = it-f1
19 binary search
20 transporting no fields.
21 if sy-subrc <> 0.
22 insert it index sy-tabix.
23 endif.
24 enddo.
25
26 loop at it.
27 write: / it-f1, it-f2, it-f3, it-f4.
28 endloop.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<BLOCKQUOTE>
The code in Listing 11.10 produces this output:
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
10 1 XX 1
20 2 BB 2
30 3 XX 3
40 4 DD 4
50 5 XX 5
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 9 and 10 add two rows to <TT>it</TT>. (They are purposely
out of order for sake of example.)
<LI>Line 12 sorts it by <TT>f1</TT> ascending so that the following
<TT>read table</TT> can use the <TT>binary search</TT> addition.
<LI>Line 13 loops five times. Each time through the loop it creates
a new row for <TT>it</TT> and places it in the header line (lines
14, 15, and 16).
<LI>Lines 17 and 21 determine whether a row already exists that
has the same <TT>f1</TT> value as that in the header line. If
that row is not found, <TT>sy-subrc</TT> is set to non-zero and
<TT>sy-tabix</TT> is set to the index of the row with the next
higher value, or to the number of rows in the internal table plus
1.
<LI>Line 22 inserts the new row before the row pointed to by <TT>sy-tabix</TT>.
This maintains the current sort order.
</UL>
<H3><A NAME="SortingtheContentsofanInternalTable">
Sorting the Contents of an Internal Table</A></H3>
<P>
To sort the contents of an internal table, use the <TT>sort</TT>
statement. Rows can be sorted by one or more columns in ascending
or descending order. The sort sequence itself can also be modified.
<H4>Syntax for the sort Statement</H4>
<P>
The following is the syntax for the <TT>sort</TT> statement.
<BLOCKQUOTE>
<PRE>
sort <I>it</I> [descending] [as text] [by <I>f1</I> [ascending|descending] [as text]
<I>f2</I> ...].
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>it</I></TT> is the
name of an internal table.
<LI><TT><I>f1</I></TT> and <TT><I>f2</I></TT>
are components of <TT><I>it</I></TT><I>.</I>
<LI><TT>...</TT> represents any number of field names optionally
followed by <TT>ascending</TT>, <TT>descending</TT>, and/or <TT>as
text</TT>.
</UL>
<P>
The following points apply:
<UL>
<LI><TT>ascending</TT> is the default sort order.
<LI>If <TT>descending</TT> appears after <TT>sort</TT> and before
any component names, it becomes the default and applies to all
components. This default can be overridden on an individual component
by specifying <TT>ascending</TT> after the component name.
<LI>If no additions are specified, the internal table is sorted
by the default key fields (all fields of type <TT>c</TT>, <TT>n</TT>,
<TT>p</TT>, <TT>d</TT>, and <TT>t</TT>) in ascending order.
</UL>
<P>
The sort order for rows that have the same value is not predictable.
Rows having the same value in a sorted column can appear in any
order after the sort. Listing 11.11 shows examples of the <TT>sort</TT>
statement and also illustrates this point.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 11.11 Use the sort Statement to Reorder
the Rows of an Internal Table<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1111.
2 data: begin of it occurs 5,
3 i like sy-index,
4 t,
5 end of it,
6 alpha(5) value 'CBABB'.
7
8 do 5 times varying it-t from alpha+0 next alpha+1.
9 it-i = sy-index.
10 append it.
11 enddo.
12
13 loop at it.
14 write: / it-i, it-t.
15 endloop.
16
17 skip.
18 sort it by t.
19 loop at it.
20 write: / it-i, it-t.
21 endloop.
22
23
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -