Friday, 23 August 2013

AUGUST 23

Deleting Lines from an ABAP Internal Table:

DELETE is the statement to delete one or more lines from an ABAP Internal Table. Use the INDEX addition to delete a single line. If we use the INDEX addition and the operation is successful,  the  line with the corresponding index in the internal table will be deleted and the indexes of the subsequent lines will be reduced by one.
Syntax:
DELETE <internal table> [INDEX <index>].
We can also use the above DELETE statement without INDEX addition inside LOOP. Inside LOOP if we do not specify the INDEX, then the current loop line will be deleted.
We can use the WHERE clause to delete single or multiple lines. All the lines that meet the logical condition will be deleted
Syntax:
DELETE <internal table> [FROM <n1>] [TO <n2>] [WHERE <condition>].

With WHERE clause we can also specify the lines between certain indices that we want to delete by specifying indexes in FROM and TO additions.
Example:

*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
       id(5)     TYPE n,
       name(10)  TYPE c,
       place(10) TYPE c,
       age       TYPE i,
       END OF ty_student.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it  TYPE TABLE OF ty_student.

gwa_student-id     = 1.
gwa_student-name   = 'JOHN'.
gwa_student-place  = 'London'.
gwa_student-age    = 20.
INSERT gwa_student INTO TABLE it.

gwa_student-id     = 2.
gwa_student-name   = 'JIM'.
gwa_student-place  = 'New York'.
gwa_student-age    = 21.
INSERT gwa_student INTO TABLE it.

gwa_student-id     = 3.
gwa_student-name   = 'JACK'.
gwa_student-place  = 'Bangalore'.
gwa_student-age    = 20.
INSERT gwa_student INTO TABLE it.

gwa_student-id     = 4.
gwa_student-name   = 'ROB'.
gwa_student-place  = 'Bangalore'.
gwa_student-age    = 22.
INSERT gwa_student INTO TABLE it.

WRITE:/ 'Values in IT before DELETE' COLOR 4.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

SKIP.
WRITE:/ 'Values in IT after DELETE' COLOR 4.

*Delete second line from IT

DELETE it INDEX 2.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

SKIP.
WRITE:/ 'Values in IT after DELETE using WHERE Clause' COLOR 4.

*Delete entries from IT where place is Bangalore

DELETE it WHERE place = 'Bangalore'.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

Output:







                                                                                                                                                                                    
                                               

Thursday, 22 August 2013

AUGUST 22

Changing Lines in ABAP Internal Tables:

MODIFY is the statement to change single or multiple lines in an internal table. Use the INDEX addition to change a single line. If we use the INDEX addition and the operation is successful,  the contents of the work area overwrites the contents of the line with the corresponding index.
Instead of changing all the values of a row we can specify the fields we want to change by specifying the field names in the TRANSPORTING addition.
Syntax:

MODIFY <internal table> FROM <work area> [INDEX <index>]
                              [TRANSPORTING <f1> <f2> ... ].

We can also use the above MODIFY statement without INDEX addition inside LOOP. Inside LOOP if we do not specify the INDEX, then the current loop line will be modified.
We can use the WHERE clause to change single or multiple lines. All the lines that meet the logical condition will be processed.
Syntax:

MODIFY <internal table> FROM <work area>
          TRANSPORTING <f1> <f2> ... WHERE <condition>.

Example:

*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
       id(5)     TYPE n,
       name(10)  TYPE c,
       place(10) TYPE c,
       age       TYPE i,
       END OF ty_student.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it  TYPE TABLE OF ty_student.

gwa_student-id     = 1.
gwa_student-name   = 'JOHN'.
gwa_student-place  = 'London'.
gwa_student-age    = 20.
INSERT gwa_student INTO TABLE it.

gwa_student-id     = 2.
gwa_student-name   = 'JIM'.
gwa_student-place  = 'New York'.
gwa_student-age    = 21.
INSERT gwa_student INTO TABLE it.

gwa_student-id     = 3.
gwa_student-name   = 'JACK'.
gwa_student-place  = 'Bangalore'.
gwa_student-age    = 20.
INSERT gwa_student INTO TABLE it.

gwa_student-id     = 4.
gwa_student-name   = 'ROB'.
gwa_student-place  = 'Bangalore'.
gwa_student-age    = 22.
INSERT gwa_student INTO TABLE it.

