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

📄 ch11.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<BR>
</B>
<BLOCKQUOTE>
<PRE>
        1 report ztx1104.
        2 data: begin of it occurs 3,
        3           f1(1),
        4           f2(2),
        5           end of it.
        6 it-f1 = 'A'.
        7 it-f2 = 'XX'.
        8 append it to it.    &quot;appends header line IT to body IT
        9 it-f1 = 'B'.
       10 it-f2 = 'YY'.
       11 append it.          &quot;same as line 8
       12 it-f1 = 'C'.
       13 append it.          &quot;the internal table now contains three rows.
       14 sy-tabix = sy-subrc = 99.
       15 loop at it.         &quot;same as: loop at it into it
       16     write: / sy-tabix, it-f1, it-f2.
       17     endloop.
       18 write: / 'done. sy-tabix =', sy-tabix,
       19        / '      sy-subrc =', sy-subrc.
</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.4 produces this output:
<BLOCKQUOTE>
<PRE>
         1  A XX
            2  B YY
            3  C YY
   done. sy-tabix =         99
       sy-subrc =      0
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 2 through 13 are the same as those in Listing 11.3.
They add three rows to the internal table.
<LI>Line 14 sets <TT>sy-tabix</TT> and <TT>sy-subrc</TT> to an
arbitrary value.
<LI>Line 15 begins a loop that reads the first row from the internal
table named <TT>it</TT>, placing it into the header line <TT>it</TT>.
<LI>Line 16 writes out the value of <TT>sy-tabix</TT> and values
in the components of the header line.
<LI>Line 17 marks the end of the loop. It returns to line 15.
<LI>Line 15 is re-executed. This time, the second row from the
internal table is placed into the header line, overwriting the
previous contents.
<LI>Line 16 is executed, again writing out values from the header
line.
<LI>On line 17, the loop repeats until all rows have been retrieved
from the internal table. When the last row has been read, it stops
looping.
<LI>On line 18, <TT>sy-tabix</TT> has been restored to the value
it had before the loop was executed. <TT>Sy-subrc</TT> contains
zero, indicating that rows were retrieved from the internal table
by the <TT>loop at</TT> statement.
</UL>
<H5>Restricting the Rows Read from the Internal Table</H5>
<BLOCKQUOTE>
Using the <TT>from</TT>, <TT>to</TT>, and <TT>where</TT> additions,
you can restrict the number of rows read from the internal table.
Listing 11.5 expands on Listing 11.4, looping at the internal
table four times. Each time it writes out only certain rows.
</BLOCKQUOTE>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 11.5&nbsp;&nbsp;This Program Is Like Listing 11.4,
but Only Some of the Rows Are Read<BR>
</B>
<BLOCKQUOTE>
<PRE>
        1 report ztx1105.
        2 data: begin of it occurs 3,
        3           f1(1),
        4           f2(2),
        5           end of it.
        6 it-f1 = 'A'.
        7 it-f2 = 'XX'.
        8 append it.
        9 it-f1 = 'B'.
       10 it-f2 = 'YY'.
       11 append it.
       12 it-f1 = 'C'.
       13 append it.                  &quot;it now contains three rows
       14
       15 loop at it where f2 = 'YY'. &quot;f2 is right, it-f2 would be wrong here
       16     write: / sy-tabix, it-f1, it-f2.
       17     endloop.
       18 skip.
       19
       20 loop at it to 2.            &quot;same as: loop at it from 1 to 2.
       21     write: / sy-tabix, it-f1, it-f2.
       22     endloop.
       23 skip.
       24
       25 loop at it from 2.          &quot;same as: loop at it from 2 to 3.
       26     write: / sy-tabix, it-f1, it-f2.
       27     endloop.
       28 skip.
       29
       30 loop at it from 2 where f1 = 'C'.
       31     write: / sy-tabix, it-f1, it-f2.
       32     endloop.
