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

📄 ch07.htm

📁 这个是sap开发语言abap的教育文档
💻 HTM
📖 第 1 页 / 共 4 页
字号:
</CENTER>
<P>
<P>
Examples of valid hexadecimal literals are <TT>'00'</TT>, <TT>'A2E5'</TT>,
and <TT>'F1F0FF'</TT>. Examples of invalid hexadecimal literals
are <TT>'a2e5'</TT> (contains lowercase characters), <TT>'0'</TT>
(contains an uneven number of characters), <TT>&quot;FF&quot;</TT>
(enclosed in double quotes), and <TT>x'00'</TT> (should not have
a preceding <TT>x</TT>).<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>
Hexadecimal values are rarely used in ABAP/4, because they are usually machine-dependent. The use of a hexadecimal value that creates machine dependence should be avoided.</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Table 7.1 covers the right and wrong way of coding numeric and
hexadecimal literals.
<P>
<CENTER><B>Table 7.1&nbsp;&nbsp;Right and Wrong Ways to Code Literals</B></CENTER><CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><B>Right</B></CENTER></TD><TD WIDTH=67><CENTER><B>Wrong</B></CENTER>
</TD><TD WIDTH=288><CENTER><B>Explanation</B></CENTER></TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><TT>-99</TT></CENTER></TD><TD WIDTH=67><CENTER><TT>99-</TT></CENTER>
</TD><TD WIDTH=288>Trailing sign not permitted on a numeric unless it's within quotes.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><TT>'-12'</TT></CENTER></TD><TD WIDTH=67><CENTER>&nbsp;</CENTER>
</TD><TD WIDTH=288>Numerics within quotes can have leading<TT>'12-'</TT> or trailing sign.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><TT>'12.3'</TT></CENTER></TD><TD WIDTH=67><CENTER><TT>12.3</TT></CENTER>
</TD><TD WIDTH=288>Numerics containing a decimal point must be enclosed in single quotes.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><TT>'Hi'</TT></CENTER></TD><TD WIDTH=67><CENTER><TT>&quot;Hi&quot;</TT></CENTER>
</TD><TD WIDTH=288>Double quotes are not permitted.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER>Right</CENTER></TD><TD WIDTH=67><CENTER>Wrong</CENTER>
</TD><TD WIDTH=288>Explanation</TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><TT>'Can''t'</TT></CENTER></TD><TD WIDTH=67><CENTER><TT>'Can't'</TT></CENTER>
</TD><TD WIDTH=288>To represent a single quote, use two consecutive single quotes.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><TT>'Hi'</TT></CENTER></TD><TD WIDTH=67><CENTER><TT>'Hi</TT></CENTER>
</TD><TD WIDTH=288>Trailing quote is missing.</TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><TT>'7E1'</TT></CENTER></TD><TD WIDTH=67><CENTER><TT>7E1</TT></CENTER>
</TD><TD WIDTH=288>Floating-point values must be enclosed within quotes.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><TT>'7e1'</TT></CENTER></TD><TD WIDTH=67><CENTER>&nbsp;</CENTER>
</TD><TD WIDTH=288>Lowercase e is allowed for floating-point literals.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><TT>'0A00'</TT></CENTER></TD><TD WIDTH=67><CENTER><TT>'0a00'</TT></CENTER>
</TD><TD WIDTH=288>Lowercase in hexadecimal literals gives incorrect results.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><TT>'0A00'</TT></CENTER></TD><TD WIDTH=67><CENTER><TT>'A00'</TT></CENTER>
</TD><TD WIDTH=288>An uneven number of hexadecimal digits gives incorrect results.
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=67><CENTER><TT>'0A00'</TT></CENTER></TD><TD WIDTH=67><CENTER><TT>X'0A00'</TT></CENTER>
</TD><TD WIDTH=288>A preceding or following character is not permitted for hexadecimal literals.
</TD></TR>
</TABLE>
</CENTER>
<H3><A NAME="DefiningVariables">
Defining Variables</A></H3>
<P>
Two statements are commonly used to define variables in an ABAP/4
program:
<UL>
<LI><TT>data</TT>
<LI><TT>parameters</TT>
</UL>
<H4>Using the <TT>data</TT> Statement to Define Variables</H4>
<P>
Using <TT>data</TT> statement, variables can be declared for the
program. Variables defined in the data statement are assigned
to a data type and can also have defaults. 
<H5>Syntax for the <TT>data</TT> Statement</H5>
<BLOCKQUOTE>
The following is the syntax for defining a variable using the
<TT>data</TT> statement:
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
data <I>v1</I>[(<I>l</I>)] [type <I>t</I>] [decimals <I>d</I>] [value '<I>xxx</I>'].
</PRE>
</BLOCKQUOTE>
<BLOCKQUOTE>
or
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
data <I>v1</I> like <I>v2</I> [value '<I>xxx</I>'].
</PRE>
</BLOCKQUOTE>
<BLOCKQUOTE>
where:

