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

📄 ch12.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<P>
<B>Listing 12.6&nbsp;&nbsp;Use the free Statement to Delete All
Rows from an Internal Table and Free the Associated Memory<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1206.
2  data: begin of it occurs 3,
3            f1 like sy-index,
4            end of it.
5 
6  do 3 times.
7      it-f1 = sy-index.
8      append it.
 9      enddo.
10
11 loop at it.
12     write it-f1.
13     endloop.
14
15 free it.
16 if it[] is initial.
17     write: / 'no rows exist in it after free'.
18     endif.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 12.6 produces this output:
<BLOCKQUOTE>
<PRE>
         1           2           3  
no rows exist in it after free      
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Line 15 deletes all rows from the internal table and frees the
associated memory.
<H3><A NAME="UsingrefreshtoDeleteInternalTableContents">
Using refresh to Delete Internal Table Contents</A></H3>
<P>
Use the <TT>refresh</TT> statement to delete all rows from an
internal table but leave the memory allocated.
<H4>Syntax for the refresh Statement</H4>
<P>
The following is the syntax for the <TT>refresh</TT> statement.
<BLOCKQUOTE>
<PRE>
refresh it.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>it</I></TT> is an internal
table with or without a header line.
</UL>
<P>
The following points apply:
<UL>
<LI>All rows are deleted. All memory used by the body of the internal
table remains allocated.
<LI>The header line, if it exists, is unchanged.
</UL>
<P>
Use <TT>refresh</TT> when you want to delete all rows but you
intend to fill the internal table back up again. For example,
if you are producing a sales report by department, you might fill
the internal table with all sales for one department, process
the data, and write it out. Then, after a <TT>refresh</TT>, you
could fill the internal table with the data for the next department,
write it out, and so on.
<P>
If you intend to refill a table immediately after clearing it,
<TT>refresh</TT> is more efficient than <TT>free</TT> because
it avoids unnecessary memory allocations.
<P>
Listing 12.7 shows how to use the <TT>refresh</TT> statement.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 12.7&nbsp;&nbsp;Use the refresh Statement to Delete
All Rows from an Internal Table<BR>
</B>
<BLOCKQUOTE>
<PRE>
 1  report ztx1207.
 2  data: begin of it occurs 3,
 3            f1 like sy-index,
 4            end of it,
 5        i like sy-index.
 6
 7  do 3 times.
 8      i = sy-index.
 9      do 3 times.
10         it-f1 = i * sy-index.
11         append it.
12         enddo.
13     write: / ''.
14     loop at it.
15         write it-f1.
16         endloop.
17     refresh it.
18     enddo.
19
20 free it.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 12.7 produces this output:
<BLOCKQUOTE>
<PRE>
           1           2           3
           2           4           6
           3           6           9
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 7 begins a loop that is executed three times. It contains
a nested inner loop.
<LI>Line 8 stores the current value of <TT>sy-index</TT> from
the outer loop.
<LI>Line 9 begins the inner loop.
<LI>On line 10, the number of the inner loop pass is multiplied
by the number of the outer loop pass.
<LI>Line 11 adds each row to the internal table.
<LI>Line 13 begins a new line of output.
<LI>Lines 14 through 16 write out the contents of the internal
table.
<LI>Line 17 deletes all rows from the internal table but does
not free the memory. Refresh is used here instead of <TT>free</TT>
because the outer loop repeats and refills the internal table
again immediately.
<LI>Line 20 deletes all rows and frees the memory for the internal
table before the list is shown. This makes the program more efficient.
</UL>
<H3><A NAME="UsingclearwithanInternalTable">
Using clear with an Internal Table</A></H3>
<P>
You can use the <TT>clear</TT> statement to do either of the following:
<UL>
<LI>Delete all rows from an internal table and leave the memory
allocated.
<LI>Clear the header line (set its components to blanks and zeros).
</UL>
<H4>Syntax for the clear Statement When Used with an Internal
Table</H4>
<P>
The following is the syntax for the <TT>clear</TT> statement when
used with an internal table.
<BLOCKQUOTE>
<PRE>
clear <I>it</I> | clear <I>it</I>[]
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>it</I></TT> is the
name of an internal table.
</UL>
<P>
The following points apply:
<UL>
<LI>If <TT><I>it</I></TT> has
a header line, <TT>clear it[]</TT> deletes all rows. <TT>clear
it</TT> clears the header line.
<LI>If <TT><I>it</I></TT> does
not have a header line, both forms delete all rows and leave the
memory allocated.
</UL>
<P>
The effect of <TT>clear</TT> on an internal table is summarized
in Table 12.3. The effect of <TT>clear</TT> varies depending on
whether the internal table has a header line or not.
<P>
Table 12.3&nbsp;&nbsp;Effect of <TT>CLEAR</TT> on an Internal
Table<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=192><BR>
<CENTER><B>Statement</B></CENTER></TD>
<TD WIDTH=192><CENTER><B>If it has a <BR>
header line</B></CENTER>
</TD><TD WIDTH=192><CENTER><B>If it doesn't have<BR>
a header line</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>clear it</TT></TD><TD WIDTH=192>Clears the header line
</TD><TD WIDTH=192>Deletes all rows</TD></TR>
<TR VALIGN=TOP><TD WIDTH=192><TT>clear it[]</TT></TD><TD WIDTH=192>Deletes all rows
</TD><TD WIDTH=192>Deletes all rows</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
The program in Listing 12.8 illustrates the use of the <TT>clear</TT>
statement with an internal table.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 12.8  The clear Statement Can be Used to Clear the
Header Line or Delete the Contents of an Internal Table<BR>
</B>
<BLOCKQUOTE>
<PRE>
 1  report ztx1208.
 2  data: begin of it occurs 3,
 3            f1,
 4            end of it.
 5 
 6  it-f1 = 'X'.
 7  append: it, it.
 8 
 9  clear it.         &quot;it has a header line so clears the header line
