chapter 13_database operations

Upload: raul-thomas

Post on 03-Jun-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Chapter 13_Database Operations

    1/14

    IBM Global Services

    2005 IBM CorporationDatabase Operations | 6.13 March-2005

    Database Operations

  • 8/12/2019 Chapter 13_Database Operations

    2/14

    IBM Global Services

    2005 IBM Corporation2 March-2005Database Operations | 6.13

    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

  • 8/12/2019 Chapter 13_Database Operations

    3/14

    IBM Global Services

    2005 IBM Corporation3 March-2005Database Operations | 6.13

    Overview

    Academy Awards

    Year

    Category

    1994

    PIC

    Winner Forrest Gump

    Notes Tom Hanks had an Oscar-winning performance.

    Critic Dean

    UPDATE

    INSERTMODIFY

    DELETE

    We will now add functionality to the Updatepushbutton by learning the database operation

    UPDATE. We will also learn INSERT, MODIFY,

    and DELETE.

    UpdateExit

    Enter Name

    YMOVIEDatabase Table

  • 8/12/2019 Chapter 13_Database Operations

    4/14

    IBM Global Services

    2005 IBM Corporation4 March-2005Database Operations | 6.13

    Open versus Native SQL

    ABAP

    Program

    Open SQL

    Native SQL

    Database

    Interface

    Layer

    ** MZA08TOP - Top Include **

    PROGRAM SAPMZA08 MESSAGE-ID ZA.

    TABLES YMOVIE.

    DATA OKCODE(4).

    UPDATEINSERT

    MODIFY

    DELETE

    YMOVIEDatabase Table

    TABLES statement

    defines the work area forYMOVIE table

  • 8/12/2019 Chapter 13_Database Operations

    5/14

    IBM Global Services

    2005 IBM Corporation5 March-2005Database Operations | 6.13

    Open versus Native SQL (Contd.)

    ABAP

    Program

    Open SQL

    Native SQL

    Database

    Interface

    Layer

    ** MZA08TOP - Top Include **

    PROGRAM SAPMZA08 MESSAGE-ID ZA.

    TABLES YMOVIE.

    DATA OKCODE(4).

    UPDATEINSERT

    MODIFY

    DELETE

    YMOVIEDatabase Table

    TABLES statement

    defines the work area forYMOVIE table

  • 8/12/2019 Chapter 13_Database Operations

    6/14

    IBM Global Services

    2005 IBM Corporation6 March-2005Database Operations | 6.13

    Update

    UPDATE YMOVIE.Use values in field string created with

    TABLES statement to update allfields

    of onerecord.

    UPDATE YMOVIESET WINNER = Forrest Gump

    NOTES = Great movie!

    WHERE AAYEAR = 1994

    AND CATEGORY = PIC.

    Update the specified fields of onerecord (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

    notused in the WHERE clause).

  • 8/12/2019 Chapter 13_Database Operations

    7/14

    IBM Global Services

    2005 IBM Corporation7 March-2005Database Operations | 6.13

    Insert

    INSERT YMOVIE.Use values in field string

    created with TABLES

    statement to insert onerecord.

    INSERT INTO YMOVIE

    VALUES MOVIE_REC.Use values in MOVIE_REC field

    string to insert onerecord.

    MOVIE_REC field string must be at least

    the same length as a record in YMOVIE.

  • 8/12/2019 Chapter 13_Database Operations

    8/14

    IBM Global Services

    2005 IBM Corporation8 March-2005Database Operations | 6.13

    Modify

    MODIFY YMOVIE.Use values in field string created with

    TABLES statement to modify onerecord.

    MODIFY YMOVIEFROM MOVIE_REC. Use values in MOVIE_REC field

    string to modify onerecord.

    If the record does notexist, MODIFY will insertthe record.

    If the record does exist, MODIFY will updatethe record.

    MOVIE_REC field string must be at leastthe same length as a record in YMOVIE.

  • 8/12/2019 Chapter 13_Database Operations

    9/14

    IBM Global Services

    2005 IBM Corporation9 March-2005Database Operations | 6.13

    Delete

    DELETE YMOVIE.Use values in field string created with

    TABLES statement to delete onerecord.

    DELETE FROM YMOVIE

    WHERE AAYEAR = 1994

    AND CATEGORY = PIC.

    Use values in WHERE clause to delete

    onerecord (because full primary key is

    used in the WHERE clause).

    DELETE FROM YMOVIE

    WHERE CATEGORY = PIC.

    Use values in WHERE clause to delete

    multiplerecords (because full primary

    key is notused in the WHERE clause).

  • 8/12/2019 Chapter 13_Database Operations

    10/14

    IBM Global Services

    2005 IBM Corporation10 March-2005Database Operations | 6.13

    UPDATE YMOVIE FROM TABLE MOVIE_ITAB.

    INSERT YMOVIE FROM TABLE MOVIE_ITAB.

    MODIFY YMOVIE FROM TABLE MOVIE_ITAB.

    DELETE YMOVIE FROM TABLE MOVIE_ITAB.

    Internal Table Database Operations

    MOVIE_ITAB internal table

    must be at least the same length

    as the pr imary keyof YMOVIE.

    MOVIE_ITAB internal table

    must be at least the same length

    as a recordin YMOVIE.

  • 8/12/2019 Chapter 13_Database Operations

    11/14

    IBM Global Services

    2005 IBM Corporation11 March-2005Database Operations | 6.13

    Logical Unit of Work

    Screen

    1

    Database

    Transaction

    Screen

    2

    Database

    Transaction

    Screen

    3

    Database

    Transaction

    Update Transaction

    (Logical Unit of Work)

    SELECT ASELECT B

    UPDATE A DELETE B

  • 8/12/2019 Chapter 13_Database Operations

    12/14

    IBM Global Services

    2005 IBM Corporation12 March-2005Database Operations | 6.13

    Commit or Rollback

    ** MZA08I01 - PAI Modules **

    MODULE UPDATE INPUT.

    IF OKCODE = UPDA.

    UPDATE YMOVIE.

    IF SY-SUBRC = 0.

    COMMIT WORK.

    MESSAGE S002.

    ELSE.

    ROLLBACK WORK.MESSAGE I003.

    ENDIF.

    ENDIF.

    ENDMODULE.

    Update

    If the update is successful, the

    changes will be confirmed in

    the database.

    If the update isnot successful,

    the changes will be cancelled

    and rolled back from the database.

  • 8/12/2019 Chapter 13_Database Operations

    13/14

    IBM Global Services

    2005 IBM Corporation13 March-2005Database Operations | 6.13

    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.

    Summary

  • 8/12/2019 Chapter 13_Database Operations

    14/14

    IBM Global Services

    2005 IBM Corporation14 March-2005Database Operations | 6.13

    Summary (Contd.)

    The term logical unit of work (LUW) refers to a collection of all-or-nothingactions 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.