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

📄 ch13.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<P>
<B>Listing 13.2&nbsp;&nbsp;What Happens if Table Structures Differ
When Using the Select-into Statement<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1302.
2  tables ztxlfa1.
3  data               begin of it occurs 2.
4  include structure      ztxlfa1.
5  data:                  invoice_amt type p,
6                         end of it.
7 
8  select * from ztxlfa1 into table it where land1 = 'DE'.
9  loop at it.
10     write: / it-lifnr, it-land1, it-regio, it-invoice_amt.
11     endloop.
12
13 skip.
14 select lifnr land1 regio from ztxlfa1 into table it where land1 = 'DE'.
15 loop at it.
16     write: / it-mandt, it-lifnr, it-land1, it-regio.
17     endloop.
18 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.2 produces this output:
<BLOCKQUOTE>
<PRE>
V11        DE  07                0 
V12        DE  14                0 
V8         DE  03                0 

V11 DE         07
V12 DE         14
V8  DE         03
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 3 defines <TT>it</TT> as having all of the fields of
DDIC structure <TT>ztxlfa1</TT>, plus an additional field.
<LI>Line 8 selects all fields from <TT>ztxlfa1</TT>. They fill
the beginning of each row in <TT>it</TT>. <TT>Invoice_amt</TT>
is set to zero.
<LI>Line 14 selects <TT>lifnr land1</TT> and <TT>regio</TT> into
the first part of each row of <TT>it</TT>. However, <TT>it</TT>
begins with <TT>mandt</TT>, not <TT>lifnr</TT>, because the DDIC
structure <TT>ztxlfa1</TT> begins with <TT>mandt</TT>. The output
shows that the first three bytes of <TT>lifnr</TT> end up in <TT>it-mandt</TT>,
<TT>it-lifnr</TT> is shifted left three bytes and also contains
<TT>land1</TT>, and <TT>regio</TT> is empty.
</UL>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 13.3&nbsp;&nbsp;Your Program Will Produce a Short Dump
If You Try to Put More Fields into an Internal Table than Exist
in That Table<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1303.
2  tables ztxlfa1.
3  data: begin of it occurs 23,
4            lifnr like ztxlfa1-lifnr,    &quot;this is a char 10 field
5            land1 like ztxlfa1-land1,    &quot;this is a char  3 field
6            end of it.
7 
8  *The next line causes a short dump. The internal table is too narrow.
9  select * from ztxlfa1 into table it.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
The code in Listing 13.3 produces the output shown in Figure 13.5.
<P>
<A HREF="javascript:popUp('f13-5.gif')"><B>Figure 13.5 :</B> <I>This short dump occurs when you run report
ztx1303. It happens when you select more fields than will fit
into the internal table</I>.</A>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 3 defines an internal table that has two components.
The total length of <TT>it</TT> is 13 bytes.
<LI>Line 9 selects all fields from <TT>ztxlfa1</TT> into <TT>it</TT>.
The total length of all of these fields is more than 13 bytes.
Consequently, a short dump occurs.
</UL>
<H4>Using the corresponding fields Addition</H4>
<P>
If the components of the internal table aren't in the same order
as those from the database, or if they don't have the same data
type and length, you can use the <TT>corresponding fields</TT>
addition. This addition has the same effect as the <TT>move corresponding</TT>
statement does on a field string: It moves fields from the database
table into fields <I>of the same name</I> in the internal table
body.<P>
<CENTER>
<TABLE BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
<TT>corresponding</TT> fields performs one assignment per field instead of a single assignment for the entire row, so it adds overhead to the <TT>select</TT> statement. You should use it only when necessary.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
The following points apply:
<UL>
<LI>The order of the sending and receiving fields does not matter.
<LI>Sending fields that do not have a corresponding receiving
field are discarded.
<LI>Receiving fields that do not have a corresponding sending
field are unchanged.
</UL>
<P>
Listing 13.4 shows efficient and inefficient uses for this addition.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 13.4&nbsp;&nbsp;Using the corresponding fields Addition
to Fill Internal Tables from the Database<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1304.
2  tables ztxlfa1.
3  data: begin of it1 occurs 23,
4            lifnr     like ztxlfa1-lifnr,
5            lifnr_ext like ztxlfa1-lifnr,
6            land1     like ztxlfa1-land1,
7            end of it1,
8        begin of it2 occurs 23,
9            lifnr     like ztxlfa1-lifnr,
10           land1     like ztxlfa1-land1,
11           end of it2.
12
13 * This is efficient usage:
14 select lifnr land1 from ztxlfa1
15     into corresponding fields of table it1
16     where lifnr between 'V10' and 'V12'.
17 loop at it1.
18     write: / it1-lifnr, it1-land1.
19     endloop.
20
21 * This is inefficient:
22 select * from ztxlfa1
23     into corresponding fields of table it2
24     where lifnr between 'V10' and 'V12'.
25 skip.
26 loop at it1.
27     write: / it1-lifnr, it1-land1.
28     endloop.
29
30 * Instead, write:
31 select lifnr land1 from ztxlfa1 into table it2
32     where lifnr between 'V10' and 'V12'.
33 skip.
34 loop at it2.
35     write: / it2-lifnr, it2-land1.
36     endloop.
37 free: it1, it2.
</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.4 produces this output:
<BLOCKQUOTE>
<PRE>
     V10        CC 
     V11        DE 
     V12        DE 
                   
     V10        CC 
     V11        DE 
     V12        DE 
                   
     V10        CC 
     V11        DE 
     V12        DE 
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>Line 14 reads the <TT>lifnr</TT> and <TT>land1</TT> fields
from table <TT>ztxlfa1</TT> into the <TT>lifnr</TT> and <TT>land1</TT>
fields of internal table <TT>it1</TT>. The internal table has
field <TT>lifnr_ext</TT> between <TT>lifnr</TT> and <TT>land1</TT>,
so you cannot use <TT>into table</TT>. If <TT>it1</TT> requires
the fields to be in this order, the usage of <TT>corresponding
fields</TT> is appropriate here.
<LI>Line 22 selects all fields from table <TT>ztxlfa1</TT> into
only two fields of table <TT>it2</TT>. This is inefficient. Instead,
line 31 shows that by selecting only the needed fields in the
correct order, you can use <TT>into table</TT>.
</UL>
<H3><A NAME="AddingRowsonebyoneUsingselect">
Adding Rows one by one Using select</A></H3>
<P>
Using <TT>select</TT> to add rows one at a time requires the use
of a work area and a second statement such as <TT>append</TT>,
<TT>insert</TT>, or <TT>collect</TT>. Omitting the <TT>table</TT>
addition causes the row to be assigned to a work area. It is common
to use the header line of an internal table as an explicit work
area. Alternatively, you can use the default table work area (defined
using <TT>tables</TT>).
<P>
Listing 13.5 shows some examples of using <TT>select</TT> to add
rows one at a time to an internal table.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 13.5&nbsp;&nbsp;Using select with the append Statement
to Fill Internal Tables<BR>
</B>
<BLOCKQUOTE>
<PRE>
1  report ztx1305.
2  tables ztxlfa1.
3  data it like ztxlfa1 occurs 2 with header line.
4 
5  *Do it this way
6  select * from ztxlfa1 into it   &quot;notice 'table' is omitted so the
7           where land1 = 'DE'.    &quot;row goes into the header line of it
8      append it.
9      endselect.
10
11 loop at it.
12     write: / it-lifnr, it-land1, it-regio.
13     endloop.
14 refresh it.
15
16 *Or this way
17 select * from ztxlfa1           &quot;no 'into' so the row goes into the
18          where land1 = 'DE'.    &quot;default table work area
19     append ztxlfa1 to it.       &quot;and then is appended to it
20     endselect.
21
22 skip.
23 loop at it.
24     write: / it-lifnr, it-land1, it-regio.
25     endloop.
26 refresh it.
27
28 *Not this way
29 select * from ztxlfa1           &quot;row goes into default table work area
30          where land1 = 'DE'.
31     it = ztxlfa1.               &quot;then is assigned to the header line
32     append it.
33     endselect.
34
35 skip.
36 loop at it.
37     write: / it-lifnr, it-land1, it-regio.
38     endloop.
39 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.5 produces this output:
<BLOCKQUOTE>
<PRE>
     V11        DE  07
     V12        DE  14
     V8         DE  03
                     
     V11        DE  07
     V12        DE  14
     V8         DE  03
                     
     V11        DE  07
     V12        DE  14
     V8         DE  03
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<UL>
<LI>On line 6, <TT>table</TT> does not appear before <TT>it</TT>,
so the fields from <TT>ztxlfa1</TT> go into the header line for
<TT>it</TT>. Line 8 appends the header line to <TT>it</TT>.
<LI>On line 17, there is no <TT>into</TT> clause, so each row
goes into the default table work area <TT>ztxlfa1</TT>. Line 19
appends work area <TT>ztxlfa1</TT> to the internal table (the
header line is not used).
<LI>Line 29 places each row into work area <TT>ztxlfa1</TT>. Line
31 moves <TT>ztxlfa1</TT> to the header line of <TT>it</TT> before
appending. This is the least efficient method.
</UL>
<P>
<TT>into corresponding fields</TT> can also be used to place data
into a work area instead of into the body of the internal table.
The effect is similar to that of the <TT>move-corresponding</TT>
statement and is more efficient. Listing 13.6 shows how.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 13.6&nbsp;&nbsp;How the corresponding fields Addition
to the select Statement Produces the Same Effect as the move-corresponding
Statement<BR>
</B>
<BLOCKQUOTE>
<PRE>
 1 report ztx1306.
 2 tables ztxlfa1.
 3 data: begin of it occurs 2,
 4           lifnr     like ztxlfa1-lifnr,
 5           row_id    like sy-index,
 6           land1     like ztxlfa1-land1,
 7           end of it.
 8
 9 *Do it this way:
10 select lifnr land1 from ztxlfa1
11        into corresponding fields of it   &quot;notice 'table' is omitted
12        where land1 = 'DE'.
13     append it.
14     endselect.
15
16 loop at it.
17     write: / it-lifnr, it-row_id, it-land1.
18     endloop.
19 refresh it.
20
21 *Not this way:
22 select * from ztxlfa1
23        where land1 = 'DE'.
24     move-corresponding ztxlfa1 to it.
25     append it.
26     endselect.
27
28 skip.
29 loop at it.
30     write: / it-lifnr, it-row_id, it-land1.
31     endloop.
32 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.6 produces this output:
<BLOCKQUOTE>
<PRE>
     V11                 0  DE
     V12                 0  DE
     V8                  0  DE
     
     V11                 0  DE
     V12                 0  DE
     V8                  0  DE
</PRE>

⌨️ 快捷键说明

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