10 write: 'f1=', it-f1.
11
12 write: / ''.
13 loop at it.
14    write it-f1.
15    endloop.
16
17 clear it[].       &quot;same as: refresh it.
18 loop at it.
19    write it-f1.
20    endloop.
21 write: / '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 12.8 produces this output:
<BLOCKQUOTE>
<PRE>
f1=            
  X X          
sy-subrc=     4
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 6 places <TT>'X'</TT> in the header line of <TT>it</TT>.
<LI>Line 7 appends two rows to <TT>it</TT>; both have <TT>'X'</TT>
in <TT>f1</TT>.
<LI>Line 9 clears the header line for <TT>it</TT>.
<LI>Line 10 writes blanks, showing that the header line for <TT>it</TT>
is clear.
<LI>Lines 13 through 15 produce output to show that the internal
table still contains two rows.
<LI>Line 17 clears the body of <TT>it</TT>, effectively deleting
all rows and leaving the memory allocated. The contents of the
header line are unchanged.
<LI>Lines 18 through 20 do not produce any output because the
internal table is empty.
<LI>Line 21 shows the return code after the loop. This again confirms
that no rows exist in the internal table.
</UL>
<H3><A NAME="UsingthedeleteStatementtoDeleteRowsfromanInternalTable">
Using the delete Statement to Delete Rows from an Internal
Table</A></H3>
<P>
Using the <TT>delete</TT> statement, you can delete one or more
rows from an internal table.
<H4>Syntax for the delete Statement</H4>
<P>
The following is the syntax for the <TT>delete</TT> statement.
<BLOCKQUOTE>
<PRE>
delete <I>it</I>  <I>(a)</I> [index <I>n</I>]
           <I>(b)</I> [from <I>i</I>] [to <I>j</I>]
           <I>(c)</I> [where <I>exp</I>]
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>n</I></TT>, <TT><I>fn</I></TT>,
and <TT><I>tn</I></TT> are numeric
literals, variables, or constants.
<LI><TT><I>exp</I></TT> is a logical
expression involving components of <TT><I>it</I></TT>.
</UL>
<P>
The following points apply:
<UL>
<LI>The additions following <TT><I>(a)</I></TT>,<I>
</I><TT><I>(b)</I></TT>,<I> </I>and
<TT><I>(c)</I></TT> are all optional.
<LI>Only one of <TT><I>(a)</I></TT>,<I>
</I><TT><I>(b)</I></TT>,<I> </I>or
<TT><I>(c)</I></TT> can be specified.
<LI><TT>delete it</TT> without any additions can only be used
inside <TT>loop at it</TT>. In that case, it deletes the current
row.
<LI>If <TT>index <I>n</I></TT>
is specified, the <I>n</I>th row of <TT><I>it</I></TT>
is deleted.
<LI>If <TT>from <I>i</I></TT>
is specified, rows are deleted beginning with the <I>i</I>th row.
<LI>If <TT>to <I>j</I></TT> is
specified, rows are deleted up to and including the <I>j</I>th
row.
<LI>If <TT>from</TT> is not specified with <TT>to</TT>, <TT>from
1</TT> is assumed.
<LI>If <TT>to</TT> is not specified with <TT>from</TT>, to the
last row in the table is assumed.
<LI>The expression <TT><I>exp</I></TT>
must have a component of <TT><I>it</I></TT>
on the left side of each comparison. For example, if <TT><I>it</I></TT>
has components <TT><I>f1</I></TT>
and <TT><I>f2</I></TT>, <TT><I>exp</I></TT>
could be <TT>where f1 = 'A' and f2 = 'B'</TT>.
</UL>
<P>
Listing 12.9 shows a sample program that deletes data from an
internal table using the <TT>delete</TT> statement.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 12.9&nbsp;&nbsp;Deleting Rows from an Internal Table
Can also be Done Using the delete Statement<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1209.
2  data: begin of it occurs 12,
3            f1,
4            end of it,
 5            alpha(12) value 'ABCDEFGHIJKL'.
 6 
 7  do 12 times varying it-f1 from alpha+0 next alpha+1.
 8      append it.
 9      enddo.
10
11 loop at it.
12     write: / sy-tabix, it-f1.
13     endloop.
14
15 delete it index 5.
16 skip.
17 loop at it.
18     write: / sy-tabix, it-f1.
19     endloop.
20
21 delete it from 6 to 8.
22 skip.
23 loop at it.
24     write: / sy-tabix, it-f1.
25     endloop.
26
27 delete it where f1 between 'B' and 'D'.
28 skip.
29 loop at it.
30     write: / sy-tabix, it-f1.
31     endloop.
32
33 loop at it where f1 between 'E' and 'J'.
34     delete it.
35     endloop.
36
37 skip.
38 loop at it.
39     write: / sy-tabix, it-f1.
40     endloop.

⌨️ 快捷键说明

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