WRITE:/ 'Values in IT before MODIFY' COLOR 4.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

SKIP.
WRITE:/ 'Values in IT after MODIFY' COLOR 4.

gwa_student-id     = 4.
gwa_student-name   = 'ROB'.
gwa_student-place  = 'Mumbai'.
gwa_student-age    = 25.

*Change all the columns of row 4 with work area values
MODIFY it FROM gwa_student INDEX 4.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

SKIP.
WRITE:/ 'Values in IT after Transporting addition' COLOR 4.

gwa_student-id     = 9.
gwa_student-name   = 'TOM'.
gwa_student-place  = 'Bangalore'.
gwa_student-age    = 30.
*Change specific columns of row 4 with work area values by
*using TRANSPORTING addition
MODIFY it FROM gwa_student INDEX 4 TRANSPORTING place.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.
ENDLOOP.

SKIP.
WRITE:/ 'Values in IT after MODIFY using WHERE Clause' COLOR 4.

gwa_student-place  = 'Mumbai'.

*Change multiple rows using WHERE clause
MODIFY it FROM gwa_student TRANSPORTING place
                           WHERE place = 'Bangalore'.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5, 18 'Place' COLOR 5,
        37 'Age' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name, gwa_student-place,
          gwa_student-age.

ENDLOOP.

Wednesday, 14 August 2013

AUGUST 14

Today I only revised all the concepts studied so far.  There were continuous holidays due to independence day , weekend off , raksha bandhan holiday. I have taken half day leave so that i could board the train to my home.So, practically I did nothing new today. Only a brief go-through over all the topics.

I was back on August 21 morning. Though it was a tiresome journey,still I went for my training classes. But to my unfortunate luck, there was some electric fault and no networks were available to work. And hence got one more day off. Overall , I enjoyed the holidays from August 14 to August 21.

Tuesday, 13 August 2013

AUGUST 13

Inserting lines into Internal Tables:
We can insert one or more lines to ABAP internal tables using the INSERT statement. To insert a single line, first place the values we want to insert in a work area and use the INSERT statement to insert the values in the work area to internal table.
Syntax to insert a line to internal table

INSERT <work area> INTO TABLE <internal table>.
                       OR
INSERT <work area> INTO <internal table> INDEX <index>.

The first INSERT statement without INDEX addition will simply add the record to the end of the internal table. But if we want to insert the line to specific location i.e. if we want to insert it as second record then we need to specify 2 as the index in the INSERT statement.

Example:
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
       id(5)    TYPE n,
       name(10) TYPE c,
       END OF ty_student.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it TYPE TABLE OF ty_student.

gwa_student-id    = 1.
gwa_student-name  = 'JOHN'.
INSERT gwa_student INTO TABLE it.

gwa_student-id    = 2.
gwa_student-name  = 'JIM'.
INSERT gwa_student INTO TABLE it.

gwa_student-id    = 3.
gwa_student-name  = 'JACK'.
INSERT gwa_student INTO TABLE it.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name.
ENDLOOP.

SKIP.
WRITE:/ 'After using Index addition' COLOR 4.

gwa_student-id    = 4.
gwa_student-name  = 'RAM'.
INSERT gwa_student INTO it INDEX 2.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name.
ENDLOOP.

We can also insert multiple lines to an internal table with a single INSERT statement i.e. we can insert the lines of one internal table to another internal table.

Syntax to insert multiple lines to internal table

INSERT LINES OF <itab1> [FROM <index 1>] [TO <index 2>] INTO TABLE <itab2>.
                                        OR
INSERT LINES OF <itab1> [FROM <index 1>] [TO <index 2>] INTO
<itab2> INDEX <index>.

Example:
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
       id(5)    TYPE n,
       name(10) TYPE c,
       END OF ty_student.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it  TYPE TABLE OF ty_student,
      it2 TYPE TABLE OF ty_student,
      it3 TYPE TABLE OF ty_student,
      it4 TYPE TABLE OF ty_student.

gwa_student-id    = 1.
gwa_student-name  = 'JOHN'.
INSERT gwa_student INTO TABLE it.

gwa_student-id    = 2.
gwa_student-name  = 'JIM'.
INSERT gwa_student INTO TABLE it.

gwa_student-id    = 3.
gwa_student-name  = 'JACK'.
INSERT gwa_student INTO TABLE it.

gwa_student-id    = 4.
gwa_student-name  = 'ROB'.
INSERT gwa_student INTO TABLE it.

WRITE:/ 'Inserting all the lines of IT to IT2' COLOR 4.

INSERT LINES OF it INTO TABLE it2.

WRITE:/ 'Display values of IT2' COLOR 1.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5.
LOOP AT it2 INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name.
ENDLOOP.

SKIP.
WRITE:/ 'Inserting only lines 2 & 3 of IT to IT3' COLOR 4.

INSERT LINES OF it FROM 2 TO 3 INTO TABLE it3.

WRITE:/ 'Display values of IT3' COLOR 1.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5.
LOOP AT it3 INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name.
ENDLOOP.

gwa_student-id    = 1.
gwa_student-name  = 'RAM'.
INSERT gwa_student INTO TABLE it4.

gwa_student-id    = 4.
gwa_student-name  = 'RAJ'.
INSERT gwa_student INTO TABLE it4.

SKIP.
WRITE:/ 'Inserting only lines 2 & 3 of IT to IT4 at 2' COLOR 4.

INSERT LINES OF it FROM 2 TO 3 INTO it4 INDEX 2.

WRITE:/ 'Display values of it4' COLOR 1.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5.
LOOP AT it4 INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name.
ENDLOOP.



Monday, 12 August 2013

AUGUST 12

Deleting Values from Database Tables:

DELETE is the open SQL statement to delete entries from database table. First we declare a work area as the line structure of database table and fill the work area with the  specific key that we want to delete from the database table. Then delete the entries from the database table using DELETE statement.
The syntax for the DELETE statement is as follows.
DELETE <database table> FROM <work area>
If the database table contains a line with the same primary key as specified in the work area, the operation is completed successfully.
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.


DELETE zemployee FROM gwa_employee.


We can also multiple lines from the table using the WHERE clause in the DELETE statement.
DELETE FROM <database table> WHERE <condition>
Example:
DELETE FROM zemployee WHERE dept_id = 2. 

Internal Tables

An Internal table is a temporary table gets created in the memory of application server during program execution and gets destroyed once the program ends. It is used to hold data temporarily or manipulate the data. It contains one or more rows with same structure.
An internal table can be defined using the keyword TABLE OF in the DATA statement. Internal table can be defined by the following ways.
Example:
TYPES: BEGIN OF ty_student,
       id(5)    TYPE n,
       name(10) TYPE c,
       END OF ty_student.

DATA: gwa_student TYPE ty_student.

"Referring to local data type
DATA: it1 TYPE TABLE OF ty_student.
"Referring to local data object
DATA: it2 LIKE TABLE OF gwa_student.
"Referring to data type in ABAP dictionary
DATA: it3 TYPE TABLE OF mara.

we use the APPEND statement to add data to internal table. First we define the work area i.e. define a field string with a structure similar to row of the internal table , then place the data in the work area and use the APPEND statement to add the data from work area to internal table.
Example:
DATA: gwa_student TYPE ty_student.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
"Referring to local data type
DATA: it TYPE TABLE OF ty_student.

gwa_student-id    = 1.
gwa_student-name  = 'JOHN'.
APPEND gwa_student TO it.                                                                                       

gwa_student-id    = 2.
gwa_student-name  = 'JIM'.
APPEND gwa_student TO it.

gwa_student-id    = 3.
gwa_student-name  = 'JACK'.

APPEND gwa_student TO it.

Usually internal tables are used to hold data from database tables temporarily for displaying on the screen or further processing. To fill the internal table with database values, we can use SELECT statement to read the records from the database one by one, place it in the work area and then APPEND the values in the work area to internal table.
Example:
DATA: gwa_employee TYPE zemployee,
      gt_employee  TYPE TABLE OF zemployee.

SELECT * FROM zemployee INTO gwa_employee.
  APPEND gwa_employee TO gt_employee.
ENDSELECT.

After ENDSELECT the internal table GT_EMPLOYEE contains all the records that are present in table ZEMPLOYEE.
Using INTO TABLE addition to SELECT statement we can also read multiple records directly into the internal table directly. No work area used in this case. This select statement will not work in loop, so no ENDSELECT is required.
Example:
SELECT * FROM zemployee INTO TABLE gt_employee.

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: