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

📄 ch11.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>

<HEAD>
<SCRIPT LANGUAGE="JavaScript">


<!--

function popUp(pPage) {
 popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
 figDoc= popUpWin.document;
 zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
 zhtm += '</head>';
 zhtm += '<BODY bgcolor="#FFFFFF">';
 zhtm += '<IMG SRC="' + pPage + '">';
 zhtm += '<P><B>' + pPage + '</B>';
 zhtm += '</BODY></HTML>';
 figDoc.write(zhtm);
 figDoc.close();
 popUpWin.focus();
 }

//-->


</SCRIPT>

        <META NAME="GENERATOR" Content="Symantec Visual Page 1.0.1">
        <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
        <TITLE>Sams Teach Yourself ABAP/4&reg; in 21 Days -- Day 11- Internal Tables</TITLE>
</HEAD>

<BODY TEXT="#000000" BGCOLOR="#FFFFFF">

<CENTER>
<H1><IMG SRC="../button/sams.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/sams.gif" BORDER="0"></H1>
</CENTER>
<CENTER>
<P><A HREF="../ch10/ch10.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch10/ch10.htm"><IMG SRC="../button/previous.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/previous.gif" BORDER="0"></A>
<A HREF="../index.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/index.htm"><IMG SRC="../button/contents.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/contents.gif" BORDER="0"></A> 
<A HREF="../ch12/ch12.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch12/ch12.htm"><IMG SRC="../button/next.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/next.gif" BORDER="0"></A> 

<H1>Sams Teach Yourself ABAP/4<sup>&reg;</sup> in 21 Days</H1></CENTER>
<HR SIZE=4>




<H1>Day&nbsp;11</H1>
<H1>Internal Tables</H1>


<UL>
<LI><A HREF="#ChapterObjectives">
Chapter Objectives</A>
<LI><A HREF="#InternalTableBasics">
Internal Table Basics</A>
<UL>
<LI><A HREF="#DefinitionofanInternalTable">
Definition of an Internal Table</A>
<LI><A HREF="#AddingDatatoanInternalTableUsingtheappendStatement">
Adding Data to an Internal Table Using the append Statement
</A>
<LI><A HREF="#ReadingDatafromanInternalTable">
Reading Data from an Internal Table</A>
<LI><A HREF="#SortingtheContentsofanInternalTable">
Sorting the Contents of an Internal Table</A>
</UL>
<LI><A HREF="#Summary">
Summary</A>
<LI><A HREF="#QampABR">
Q&amp;A<BR>
</A>
<LI><A HREF="#Workshop">
Workshop</A>
<UL>
<LI><A HREF="#QuizQuestions">
Quiz Questions</A>
<LI><A HREF="#Exercise">
Exercise 1</A>
</UL></UL>

<HR>
<H2><A NAME="ChapterObjectives"><FONT SIZE=5 COLOR=#FF0000>
Chapter Objectives</FONT></A></H2>
<P>
After you complete this chapter, you should be able to:
<UL>
<LI>Define an internal table with or without a header line using
the <TT>data</TT> and <TT>include structure</TT> statements.
<LI>Fill an internal table by using <TT>append</TT> via a header
line or an explicit work area.
<LI>Use <TT>loop at</TT> and <TT>loop at...where</TT> to read
the contents of an internal table, and use <TT>sy-tabix</TT> to
determine the current row index.
<LI>Locate a single row in an internal table by using <TT>read
table</TT> and its variations.
<LI>Sort internal tables and use the <TT>as text</TT> addition.
</UL>
<H2><A NAME="InternalTableBasics"><FONT SIZE=5 COLOR=#FF0000>
Internal Table Basics</FONT></A></H2>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
An <I>internal table</I> is a temporary table stored in RAM on
the application server. It is created and filled by a program
during execution and is discarded when the program ends. Like
a database table, an internal table consists of one or more rows
with an identical structure, but unlike a database table, it cannot
hold data after the program ends. Use it as temporary storage
for manipulating data or as a temporary private buffer.
<H3><A NAME="DefinitionofanInternalTable">
Definition of an Internal Table</A></H3>
<P>
An internal table consists of a body and an optional header line
(see Figure 11.1).<BR>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
The <I>body</I> holds the rows of the internal table. All rows
within it have the same structure. The term &quot;internal table&quot;
itself usually refers to the body of the internal table.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
The <I>header line</I> is a field string with the same structure
as a row of the body, but it can only hold a single row. It is
a buffer used to hold each record before it is added or each record
as it is retrieved from the internal table.
<P>
Figure 11.1 shows the definition of an internal table named <TT>it</TT>.
<P>
<A HREF="javascript:popUp('f11-1.gif')"><B>Figure 11.1 :</B> <I>The definition of an internal table named
it is shown at the top of this figure. The header line and body
are also shown</I>.</A><P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
In this book, a naming convention is used for internal tables. For simple programs with only a single internal table, the internal table name will usually be it (for internal table). If multiple internal tables exist, the name of each will usually begin with it.</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
To define an internal table body, use <TT>occurs <I>n</I></TT>
on the definition of any field string except <TT>tables</TT>.
<TT>occurs</TT> creates the body of the internal table. The memory
for the body is not allocated until the first row is added to
it. Without <TT>occurs</TT>, you only have a field string.
<P>
To define an internal table with a header line, you must include
either <TT>begin of</TT> or <TT>with header line</TT> in the definition.
A header line is automatically created if <TT>begin of</TT> appears
in the definition. If you use <TT>like</TT> instead of <TT>begin
of</TT>, the internal table will not have a header line unless
you add <TT>with header line</TT> after the <TT>occurs</TT> clause.
This is illustrated in lines 2 through 10 of Listing 11.1.
<P>
The only time you would create an internal table without a header
line is in the case of a nested internal table. A nested internal
table cannot have a header line.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 11.1&nbsp;&nbsp;Basic Ways of Defining Internal Tables
with and Without a Header Line<BR>
</B>
<BLOCKQUOTE>
<PRE>
 1 report ztx1101.
 2 data: begin of it1 occurs 10,          &quot;has a header line
 3         f1,
 4         f2,
 5         f3,
 6         end of it1.
 7
 8 data it2 like ztxlfa1 occurs 100.      &quot;doesn't have a header line
 9
10 data it3 like ztxlfa1 occurs 100 with header line. &quot;it does now
</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.1 does not produce any output.
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 2 begins the definition of an internal table named <TT>it1</TT>.
It is an internal table because of the presence of the <TT>occurs</TT>
clause. It has three components: <TT>f1</TT>, <TT>f2</TT>, and
<TT>f3</TT>. Each is type <TT>c</TT>, length 1. This internal
table has a header line because the definition contains the words
<TT>begin of</TT>.
<LI>Line 8 defines internal table <TT>it2</TT>. It has the same
components as the DDIC structure <TT>ztxlfa1</TT>. It does not
have a header because neither <TT>begin of</TT> nor <TT>with header
line</TT> appear in the definition.
<LI>The definition of <TT>it3</TT> on line 10 is almost the same
as the one for <TT>it2</TT> on line 8. The only difference is
the addition <TT>with header line</TT>. This, of course, causes
<TT>it3</TT> to have a header line.
</UL>
<H4>Clearing Up the Main Source of Confusion Surrounding Internal
Tables</H4>
<P>
Figure 11.1 contains a typical internal table definition. It defines
<I>two</I> things: the body of an internal table and a header
line. Confusion arises because both the body and the header line
are named <TT>it</TT>. <TT>it</TT> is the name of the body, and
<TT>it</TT> is also the name of the header line. Where you place
the name <TT>it</TT> in your code determines to what you are referring.
<P>
When used in an assignment statement, <TT>it</TT> always refers
to the header line. For example, <TT>it-f1 = 'A'</TT> refers to
component <TT>f1</TT> of the header line named <TT>it</TT>. Or
in <TT>f3 = it-f1</TT>, the header line <TT>it</TT> is also referred
to.
<H4>Examples of Internal Table Definitions</H4>
<P>
To define internal tables with and without header lines, use the
forms recommended in Listing 11.2.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 11.2&nbsp;&nbsp;These Definitions Create Internal Tables
with and Without a Header Line<BR>
</B>
<BLOCKQUOTE>
<PRE>
 1 report ztx1102.
 2 data: begin of it1 occurs 10,          &quot;has a header line
 3         f1,
 4         f2,
 5         f3,
 6         end of it1.
 7
 8 data it2 like ztxlfa1 occurs 100.      &quot;doesn't have a header line
 9
10 data it3 like ztxlfa1 occurs 100 with header line. &quot;it does now
11
12 data it4 like it3 occurs 100.          &quot;doesn't have a header line
13
14 data     begin of it5 occurs 100.      &quot;has a header line
15 include      structure ztxlfa1.
16 data         end of it5.
17
18 data     begin of it6 occurs 100.      &quot;this is why you might use
19 include      structure ztxlfa1.        &quot;the include statement
20 data:        delflag,
21              rowtotal,
22              end of it6.
23
24 data: begin of it7 occurs 100,         &quot;don't do it this way
25           s like ztxlfa1,              &quot;component names will be
26           end of it7.                  &quot;prefixed with it7-s-
27
28 data it8 like sy-index occurs 10
29     with header line.
30
31 data: begin of it9 occurs 10,
32           f1 like sy-index,
33           end of it9.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
The code in Listing 11.2 does not produce any output.
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 2 through 10 are the same as those in Listing 11.1;
they are included here for your review.
<LI>On line 12, <TT>it4</TT> is defined like the field string
named <TT>it3</TT>, <I>not</I> like the internal table <TT>it3</TT>.
Thus, an <TT>occurs</TT> addition is required to make it an internal
table; this one does not have a header line because there is no
<TT>begin of</TT> or <TT>with header line</TT>.
<LI>Lines 14 through 16 show the old way of defining an internal
table. <TT>It5</TT> is the same as <TT>it3</TT> defined on line
10. The <TT>like</TT> addition was only introduced in release
3.0; prior programs use definitions like this one. <TT>Include
structure</TT> is not used now except for cases such as the following
definition of <TT>it6</TT>. (At this time, you might want to review
the <TT>include structure</TT> statement covered on Day 5, &quot;The
Data Dictionary, Part 3.&quot;)
<LI>This is the only practical reason you would use the <TT>include</TT>
statement now (after release 3). It enables you to create a field
string having all components from a DDIC structure plus some additional
ones. The definition of <TT>it6</TT> contains the components of
<TT>ztxlfa1</TT> and two additional fields <TT>delflag</TT> and
<TT>rowtotal</TT>. This internal table has a header line because
you see <TT>begin of</TT> in the definition.
<LI>Lines 24 through 26 show the wrong way to use <TT>like</TT>.
The components of this internal table all begin with <TT>it7-s</TT>,
for example, <TT>it7-s-lifnr</TT> and <TT>it7-s-land1</TT>. Although
it is workable, it is probably not what was intended, and introduces
an unnecessary level into the component names.
<LI>When defining an internal table that has a single component,
you can use either the declaration on line 28 or on line 31.
</UL>
<H3><A NAME="AddingDatatoanInternalTableUsingtheappendStatement">
Adding Data to an Internal Table Using the append Statement
</A></H3>
<P>
To add a single row to a internal table, you can use the <TT>append</TT>
statement. <TT>append</TT> copies a single row from any work area
and places it in the body at the end of the existing rows. The
work area can be the header line, or it can be any other field
string having the same structure as a row in the body.
<H4>Syntax for the append Statement</H4>
<P>
The following is the syntax for the <TT>append</TT> statement.
<BLOCKQUOTE>
<PRE>

⌨️ 快捷键说明

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