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

📄 ch13.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 13- Advanced Internal Tables, Part 2</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="../ch12/ch12.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch12/ch12.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="../ch14/ch14.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch14/ch14.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;13</H1>
<H1>Advanced Internal Tables, Part 2</H1>


<UL>
<LI><A HREF="#ChapterObjectives">
Chapter Objectives</A>
<LI><A HREF="#FillinganInternalTablefromaDatabaseTable">
Filling an Internal Table from a Database Table</A>
<UL>
<LI><A HREF="#SelectingMultipleRowsDirectlyintoanInternalTable">
Selecting Multiple Rows Directly into an Internal Table</A>
<LI><A HREF="#AddingRowsonebyoneUsingselect">
Adding Rows one by one Using select</A>
<LI><A HREF="#SummingUpselectInternalTablesandEfficiency">
Summing Up select, Internal Tables, and Efficiency</A>
</UL>
<LI><A HREF="#UsingthelfalfblfcandlfcSampleTables">
Using the lfa1, lfb1, lfc1, and lfc3 Sample Tables</A>
<LI><A HREF="#ControlBreakProcessing">
Control Break Processing</A>
<UL>
<LI><A HREF="#UsingtheatfirstandatlastStatements">
Using the at first and at last Statements</A>
<LI><A HREF="#UsingtheatnewandatendofStatements">
Using the at new and at end of Statements</A>
<LI><A HREF="#UsingthesumStatement">
Using the sum Statement</A>
<LI><A HREF="#UsingtheonchangeofStatement">
Using the on change of Statement</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="#Quiz">
Quiz</A>
<LI><A HREF="#Exercise1">
Exercise 1</A>
<LI><A HREF="#Exercise2">
Exercise 2</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 will be able to:
<UL>
<LI>Fill an internal table from a database table using the most
efficient constructs
<LI>Perform control break processing on internal tables using
<TT>at</TT> and <TT>on change of</TT>
</UL>
<H2><A NAME="FillinganInternalTablefromaDatabaseTable"><FONT SIZE=5 COLOR=#FF0000>
Filling an Internal Table from a Database Table</FONT></A></H2>
<P>
Quite often in ABAP/4, the data you put into an internal table
comes from one or more database tables. This section presents
the best ways to do this.
<P>
The ways of filling an internal table from the database fall into
two categories:
<UL>
<LI>Selecting multiple rows directly into the body of an internal
table
<LI>Selecting single rows into a work area and then appending
</UL>
<H3><A NAME="SelectingMultipleRowsDirectlyintoanInternalTable">
Selecting Multiple Rows Directly into an Internal Table</A></H3>
<P>
To select multiple rows directly into the body of an internal
table, use the <TT>into table</TT> addition of the <TT>select</TT>
statement. It takes the selected rows and places them into the
body of an internal table in a single operation known as an array
operation. No work areas are used or needed.
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
An <I>array operation</I> is any statement that performs an operation
on multiple rows of an internal table, instead of a single row
at a time. Array operations are always more efficient than single
row operations.<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
<TT>select into table </TT>is the most efficient way to fill an internal table from the database.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<H4>Syntax for the into table Addition of the select Statement
</H4>
<P>
The following is the syntax for the <TT>into table</TT> addition
of the <TT>select</TT> statement.
<BLOCKQUOTE>
<PRE>
<I>(a)</I> select *
<I>(b)</I> select <I>f1</I> <I>f2</I> . . .
                         
             from <I>dbtab</I> into [corresponding fields of] table <I>it</I>.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>dbtab</I></TT> is the
name of a database table.
<LI><TT><I>f1</I></TT> and <TT><I>f2</I></TT>
are fields within <TT><I>dbtab</I></TT><I>.</I>
<LI><TT><I>it</I></TT> is the
name of an internal table.
</UL>
<P>
The following points apply:
<UL>
<LI><TT><I>it</I></TT> can have
a header line.
<LI>Other additions, such as <TT>where</TT> and <TT>order by</TT>,
can follow <TT><I>it</I></TT>.
<LI><TT>endselect</TT> is not used with <TT>into table</TT>. <TT>select
into table</TT> does not start a loop, consequently no <TT>endselect</TT>
is needed.
</UL>
<P>
The <TT>select into table</TT> statement places all selected rows
directly into the body of <TT><I>it</I></TT>.
Existing internal table contents are first discarded. Listing
13.1 contains a sample program using <TT>select into table</TT>.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 13.1&nbsp;&nbsp;This Program Reads Rows from a Database
Table Directly into an Internal Table<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1301.
2  tables ztxlfa1.
3  data it like ztxlfa1 occurs 23 with header line.
4 
5  select * from ztxlfa1 into table it.   &quot;don't code an endselect
6  loop at it.
7      write: / it-lifnr, it-name1.
8      endloop.
9 
10 select * from ztxlfa1 into table it
11     where lifnr between 'V2' and 'V5'.
12 skip.
13 loop at it.
14     write: / it-lifnr, it-name1.
15     endloop.
16 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 13.1 produces this output:
<BLOCKQUOTE>
<PRE>
V9         Code Now, Specs Later Ltd.         
1000       Parts Unlimited                    
1010       Industrial Pumps Inc.              
1020       Chemical Nation Ltd.               
1030       ChickenFeed Ltd.                   
1050       The Bit Bucket                     
1060       Memory Lane Ltd.                   
1070       Flip My Switch Inc.                
V10        Duncan's Mouse Inc.                
V6         Anna Banana Ltd.                  
V8         Smile When You Say That Ltd.       
V11        Weiner Schnittsel Inc.             
V12        Saurkrouten                        
1040       Motherboards Inc.                  
1080       Silicon Sandwich Ltd.              
1090       Consume Inc.                       
2000       Monitors and More Ltd.             
V1         Quantity First Ltd.                
V2         OverPriced Goods Inc.              
V3         Fluffy Bunnies Ltd.                
V4         Moo like a Cow Inc.                
V5         Wolfman Sport Accessories Inc.     
V7         The Breakfast Club Inc.            
                                              
