Wednesday, 7 August 2013

AUGUST 7


Messages in SAP:
Messages are usually used to tell the user what is going on. The following types of messages are available in ABAP.

ATerminationThe message appears in a dialog box, and the program terminates. When the user has confirmed the message, control returns to the next-highest area menu.
EErrorDepending on the program context, an error dialog appears or the program terminates.
IStatusThe message appears in a dialog box. Once the user has confirmed the message, the program continues immediately after the MESSAGE statement.
SErrorThe program continues normally after the MESSAGE statement, and the message is displayed in the status bar of the next screen.
WWarningDepending on the program context, an error dialog appears or the program terminates.
XExitNo message is displayed, and the program terminates with a short dump. Program terminations with a short dump normally only occur when a runtime error occurs.
The syntax for issuing a message is as follows.
MESSAGE  <message> TYPE <message_type>.

We can issue a status message as follows. Status message will be displayed in the status bar. After the message is displayed the program continues after the MESSAGE statement.
Example:
MESSAGE  'This is a status message'   TYPE   'S'.
Output:
Information message will be displayed in a dialog box. Once the user has confirmed the message, the program continues immediately after the MESSAGE statement.

Example:

MESSAGE  'This is an information message'   TYPE 'I'.

Output:
Error message in report programs will be displayed in the status bar and when the user press enter, the program terminates.

Example:

MESSAGE  'This is an error message'  TYPE   'E'.
Warning message behaves similar to error message in report programs.

Termination Message appears in a dialog box, and the program terminates. When the user has confirmed the message, control returns to the next-highest area menu.
Example:

MESSAGE  'This is a termination message' TYPE 'A'.

Output:

Exit Message – No message is displayed, and the program terminates with a short dump. Short dumps can be viewed in t-code ST22.
Example:
MESSAGE  'This produces short dump'   TYPE 'X'.
Output:

Tuesday, 6 August 2013

AUGUST 6


ABAP data dictionary :
ABAP data dictionary is used to create and manage data definitions (mete data). ABAP Dictionary is used to create Tables, Data Elements, Domains, Views, Lock Objects etc.
Data elements and Domains:
While creating a table in data dictionary each table field is assigned to a data element. Each data element is in turn assigned to a domain.
  • Domain describes the technical attributes such as data type and length of a table field.
  • Data Element gives the field labels and documentation for the table field.
Creating Domains:
  • To create a domain go to t-code SE11.
  • Select the Domain radio button, enter the the name of the domain that we want to create and then press create.
  • provide short description for the domain that we want to create.now place the cursor in the data type and press F4 to get the list of SAP data types.
  • In the popup window select the correct data type.
  • now enter the number of characters as required. Enter the decimal places if it applicable to data type that we have selected. 
  • Save and activate the domain.
Creating Data Elements:
  • To create a data element go to t-code SE11.
  • Select the Data type radio button, enter the name of the data element and press create.

  • Enter short description. Assign a domain to the data element. Press field label tab to maintain the field labels for the data element.

  • Enter the field labels.

  •  Save and activate the data element.
Creating SAP table:
Every table has a unique name and consists of rows and columns. The number of columns in a table is fixed but can have any number of rows.
  •  Go to ABAP dictionary (SE11) to create a SAP table. Enter the name of the table to be created and press enter.
  • Enter a proper short description for the table and maintain delivery class as ‘A’(Application Table).
  •  Now press on Fields tab to maintain the fields of the table.
  • Enter the fields of the table and maintain the proper data elements for the table fields. We can use the standard data elements or  can create our own data elements.
  • Maintain the primary key and press save. To maintain the technical attributes of table like tablespace, size etc. press the Technical attributes button on application toolbar.
  • Enter Data class as APPL0, Size category as 0 and then click  Save.
  • Now press back, save and activate the table.
Maintaining Test Data in SAP table:
After creating a table we can test it by maintaining a couple of entries in the table.The below mentioned procedure is used to maintain entries in the table.
 This procedure must be used only in testing or development environment not in production environment.

  • Display the table in ABAP Dictionary(SE11). Use the menu path Utilities->Table Contents->Create Entries.
  • If this menu path is disabled then go to Delivery and Maintenance tab and enter “Display/Maintenance Allowed” in “Data Browser/Table View Maint.” listbox.
  • Save and activate the table. Then go back to menu path Utilities->Table Contents->Create Entries.
  • Enter some test values in the fields and press save.
  • Observe the message in the status bar. 
  • To display the values in the table go to Utilities->Table Contents->Display
  • In the selection screen enter the selection criteria if you want to filter the records you want to display and press execute.
  • All the records in the table will be displayed if no selection criteria specified in the selection screen.



Monday, 5 August 2013

AUGUST 5


Programs for various string operations.

CONCATENATE – Combines 2 or more strings into one string.

Example-1:
Report z01
DATA: s1(10) VALUE 'Hello',
      s2(10) VALUE 'ABAP',
      s3(10) VALUE 'World',
      result1(30),
      result2(30).

CONCATENATE s1 s2 s3 INTO result1.
CONCATENATE s1 s2 s3 INTO result2 SEPARATED BY '-'.

WRITE / result1.
WRITE / result2.

Output:


SPLIT – Splits a string into 2 or more smaller strings.

Example-2:
Report z02
DATA: s1(10), s2(10), s3(10),
      source(20) VALUE 'abc-def-ghi'.

SPLIT source AT '-' INTO s1 s2 s3.

WRITE:/ 'S1 - ', s1.
WRITE:/ 'S2 - ', s2.
WRITE:/ 'S3 - ', s3.

Output:

SEARCH – Searches for a sub string in main string. If found then sy-subrc is set to 0, else set to 4.

Example-3:

Report z03
DATA: string(30) VALUE 'SAP ABAP Development',
      str(10) VALUE 'ABAP'.

SEARCH string FOR str.
IF sy-subrc = 0.
  WRITE:/ 'Found'.
ELSE.
  WRITE:/ 'Not found'.
ENDIF.

Output:

REPLACE – Replaces the sub string with another sub string specified, in the main string. If replaced successfully then sy-subrc is set to 0, else set to 4.

Example-4:
Report z04
DATA: string(30) VALUE 'SAP ABAP Development',
      str(10) VALUE 'World'.

REPLACE 'Development' WITH str INTO string.
WRITE:/ string.

Output:



Friday, 2 August 2013

AUGUST 2


Example programs for basic operations in SAP.

Example-1:
Assigning values to ABAP variables

Report 01
DATA: a TYPE i,
      b TYPE i,
      c TYPE i,
      d TYPE i.

a = 10.
b = a.
MOVE 20 TO c.
MOVE c TO d.

WRITE:/ a, b, c, d.

Output:
Example-2:
Basic Arithmetic Operations

Report 02
DATA: a TYPE i,
      b TYPE i,
      c TYPE i,
      d TYPE i.

*Using Mathematical Expressions
a = 10 + 20.
b = 20 - 10.
c = 10 * 2.
d = 100 / 2.

WRITE:/ 'Using Expressions'.
WRITE:/ a, b, c, d.

*Using Keywords
add 10 to a.
subtract 5 from b.
multiply c by 2.
divide d by 2.

WRITE:/ 'Using Keywords'.
WRITE:/ a, b, c, d.

Output:

Eample-3:
Clear ABAP variables

keyword CLEAR is used to set the variables to default values.

Report 03
DATA: a TYPE i,
      b TYPE i.

a = 10 + 20.
b = 20 - 10.

WRITE:/ 'Before Clear'.
WRITE:/ a, b.

clear: a, b.

WRITE:/ 'After Clear'.
WRITE:/ a, b.

Output:



Thursday, 1 August 2013

AUGUST 1


Studied the use of while, continue and check statements with example programs.

Using the while Statement

The while statement is a looping mechanism similar to do.

Syntax for the while Statement