</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.5 produces this output:
<BLOCKQUOTE>
<PRE>
        2  B YY
         3  C YY

         1  A XX
         2  B YY

         2  B YY
         3  C YY

        3  C YY
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 2 through 13 add three rows to the internal table.
<LI>Lines 15 through 17 read only those rows from the internal
table where <TT>f2</TT> has a value equal to <TT>'YY'</TT>. Only
rows 2 and 3 satisfy the criteria.
<LI>Line 18 skips a line in the output, in effect writing a blank
line.
<LI>Lines 20 through 22 read only rows 1 and 2 from the internal
table.
<LI>Lines 25 through 27 read only rows 2 and 3 from the internal
table.
<LI>Line 30 reads beginning from row 2, looking for all rows having
an <TT>f1</TT> value of <TT>'C'</TT>. Row 3 is the only matching
row in this range.
</UL>
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>CAUTION</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Although the where returns a subset of the table contents, a full table scan is always performed. Caution</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
If you make sure the data type and length of the field in <TT><I>exp</I></TT>
matches the internal table field exactly, no conversions will
be performed. This causes your loop to run more quickly. If conversion
is required, it is performed on every loop pass and this can slow
processing down considerably. Use <TT>like</TT> or constants to
avoid conversion.
<H5>Using exit, continue, stop, and check</H5>
<BLOCKQUOTE>
Table 11.1 illustrates the effects of <TT>exit</TT>, <TT>continue</TT>,
and <TT>check</TT> when used within a <TT>loop at</TT>/<TT>endloop</TT>
construct.<BR>
</BLOCKQUOTE>
<P>
<CENTER><B>Table 11.1&nbsp;&nbsp;Statements that Can Alter Loop
Processing for Internal Tables</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=96><CENTER><B>Statement</B></CENTER></TD><TD WIDTH=480><CENTER><B>Effect</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT>exit</TT></TD><TD WIDTH=480>Terminates the loop immediately. Processing continues at the statement immediately following <TT>endloop</TT>.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT>Continue</TT></TD><TD WIDTH=480>Goes immediately to the <TT>endloop</TT> statement, bypassing all following statements within the loop. The next row is returned from the internal table and processing continues from the top of the loop. If there are no more rows to retrieve, processing continues at the first statement following <TT>endloop</TT>.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT>Check <I>exp</I></TT>
</TD><TD WIDTH=480>If <TT><I>exp</I></TT> is true, processing continues as if this statement had not been executed. If <TT><I>exp</I></TT><I> </I>is false, its effect is the same as <TT>continue</TT>.
</TD></TR>
</TABLE>
</CENTER>
<P>
<H4>Reading a Single Row Using the read table Statement</H4>
<P>
To locate and read a single row from an internal table, use <TT>read
table</TT>. It reads a single row that matches specific criteria
and places it in a work area.
<H5>Syntax for the read table Statement</H5>
<BLOCKQUOTE>
The following is the syntax for the <TT>read table</TT> statement.
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
read table <I>it</I> [into <I>wa</I>] [index <I>i</I> | with key <I>keyexp </I>[binary search] ] [comparing <I>cmpexp</I>] [transporting <I>texp</I>].
</PRE>
</BLOCKQUOTE>
<BLOCKQUOTE>
where:

<UL>
<LI><TT><I>it</I></TT> is the
name of an internal table.
<LI><TT><I>wa</I></TT> is the
name of a work area.
<LI><TT><I>i</I></TT> is an integer
literal, constant, or variable representing a relative row number.
For example, <TT>1</TT> means the first row in the table, <TT>2</TT>
means the second, and so on.
<LI><TT><I>keyexp</I></TT> is
an expression representing a value to be found.
<LI><TT><I>cmpexp</I></TT> is
a comparison expression representing a test to be performed on
the found row.
<LI><TT><I>texp</I></TT> is an
expression representing the fields to be transported to the work
area after a row has been found.
<LI>If both <TT>comparing</TT> and <TT>transporting</TT> are specified,
<TT>comparing</TT> must come first.
</UL>
<P>
The following points apply:
<UL>
<LI><TT><I>wa</I></TT> must have
the same structure as a row of the body.
<LI><TT><I>wa</I></TT> can be
the header line, or it can be any field string having the same
structure as a row in the body.
<LI>If you do not specify a work area, by default the system uses
the header line. For example, <TT>read table it into it</TT> reads
one row from the internal table <TT><I>it</I></TT>,
placing it into the header line <TT><I>it</I></TT>.
An equivalent statement is <TT>read table it</TT>.
</UL>
</BLOCKQUOTE>
<H5>Using the index Addition</H5>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<BLOCKQUOTE>
An internal table row's <I>index</I> is its relative row number.
For example, the first row in the table is index 1, the second
is index 2, and so on. On the <TT>read table</TT> statement, if
<TT>index <I>i</I></TT> is specified,
the system retrieves the <TT><I>i</I></TT>th
row from the internal table and places it into the work area.
For example, <TT>read table it index 7</TT> reads the seventh
row from the internal table and places it in the header line.
</BLOCKQUOTE>
<BLOCKQUOTE>
If the read was successful (that is, if the <TT><I>i</I></TT>th
row exists), <TT>sy-subrc</TT> is set to zero and <TT>sy-tabix</TT>
is set to <TT><I>i</I></TT> (see
Listing 11.6).
</BLOCKQUOTE>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 11.6&nbsp;&nbsp;The index Addition on the read table
Statement Locates a Single Row by Its Relative Row Number<BR>
</B>
<BLOCKQUOTE>
<PRE>
     1 report ztx1106.
     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.
    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.
    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 read table it index 4.
    27 write: /,
    28        / 'sy-subrc =', sy-subrc,
    29        / 'sy-tabix =', sy-tabix,
    30        / it-f1, it-f2, it-f3, it-f4,
    31        / 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">
<P>
The code in Listing 11.6 produces this output:
<BLOCKQUOTE>
<PRE>
    sy-subrc =     0
    sy-tabix =          2
    20          2  BB               2

    sy-subrc =     0
    sy-tabix =          1
    20          2  BB               2
    10          1  AA               1

    sy-subrc =     4
    sy-tabix =          0

⌨️ 快捷键说明

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