📄 ch13.htm
字号:
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>On line 11, the <TT>table</TT> addition is omitted from before
<TT>it</TT> so that the row goes into the header line. <TT>lifnr</TT>
goes into <TT>it-lifnr</TT> and <TT>land1</TT> goes into <TT>it-land1</TT>.
Line 7 appends the header line to <TT>it</TT>.
</UL>
<P>
Line 22 moves rows into work area <TT>ztxlfa1</TT>. Line 24 uses
an additional statement-<TT>move-corresponding</TT>-to transfer
the work area to the header line <TT>it</TT>. Line 25 then appends
the header line to the body of <TT>it</TT>. This accomplishes
the same result as the first <TT>select</TT>, but it involves
an additional statement and so is less efficient.
<H3><A NAME="SummingUpselectInternalTablesandEfficiency">
Summing Up select, Internal Tables, and Efficiency</A></H3>
<P>
Table 13.2 contains a list of the various forms of <TT>select</TT>
as it is used with internal tables and their relative efficiency.
They are in descending order of most-to-least efficient.<BR>
<P>
<CENTER><B>Table 13.2 Various Forms of </B><TT><B>SELECT</B></TT><B>
when Filling an Internal Table</B></CENTER><CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=288><CENTER><B>Statement(s)</B></CENTER></TD><TD WIDTH=192><CENTER><B>Writes To</B></CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=288><TT>select into table <I>it</I></TT>
</TD><TD WIDTH=192>Body</TD></TR>
<TR VALIGN=TOP><TD WIDTH=288><TT>select into corresponding fields of table <I>it</I></TT>
</TD><TD WIDTH=192>Body</TD></TR>
<TR VALIGN=TOP><TD WIDTH=288><TT>select into <I>it</I></TT>
</TD><TD WIDTH=192>Header line</TD></TR>
<TR VALIGN=TOP><TD WIDTH=288><TT>select into corresponding fields of <I>it</I></TT>
</TD><TD WIDTH=192>Header line</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="UsingthelfalfblfcandlfcSampleTables"><FONT SIZE=5 COLOR=#FF0000>
Using the lfa1, lfb1, lfc1, and lfc3 Sample Tables</FONT></A></H2>
<P>
Before discussing the next topic, it will be helpful to become
more acquainted with the sample tables <TT>ztxlfa1</TT>, <TT>ztxlfb1</TT>,
<TT>ztxlfc1</TT>,<B> </B>and <TT>ztxlfc3</TT>; they will be frequently
used in the examples and exercises that follow. These tables are
based on the R/3 tables <TT>lfa1</TT>, <TT>lfb1</TT>, <TT>lfc1</TT>,<B>
</B>and <TT>lfc3</TT>. Please review the structures of these tables
within the DDIC as you read the descriptions below.
<P>
SAP designed R/3 to be usable by a conglomerate that consists
of multiple companies. During initial R/3 configuration, a company
code is assigned to each company in the conglomerate. The company
code forms part of the primary key in many master data tables,
which enables the conglomerate to keep the information for all
of its companies in a single database. Management can run reports
for an individual company code as well as consolidated reports
spanning multiple companies.
<P>
Table <TT>lfa1</TT> contains vendor master information that is
consistent across all companies. Ignoring <TT>mandt</TT>, its
primary key consists only of <TT>lifnr</TT>, the vendor number.
The fields of <TT>lfa1</TT>, for example, are vendor name and
address, telephone numbers, spoken language, and industry key
(defined by the type of product the vendor produces, such as chemical,
agricultural, and so on).
<P>
Table <TT>lfb1</TT> contains vendor master information that is
specific to a company. Its primary key consists of <TT>lifnr</TT>
and <TT>bukrs</TT>: the company code field (see Figure 13.6).
Stored within <TT>lfb1</TT> are the company's account number with
the vendor, reconciliation account number, withholding tax information,
an interest calculation indicator, and so on.
<P>
<A HREF="javascript:popUp('f13-6.gif')"><B>Figure 13.6 :</B> <I>The relationship between the primary keys</I>.</A>
<P>
Figure 13.6 is the relationship between the primary keys of tables
<TT>lfa1</TT> through <TT>lfc3</TT>. They all begin with <TT>lifnr</TT>,
and <TT>lfa1</TT> uses it as the entire primary key. <TT>lfb1</TT>
uses the vendor number and company code. <TT>lfc1</TT> uses vendor
number, company code, and fiscal year. <TT>lfc3</TT> uses those
same fields plus a special G/L indicator.
<P>
Table <TT>lfc1</TT> contains G/L (general ledger) transaction
figures. Each row holds one fiscal year's worth of transaction
figures for a vendor within a company. The primary key consists
of <TT>lifnr</TT>, <TT>bukrs</TT>, and <TT>gjahr</TT>, the fiscal
year.
<P>
The set of fields named <TT>umNNs</TT>, <TT>umNNh</TT>, and <TT>umNNu</TT>
is repeated 16 times within an <TT>lfc1</TT> row. Each of the
first 12 sets contains the debit postings, credit postings, and
sales for one posting period (usually a month) of the fiscal year.
The last four sets are additional closing periods used to contain
year-end accounting adjustments.<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
There can be either 12 or 13 posting periods in a fiscal year. This is deter-mined when the system is initially configured. If there are 13, then there are only three additional closing periods.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Table <TT>lfc3</TT> contains special G/L transaction figures.
Special G/Ls use an alternative reconciliation account in the
general ledger. Each row of <TT>lfc3</TT> holds a summary of the
special G/Ls for an entire fiscal year in three fields: <TT>saldv</TT>,
<TT>solll</TT>, and <TT>habnl</TT>. These contain the balance
carried forward and the total debit and credit posting for the
fiscal year. The primary key is the same as <TT>lfc1</TT>, plus
a special G/L indicator: <TT>shbkz</TT>. The value in this field
indicates which alternative reconciliation account is used.
<P>
Most master data tables use a similar model for their primary
key structures.
<P>
In this book, simplified versions of these tables are used: <TT>ztxlfa1</TT>,
<TT>ztxlfb1</TT>, <TT>ztxlfc1</TT>, and <TT>ztxlfc3</TT>. These
were created and populated by the setup routine.
<H2><A NAME="ControlBreakProcessing"><FONT SIZE=5 COLOR=#FF0000>
Control Break Processing</FONT></A></H2>
<P>
After you fill an internal table with data, you often need to
write the data out. This output will frequently contain summary
information (such as totals) at the top or bottom of the report.
There might also be interim summaries (such as subtotals) within
the body of the report.
<P>
For example, suppose you need to write the G/L figures from <TT>ztxlfc1</TT>
for each vendor, with subtotals by fiscal year and a grand total
at the bottom of the report.
<P>
To do this, you can read the data into an internal table and then,
within <TT>loop at</TT>, use the following statements:
<UL>
<LI><TT>at first</TT> / <TT>endat</TT>
<LI><TT>at last</TT> / <TT>endat</TT>
<LI><TT>at new</TT> / <TT>endat</TT>
<LI><TT>at end of</TT> / <TT>endat</TT>
<LI><TT>sum</TT>
<LI><TT>on change of</TT> / <TT>endon</TT>
</UL>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
The first statement of each of these statement pairs-except for
<TT>sum</TT>-controls when the code that lies between them is
executed. This type of control is called a <I>control break</I>.
Their purpose is to execute the code between them whenever a specific
condition in the data is detected during the processing of the
loop.
<H3><A NAME="UsingtheatfirstandatlastStatements">
Using the at first and at last Statements</A></H3>
<P>
Use the <TT>at first</TT> and <TT>at last</TT> statements to perform
processing during the first or last loop pass of an internal table.
<H4>Syntax for the at first and at last Statements</H4>
<P>
The following is the syntax for the <TT>at first</TT> and <TT>at
last</TT> statements.
<BLOCKQUOTE>
<PRE>
loop at <I>it</I>.
---
at first.
---
endat.
---
at last.
---
endat.
---
endloop.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>it</I></TT> is an internal
table.
<LI><TT>---</TT> represents any number of lines of code (even
zero).
</UL>
<P>
The following points apply:
<UL>
<LI>These statements can only be used within <TT>loop at</TT>;
they cannot be used within <TT>select</TT>.
<LI><TT>at first</TT> does not have to come before <TT>at last</TT>.
The order of these statements can be interchanged.
<LI>These statements can appear multiple times within the same
loop. For example, you could have two <TT>at first</TT> and three
<TT>at last</TT> statements within one loop and they can appear
in any order.
<LI>These statements should not be nested inside of one another
(that is, <TT>at last</TT> should not be placed inside of <TT>at
first</TT> and <TT>endat</TT>).
<LI>There are no additions to these statements.
</UL>
<P>
The first time through the loop, the lines of code between <TT>at
first</TT> and <TT>endat</TT> are executed. The last time through
the loop, the lines of code between <TT>at last</TT> and <TT>endat</TT>
are executed. If there are multiple occurrences of <TT>at first</TT>,
they are all executed. <TT>at last</TT> behaves in a similar fashion.
<P>
Use <TT>at first</TT> for:
<UL>
<LI>Loop initialization processing
<LI>Writing totals at the top of a report
<LI>Writing headings
</UL>
<P>
Use <TT>at last</TT> for:
<UL>
<LI>Loop termination processing
<LI>Writing totals at the bottom of a report
<LI>Writing footings
</UL>
<P>
Listing 13.7 shows a sample program that uses these constructs.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 13.7 Using at first to Write Headings and
at last to Underline the Last Line<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1307.
2 tables ztxlfc3.
3 data it like ztxlfc3 occurs 25 with header line.
4 select * from ztxlfc3 into table it where shbkz = 'Z'.
5 loop at it.
6 at first.
7 write: / 'Vendor',
8 12 'Cpny',
9 17 'Year',
10 22 'Bal C/F'.
11 uline.
12 endat.
13 write: / it-lifnr,
14 12 it-bukrs,
15 17 it-gjahr,
16 22 it-saldv.
17 at last.
18 write: / '----------',
19 12 '----',
20 17 '----',
21 22 '-------------------'.
22 endat.
23 endloop.
24 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.7 produces this output:
<BLOCKQUOTE>
<PRE>
Vendor Cpny Year Bal C/F
----------------------------------------
1000 1000 1995 0.00
1000 1000 1996 5,000.00
1000 1000 1998 4,000.00
1040 4000 1997 0.00
1070 2000 1997 1,000.00
1090 2000 1997 250.50
V1 1000 1992 1,000.00
V1 3000 1990 1,000.00
V1 3000 1994 1,000.00
V4 4000 1997 100.00
V6 2000 1997 1,000.00
V6 4000 1997 0.00
---------- ---- ---- -------------------
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Lines 6 through 11 are only executed the first time through
the loop. This example writes headings followed by an underline
(line 11).
<LI>Lines 13 through 16 write out the contents of a row. They
are executed for every row.
<LI>Lines 17 through 22 are only executed the last time through
the loop. This example writes an underline at the bottom of each
of the columns.
</UL>
<H4>Using the Unexpected: Component Values Gone</H4>
<P>
Between the <TT>at first</TT> and <TT>endat</TT>, or between the
<TT>at last</TT> and <TT>endat</TT>, the component values of the
work area row will not contain any data. The default key fields
are filled with <TT>*</TT> (asterisks) and the numeric fields
are set to zeros. The <TT>endat</TT> restores the contents to
the values they had prior to entering the <TT>at</TT>. Changes
to the work area within <TT>at</TT> and <TT>endat</TT> are lost.
<P>
Listing 13.8 demonstrates that the components of the default key
fields are filled with asterisks, and the non-key fields filled
with zeros, inside an <TT>at</TT> or <TT>endat</TT> statement.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 13.8 The Contents of the Internal Table
Fields Between at and endat<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx1308.
2 tables ztxlfc3.
3 data it like ztxlfc3 occurs 1 with header line.
4 select * up to 1 rows from ztxlfc3 into table it.
5 loop at it.
6 write: / 'Before ''at first'':',
7 / it-lifnr, it-bukrs, it-gjahr, it-shbkz, it-saldv,
8 it-solll.
9 at first.
10 write: / 'Inside ''at first'':',
11 / it-lifnr, it-bukrs, it-gjahr, it-shbkz, it-saldv,
12 it-solll.
13 it-lifnr = 'XXXX'.
14 endat.
15 write: / 'Between ''at first'' and ''at last'':',
16 / it-lifnr, it-bukrs, it-gjahr, it-shbkz, it-saldv,
17 it-solll.
18 at last.
19 write: / 'Inside ''at last'':',
20 / it-lifnr, it-bukrs, it-gjahr, it-shbkz, it-saldv,
21 it-solll.
22 it-lifnr = 'XXXX'.
23 endat.
24 write: / 'After ''at last'':',
25 / it-lifnr, it-bukrs, it-gjahr, it-shbkz, it-saldv,
26 it-solll.
27 endloop.
28 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.8 produces this output:
<BLOCKQUOTE>
<PRE>
Before 'at first':
1000 1000 1990 A 1,000.00 500.00
Inside 'at first':
********** **** **** * 0.00 0.00
Between 'at first' and 'at last':
1000 1000 1990 A 1,000.00 500.00
Inside 'at last':
********** **** **** * 0.00 0.00
After 'at last':
1000 1000 1990 A 1,000.00 500.00
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 4 selects one record from <TT>ztxlfc3</TT> into <TT>it</TT>.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -