📄 ch08.htm
字号:
<!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® in 21 Days -- Day 8- Defining Data in ABAP/4, 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="gla02.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch08/gla02.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="../ch09/ch09.htm" tppabs="http://pbs.mcp.com/ebooks/0672312174/ch09/ch09.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>®</sup> in 21 Days</H1></CENTER>
<HR SIZE=4>
<H1>Day 8</H1>
<H1>Defining Data in ABAP/4, Part 2</H1>
<UL>
<LI><A HREF="#ChapterObjectives">
Chapter Objectives</A>
<LI><A HREF="#DefiningConstants">
Defining Constants</A>
<UL>
<LI><A HREF="#SyntaxfortheTTFONTSIZECONSTANTSFONTTTFONTSIZEStatementFONT">
Syntax for the <TT>CONSTANTS</TT>
Statement</A>
</UL>
<LI><A HREF="#DefiningFieldStrings">
Defining Field Strings</A>
<UL>
<LI><A HREF="#UsingtheTTFONTSIZEDATAFONTTTFONTSIZEStatementtoDefineaFieldStringFONT">
Using the <TT>DATA</TT> Statement
to Define a Field String</A>
<LI><A HREF="#UsingaFieldStringasaVariableofTypeTTFONTSIZEcharFONTTT">
Using a Field String as a Variable of Type <TT>char</TT>
</A>
<LI><A HREF="#UsingtheTTFONTSIZETABLESFONTTTFONTSIZEStatementtoDefineaFieldStringFONT">
Using the <TT>TABLES</TT>
Statement to Define a Field String</A>
<LI><A HREF="#FieldStringDefinedUsingTTFONTSIZETABLESFONTTTFONTSIZEInteractingwithFONTTTFONTSIZESELECTFONTTT">
Field String Defined Using <TT>TABLES</TT>
Interacting with <TT>SELECT</TT></A>
<LI><A HREF="#VisibilityofaFieldStringDefinedUsingTTFONTSIZETABLESFONTTT">
Visibility of a Field String Defined Using <TT>TABLES</TT>
</A>
</UL>
<LI><A HREF="#DefiningTypes">
Defining Types</A>
<UL>
<LI><A HREF="#SyntaxfortheTTFONTSIZETYPESFONTTTFONTSIZEStatementFONT">
Syntax for the <TT>TYPES</TT>
Statement</A>
</UL>
<LI><A HREF="#StructuredTypes">
Structured Types</A>
<UL>
<LI><A HREF="#TypeGroups">
Type Groups</A>
<LI><A HREF="#CreatingaTypeGroup">
Creating a Type Group</A>
</UL>
<LI><A HREF="#Summary">
Summary</A>
<LI><A HREF="#QampA">
Q&A</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 should be able to:
<UL>
<LI>Use the <TT>tables</TT> statement to define field strings
and understand the difference between field strings defined using
<TT>data</TT> and those defined using <TT>tables</TT>
<LI>Understand the <TT>types</TT> statement and use it to define
your own data types
</UL>
<H2><A NAME="DefiningConstants"><FONT SIZE=5 COLOR=#FF0000>
Defining Constants</FONT></A></H2>
<P>
A constant is almost identical to a variable except that its value
cannot be changed. To define one, you use the <TT>constants</TT>
statement.
<P>
Use a constant when you need to include the same literal multiple
times in a program. You can define a constant with the same value
as the literal and use the constant in the body of the program
in place of the literal. Later, if you need to change the value
of the literal, you can simply change the value of the constant,
causing its value to be updated wherever it is used in the program.
<P>
ABAP/4 has one pre-defined constant: <TT>SPACE</TT>. It is a constant
having a value equal to spaces. You can use it in place of the
literal <TT><B>' '</B></TT>.
<H3><A NAME="SyntaxfortheTTFONTSIZECONSTANTSFONTTTFONTSIZEStatementFONT">
Syntax for the <TT><FONT SIZE=4>CONSTANTS</FONT></TT><FONT SIZE=4>
Statement</FONT></A></H3>
<P>
The following code demonstrates the syntax for defining a constant.
It is similar to the <TT>data</TT> statement; however, the addition
<TT>value</TT> is required. In all other ways, constants conform
to the same rules as variables defined using the <TT>data</TT>
statement. See Listing 8.1 for examples of constant definitions.
<BLOCKQUOTE>
<PRE>
constants <I>c1</I>[(<I>l</I>)] [type <I>t</I>] [decimals <I>d</I>] value '<I>xxx</I>'.
</PRE>
</BLOCKQUOTE>
<P>
or
<BLOCKQUOTE>
<PRE>
constants <I>c1</I> like <I>cv</I> value '<I>xxx</I>'.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>c1</I></TT> is the
name of the constant.
<LI><TT><I>cv</I></TT> is the
name of a previously defined constant or variable, 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>).
<LI><TT>'<I>xxx</I>'</TT> is a
literal that supplies the value of the constant.
</UL>
<P>
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.1 Definitions of Constants<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 constants c1(2) type c value 'AA'.
2 constants c2 like c1 value 'BB'.
3 constants error_threshold type i value 5.
4 constants amalgamation_date like sy-datum value '19970305'.
</PRE>
</BLOCKQUOTE>
<HR>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Constants can be defined in a <I>type pool</I>. When they are, they can be shared by multiple programs. For more information, see the section called "Type Pools" at the end of this chapter.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="DefiningFieldStrings"><FONT SIZE=5 COLOR=#FF0000>
Defining Field Strings</FONT></A></H2>
<P>
<IMG SRC="../button/newterm.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/newterm.gif">
<P>
A <I>field string</I> is a type of variable, and is the equivalent
of a structure in the DDIC but is defined within an ABAP/4 program.
Like a structure, a field string is a series of fields grouped
together under a common name. The difference lies mainly in where
the definition resides. The term structure in R/3 applies only
to a Data Dictionary object containing a collection of fields.
The term field string applies to a collection of fields defined
in an ABAP/4 program.
<P>
Two statements are usually used to define field strings in an
ABAP/4 program:
<UL>
<LI><TT>data</TT>
<LI><TT>tables</TT>
</UL>
<H3><A NAME="UsingtheTTFONTSIZEDATAFONTTTFONTSIZEStatementtoDefineaFieldStringFONT">
Using the <TT><FONT SIZE=4>DATA</FONT></TT><FONT SIZE=4> Statement
to Define a Field String</FONT></A></H3>
<P>
A field string defined using the <TT>data</TT> statement is a
modifiable data object. It can have global or local visibility.
<H4>Syntax for Defining a Field String Using the <TT>DATA</TT>
Statement</H4>
<P>
The following is the syntax for defining a field string using
the <TT>data</TT> statement.
<BLOCKQUOTE>
<PRE>
data: begin of <I>fs1</I>,
<I>f1</I>[(<I>l</I>)] [type <I>t</I>] [decimals <I>d</I>] [value '<I>xxx</I>'],
<I> f2</I>[(<I>l</I>)] [type <I>t</I>] [decimals <I>d</I>] [value '<I>xxx</I>'],
<I> ...
</I> end of <I>fs1</I>.
</PRE>
</BLOCKQUOTE>
<P>
or
<BLOCKQUOTE>
<PRE>
data begin of <I>fs1</I>.
data <I>f1</I>[(<I>l</I>)] [type <I>t</I>] [decimals <I>d</I>] [value '<I>xxx</I>'].
data<I> f2</I>[(<I>l</I>)] [type <I>t</I>] [decimals <I>d</I>] [value '<I>xxx</I>'].
<I> ...
</I>[include structure <I>st1</I>.]
data end of <I>fs1</I>.
</PRE>
</BLOCKQUOTE>
<P>
or
<BLOCKQUOTE>
<PRE>
data <I>fs1</I> like <I>fs2</I>.
</PRE>
</BLOCKQUOTE>
<P>
where:
<UL>
<LI><TT><I>fs1</I></TT> is the
field string name.
<LI><TT><I>f1</I></TT><I> </I>and<I>
</I><TT><I>f2</I></TT> are the
fields (also called components) of the field string.
<LI><TT><I>fs2</I></TT> is the
name of a previously defined field string, or is the name of 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>).
<LI><TT>'<I>xxx</I>'</TT> is a
literal that supplies a default value.
<LI><TT><I>st1</I></TT> is the
name of a structure or table in the Data Dictionary.
</UL>
<P>
Field strings follow the same rules as variables defined using
the <TT>data</TT> statement. To refer to an individual component,
its name must be prefixed by the name of the field string and
a dash (<TT><B>-</B></TT>). For
example, to write the <TT>number</TT> component of the <TT>cust_info</TT>
field string, you would use the statement <TT>write cust_info-number</TT><B>.</B>
<P>
The <TT>include</TT> statement is not part of the <TT>data</TT>
statement; it is a separate statement. Therefore, it cannot be
chained to a <TT>data</TT> statement. The statement before it
must be concluded with a period.
<P>
Examples of programs that define and use field strings are shown
in Listings 8.2 through 8.6.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.2 A Simple Example of a Field String Defined
Using the </B><TT><B>DATA</B></TT><B>
Statement<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0802.
2 data: begin of totals_1,
3 region(7) value 'unknown',
4 debits(15) type p,
5 count type i,
6 end of totals_1,
7 totals_2 like totals_1.
8
9 totals_1-debits = 100.
10 totals_1-count = 10.
11 totals_2-debits = 200.
12
13 write: / totals_1-region, totals_1-debits, totals_1-count,
14 / totals_2-region, totals_2-debits, totals_2-count.
</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>totals_1</TT>.
It contains three fields, the first of which is initialized with
the value <TT>'unknown'</TT>. On line 7, field string <TT>totals_2</TT>
is defined exactly like <TT>totals_1</TT>. The value of <TT>totals_1-region</TT>
is <I>not</I> propagated to <TT>totals_2-region</TT>. On lines
9 through 11 values are assigned to components of the field strings,
and on lines 13 and 14, the values of all components are written
out.
<P>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
<HR>
<P>
<B>Listing 8.3 A Field String Can Contain Another Field
String<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0803.
2 data: begin of names,
3 name1 like ztxkna1-name1,
4 name2 like ztxkna1-name2,
5 end of names.
6 data: begin of cust_info,
7 number(10) type n,
8 nm like names, "like a field string
9 end of cust_info.
10
11 cust_info-number = 15.
12 cust_info-nm-name1 = 'Jack'.
13 cust_info-nm-name2 = 'Gordon'.
14
15 write: / cust_info-number,
16 cust_info-nm-name1,
17 cust_info-nm-name2.
</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>names</TT>. It
contains two fields that are defined like fields of table <TT>ztxkna1</TT>
in the Data Dictionary. They are not given any initial values.
On line 8, component <TT>cust_info-name</TT> is defined <TT>like</TT>
field string <TT>names</TT>. When it is used on lines 12 and 13,
<TT>nm</TT> is included in the component name.
<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 + -