Friday, 9 August 2013

AUGUST 9


Inserting values in Database Table:

INSERT is the open SQL statement to add values to the database table. First we declare a work area as the line structure of database table and fill the work area with the desired values. Then add the values in the work area to the database table using INSERT statement.
The syntax for the INSERT statement is as follows.
INSERT <database table> FROM <work area>
or
INSERT INTO <database table> VALUES <work area>
Example:
DATA: gwa_employee TYPE zemployee.

gwa_employee-id      = 6.
gwa_employee-name    = 'MARY'.
gwa_employee-place   = 'FRANKFURT'.
gwa_employee-phone   = '7897897890'.
gwa_employee-dept_id = 5.

INSERT zemployee FROM gwa_employee.

Changing values in Database Tables:

UPDATE is the open SQL statement to change the values in the database table. First declare a work area as the line structure of database table and fill the work area with the desired values for a specific key in the database table. Then update the values for the specified key in the database table using UPDATE statement.

The syntax for the UPDATE statement is as follows.


UPDATE <database table> FROM <work area>

Example:
DATA: gwa_employee TYPE zemployee.

gwa_employee-id      = 6.
gwa_employee-name    = 'JOSEPH'.
gwa_employee-place   = 'FRANKFURT'.
gwa_employee-phone   = '7897897890'.
gwa_employee-dept_id = 5.

UPDATE zemployee FROM gwa_employee.

We can also change certain columns in the database table using the following syntax
UPDATE <target> SET <set1> <set 2> … [WHERE <condition>].
The WHERE clause determines the lines that are changed. If we do not specify a WHERE clause, all lines will be changed.
Example:
UPDATE zemployee SET place = 'MUMBAI' WHERE dept_id = 2.

Inserting or changing Values:

MODIFY is the open SQL statement to insert or change entries in the database table. If the database table contains no line with the same primary key as the line to be inserted, MODIFY works like INSERT, that is, the line is added. If the database already contains a line with the same primary key as the line to be inserted, MODIFY works like UPDATE, that is, the line is changed.

The syntax for the MODIFY statement is as follows.

MODIFY <database table> FROM <work area>


If the database table does not already contain a line with the same primary key as specified in the work area, a new line is inserted. If the database table does already contain a line with the same primary key as specified in the work area, the existing line is overwritten.

Example:
DATA: gwa_employee TYPE zemployee.

gwa_employee-id      = 6.
gwa_employee-name    = 'JOSEPH'.
gwa_employee-place   = 'FRANKFURT'.
gwa_employee-phone   = '7897897890'.
gwa_employee-dept_id = 5.

MODIFY zemployee FROM gwa_employee.

Since there is no entry with the key 6, a new entry will be added to the table.
Example:
DATA: gwa_employee TYPE zemployee.

gwa_employee-id      = 6.
gwa_employee-name    = 'JOHNNY'.
gwa_employee-place   = 'LONDON'.
gwa_employee-phone   = '7897897890'.
gwa_employee-dept_id = 3.


MODIFY zemployee FROM gwa_employee.


Since there is an entry with the key 6, the values in the existing record will be modified.

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