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.