Tuesday 16 July 2013

JULY 16

Today I learned about constant definition and declaration in ABAP/4 , definition of field strings using DATA statements and various examples to implement the field strings.

Defining Constants

A constant is almost identical to a variable except that its value cannot be changed. To define one, we use the constants statement.
We use a constant when we need to include the same literal multiple times in a program. we 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 we need to change the value of the literal,we can simply change the value of the constant, causing its value to be updated wherever it is used in the program.
ABAP/4 has one pre-defined constant: SPACE. It is a constant having a value equal to spaces. we can use it in place of the literal ' '.

Syntax for the CONSTANTS Statement:

The following code demonstrates the syntax for defining a constant. It is similar to the data statement; however, the addition value is required. In all other ways, constants conform to the same rules as variables defined using the data statement.

constants c1[(l)] [type t] [decimals d] value 'xxx'.
or
constants c1 like cv value 'xxx'.

where:
  • c1 is the name of the constant.
  • cv 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.
  • (l) is the internal length specification.
  • t is the data type.
  • d is the number of decimal places (used only with type p).
  • 'xxx' is a literal that supplies the value of the constant.

Example illustrating  Definitions of Constants:
constants c1(2) type c value 'AA'.
constants c2 like c1 value 'BB'.
constants error_threshold type i value 5.
constants amalgamation_date like sy-datum value '19970305'.

Defining Field Strings


field string is a type of variable, 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.
Two statements are usually used to define field strings in an ABAP/4 program:
  • data
  • tables

Using the DATA Statement to Define a Field String

A field string defined using the data statement is a modifiable data object. It can have global or local visibility.

Syntax for Defining a Field String Using the DATA Statement

The following is the syntax for defining a field string using the data statement.
data: begin of fs1,
f1[(l)] [type t] [decimals d] [value 'xxx'],
f2[(l)] [type t] [decimals d] [value 'xxx'],
... end of fs1.
OR
data : begin of fs1.
data f1[(l)] [type t] [decimals d] [value 'xxx'].
data f2[(l)] [type t] [decimals d] [value 'xxx'].
... 
[include structure st1.] 
data end of fs1.
OR
data fs1 like fs2.
where:
  • fs1 is the field string name.
  • f1 and f2 are the fields (also called components) of the field string.
  • fs2 is the name of a previously defined field string, or is the name of a table or structure in the Data Dictionary.
  • (l) is the internal length specification.
  • t is the data type.
  • d is the number of decimal places (used only with type p).
  • 'xxx' is a literal that supplies a default value.
  • st1 is the name of a structure or table in the Data Dictionary.


Field strings follow the same rules as variables defined using the data statement. To refer to an individual component, its name must be prefixed by the name of the field string and a dash (-). For example, to write the number component of the cust_info field string, we would use the statement write cust_info-number.
The include statement is not part of the data statement; it is a separate statement. Therefore, it cannot be chained to a data statement. The statement before it must be concluded with a period.

Various programs that define and use field strings are shown in following examples:

Example-1:

A Simple Example of a Field String Defined Using the DATA Statement
 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.
 Description:

Line 2 begins the definition of field string totals_1. It contains three fields, the first of which is initialized with the value 'unknown'. On line 7, field string totals_2 is defined exactly like totals_1. The value of totals_1-region is not propagated to totals_2-region. 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.

Example-2:

Exaample to show a Field String Can Contain Another Field String
 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.
Description: 
Line 2 begins the definition of field string names. It contains two fields that are defined like fields of table ztxkna1 in the Data Dictionary. They are not given any initial values. On line 8, component cust_info-name is defined like field string names. When it is used on lines 12 and 13, nm is included in the component name.

Example-3:

 A Field String Can Be Defined Exactly Like a  Structure
 1 report ztx0804.
 2 data: 
 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.

Description:
On line 3 my_addr is defined like the DDIC structure ztxaddr.

Example-4:

If we Use LIKE Instead of INCLUDE, we Get a Different Result. The Names of the Included Fields Are Prefixed by an Intermediate Component Name.
 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.
 Description:
Line 2 begins the definition of field string lfa1_with_extra_field. The statement ends with a period because include structure is not part of the data statement; it is a separate statement. On line 3, the structure of table ztxlfa1 is included into the field string. On line 4, another field is included in the field string after the fields of table ztxlfa1. Any number of fields could be included, more structures could be included here as well, or any combination thereof.