⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch11.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
    20          2  BB               2
    10          1  AA               1
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 2 through 7 define an internal table with a header line.
<LI>Line 8 defines a work area <TT>wa</TT> like the header line
of <TT>it</TT>.
<LI>Lines 10 through 12 append three rows to the internal table.
<LI>Line 14 reads the second row. Because no work area is specified,
the contents are placed in the header line. After the read, <TT>sy-subrc</TT>
is set to zero. This indicates the row existed and that <TT>sy-tabix</TT>
is set to the row number.
<LI>Line 19 reads row 1 into the work area <TT>wa</TT>. The contents
of the header line are unchanged after the read.
<LI>Line 26 attempts to read row 4 into the header line. There
is no row 4, so <TT>sy-subrc</TT> is set to <TT>4</TT>, <TT>sy-tabix</TT>
is <TT>0</TT>, and the header line and work area <TT>wa</TT> are
unchanged.
</UL>
<H5>Using the with key Addition</H5>
<BLOCKQUOTE>
If <TT>with key <I>keyexp</I></TT>
is specified, the system finds a row that matches the key expression
and places it in the header line. Table 11.2 describes key expressions
and their effects. Using a key expression, you can specify a single
row to be retrieved. If more than one row matches the expression,
the first one found (the one with the lowest index) is returned.
<BR>
</BLOCKQUOTE>
<P>
<CENTER><B>Table 11.2&nbsp;&nbsp;Key Expressions and Their Effects</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=192><CENTER><B>Key Expression</B></CENTER></TD>
<TD WIDTH=384><CENTER><B>Effect</B></CENTER></TD></TR>
<TR VALIGN=TOP><TD WIDTH=192>c1 = v1 c2 = v2 ...</TD><TD WIDTH=384>Locates the first row in the internal table where component c1 has the value v1 and component c2 has the value v2, and so on. v1 is a literal, constant, or variable.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>(f1) = v1 (f2) = v2 ...</TT></TD><TD WIDTH=384>Same as above, but <TT>f1</TT> is a variable that contains the name of the component to be compared. The value in <TT>f1</TT> must be in uppercase. If <TT>f1</TT> is blank, the comparison is ignored.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>= wa</TT></TD><TD WIDTH=384><TT>wa</TT> is a work area identical in structure to an internal table row. This key expression locates the first row in the internal table that is exactly equal to the contents of <TT>wa</TT>. Blanks are treated as values to be found; they do not match any value other than blanks.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>Wa</TT></TD><TD WIDTH=384><TT>wa</TT> is a work area identical to or shorter than the structure of the internal table. If <TT>wa</TT> has <I>n</I> fields, the fields of <TT>wa</TT> match the first <I>n</I> fields of an internal table row. This key expression locates the first row in the internal table whose first <I>n</I> fields match the contents of <TT>wa</TT>. Blanks are treated as values to be found; they do not match any value other than blanks.
</TD></TR>
</TABLE>
</CENTER>
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
In release 4, conversions necessary for the first three key expressions in Table 11.2 are performed with the same order of precedence as that used for logi-cal expressions. In systems prior, conversions were performed by converting from the value on the right to the data type and length of the component on the left.</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Table 11.3 describes the values of <TT>sy-subrc</TT> and <TT>sy-tabix</TT>
after a <TT>read table <I>it</I>
with key...</TT> has executed.<BR>
<P>
<CENTER><B>Table 11.3&nbsp;&nbsp;Values Assigned to </B><TT><B>SY-SUBRC</B></TT><B>
and </B><TT><B>SY-TABIX</B></TT></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=288><CENTER><B>Result</B></CENTER></TD><TD WIDTH=96><CENTER><B>sy-subrc</B></CENTER>
</TD><TD WIDTH=192><CENTER><B>sy-tabix</B></CENTER></TD></TR>
<TR VALIGN=TOP><TD WIDTH=288>Read was successful (a matching row was found)
</TD><TD WIDTH=96><CENTER>0</CENTER></TD><TD WIDTH=192>index of matching row
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=288>Read unsuccessful, but a row with a key greater than the one requested exists
</TD><TD WIDTH=96><CENTER>4</CENTER></TD><TD WIDTH=192>index of row with next higher key
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=288>Read unsuccessful, and no rows found having a higher key 
</TD><TD WIDTH=96><CENTER>8</CENTER></TD><TD WIDTH=192>number of rows in <I>it</I> + 1
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Listing 11.7 illustrates the use of key expressions.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 11.7&nbsp;&nbsp;Using a Key Expression, You Can Search
for a Row by Specifying a Value Instead of an Index<BR>
</B>
<BLOCKQUOTE>
<PRE>
     1 report ztx1107.
     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       begin of w1,
     9           f1 like it-f1,
    10           f2 like it-f2,
    11           end of w1,
    12       w2 like it,
    13       f(8).
    14
    15 it-f1 = '10'. it-f3 = 'AA'. it-f2 = it-f4 = 1. append it.
    16 it-f1 = '20'. it-f3 = 'BB'. it-f2 = it-f4 = 2. append it.
    17 it-f1 = '30'. it-f3 = 'CC'. it-f2 = it-f4 = 3. append it.
    18
    19 read table it with key f1 = '30' f2 = 3.
    20 write: / 'sy-subrc =', sy-subrc,
    21        / 'sy-tabix =', sy-tabix,
    22        / it-f1, it-f2, it-f3, it-f4.
    23
    24 f = 'F2'.                                 &quot;must be in uppercase
    25 read table it into w2 with key (f) = 2.
    26 write: /,
    27        / 'sy-subrc =', sy-subrc,
    28        / 'sy-tabix =', sy-tabix,
    29        / it-f1, it-f2, it-f3, it-f4,
    30        / w2-f1, w2-f2, w2-f3, w2-f4.
    31
    32 clear w2.
    33 w2-f1 = '10'. w2-f3 = 'AA'. w2-f2 = w2-f4 = 1.
    34 read table it with key = w2.
    35 write: /,
    36        / 'sy-subrc =', sy-subrc,
    37        / 'sy-tabix =', sy-tabix,
    38        / it-f1, it-f2, it-f3, it-f4.
    39
    40 w1-f1 = '20'. w1-f2 = 2.
    41 read table it into w2 with key w1.
    42 write: /,
    43        / 'sy-subrc =', sy-subrc,
    44        / 'sy-tabix =', sy-tabix,
    45        / it-f1, it-f2, it-f3, it-f4.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 11.7 produces this output:
<BLOCKQUOTE>
<PRE>
    sy-subrc =     0
    sy-tabix =          3
    30          3  CC               3
    
    sy-subrc =     0
    sy-tabix =          2
    30          3  CC               3
    20          2  BB               2
    
    sy-subrc =     0
    sy-tabix =          1
    10          1  AA               1

    sy-subrc =     0
    sy-tabix =          2
    10          1  AA               1
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 2 through 7 define an internal table with a header line.
<LI>Line 8 defines a work area <TT>w1</TT> like the first two
fields of header line <TT>it</TT>.
<LI>Line 9 defines a work area <TT>w2</TT> like the entire length
of header line <TT>it</TT>.
<LI>Lines 15 through 17 append three rows to the internal table.
<LI>Line 19 reads a row that has value <TT>'30'</TT> in <TT>f1</TT>
and <TT>3</TT> in <TT>f2</TT>. <TT>Sy-subrc</TT> is set to zero
to indicate a matching row was found and <TT>sy-tabix</TT> is
set to the row number. Because no work area is specified, the
contents are placed in the header line.
<LI>Line 25 searches for a row having a value of <TT>2</TT> in
the <TT>f2</TT> column. It is found and placed into work area
<TT>w2</TT>. The contents of the header line are unchanged after
the read.
<LI>Line 34 searches for a row having the values specified in
all fields of <TT>w2</TT>. With this syntax, <TT>w2</TT> must
match the entire structure of <TT>it</TT>. The contents of the
matching row are placed in the header line. Fields containing
blanks are searched for in the table; they do not match all values
as they do when searching using the default key (see the next
section).
<LI>Line 41 searches for a row having the values specified in
all fields of <TT>w1</TT>. With this syntax, <TT>w1</TT> can be
shorter than <TT>it</TT>. The contents of the matching row are
placed in the header line. As before, blank values are treated
as values to be found and match only themselves.
</UL>
<H5>Using the binary search Addition</H5>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<BLOCKQUOTE>
Whenever you use the <TT>with key</TT> addition, you should also
use the <TT>binary search</TT> addition, which causes the row
to be located using a binary search algorithm instead of a linear
table scan. This results in the same performance gains as those
achieved when searching a database table via an index. Beforehand,
the table must be sorted in ascending order by the components
specified in the key expression (see the following section on
sorting).<BR>
</BLOCKQUOTE>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>CAUTION</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Only blanks in a default key field will match all values. This means that you cannot clear types <TT>d</TT>, <TT>t</TT>, <TT>n</TT>, and <TT>x </TT>and obtain a match-clearing will set them to zeros, not blanks. You must force blanks into these fields for them to match. You can do this using a field string (see the following example), using a subfield, or using a field symbol (see the previous section on field symbols for more information).
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<H5>No Additions</H5>
<BLOCKQUOTE>
If neither an <TT>index</TT> nor a key expression are specified,
the table is scanned from the beginning for a row that matches
the <I>default key</I> contained in the header line. The default
key is an imaginary key composed of all character fields in the
header line (types <TT>c</TT>, <TT>d</TT>, <TT>t</TT>, <TT>n</TT>,
and <TT>x</TT>). A blank value in a default key column causes
all values in that column to match. Afterward, the values of <TT>sy-subrc</TT>
and <TT>sy-tabix</TT> are set they same way as they are after
a <TT>read table</TT> using a key expression.
</BLOCKQUOTE>
<BLOCKQUOTE>
Listing 11.8 provides an example.
</BLOCKQUOTE>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 11.8&nbsp;&nbsp;This Program Finds a Row Having the
Same Values as the Default Key Fields in the Header Line. Blanks
in a Default Key Field Cause a Column to be Ignored.<BR>
</B>
<BLOCKQUOTE>
<PRE>
     1 report ztx1108.
      2 data: begin of it occurs 3,
      3           f1(2) type n,   &quot;character field -     part of default key
      4           f2    type i,   &quot;numeric   field - not part of default key
      5           f3(2) type c,   &quot;character field -     part of default key
      6           f4    type p,   &quot;numeric   field - not part of default key
      7           end of it.
      8
      9 it-f1 = '10'. it-f3 = 'AA'. it-f2 = it-f4 = 1. append it.
     10 it-f1 = '20'. it-f3 = 'BB'. it-f2 = it-f4 = 2. append it.
     11 it-f1 = '30'. it-f3 = 'CC'. it-f2 = it-f4 = 3. append it.
     12
     13 sort it by f1 f3.
     14 clear it.
     15 it(2) = ' '. it-f3 = 'BB'.
     16 read table it binary search.
     17 write: / 'sy-subrc =', sy-subrc,
     18        / 'sy-tabix =', sy-tabix,
     19        / 'f1=', it-f1, 'f2=', it-f2, 'f3=', it-f3, 'f4=', it-f4.
     20
     21 clear it.
     22 it-f1 = '30'. it-f3 = 'AA'.
     23 read table it binary search.
     24 write: / 'sy-subrc =', sy-subrc,
     25        / 'sy-tabix =', sy-tabix,
     26        / 'f1=', it-f1, 'f2=', it-f2, 'f3=', it-f3, 'f4=', it-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.8 produces this output:
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
     sy-subrc =     0
     sy-tabix =          2
     f1= 20 f2=          2  f3= BB f4=               2
     sy-subrc =     4
     sy-tabix =          3
     f1= 30 f2=          0  f3= AA f4=               0
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 2 through 7 define an internal table with a header line.
<TT>F1</TT> and <TT>f3</TT> are the character data types and thus
form the default key.
<LI>Lines 9 through 11 append three rows to the internal table.
<LI>Line 13 sorts <TT>it</TT> by <TT>f1</TT> and <TT>f3</TT> in
ascending order.
<LI>Line 14 clears the contents of the header line, setting all
components to blanks and zeros. <TT>f1</TT> is type <TT>n</TT>,
and so is set to zeros.
<LI>Using a subfield, line 15 forces blanks into the <TT>f1</TT>
field so that it will match all values. The other default key
field (<TT>f3</TT>) is set to <TT>'BB'</TT>.
<LI>Line 16 performs a binary search for a row containing <TT>'BB'</TT>
in component <TT>f3</TT>.
<LI>After the search, the output shows that <TT>sy-subrc</TT>
is set to zero, indicating that a row was found matching the criteria.
<TT>Sy-tabix</TT> contains <TT>2</TT>, the relative row number
of the matching row. The header line has been populated with the
contents of the matching row and is written out.
<LI>Line 21 clears the header line.
<LI>Line 22 places a <TT>'30'</TT> and <TT>'AA'</TT> in the default
key fields <TT>f1</TT> and <TT>f3</TT>.
<LI>Line 23 performs a binary search for <TT>f1 = '30'</TT> and
<TT>f3 = 'AA'</TT>. The return value of <TT>sy-subrc</TT> is <TT>4</TT>.
This indicates that a matching row did not exist, and <TT>sy-tabix</TT>
contains <TT>3</TT>-the number of rows in the table. The contents
of the header line are unchanged from what it was on line 22.
</UL>
<BLOCKQUOTE>
In this example, the internal table only contains a few rows,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -