Wednesday, 17 July 2013

JULY 17


Detailed study of field strings was done today. Brief of it is given below with illustrations.

Using a Field String as a Variable of Type "char"

Not only can we address the individual components of a field string, we can also address all components at once as if they were a single variable of type char.  The following example illustrates this concept.
Example-1:

 Using a Field String as Both Multiple Variables and as a Single Variable of Type CHAR 

 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.
Output:
A B C
ABC
X Y Z 

XYZ 



Description: 

Lines 2 through 6 define field string fs1. 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 fs1 as if it were a single variable defined as char 3. On line 11, the value 'XYZ' is assigned to the field string, again treating it as a char variable. The resulting output shows that the individual components reflect the change because they are accessing the same storage.


Example-2:


An Example of Assignment Involving Two Field Strings
 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.
Output: 
A B C

Description:
Lines 2 through 6 define field string fs1. Line 7 defines field string fs2 exactly like fs1. On line 9, fs1 is moved to fs2, just as if it were a single variable of type char. On line 10, the components of fs2 are written out.


Using the TABLES Statement to Define a Field String:

A field string defined using the tables statement is a modifiable data object. Field strings defined using the tables statement follow the same rules as field strings defined using the data statement.

Syntax for Defining a Field String Using the TABLES Statement

The following is the syntax for defining a field string using the tables statement.
tables fs1.
where:
  • fs1 is the field string name. A table or structure of the same name must exist in the Data Dictionary.
 Example:
Field String Defined Using the TABLES Statement
1 report ztx0809.
2 tables ztxlfa1.
3
4 ztxlfa1-name1 = 'Bugsy'.
5 ztxlfa1-land1 = 'US'.
6
7 write: / ztxlfa1-name1, ztxlfa1-land1.
Description:
Line 2 defines field string ztxlfa1. 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.

Field String Defined Using TABLES Interacting with SELECT:

The tables statement does more than just define a field string. It does two things:
  • It defines a field string.
  • It gives the program access to a database table of the same name, if one exists.
Example:
1 report ztx0810.
2 tables ztxlfa1.
3 select * from ztxlfa1 into z lfa1 order by lifnr.
4     write / z lfa1-lifnr.
5     endselect.

Description:
Line 2 defines field string ztxlfa1. 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 select statement. Each time line 3 is executed, a record is read from the database table ztxlfa1 into field string ztxlfa1. For each record, the value of component ztxlfa1-lifnr is written out (line 4).


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.

Friday, 12 July 2013

JULY 12


On ninth day I was taught about further details for "PARAMETERS " statement and various additions that can be used with it along with an example to implement it.

The following points also apply to the parameters statement :

  • The default data type is c (character).
  • The default value is 0, except for data type c, which is blank.
  • The value addition accepts a literal, a sy variable, or a variable previously defined in the program.
  • When using the like addition, the parameter being defined obtains its length and data type from the referenced variable. we cannot specify them on the same statement with like.
  • When using the like addition, the value is not obtained from the referenced variable. we can specify the value addition to give the parameter a default value. If we do not, it is assigned a default initial value of 0 (or blank for a character data type).
  • Like data, the parameters statement can appear anywhere in a program, but the definition must physically come before the statements that access it.
  • Parameters appear on a selection screen in the same order that they are defined in the program.
  • All parameters statements, no matter where they appear in the program, are collected together by the ABAP/4 interpreter and displayed on the selection screen at the same time. Even if we put a parameter statement in the middle of the program, that parameter will still be displayed on the selection screen before the program executes.
  • The parameters are displayed in an SAP standard format. To modify their appearance, for example, to move the input field left or move the label right, the selection-screen statement is uesd.

VARIOUS ADDITIONS WITH "PARAMETER" STATEMENTS ARE:

Using the Addition: lower case

All values entered into a parameter are translated into uppercase by default. To turn off this translation, use the addition lower case. This addition only applies to character fields.

Using the Addition: as checkbox

A check box has only two states: ticked and clear. we use them when we want to present the user with an on/off or true/false type of choice. we can use more than one check box on a screen. If we have multiple check boxes on a screen, they operate completely independently from each another.
To display a parameter as a check box, use the addition as checkbox. we cannot specify a data type or length; it will default to type c and length 1. The parameter will contain a capital X if the check box is ticked; it will contain a blank if the check box is clear. If the check box should initially contain a tickmark, use a default value of capital X.
Space and capital X are the only valid values. No other values are valid for a check box.

Using the Addition: radiobutton group g

Like check boxes, a radio button also has two states: selected and not selected. Unlike check boxes, radio buttons never operate alone, they operate only in groups. we can have any number of radio buttons in a group, but only one can be selected at a time. They are used when we need to present the user with a list of alternatives in which only one option can be chosen.
To display a parameter as a radio button, use the addition radiobutton group g. we cannot specify a data type or length; it will default to type c and length 1g is an arbitrary group name one to four characters long. we can have more than one group in a program.
The parameter will contain a capital X if the radio button is selected; it will contain a blank if it is not selected. To be initially selected, the radio button should contain a default value of capital X. No other values are valid for a radio button.
The additions to the parameters statement are described in short in the following table:

  Additions to the PARAMETERS Statement and Their Uses

