📄 ch08.htm
字号:
<HR>
<P>
<B>Listing 8.4 A Field String Can Be Defined Exactly
Like a DDIC Table or Structure<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0804.
2 data: my_lfa1 like ztxlfa1, "like a table in the DDIC
3 my_addr like ztxaddr. "like a structure in the DDIC
4
5 my_lfa1-name1 = 'Andrea Miller'.
6 my_lfa1-telf1 = '1-243-2746'.
7 my_addr-land1 = 'CA'.
8
9 write: / my_lfa1-name1,
10 my_lfa1-name2,
11 my_addr-land1.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
On line 2, <TT>my_lfa1</TT> is defined exactly like the DDIC table
<TT>ztxlfa1</TT>, and <TT>my_addr</TT> is defined like the DDIC
structure <TT>ztxaddr</TT>.<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
To view the DDIC definition of table <TT>ztxlfa1</TT>, double-click on its name in your source code.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.5 DDIC Tables and Structures Can also
be Included in a Field String<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0805.
2 data begin of fs1.
3 include structure ztxlfa1.
4 data: extra_field(3) type c,
5 end of fs1.
6
7 fs1-lifnr = 12.
8 fs1-extra_field = 'xyz'.
9
10 write: / fs1-lifnr,
11 fs1-extra_field.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Line 2 begins the definition of field string <TT>fs1</TT>. The
statement ends with a period because <TT>include structure</TT>
is not part of the <TT>data</TT> statement; it is a separate statement.
On line 3, the structure of table <TT>ztxlfa1</TT> is included
into the field string. On line 4, another field is included in
the field string after the fields of table <TT>ztxlfa1</TT>. Any
number of fields could be included, more structures could be included
here as well, or any combination thereof.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.6 If You Use </B><TT><B>LIKE</B></TT><B>
Instead of </B><TT><B>INCLUDE</B></TT><B>,
You Get a Different Result. The Names of the Included Fields Are
Prefixed by an Intermediate Component Name.<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0806.
2 data: begin of fs1,
3 mylfa1 like ztxlfa1,
4 extra_field(3) type c,
5 end of fs1.
6
7 fs1-mylfa1-lifnr = 12.
8 fs1-extra_field = 'xyz'.
9
10 write: / fs1-mylfa1-lifnr,
11 fs1-extra_field.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Line 2 begins the definition of field string <TT>lfa1_with_extra_field</TT>.
The statement ends with a period because <TT>include structure</TT>
is not part of the <TT>data</TT> statement; it is a separate statement.
On line 3, the structure of table <TT>ztxlfa1</TT> is included
into the field string. On line 4, another field is included in
the field string after the fields of table <TT>ztxlfa1</TT>. Any
number of fields could be included, more structures could be included
here as well, or any combination thereof.
<H3><A NAME="UsingaFieldStringasaVariableofTypeTTFONTSIZEcharFONTTT">
Using a Field String as a Variable of Type <TT><FONT SIZE=4>char</FONT></TT>
</A></H3>
<P>
Not only can you address the individual components of a field
string, you can also address all components at once as if they
were a single variable of type <TT>char</TT>. Listing 8.7 illustrates
this concept.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.7 An Example of Using a Field String as
Both Multiple Variables and as a Single Variable of Type </B><TT><B>CHAR
<BR>
</B></TT>
<BLOCKQUOTE>
<PRE>
1 report ztx0807.
2 data: begin of fs1,
3 c1 value 'A',
4 c2 value 'B',
5 c3 value 'C',
6 end of fs1.
7
8 write: / fs1-c1, fs1-c2, fs1-c3,
9 / fs1.
10
11 fs1 = 'XYZ'.
12
13 write: / fs1-c1, fs1-c2, fs1-c3,
14 / fs1.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 8.7 should produce this output:
<BLOCKQUOTE>
<PRE>
A B C
ABC
X Y Z
XYZ
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Lines 2 through 6 define field string <TT>fs1</TT>. It has three
components, each with a default value. On line 8, each component
is written out individually. On line 9, the field string is written
out as if it were a single variable. Consequently, the output
shows the contents of <TT>fs1</TT> as if it were a single variable
defined as <TT>char</TT> <TT>3</TT>. On line 11, the value <TT>'XYZ'</TT>
is assigned to the field string, again treating it as a <TT>char</TT>
variable. The resulting output shows that the individual components
reflect the change because they are accessing the same storage.
<P>
One field string can be assigned to another, if they have the
same structure, as shown in Listing 8.8.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.8 An Example of Assignment Involving Two
Field Strings<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0808.
2 data: begin of fs1,
3 c1 value 'A',
4 c2 value 'B',
5 c3 value 'C',
6 end of fs1,
7 fs2 like fs1.
8
9 fs2 = fs1.
10 write: / fs2-c1, fs2-c2, fs2-c3.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/output.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/output.gif">
<P>
The code in Listing 8.8 should produce this output:
<BLOCKQUOTE>
<PRE>
A B C
</PRE>
</BLOCKQUOTE>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Lines 2 through 6 define field string <TT>fs1</TT>. Line 7 defines
field string <TT>fs2</TT> exactly like <TT>fs1</TT>. On line 9,
<TT>fs1</TT> is moved to <TT>fs2</TT>, just as if it were a single
variable of type <TT>char</TT>. On line 10, the components of
<TT>fs2</TT> are written out.<P>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
A field string containing numeric data types needs special consideration dur-ing assignments. These considerations are covered on Day 9, in the "Assignments" section.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H3><A NAME="UsingtheTTFONTSIZETABLESFONTTTFONTSIZEStatementtoDefineaFieldStringFONT">
Using the <TT><FONT SIZE=4>TABLES</FONT></TT><FONT SIZE=4>
Statement to Define a Field String</FONT></A></H3>
<P>
A field string defined using the <TT>tables</TT> statement is
a modifiable data object. Field strings defined using the <TT>tables</TT>
statement follow the same rules as field strings defined using
the <TT>data</TT> statement. An example of a program using a <TT>tables</TT>
field string appears in Listing 8.9.
<H4>Syntax for Defining a Field String Using the <TT>TABLES</TT>
Statement</H4>
<P>
The following is the syntax for defining a field string using
the <TT>tables</TT> statement.
<BLOCKQUOTE>
<PRE>
tables <I>fs1</I>.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>fs1</I></TT> is the
field string name. A table or structure of the same name must
exist in the Data Dictionary.
</UL>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.9 Simple Example of a Field String Defined
Using the </B><TT><B>TABLES</B></TT><B>
Statement<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0809.
2 tables ztxlfa1.
3
4 ztxlfa1-name1 = 'Bugsy'.
5 ztxlfa1-land1 = 'US'.
6
7 write: / ztxlfa1-name1, ztxlfa1-land1.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Line 2 defines field string <TT>ztxlfa1</TT>. Its definition is
exactly like the Data Dictionary table of the same name. On lines
4 and 5, values are given to two of its components, which are
written out on line 7.
<H3><A NAME="FieldStringDefinedUsingTTFONTSIZETABLESFONTTTFONTSIZEInteractingwithFONTTTFONTSIZESELECTFONTTT">
Field String Defined Using <TT><FONT SIZE=4>TABLES</FONT></TT><FONT SIZE=4>
Interacting with </FONT><TT><FONT SIZE=4>SELECT</FONT></TT></A></H3>
<P>
The <TT>tables</TT> statement does more than just define a field
string. It does two things:
<UL>
<LI>It defines a field string.
<LI>It gives the program access to a database table of the same
name, if one exists.
</UL>
<P>
Your first <TT>select</TT> statement example is reproduced in
Listing 8.10. You can now analyze it from a new perspective.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.10 Your Second Program, Revisited<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0810.
2 tables ztxlfa1.
3 select * from ztxlfa1 into z lfa1 order by lifnr.
4 write / z lfa1-lifnr.
5 endselect.
</PRE>
</BLOCKQUOTE>
<HR>
<P>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
<P>
Line 2 defines field string <TT>ztxlfa1</TT>. Its definition is
exactly like the Data Dictionary table of the same name. There
is also a database table of the same name, so the program is given
access to that table, meaning that it can now be used in a <TT>select</TT>
statement. Each time line 3 is executed, a record is read from
the database table <TT>ztxlfa1</TT> into field string <TT>ztxlfa1</TT>.
For each record, the value of component <TT>ztxlfa1-lifnr</TT>
is written out (line 4).
<H3><A NAME="VisibilityofaFieldStringDefinedUsingTTFONTSIZETABLESFONTTT">
Visibility of a Field String Defined Using <TT><FONT SIZE=4>TABLES</FONT></TT>
</A></H3>
<P>
A field string defined using <TT>tables</TT> <I>always has both
global and external visibility no matter where it is defined in
the program.</I> This means that if you place a <TT>tables</TT>
statement in a subroutine, the definition is global and you cannot
have another definition for the same field string anywhere in
that program.
<P>
The field string is also externally visible. This means that if,
in a calling and called program, the same <TT>tables</TT> statement
appears, those field strings will share the same memory. This
concept is illustrated in Figure 8.1. When the called program
executes, it will "see" the value in <TT>ztxlfa1</TT>
from the first program. If it changes <TT>ztxlfa1</TT>, the calling
program "sees" the changes when it regains control.
<P>
<A HREF="javascript:popUp('f8-1.gif')"><B>Figure 8.1 :</B> <I>Identical tables definitions in a calling
and called program causes the memory for those field strings to
be shared</I>.</A>
<H2><A NAME="DefiningTypes"><FONT SIZE=5 COLOR=#FF0000>
Defining Types</FONT></A></H2>
<P>
You can define your own data types using the <TT>types</TT> statement
and base them on existing data types.
<H3><A NAME="SyntaxfortheTTFONTSIZETYPESFONTTTFONTSIZEStatementFONT">
Syntax for the <TT><FONT SIZE=4>TYPES</FONT></TT><FONT SIZE=4>
Statement</FONT></A></H3>
<P>
The following is the syntax for defining a type using the <TT>types</TT>
statement.
<BLOCKQUOTE>
<PRE>
types <I>t1</I>[(<I>l</I>)] [type <I>t</I>] [decimals <I>d</I>].
</PRE>
</BLOCKQUOTE>
<P>
or
<BLOCKQUOTE>
<PRE>
types <I>t1</I> like <I>v1</I>.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>t1</I></TT> is the
type name.
<LI><TT><I>v1</I></TT> is the
name of a variable previously defined in the program, or is the
name of a field that belongs to a table or structure in the Data
Dictionary.
<LI><TT>(<I>l</I>)</TT> is the
internal length specification.
<LI><TT><I>t</I></TT> is the data
type.
<LI><TT><I>d</I></TT> is the number
of decimal places (used only with type <TT>p</TT>).
</UL>
<P>
Listings 8.11 and 8.12 show examples of programs that define and
use their own data types.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -