Sei sulla pagina 1di 15

IBM Global Services

Database Operations

Database Operations |

Dec-2008

2005 IBM Corporation

IBM Global Services

Objectives
The participants will be able to:
Manipulate data in the database tables using:
UPDATE
INSERT
MODIFY
DELETE

Understand a logical unit of work (LUW)


Know how and when to use a COMMIT vs. A ROLLBACK

Database Operations |

Dec-2008

2005 IBM Corporation

IBM Global Services

Overview
We will now add functionality to the Update
pushbutton by learning the database operation
UPDATE. We will also learn INSERT, MODIFY,
and DELETE.
Exit

YMOVIE
Database Table

UPDATE
INSERT
MODIFY
DELETE

Update

Academy Awards
Year

1994

Category PIC

Winner

Forrest Gump

Notes

Tom Hanks had an Oscar-winning performance.

Critic

Dean

Database Operations |

Enter Name

Dec-2008

2005 IBM Corporation

IBM Global Services

Open versus Native SQL


** MZA08TOP - Top Include **

YMOVIE

PROGRAM SAPMZA08 MESSAGE-ID ZA.

Database Table

TABLES YMOVIE.
DATA OKCODE(4).

TABLES statement
defines the work area for
YMOVIE table

ABAP
Program

UPDATE
INSERT
MODIFY
DELETE

Database
Interface
Layer

Open SQL

Native SQL
4

Database Operations |

Dec-2008

2005 IBM Corporation

IBM Global Services

Open versus Native SQL (Contd.)


** MZA08TOP - Top Include **

YMOVIE

PROGRAM SAPMZA08 MESSAGE-ID ZA.

Database Table

TABLES YMOVIE.
DATA OKCODE(4).

TABLES statement
defines the work area for
YMOVIE table

ABAP
Program

UPDATE
INSERT
MODIFY
DELETE

Database
Interface
Layer

Open SQL

Native SQL
5

Database Operations |

Dec-2008

2005 IBM Corporation

IBM Global Services

Update
UPDATE YMOVIE.

Use values in field string created with


TABLES statement to update all fields
of one record.

UPDATE YMOVIE
SET WINNER = Forrest Gump
NOTES = Great movie!
WHERE AAYEAR = 1994
AND CATEGORY = PIC.

Update the specified fields of one


record (because full primary key is
used in the WHERE clause).

UPDATE YMOVIE
SET NOTES = Great movie!
WHERE CATEGORY = PIC.

Update the specified field of multiple


records (because full primary key is
not used in the WHERE clause).

Database Operations |

Dec-2008

2005 IBM Corporation

IBM Global Services

Insert

INSERT YMOVIE.

INSERT INTO YMOVIE


VALUES MOVIE_REC.

Use values in field string


created with TABLES
statement to insert one record.

Use values in MOVIE_REC field


string to insert one record.

MOVIE_REC field string must be same type


as a record in YMOVIE.

Database Operations |

Dec-2008

2005 IBM Corporation

IBM Global Services

Modify

MODIFY YMOVIE.

MODIFY YMOVIE
FROM MOVIE_REC.

Use values in field string created with


TABLES statement to modify one record.

Use values in MOVIE_REC field


string to modify one record.

MOVIE_REC field string must be same type


as a record in YMOVIE.

If the record does not exist, MODIFY will insert the record.
If the record does exist, MODIFY will update the record.
8

Database Operations |

Dec-2008

2005 IBM Corporation

IBM Global Services

Delete
DELETE YMOVIE.

Use values in field string created with


TABLES statement to delete one record.

DELETE FROM YMOVIE


WHERE AAYEAR = 1994
AND CATEGORY = PIC.

Use values in WHERE clause to delete


one record (because full primary key is
used in the WHERE clause).

DELETE FROM YMOVIE


WHERE CATEGORY = PIC.

Use values in WHERE clause to delete


multiple records (because full primary
key is not used in the WHERE clause).

Database Operations |

Dec-2008

2005 IBM Corporation

IBM Global Services

Internal Table Database Operations


UPDATE YMOVIE FROM TABLE MOVIE_ITAB.

INSERT YMOVIE FROM TABLE MOVIE_ITAB.

MODIFY YMOVIE FROM TABLE MOVIE_ITAB.

DELETE YMOVIE FROM TABLE MOVIE_ITAB.

MOVIE_ITAB internal table


must be same type
& length as the primary key of
YMOVIE.

10

Database Operations |

MOVIE_ITAB internal table


must be same type
as a record in YMOVIE.

Dec-2008

2005 IBM Corporation

IBM Global Services

Logical Unit of Work

Screen
1

Screen
2

Screen
3

SELECT A
SELECT B

UPDATE A

DELETE B

Database
Transaction

Database
Transaction

Database
Transaction

Update Transaction
(Logical Unit of Work)
11

Database Operations |

Dec-2008

2005 IBM Corporation

IBM Global Services

Commit or Rollback
Update

** MZA08I01 - PAI Modules **


MODULE UPDATE INPUT.
IF OKCODE = UPDA.
UPDATE YMOVIE.

If the update is successful, the


changes will be confirmed in
the database.

IF SY-SUBRC = 0.
COMMIT WORK.
MESSAGE S002.
ELSE.
ROLLBACK WORK.
MESSAGE I003.

If the update is not successful,


the changes will be cancelled
and rolled back from the database.

12

Database Operations |

ENDIF.
ENDIF.
ENDMODULE.

Dec-2008

2005 IBM Corporation

IBM Global Services

Summary
Because SQL statements are not standardized across database systems, SAP has
two forms of SQL statements: Open SQL and Native SQL.
The ABAP Open SQL statement UPDATE will change an already existing record
(or records) in the database table.
The ABAP Open SQL statement INSERT will add a non-existing record to the
database table.
A way to avoid an error with UPDATE or INSERT is to use MODIFY. The
ABAP Open SQL statement MODIFY will either insert a record (if the record does
not exist) or update a record (if the record already exists).
The ABAP Open SQL statement DELETE will remove an already existing record
(or records) from the database table.
If you are going to perform these database operations on several records, it is
more efficient to use an internal table. If you use an internal table, these database
operations are called array operations.

13

Database Operations |

Dec-2008

2005 IBM Corporation

IBM Global Services

Summary (Contd.)
The term logical unit of work (LUW) refers to a collection of all-or-nothing
actions performed at the database level as a complete unit.
The ABAP statement COMMIT WORK confirms all changes to the database
and closes an LUW. The ABAP statement ROLLBACK WORK cancels and
rolls back all changes in an LUW.

14

Database Operations |

Dec-2008

2005 IBM Corporation

IBM Global Services

Questions

What is SAP LUW ?

What is the purpose of COMMIT WORK statement ?

15

Database Operations |

Dec-2008

2005 IBM Corporation

Potrebbero piacerti anche