Thursday, 8 August 2013

AUGUST 8


Open SQl:

Open SQL is a set of ABAP statements that performs operations like reads, modifies or deletes data in the SAP database. Open SQL is independent of the database system, so the syntax of the open SQL is uniform for all the databases supported by SAP.
All open SQL statements are passed to the database interface. The DB interface converts the open SQL to native SQL and passes it on to the database.
List of Open SQL statements
OPEN SQL
DESCRIPTION
SELECTReads data from database
INSERTInserts lines to database
UPDATEChanges the contents of lines in database
MODIFYInserts lines into database or changes the contents of existing lines
DELETEDeletes lines from database

SELECT is the open SQL statement to read the data from the database. The general syntax for SELECT statement is as follows.
SELECT      <result>    INTO      <target>   FROM      <source>  [WHERE    <condition>]
CLAUSEDESCRIPTION
SELECT <result>Specifies which columns you want to read, whether one line or many lines needs to selected, and whether duplicate entries are allowed
INTO <target>Determines the target area into which the selected data is to be placed
FROM <source>Specifies the database table from which the data is to be selected
WHERE <condition>specifies which lines are to be read by specifying conditions for the selection
Example:

DATA: gwa_employee TYPE zemployee.

WRITE : /1 'Emp ID' color 5,9 'Name' color 5,17 'Place' color 5,      27 'Phone' color 5,39 'Dept' color 5.SELECT * FROM zemployee INTO gwa_employee.
       ENDSELECT.

  WRITE :/ 1gwa_employee-id,9 gwa_employee-name,
        17 gwa_employee-place,27 gwa_employee-phone, 39 gwa_employee-dept_id.

Output:




In the above code,

  • GWA_EMPLOYEE is the work area to hold one record of table ZEMPLOYEE at a time.
  • SELECT * specifies all the rows and columns are read from the database.
  • SELECT – ENDSELECT works in a loop, so the code between SELECT and ENDSELECT will be executed for each record found in the database table.
  • WRITE statements are used to output the values in the list.
  • If the SELECT statement returns any record then the value of the system variable SY-SUBRC is set to zero else a non zero value will be set.
  • After the SELECT statement is executed, the value of the system variable SY-DBCNT contains the number of records read from the database. The value of SY-DBCNT is zero if no records are read from the database.

Selective Reading 
In Reading Data using Open SQL we have read all the rows from the database. if we want to read only certain records that match a certain criteria then we need to use the where clause of the SELECT statement.
Example:
 Program to read only the employees with department ID 2.
DATA: gwa_employee TYPE zemployee.
WRITE:/1 'Emp ID' COLOR 5,9 'Name' COLOR 5,17 'Place' COLOR 5,
      27 'Phone' COLOR 5,39 'Dept' COLOR 5.
SELECT * FROM zemployee INTO gwa_employee
                        WHERE dept_id = 2.
  WRITE:/1 gwa_employee-id,9 gwa_employee-name,
        17 gwa_employee-place,27 gwa_employee-phone,
        39 gwa_employee-dept_id.
ENDSELECT.
Output:
 if we want to select only certain columns from the database table instead of all the columns,then we need to specify the field list(field names) in the SELECT statement instead of specifying ‘*’.

Example:
SELECT id phone dept_id FROM zemployee INTO CORRESPONDING FIELDS OF
                        gwa_employee
                        WHERE dept_id = 2.
  WRITE:/1 gwa_employee-id,9 gwa_employee-name,
        17 gwa_employee-place,27 gwa_employee-phone,
        39 gwa_employee-dept_id.
ENDSELECT.

Output:

To select a single record from the database we use SELECT SINGLE instead of SELECT statement. SELECT SINGLE picks the first record found in the database that satisfies the condition in WHERE clause. SELECT SINGLE does not work in loop, so no ENDSELECT is required.
Example:

SELECT SINGLE * FROM zemployee INTO gwa_employee
                        WHERE dept_id = 2.
WRITE:/1 gwa_employee-id,9 gwa_employee-name,
      17 gwa_employee-place,27 gwa_employee-phone,
      39 gwa_employee-dept_id.

Output:





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 IFs 
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.