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

📄 ch11.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
append [<I>wa</I> to] [initial line to] <I>it</I>.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>wa</I></TT> is the
name of a work area.
<LI><TT><I>it</I></TT> is the
name of a previously defined internal table.
</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. In effect, the header line is the default work
area.
<LI>After <TT>append</TT>, <TT>sy-tabix</TT> is set to the relative
row number of the row just appended. For example, after appending
the first row, <TT>sy-tabix</TT> will be set to <TT>1</TT>. After
appending the second row, <TT>sy-tabix</TT> will be <TT>2</TT>,
and so on.
</UL>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
The statement <TT>append it to it</TT> appends the header line
named <TT>it</TT> to the body named <TT>it</TT>. The statement
<TT>append it</TT> does the same thing, because the default work
area is the header line. Being more succinct, the latter is usually
used.
<P>
A work area mentioned explicitly in the <TT>append</TT> statement
is known as an <I>explicit work area</I>. Any field string having
the same structure as a row of the internal table can be used
as an explicit work area. If a work area is not mentioned, the
<I>implicit work area</I> (the header line) is used.
<P>
The statement <TT>append initial line to it</TT> appends a row
containing initial values (blanks and zeros) to the internal table.
It is the same as executing the following two statements in succession:
<TT>clear it</TT> and <TT>append it.</TT>
<P>
Listing 11.3 shows a sample program that appends three rows to
an internal table.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 11.3&nbsp;&nbsp;This Program Adds Three Rows to Internal
Table it from the Header Line<BR>
</B>
<BLOCKQUOTE>
<PRE>
 1 report ztx1103.
 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 write: / 'sy-tabix =', sy-tabix.
10 it-f1 = 'B'.
11 it-f2 = 'YY'.
12 append it.          &quot;same as line 8
13 write: / 'sy-tabix =', sy-tabix.
14 it-f1 = 'C'.
15 append it.          &quot;the internal table now contains three rows.
16 write: / 'sy-tabix =', sy-tabix.
</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.3 produces this output:
<BLOCKQUOTE>
<PRE>
sy-tabix =              1
sy-tabix =          2
sy-tabix =          3
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 2 defines an internal table named <TT>it</TT> and a header
line named <TT>it</TT>. Both have two components: <TT>f1</TT>
and <TT>f2</TT>.
<LI>Lines 6 and 7 set the contents of header line fields <TT>f1</TT>
and <TT>f2</TT> (shown in Figure 11.2). Here, <TT>it</TT> refers
to the header line.<P>
<A HREF="javascript:popUp('f11-2.gif')"><B>Figure 11.2 :</B> <I>These are assignment statements, so it
refers to the header line</I>.</A><P>
<LI>Line 8 copies the contents of the header line named <TT>it</TT>
to the body named <TT>it</TT> (shown in Figure 11.3).<P>
<A HREF="javascript:popUp('f11-3.gif')"><B>Figure 11.3 :</B> <I>Append it to it copies the contents of
the header line it to the body it. f1 f2 A X X it header line</I>.</A><P>
<LI>Lines 10 and 11 assign new contents to the components of the
header line, overwriting the existing contents (shown in Figure
11.4).<P>
<A HREF="javascript:popUp('f11-4.gif')"><B>Figure 11.4 :</B> <I>These assignment statements overwrite the
existing contents of the header line it</I>.</A><P>
<LI>Line 12 appends the new contents of the header line to the
body (see Figure 11.5). This statement is functionally equivalent
to the one on line 8. Because a work area is not named, the default
work area-the header line-is used.<P>
<A HREF="javascript:popUp('f11-5.gif')"><B>Figure 11.5 :</B> <I>The default work area is the header line.
Here, it is implicitly appended to the body. B Y Y it header line
append it</I>.</A><P>
<LI>Line 14 changes the value of <TT>it-f1</TT> and leaves the
in <TT>it-f2</TT> alone (see Figure 11.6).<P>
<A HREF="javascript:popUp('f11-6.gif')"><B>Figure 11.6 :</B> <I>The value of header line component it-f1
is overwritten. The value of it-f2 is left alone. f1 f2 C Y Y
it header line it-f1 = 'C'</I>.</A><P>
<LI>Line 15 appends a third row from the header line <TT>it</TT>
to the body <TT>it</TT> (see Figure 11.7).<P>
<A HREF="javascript:popUp('f11-7.gif')"><B>Figure 11.7 :</B> <I>Here, the contents of the implicit work
area are appended to the body. C Y Y it header line append it</I>.</A>
</UL>
<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
In actuality, your Basis consultant sets a limit on the maximum amount of extended memory an internal table can allocate. If you exceed that amount, your program will allocate private memory from the work process and it will no longer be able to roll your program out. This makes the work process unavailable for use by other programs until your program has completely finished processing. To avoid this problem, you should use an extract instead.</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<H4>Using the occurs Addition</H4>
<P>
<TT>occurs</TT> does not limit the number of rows that can be
added to the internal table. For example, if you specify <TT>occurs
10</TT>, you can put more than 10 rows into the internal table.
The number of rows you can put into an internal table is theoretically
only limited by the amount of virtual memory available on the
application server.
<P>
The system uses the <TT>occurs</TT> clause only as a guideline
to determine how much memory to allocate. The first time a row
is added to the internal table, enough memory is allocated to
hold the number of rows specified on the <TT>occurs</TT> clause.
If you use that memory up, more is allocated as needed.
<P>
Alternatively, you can specify <TT>occurs 0</TT>. If you do this,
the system allocates 8KB pages of memory at a time. However, there
are no advantages to using <TT>occurs 0</TT> other than the fact
it is only a little easier to code <TT>occurs 0</TT> than it is
to estimate the size of the internal table.<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Don't use <TT>occurs 0 </TT>if you expect to store less than 8KB in an internal table. If you do, the system will allocate 8KB from the paging area. Memory will be wasted and paging could increase, resulting in poorer performance.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
For peak performance and minimum wasted memory, choose an <TT>occurs</TT>
value equal to the average maximum number of rows in the table.
For example, if most of the time the internal table is filled
with a maximum of 100 rows, but every once in a while it is filled
with a maximum of 1,000 rows, set the <TT>occurs</TT> value to
100.
<P>
Depending on the <TT>occurs</TT> clause and the size of the internal
table, the system will allocate memory from either the program
roll area or the system paging area, or both. Memory accesses
in the roll area can be slightly faster than accesses in the paging
area. Allocations in the paging area are always one page in size
(usually 8KB, or 8,192 bytes).
<P>
There are two methods that the system uses to perform allocations.
In the first, <TT>occurs</TT> is non-zero, and the size of the
internal table (calculated from <TT>occurs</TT> * number of bytes
per row) is less than or equal to 8KB. (The number of bytes per
row can be obtained using the <TT>describe table </TT>statement
detailed in the next chapter, or via the Field view in the debugger.)
When the first row is added to the internal table, memory is allocated
from the program roll area. The amount allocated is equal to the
calculated size of the internal table. If this amount is exceeded,
an equal amount is allocated and the process is repeated until
the total exceeds 8KB. When this happens, subsequent allocations
obtain 8KB pages from the paging area.
<P>
If you specify <TT>occurs 0</TT>, all allocations are done in
the paging area, causing one 8KB page to be allocated at a time.
If the calculated size of the internal table is greater than 8KB,
the system changes the <TT>occurs</TT> value to zero when the
first allocation is done and all allocations are satisfied from
the paging area. You can see the change to <TT>occurs</TT> by
using the <TT>describe table</TT> statement after the first allocation
and displaying the value of <TT>sy-toccu</TT>. This statement
is described on Day 9, &quot;Assignments, Conversions, and Calculations.&quot;
<H3><A NAME="ReadingDatafromanInternalTable">
Reading Data from an Internal Table</A></H3>
<P>
Two statements are commonly used to read the data from an internal
table:
<UL>
<LI><TT>loop at</TT>
<LI><TT>read table</TT>
</UL>
<P>
Use <TT>loop at</TT> to read multiple rows from the internal table.
Use <TT>read table</TT> to read a single row.
<H4>Reading Multiple Rows Using the loop at Statement</H4>
<P>
To read some or all rows from an internal table, you can use the
<TT>loop at</TT> statement. <TT>Loop at</TT> reads the contents
of the internal table, placing them one at a time into a work
area.
<H5>Syntax for the loop at Statement</H5>
<BLOCKQUOTE>
The following is the syntax for the <TT>loop at</TT> statement.
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
   loop at <I>it</I> [into <I>wa</I>] [from <I>m</I>] [to <I>n</I>] [where <I>exp</I>].
    ---
   endloop.