<UL>
<LI><TT><I>v1</I></TT> is the
variable name.
<LI><TT><I>v2</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>).
<LI><TT>'<I>xxx</I>'</TT> is a
literal that supplies a default value.
</UL>

Listing 7.3 shows examples of variables defined with the <TT>data</TT>
statement.
</BLOCKQUOTE>
<BLOCKQUOTE>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
</BLOCKQUOTE>
<HR>
<P>
<B>Listing 7.3&nbsp;&nbsp;Examples of Variables Defined with the
</B><TT><B>DATA</B></TT><B> Statement
<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 data f1(2) type c.
2 data f2 like f1.
3 data max_value type i value 100.
4 data cur_date type d value '19980211'.
</PRE>
</BLOCKQUOTE>
<HR>
<BLOCKQUOTE>
Variable names can be 1 to 30 characters long. They can contain
any characters except <BR>
<TT>(</TT><B> </B><TT>)</TT><B> </B><TT><B>+</B></TT><B>
</B><TT><B>.</B></TT><B> </B><TT><B>,</B></TT><B>
</B><TT><B>:</B></TT><B> </B>and
must contain at least one alphabetic character.
</BLOCKQUOTE>
<BLOCKQUOTE>
SAP recommends that variable names should always begin with a
character and they should not contain a dash. A dash has special
meaning (see field strings below). Instead of a dash, you should
use an underscore ( <TT>_</TT> ).<BR>
</BLOCKQUOTE>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
Do not use <TT>USING </TT>or <TT>CHANGING </TT>as variable names. Although they are syntacti-cally correct, they can cause problems if you try to pass them to subroutines.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<BLOCKQUOTE>
</BLOCKQUOTE>
<BLOCKQUOTE>
The following points also apply to the <TT>data</TT> statement:

<UL>
<LI>The default length depends on the data type. (Default lengths
are listed in Table 7.2.)
<LI>The default data type is <TT>c</TT> (character).
<LI>The default initial value is <TT>0</TT>, except for data type
<TT>c</TT>, which is blank.
<LI>The <TT>value</TT> addition only accepts a literal or constant
(see below); you cannot use a variable to supply a default value.
<LI>When using the <TT>like</TT> addition, the variable being
defined obtains its length and data type from the referenced variable.
You cannot specify them on the same statement with <TT>like</TT>.
<LI>When using the <TT>like</TT> addition, the value<I> </I>is
<I>not </I>obtained from the referenced variable. You can specify
the <TT>value</TT> addition to give the variable a default value.
If you do not, it is assigned a default initial value of <TT>0</TT>
(or blank for a <TT>character</TT> data type).
</UL>

The <TT>data</TT> statement can appear anywhere in a program.
The definition for a variable must physically come before the
statements that access it. If you place a <TT>data</TT> statement
after executable code, the statements above it cannot access the
variables it defines. For an example, see Listing 7.4.<BR>
</BLOCKQUOTE>
<BLOCKQUOTE>
<IMG SRC="../button/input.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/input.gif">
</BLOCKQUOTE>
<HR>
<P>
<B>Listing 7.4&nbsp;&nbsp;Example of a Variable That Is Incorrectly
Accessed Before It Is Defined<BR>
</B>
<BLOCKQUOTE>
<PRE>
1 report ztx0704.
2 data f1(2) value 'Hi'.
3 write: f1, f2.
4 data f2(5) value 'there'.
</PRE>
</BLOCKQUOTE>
<HR>
<BLOCKQUOTE>
<IMG SRC="../button/analysis.gif" tppabs="http://pbs.mcp.com/ebooks/0672312174/button/analysis.gif">
</BLOCKQUOTE>
<BLOCKQUOTE>
The variable F2 is defined on line 4, and the <TT>write</TT> statement
on line 3 is trying to access it. This will generate a syntax
error. The <TT>data</TT> statement on line four should be moved
before line 3.<BR>
</BLOCKQUOTE>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>TIP</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
It is good programming practice to place all <TT>data </TT>definitions together at the top of your program before the first executable statement.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<BLOCKQUOTE>
</BLOCKQUOTE>
<H4>ABAP/4 Data Types</H4>
<P>
There are two main categories of data in ABAP/4: character and
numeric. Variables receive special treatment by the ABAP/4 processor
based on their category that goes beyond the usual treatment given
by other languages. Therefore, it is especially important for
you to be able to recognize and distinguish between the character
and numeric data types.
<H5>Character Data Types</H5>
<BLOCKQUOTE>
The character data types are shown in Table 7.2. Notice that they
include type <TT>n</TT>. Internal lengths are given in bytes.
A dash in the Max length column appears for fixed length data
types.<BR>
</BLOCKQUOTE>
<P>
<CENTER><B>Table 7.2&nbsp;&nbsp;List of Character Data Types</B></CENTER><CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=77>
<BR>
<B>Data<BR>
<B>Type</B></B></TD><TD WIDTH=77><B>Internal<BR>
Description</B>
</TD><TD WIDTH=77><B>Default <BR>
Internal<BR>
Length</B></TD>
<TD WIDTH=77><B>Max<BR>
Internal<BR>
Length</B></TD><TD WIDTH=77><BR>
<B>Valid<BR>
Values</B>
</TD><TD WIDTH=77><B>Default <BR>
Initial<BR>
Value</B></TD>
</TR>
<TR VALIGN=TOP><TD WIDTH=77><CENTER><TT>c</TT></CENTER></TD><TD WIDTH=77>character
</TD><TD WIDTH=77><CENTER>1</CENTER></TD><TD WIDTH=77><CENTER>65535</CENTER>
</TD><TD WIDTH=77><CENTER>Any char</CENTER></TD><TD WIDTH=77><CENTER>Blank</CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=77><CENTER><TT>n</TT></CENTER></TD><TD WIDTH=77>numeric text
</TD><TD WIDTH=77><CENTER>1</CENTER></TD><TD WIDTH=77><CENTER>65535</CENTER>
</TD><TD WIDTH=77><CENTER>0-9</CENTER></TD><TD WIDTH=77><CENTER>0</CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=77><CENTER><TT>d</TT></CENTER></TD><TD WIDTH=77>date
</TD><TD WIDTH=77><CENTER>8 (fixed)</CENTER></TD><TD WIDTH=77><CENTER>-</CENTER>
</TD><TD WIDTH=77><CENTER>0-9</CENTER></TD><TD WIDTH=77><CENTER>00000000</CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=77><CENTER><TT>t</TT></CENTER></TD><TD WIDTH=77>time
</TD><TD WIDTH=77><CENTER>6 (fixed)</CENTER></TD><TD WIDTH=77><CENTER>-</CENTER>
</TD><TD WIDTH=77><CENTER>0-9</CENTER></TD><TD WIDTH=77><CENTER>000000</CENTER>
</TD></TR>
<TR VALIGN=TOP><TD WIDTH=77><CENTER><TT>x</TT></CENTER></TD><TD WIDTH=77>hexadecimal
</TD><TD WIDTH=77><CENTER>1</CENTER></TD><TD WIDTH=77><CENTER>65535</CENTER>
</TD><TD WIDTH=77><CENTER>Any</CENTER></TD><TD WIDTH=77><CENTER>&nbsp;</CENTER>
</TD></TR>
</TABLE>
</CENTER>
<P>
<BLOCKQUOTE>
The Default Initial Value column indicates the value given to
the variable by default if you do not specify one using the <TT>value</TT>
addition.
</BLOCKQUOTE>
<H5>Internal Representation of Variables</H5>
<BLOCKQUOTE>
Numeric text variables are called numeric character variables
and hold unsigned positive integers. Each digit occupies one byte,
and internally each is stored as a character. This is a character
data type. It can only contain the characters <TT>0</TT>-<TT>9</TT>.
</BLOCKQUOTE>
<BLOCKQUOTE>
Use numeric text to hold numbers that are used as unique identifiers,
such as document numbers, account numbers, and order numbers.
Also, use it for variables that hold a numeric extracted from
a character data type. For example, if you were to extract the
two-character month from a date field and needed a variable to
store it, you should use a type <TT>n</TT> field.
</BLOCKQUOTE>
<BLOCKQUOTE>
Date and time are fixed length data types. You should not specify
a length on the <TT>data</TT> statement for them. Values for date
and time variables are always stored internally as <TT>YYYYMMDD</TT>
and <TT>HHMMSS</TT>, respectively. The current date is available
in the system field <TT>sy-datum</TT> and the current time in
<TT>sy-uzeit</TT>.<BR>
</BLOCKQUOTE>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1>
<TR VALIGN=TOP><TD WIDTH=600><B>NOTE</B></TD></TR>
<TR VALIGN=TOP><TD WIDTH=600>
<BLOCKQUOTE>
The values of <TT>sy-datum </TT>and <TT>sy-uzeit </TT>are set at the beginning of program execution and do not change until the program ends. If you need access to the most current date and time during execution of a long-running pro-gram, use the statement <TT>get time</TT>. It updates the values of <TT>sy-datum </TT>and <TT>sy-uzeit </TT>to reflect the current date and time.
</BLOCKQUOTE>

</TD></TR>
</TABLE>
</CENTER>
<P>
<BLOCKQUOTE>
Absolute time values that have millisecond precision are not used
in R/3. However, <I>relative</I> time values are available to

⌨️ 快捷键说明

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