The following is the syntax for the while statement.
while exp [ vary f1 from s-c1 next s-c2 [ vary f2 from s2-c1 next s2-c2 ... ]
    ---
    [ exit. ]
    ---
    endwhile.
where:
  • exp is a logical expression.
  • s is a field string having the components c1 and c2.
  • f1 is a variable. The components of s must be able to be converted to the data type and length of f1.
  • ... represents any number of complete vary clauses.
  • --- represents any number of lines of code.
Example:
An Example of the Use of the WHILE Statement
1  report ztx1012.
2  data: l,                         "leading  characters
3        t,                         "trailing characters
4        done.                      "done flag
5  parameters p(25) default '    Vendor Number'.
6  while done = ' '                 "the expression is evaluated first
7      vary l from p+0  next p+1    "then vary assignments are performed
8      vary t from p+24 next p+23.
9      if l = ' ' and t = ' '.
10         l = t = '-'.
11     else.
12         done = 'X'.
13         endif.
14     endwhile.
15 write: / p.
Output:  ----Vendor Number ----
Description:
  • Lines 2 and 3 define two single character variables l and t. Line 3 defines a flag to indicate when processing is complete.
  • Line 5 defines p as character 25 with a default value.
  • On line 6, the expression on the while statement is first evaluated. It proves true the first time through the loop. The assignments on lines 7 and 8 are then performed. Line 7 assigns the first character from p to l and line 8 assigns the last character to t.
  • If l and t are both blank, line 10 assigns a dash to both. If they are not, line 12 assigns an 'X' to the done flag.
  • On line 14, endwhile copies the values from l and t back to p.
  • The while loop repeats again from line 6 as long as the done flag is blank.

Using the continue statement

The continue statement is coded within a loop. It acts like a goto, passing control immediately to the terminating statement of the loop and beginning a new loop pass. In effect, it causes the statements below it within the loop to be ignored and a new loop pass to begin. 

Syntax for the continue Statement

The following is the syntax for the continue statement.
[do/while/select/loop]
    ---
    continue.
    ---
    [enddo/endwhile/endselect/endloop]
where:
  • --- represents any number of lines of code.
The following points apply:
  • continue can only be coded within a loop.
  • continue has no additions.
Example:
An Example of the Use of the CONTINUE Statement
1  report ztx1013.
2  parameters p(20) default 'c::\\\xxx\\yyy'.
3  data: c,                    "current character
4        n.                    "next    character
5
6  do 19 times varying c from p+0 next p+1
7              varying n from p+1 next p+2.
8      if c na ':\'.
9          continue.
10         endif.
11     if c = n.
12         write: / 'duplicate', c, 'found', 'at position', sy-index.
13         endif.
14     enddo.
Output:
duplicate : found at position          2
duplicate \ found at position          4
duplicate \ found at position          5
duplicate \ found at position         10

Using the check statement

The check statement is coded within a loop. It can act very much like continue, passing control immediately to the terminating statement of the loop and bypassing the statements between. Unlike continue, it accepts a logical expression. If the expression is true, it does nothing. If it is false, it jumps to the end of the loop. 

Syntax for the check Statement

[do/while/select/loop]
    ---
    check exp.
    ---
    [enddo/endwhile/endselect/endloop]
where:
  • exp is a logical expression.
  • --- represents any number of lines of code.
Example:
 Use of the CHECK Statement
1  report ztx1014.
2  parameters p(20) default 'c::\\\xxx\\yyy'.
3  data: c,                    "current character
4        n.                    "next    character
5
6  do 19 times varying c from p+0 next p+1
7              varying n from p+1 next p+2.
8      check c ca ':\'.
9      if c = n.
10         write: / 'duplicate', c, 'found', 'at position', sy-index.
11         endif.
12     enddo.
Output:
duplicate : found at position          2
duplicate \ found at position          4
duplicate \ found at position          5
duplicate \ found at position 10

Wednesday, 31 July 2013

JULY 31


Continued with control statements- elseif, case , exit and do statements and their examples.

Using elseif

We use elseif to avoid nesting ifs. Nesting ifs can be difficult to read and maintain.
an example below shows the construct.
Example:
Using ELSEIF Is Clearer than Using Nested IF
1  report ztx1002.
2  parameters: f1 default 'A',
3              f2 default 'B',
4              f3 default 'C'.
5
6  if      f1 = f2.   write: / f1, '=', f2.
7  elseif  f1 = f3.   write: / f1, '=', f3.
8  elseif  f2 = f3.   write: / f2, '=', f3.
9  else.              write: / 'all fields are different'.
10     endif.
11
12 *lines 5-9 do the same as lines 14-26*
13
14 if f1 = f2.
15     write: / f1, '=', f2.
16 else.
17     if  f1 = f3.
18         write: / f1, '=', f3.
19     else.
20         if  f2 = f3.
21             write: / f2, '=', f3.
22         else.
23             write: / 'all fields are different'.
24             endif.
25         endif.
26     endif.
Output:
all fields are different 
all fields are different 
Description:
  • If f1 = f2, line 6 is true.
  • If f1 = f3, line 7 is true.
  • If f2 = f3, line 8 is true.
  • If none of the above are true, line 9 is true.
  • Lines 14 through 26 do the same as lines 6 through 10.

Using the case Statement

The case statement performs a series of comparisons.

Syntax for the case Statement

The following is the syntax for the case statement.
case v1.
    when v2 [ or vn ... ].
        ---
    when v3 [ or vn ... ].
        ---
    [ when others.
        --- ]
    endcase.
where:
  • v1 or v2 can be a variable, literal, constant, or field string.
  • --- represents any number of lines of code. Even zero lines are allowed.
The following points apply:
  • Only statements following the first matching when are executed.
  • when others matches if none of the preceding whens match.
  • If when others is not coded and none of the whens match, processing continues with the first statement following endcase.
  • Expressions are not allowed.
  • Field strings are treated as type c variables.
Case is very similar to if/elseif. The only difference is that on each if/elseif, we can specify a complex expression. With case, we can specify only a single value to be compared, and values are always compared for equality.
An example is given as follows:
Example:
The CASE Statement Performs a Series of Comparisons
1  report ztx1005.
2  parameters f1 type i default 2.
3
4  case f1.
5      when 1.         write / 'f1 = 1'.
6      when 2.         write / 'f1 = 2'.
7      when 3.         write / 'f1 = 3'.
8      when others.    write / 'f1 is not 1, 2, or 3'.
9      endcase.
10
11 * The following code is equivalent to the above case statement
12 if         f1 = 1.  write / 'f1 = 1'.
13     elseif f1 = 2.  write / 'f1 = 2'.
14     elseif f1 = 3.  write / 'f1 = 3'.
15     else.           write / 'f1 is not 1, 2, or 3'.
16     endif.
Output:
f1 = 2
f1 = 2
Description:
  • Line 2 defines f1 as a single character parameter having a default value 2.
  • On line 4, the case statement begins and f1 is compared with 1, 2, and 3, in sequence.
  • Line 6 matches and executes the write statement that immediately follows it. The remaining when statements are ignored.
  • The next line executed is line 12.

Using the exit Statement

The exit statement prevents further processing from occurring.

Syntax for the exit Statement

exit.
Following is shown a sample program using exit.
Example:
sing EXIT to Stop Program Processing Dead in Its Tracks
1  report ztx1006.
2  write: / 'Hi'.
3  exit.
4  write: / 'There'.
Output: Hi Description: Exit prevents further processing, so the exit on line 3 prevents line 4 from being executed.

Using the do Statement

The do statement is a basic loop mechanism.

Syntax for the do Statement

The following is the syntax for the do statement.
do [ v1 times ] [ varying f1 from s-c1 next s-c2 [ varying f2 from s2-c1 next s2-c2 ... ] ].
    ---
    [exit.]
    ---
    enddo.
where:
  • v1 is a variable, literal, or constant.
  • s is a field string having the components c1 and c2.
  • f1 is a variable. The components of s must be able to be converted to the data type and length of f1.
  • ... represents any number of complete varying clauses.
  • --- represents any number of lines of code.
The following points apply:
  • do loops can be nested an unlimited number of times.
  • exit prevents further loop processing and exits immediately out of the current loop. It does not terminate the program when inside of a do loop. Processing continues at the next executable statement after the enddo.
  • we can create an infinite loop by coding do without any additions. In that situation, use exit within the loop to terminate loop processing.
  • Modifying the value of v1 within the loop does not affect loop processing.

Tuesday, 30 July 2013

JULY 30


Studied dynamic assignment and its example program, and a brief introduction of control statements, beginning with If-else statements..

Dynamic Assignment

field-symbol is a pointer which can be dynamically assign to a field. After assignment, we can use the field-symbol anywhere in our program in place of the actual field name.  the field-symbol statementis used to define a field-symbol, and  assign to assign a field to it. The field-symbol name must begin and end with angle brackets. 
A simple example program to explain it is given as under.
Example:
 Field-Symbol Is a Reference to Another Field
1  report ztx0915.
2  data f1(3) value 'ABC'.
3  field-symbols <f>.
4  assign f1 to <f>.   "<f> can now be used in place of f1
5  write <f>.          "writes the contents of f1
6  <f> = 'XYZ'.        "assigns a new value to f1
7  write / f1.
Output:
ABC 
XYZ
Description:
  • Line 3 defines a field-symbol named <f>.
  • Line 4 assigns the field f1 to the field-symbol <f><f> now points to the storage for variable f1, and can be used in place of f1 anywhere in the program.
  • Line 5 writes out <f>, causing the contents of f1 to be written.
  • Line 6 modifies the contents of <f>, but actually modifies f1.
  • Line 7 writes out the modified contents of f1.

Common Control Statements

  •  if- elseif Statements
  •  case Statement
  •  exit Statement
  •  do Statement
  •  while Statement
  •  continue statement
  •  check statement

Using the if-elseif Statement

The if statement in ABAP/4 has relational operators for equality and inequality and special relational operators for string comparisons and for bit masks.

Syntax for the if Statement

The following is the syntax for the if statement.
if [not] exp [ and [not] exp ] [ or [not] exp ].
---
[elseif exp.
---]
[else.
---]
endif.
where:
  • exp is a logical expression that evaluates to a true or false condition.
  • --- represents any number of lines of code. Even zero lines are allowed.
The following points apply:
  • Every if must have a matching endif.
  • else and elseif are optional.
  • Parentheses can be used. Each parentheses must be separated by a space. For example, if ( f1 = f2 ) or ( f1 = f3 ) is correct, and if (f1 = f2) or (f1 = f3) is incorrect.
  • Variables can be compared with blanks or zeros using the addition is initial. For example, if f1 is initial will be true if f1 is type c and is blank. If f1 is any other data type, the statement is true if f1contains zeros.
  • To accomplish negation, not must precede the logical expression. For example, if not f1 is initial is correct. if f1 is not initial is incorrect.
  • Variables can be compared with nulls using the addition is null. For example, if f1 is null.
 The logical operators for operands of any type are listed in Table below.

  Common Comparisons and Their Alternate Forms

Comparison
Alternate
Forms
True When
v1 = v2EQv1 equals v2
v1 <> v2NE><v1 does not equal v2
v1 > v2GTv1 is greater than v2
v1 < v2LTv1 is less than v2
v1 >= v2GE=>v1 is greater than or equal tov2
v1 <= v2LE=<v1 is less than or equal to v2
v1 between v2 andv3v1 lies between v2 and v3(inclusive)
not v1 between v2and v3v1 lies outside of the range v2to v3 (inclusive)

In the above table,v1 and v2 can be variables, literals, or field strings. In the case of variables or literals, automatic conversion is performed if the data type or length does not match. Field strings are treated as type c variables.
Comparing two values that do not have the same data type will result in an internal automatic type adjustment of one or both of the values. One type will take precedence and will dictate what type of conversion is performed.
The order of precedence is:
  • If one field is type f, the other is converted to type f.
  • If one field is type p, the other is converted to type p.
  • If one field is type i, the other is converted to type i.
  • If one field is type d, the other is converted to type d. Types c and n, however, are not converted. They are compared directly.
  • If one field is type t, the other is converted to type t. Types c and n, however, are not converted. They are compared directly.
  • If one field is type nboth are converted to type p (at this point, the other field can only be type c or x).
  • At this point, one field will be type c and the other will be type xx is converted to type c.
Example:
An Example of Using IF in the Program
1  report ztx1001.
2  data: begin of s1,
3            x value 'X',
4            y value 'Y',
5            z value 'Z',
6            end of s1,
7        begin of s2,
8            x value 'X',
9            z value 'Z',
10           end of s2.
11
12 if s1-x = s2-x.
13     write: / s1-x, '=', s2-x.
14 else.
15     write: / s1-x, '<>', s2-x.
16     endif.
17
18 if s1-x between s2-x and s2-z.
19     write: / s1-X, 'is between', s2-x, 'and', s2-z.
20 else.
21     write: / s1-X, 'is not between', s2-x, 'and', s2-z.
22     endif.
23
24 if s1 = s2.        "comparing field strings byte by byte
25     write: / 's1 = s2'.
26 else.
27     write: / 's1 <> s2'.
28     endif.
29
30 if 0 = ' '.        "Watch out for this one
31     write: / '0 = '' '''.
32 else.
33     write: / '0 <> '' '''.
34     endif.
Output:
X = X
X is between X and Z
s1 <> s2
0 = ' '
Description:
  • On line 12, s1-x is compared with s2-x. Both are type c, length 1. No conversion is performed and they are equal.
  • Line 18 is similar, but uses the between operator. The value X lies in the range X to Z, and so the test proves true.
  • Line 24 compares field strings s1 and s2 as if they were type c variables. The value of s1 is therefore XYZ, and the value of s2 is XZ. They are not equal.
  • On line 30, the literal 0 is compared with a space. The zero is stored internally as type i, the other as type c. According to the order of precedence, type c is converted to type i. Converting a blank to an integer results in a zero value, and the comparison proves to be unexpectedly true.