</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>m</I></TT><I> </I>and<I>
</I><TT><I>n</I></TT> are integer
literals, constants, or variables 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>exp</I></TT> is a logical
expression restricting the rows that are read.
<LI><TT>---</TT> represents any number of lines of code. These
lines are executed once for each row retrieved from the internal
table.
</UL>

The rows are read from the internal table one at a time and placed
in succession into the work area. The lines of code between the
<TT>loop at</TT> and <TT>endloop</TT> are executed for each row
retrieved. The loop finishes automatically when the last row has
been read, and the statement following the <TT>endloop</TT> is
then executed.
</BLOCKQUOTE>
<BLOCKQUOTE>
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>loop at it into it</TT> reads
rows from the internal table <TT>it</TT>, placing them one at
a time into the header line <TT>it</TT>. An equivalent statement
is <TT>loop at it</TT>.
<LI>If <TT>from</TT> is not specified, the default is to begin
reading from the first row.
<LI>If <TT>to</TT> is not specified, the default is to read to
the last row.
<LI>Components of <TT><I>it</I></TT>
specified in the logical expression should not be preceded by
the internal table name. For example, <TT>where f1 = 'X'</TT>
is correct, but <TT>where it-f1 = 'X'</TT> will cause a syntax
error.
<LI><TT><I>exp</I></TT> can be
any logical expression. However, the first operand of each comparison
must be a component of the internal table. For example, if <TT><I>it</I></TT>
contains a component <TT><I>f1</I></TT>,
then <TT>where f1 = 'X'</TT> would be correct; <TT>where 'X' =
f1</TT> would be incorrect and causes a syntax error.
</UL>

The <TT>from</TT>, <TT>to</TT>, and <TT>where</TT> additions can
be mixed at will.
</BLOCKQUOTE>
<BLOCKQUOTE>
Within the loop, <TT>sy-tabix</TT> contains the relative row number
of the current record. For example, while processing the first
record in the internal table, the value of <TT>sy-tabix</TT> will
be <TT>1</TT>. While processing the second, <TT>sy-tabix</TT>
will be <TT>2</TT>. Upon exiting the loop, the value of <TT>sy-tabix</TT>
is reset to the value it had when the loop began. If loops are
nested, the value in <TT>sy-tabix</TT> relates to the current
loop.
</BLOCKQUOTE>
<BLOCKQUOTE>
After <TT>endloop</TT>, <TT>sy-subrc</TT> will be zero if any
rows were read. It will be non-zero if no rows were read from
the internal table.
</BLOCKQUOTE>
<BLOCKQUOTE>
Listing 11.4 expands on Listing 11.3, looping at and writing out
the rows of the internal table.
</BLOCKQUOTE>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 11.4&nbsp;&nbsp;This Program Adds 3 Rows to Internal
Table it from the Header Line, and Then Writes Them Back out Again

⌨️ 快捷键说明

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