Addition
Use
typeSame as the data statement.
DecimalsSame as the data statement.
LikeSame as the data statement.
DefaultSame as the value addition on the data statement.
ObligatoryThe user must enter a value into the field before the program will execute.
lower casePrevents values from being translated into uppercase.
as checkboxDisplays the input field as a check box.
Radiobutton groupgDisplays the input field as a radio button belonging to group g.
A sample program using parameters and to implement the above additions is given below.


 report ztx0706.
  parameters: p1(15) type c,
             p2  like p1 obligatory lower case,
              p3  like sy-datum default sy-datum,
              cb1 as checkbox,
              cb2 as checkbox,
              rb1 radiobutton group g1 default 'X',
              rb2 radiobutton group g1,
              rb3 radiobutton group g1.
  write: / 'You entered:',
         / '  p1 =', p1,
         / '  p2 =', p2,
         / '  p3 =', p3,
         / '  cb1=', cb1,
         / '  cb2=', cb2,
         / '  rb1=', rb1,
         / '  rb2=', rb2,
         / '  rb3=', rb3.

The OUTPUT generated by the above program is:





When the selection screen is shown, obligatory fields contain a question mark. These indicate to the user which fields must be filled in before continuing.

Thursday, 11 July 2013

JULY 11


 I learned about defining modifiable data objects using "DATA" Statement and "PARAMETER" statement and implementing it using programs.

 Variables(modifiable data object)

Two statements are commonly used to define variables in an ABAP/4 program:

·         data

·         parameters

Data Statement to Define Variables

Using data 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.

Syntax for the data Statement


data v1 [(l)] [type t] [decimals d] [value 'xxx'].
or
data v1 like v2 [value 'xxx'].

where:

·         v1 is the variable name.

·         v2 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.