V2         OverPriced Goods Inc.              
V3         Fluffy Bunnies Ltd.           
V4         Moo like a Cow Inc.           
V5         Wolfman Sport Accessories Inc.
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 5 retrieves all rows from database table <TT>ztxlfa1</TT>
and places them directly into the body of the internal table <TT>it</TT>.
The header line is not used and its contents are unchanged.
<LI>Line 10 is like line 5, except that it only retrieves vendor
numbers between <TT>V2</TT> and <TT>V5</TT>. The existing contents
of <TT>it</TT> are discarded before it is filled.
</UL>
<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 an <TT>endselect </TT>with <TT>select into table</TT>. A syntax error will occur.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<H4>select into table and Sorting</H4>
<P>
Suppose your program already reads data into an internal table.
If that data needs to be sorted, use the <TT>sort</TT> statement.
Don't use <TT>order by</TT> on <TT>select</TT> even if those fields
are supported by an index. On a quiet, standalone system, measurements
reveal that the <TT>sort</TT> statement is a little faster. Even
if they were at a par, <TT>sort</TT> would still be the preferred
method because it offloads cycles from the database server to
the application server.<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 order by with select into table. Use sort on the internal table instead.</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<H4>Fields You Select Must Fit into the Internal Table</H4>
<P>
Imagine this scenario:
<UL>
<LI>A row is retrieved from <TT><I>dbtab</I></TT><I>.</I>
<LI>A row is allocated in <TT><I>it</I></TT><I>.</I>
<LI>The row from <TT><I>dbtab</I></TT>
is moved byte-by-byte into the new row in <TT><I>it</I></TT><I>.</I>
</UL>
<P>
Each row from <TT><I>dbtab</I></TT>
is moved byte-by-byte into a new row of <TT><I>it</I></TT>,
which is exactly like assigning one field string to another. There
is only one difference between this and a field string assignment:
<TT><I>dbtab</I></TT> can be shorter
(in bytes) than <TT><I>it</I></TT>,
but it cannot be longer than <TT><I>it</I></TT><I>.</I>
If it is longer, a short dump (error <TT>SAPSQL_SELECT_TAB_TOO_SMALL</TT>)
occurs. The data types and lengths of each sending field in <TT><I>dbtab</I></TT>
should match the receiving field in <TT><I>it</I></TT>.
Any remaining fields in <TT><I>it</I></TT>
are filled with initial values (blanks or zeros). Figures 13.1
through 13.4 illustrate this point.
<P>
<A HREF="javascript:popUp('f13-1.gif')"><B>Figure 13.1 :</B> <I>Database table ztx1302 contains three fields:
f1, f2, and f3. f1 is a single byte, f2 is two bytes long, and
f3 is one byte</I>.</A>
<P>
<A HREF="javascript:popUp('f13-2.gif')"><B>Figure 13.2 :</B> <I>The * causes all fields to be selected,
in order, from database table ztx1302. They fit exactly into each
row of it because the structure of it matches that of the database
table</I>.</A>
<P>
<A HREF="javascript:popUp('f13-3.gif')"><B>Figure 13.3 :</B> <I>In this example, only the first two fields
are selected from database table ztx1302, for a total of three
bytes. They are placed byte-by- byte into the first three bytes
of each row of it</I>.</A>
<P>
<A HREF="javascript:popUp('f13-4.gif')"><B>Figure 13.4 :</B> <I>Again, the first two fields are selected
from database table ztx1302, but this time in a different order
than they appear in the internal table. This causes corrupt data
when they are placed byte-by-byte into each row of it. it-f1 receives
the first byte of f2, and it-f2 receives the last byte of f2 and
one byte from f1</I>.</A>
<P>
Table 13.1 summarizes the rules for using <TT>select</TT><I> </I>with<I>
</I><TT>into table</TT>. Selected fields must fit into the internal
table. This table describes the restrictions you will face.
<P>
<CENTER><B>Table 13.1&nbsp;&nbsp;Rules for Using s</B><TT><B>ELECT</B></TT><B>
with </B><TT><B>INTO TABLE</B></TT></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=96>
<BR>
<CENTER><B>Statement</B></CENTER></TD>
<TD WIDTH=192><CENTER><B>Which Fields Are<BR>
Selected?</B></CENTER>
</TD><TD WIDTH=288><BR>
<CENTER><B>Restrictions</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT>Select *</TT></TD><TD WIDTH=192>All fields in the database table
</TD><TD WIDTH=288>The internal table must contain at least as many fields as the database table, and they must be like the database table fields.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=96><TT>Select <I>f1</I></TT>
</TD><TD WIDTH=192><TT><I>f1</I></TT> from the database table
</TD><TD WIDTH=288>The internal table must begin with a field like <TT><I>f1</I></TT>, or have only one field and it must be like <TT><I>f1</I></TT><I>.</I>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Listings 13.2 and 13.3 illustrate this concept.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>

⌨️ 快捷键说明

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