·         (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.

 

Examples of Variables Defined with the DATA Statement 

data f1(2) type c.
data f2 like f1.
data max_value type i value 100.
data cur_date type d value '19980211'.


NOTE: Variable names can be 1 to 30 characters long. They can contain any characters except 
( ) + . , : and must contain at least one alphabetic character. SAP recommends that variable names should always begin with a character and they should not contain a dash. A dash has special meaning. Instead of a dash, we should use an underscore ( _ ).

The following points also apply to the data statement:

·         The default length depends on the data type.

·         The default data type is c (character).

·         The default initial value is 0, except for data type c, which is blank.

·         The value addition only accepts a literal or constant; we cannot use a variable to supply a default value.

·         When using the like addition, the variable being defined obtains its length and data type from the referenced variable. We cannot specify them on the same statement with like. 

·         When using the like addition, the value is not obtained from the referenced variable. we can specify the value addition to give the variable a default value. If we do not, it is assigned a default initial value of 0 (or blank for a character data type).

The data statement can appear anywhere in a program. The definition for a variable must physically come before the statements that access it. If we place a data statement after executable code, the statements above it cannot access the variables it defines.


Example of a Variable That Is Incorrectly Accessed Before It Is Defined

report zprogram.
data f1(2) value 'Hi'.
write: f1, f2.
data f2(5) value 'there'.

The variable F2 is defined on line 4, and the write statement on line 3 is trying to access it. This will generate a syntax error. The data statement on line four should be moved before line 3.

Parameters Statement to Define Variables

A parameter is a special type of variable that is defined using the parameters statement. parameters is a lot like the data statement, but when we run the program, the system will display the parameters as input fields on a selection screen before the program actually begins to execute. The user can enter or modify their values and then press the Execute button to begin program execution. we can use both parameters and data in the same program. The rules for parameter names are the same as for variable names, except for the following:
  • The maximum length is 8 characters instead of 30.
  • In addition to literals and constants, you can also use a variable to supply default a default value.

Syntax for the parameters Statement

parameters p1[(l)] [type t] [decimals d] ...
or
parameters p1 like v1 ...
... [default 'xxx'] [obligatory] [lower case] [as checkbox] [radiobutton
    group g].
where:
  • p1 is the parameter name.
  • v1 is the name of a previously defined variable or parameter, 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 or previously defined variable that supplies a default value.
Examples of parameters defined with the parameters statement.
parameters p1(2) type c.
 parameters p2 like p1.
 parameters max_value type i default 100.
 parameters cur_date type d default '19980211'.

Wednesday, 10 July 2013

JULY 10


 I learned about data objects, their definition and declaration.

DATA OBJECTS

Defining Data Objects:

The physical units with which ABAP statements work at runtime are called internal program data
objects. The contents of a data object occupy memory space in the program. ABAP statements

access these contents by addressing the name of the data object. For example, statements can

write the contents of data objects in lists or in the database, they can pass them to and receive

them from routines, they can change them by assigning new values, and they can compare them

in logical expressions.

Each ABAP data object has a set of technical attributes, which are fully defined at all times when

an ABAP program is running. The technical attributes of a data object are: Data type, field length,
and number of decimal places.

 Data objects are memory locations that you use to hold data while the program is running. There are two types of data objects: modifiable and non-modifiable. The types of non-modifiable data objects are literals and constants
The modifiable data objects are variables, field strings, and internal tables. 
A field string is the ABAP/4 equivalent of a structure. An internal table is the ABAP/4 equivalent of an array. When the program starts, the memory allocation for each data object occurs in the roll area of the program. While the program is running, we can read the contents of a non-modifiable data object or put data into a modifiable data object and then retrieve it. When the program ends, the system frees the memory for all data objects and their contents are lost.
Data objects have three levels of visibility: local, global, and external. The visibility of a data object indicates from where in the program the data object is accessible.
Locally visible data objects are accessible only from inside the subroutine in which they are defined. Globally visible objects can be accessed from anywhere within the program. Externally visible objects are accessible from outside of the program by another program.

The following figure displays these three levels of visibility pictorially.



The local data objects in subroutine 1A are visible only from within that subroutine. Any statement outside of it cannot access them. Similarly, the local data objects in subroutines 1B and 2A are not accessible from anywhere but within those subroutines.
Any statement in program 1, regardless of where the statement appears, can access the global data objects in program 1. Similarly, any statement in program 2 can access the global data objects in program 2.
The external data objects could be accessed from any statement in program 1 or program 2. In actuality, whether they can be or not depends on the type of external memory area used and the relationship between the two programs.


Declaring Data Objects

Apart from the interface parameters of routines, we declare all of the data objects in an ABAP program or routine in its declaration part. The declarative statements establish the data type of the object, along with any missing technical attributes, such as its length or the number of decimal places. This all takes place before the program is actually executed. The exception to this are internal tables.

Defining Literals (non-modifiable data objects)

literal is a non-modifiable data object. Literals can appear anywhere in a program, and they are defined merely by typing them where needed. There are four types: character string, numeric, floating-point, and hexadecimal.

Character String Literals

Character string literals are case-sensitive character strings of any length enclosed within single quotes. For example, 'JACK' is a character string literal containing only uppercase characters. 'Caesar the cat' is a character string literal containing a mixture of upper- and lowercase characters.
Because a character string literal is enclosed by quotes, we cannot use a single quote by itself in the value of the literal. To represent a single quote, we must use two consecutive single quotes. 
For example, the statement write 'Caesar''s tail'. will write Caesar's tail, but the statement write 'Caesar's tail' will cause a syntax error because it contains a single quote by itself.

Numeric Literals

Numeric literals are hard-coded numeric values with an optional leading sign. They are not usually enclosed in quotes. However, if a numeric literal contains a decimal, it must be coded as a character string enclosed within single quotes. If it is not, the syntax error Statement x is not defined. Please check your spelling. will occur.
For example, 256 is a numeric literal, as is -99'10.5' is a numeric literal that contains a decimal point, so it is enclosed by single quotes. A literal can be used as the default value for a variable, or it can be used to supply a value in a statement.
Examples of invalid literals are: 99- (trailing minus sign), "Confirm" (enclosed in double quotes), and 2.2 (contains a decimal but is not enclosed in quotes).

Floating-Point Literals

Floating-point literals are specified within single quotes as '<mantissa>E<exponent>'. The mantissa can be specified with a leading sign and with or without decimal places, and the exponent can be signed or unsigned, with or without leading zeros.
For example, '9.99E9''-10E-32', and '+1E09' are valid floating-point literals.

Hexadecimal Literals

hexadecimal literal is specified within single quotes as if it were a character string. The permissible values are 0-9 and A-F. There must be an even number of characters in the string, and all characters must be in uppercase.
Examples of valid hexadecimal literals are '00''A2E5', and 'F1F0FF'
Examples of invalid hexadecimal literals are 'a2e5' (contains lowercase characters), '0' (contains an uneven number of characters), "FF"(enclosed in double quotes), and x'00' (should not have a preceding x).

Right and Wrong Ways to Code Literals

Right
Wrong
Explanation
-99
99-
Trailing sign not permitted on a numeric unless it's within quotes.
'-12'

Numerics within quotes can have leading'12-'or trailing sign.
'12.3'
12.3
Numerics containing a decimal point must be enclosed in single quotes.
'Hi'
"Hi"
Double quotes are not permitted.
Right
Wrong
Explanation
'Can''t'
'Can't'
To represent a single quote, use two consecutive single quotes.
'Hi'
'Hi
Trailing quote is missing.
'7E1'
7E1
Floating-point values must be enclosed within quotes.
'7e1'

Lowercase e is allowed for floating-point literals.
'0A00'
'0a00'
Lowercase in hexadecimal literals gives incorrect results.
'0A00'
'A00'
An uneven number of hexadecimal digits gives incorrect results.
'0A00'
X'0A00'
A preceding or following character is not permitted for hexadecimal literals.