Sie sind auf Seite 1von 11

UNIT1: DATABASE UPDATE WITH OPEN SQL

Tips: If you debug program (that modify database) in an exclusive mode, database changes are only
commited if explicitly triggered from the debugger (Edit > Database > Commit).
All database changes can be rolled back explicitly from debugger (Edit > Database > Rollback).

Open SQL: Introduction

Figure 1: Overview - SQL Terms

In ABAP, there are Open SQL commands and Native SQL commands for making DB changes. The Open
SQL set of commands only comprises operations for Data Manipulation Language (DML).
Figure 2: Target Quantity and Return Values

In Open SQL system fields:


SY-SUBRC determine error (SY-SUBRC <> 0) and success (SY-SUBRC= 0) in accessing or
modifying database table.
SY-DBCNT displays number of records for which the desired database operation was actually
carried out.
Commands that process several lines usually give better performance than corresponding single-set
access (exception: mass data change using modify).
If you have masked field selections (WHERE <field> LIKE '<search_mask>'), note that '_' masks and
individual character and '%' masks a character string of any lenght.
Open SQL commands do not perform any automatic authorization checks. You need to execute explicitly
(statement AUTHORITY-CHECK).
Figure 3: Accessing Client-Specific Table

No CLIENT SPECIFIED addition in command:


Client specification in the command not possible (in WHERE clause). Only access to current
client.
CLIENT SPECIFIED addition in command
Client specification in the command is possible (in WHERE clause).
Client specified >> Access to specified client
Client not specified >> Access to all clients

If the addition CLIENT SPECIFIED is not specified and the database table contains client dependant data,
the records of the current execution client are accessed. The corresponding WHERE clause is
automatically added by database interface.
To process data from other client, you must specife CLIENT SPECIFIED in the Open SQL and the
respective client in the appropriate WHERE.

CAUTION: If an Open SQL command contains the addition CLIENT SPECIFIED without a client
specification, the datasets of all clients are accessed.
Open SQL: Syntax
INSERT, UPDATE, DELETE, and MODIFY may be used to change the content of database tables.

Creating Database Records

Figure 4: Creating a Single Record

INSERT INTO <dbtab> [CLIENT SPECIFIED] VALUES <wa>.


INSERT <dbtab> [CLIENT SPECIFIED] FROM <wa>.

<dbtab>: database table name/ view name


<wa>: line to be inserted and the structure line must be the same as <dbtab>.
The client field that may possibly exist in the structure <wa> will only be taken into consideration if the
CLIENT SPECIFIED addition is specified.
If <dbtab> is a view (in ABAP dictionary), the maintenance status must be set to "read and change" and
must only contain fields from a table.
INSERT return code (SY-SUBRC):
0: Successfully inserted
4: Error because a line with the same key already exists.
Figure 5: Creating Multiple Records

INSERT <dbtab> [CLIENT SPECIFIED] FROM TABLE <itab> [ACCEPTING DUPLICATE KEYS].
<dbtab>: database table
<itab>: internal table with the same line structure as <dbtab>.

The client field that may possibly exist in the <itab> will only be taken into consideration if the CLIENT
SPECIFIED addition is specified.

INSERT return code (SY-SUBRC):


0: Successfully inserted (all of lines)
4: Error because 1 line or more with the same key already exist. The entire insertion is discarded
(database rollback).

INSERT with ACCEPTING DUPLICATE KEYS return code (SY-SUBRC):


0: Successfully inserted (all of lines)
4: If there is an error, but all the records without errors are inserted, runtime error and database
rollback is suppressed.
SY-DBCNT contains the number of rows that were successfully inserted in the database.
Changing Database Records

Figure 6: Changing a single record

UPDATE <dbtab> [CLIENT SPECIFIED] FROM <wa>.


UPDATE <dbtab> [CLIENT SPECIFIED] SET <f1> = <g1> ...
<fn> = <gn>
WHERE <full_qualified_key>.

<dbtab>: database table name


<wa>: line to be inserted and the structure line must be the same as <dbtab>.
<f1>...<fn>: field in <dbtab> that you want to change.
<g1>...<gn>: values you want to set to change <f1>...<fn>
You can specify simple calculation operations as evaluation for numeric database fields in the SET
addition: f = g, f = f + g, f = f - g.

UPDATE return code (SY-SUBRC):


0: Line was changed
4: Line could not be changed
Figure 7: Changing several records

UPDATE <dbtab> [CLIENT SPECIFIED] FROM TABLE <itab>.


UPDATE <dbtab> [CLIENT SPECIFIED] SET <f1> = <g1> ...
<fn> = <gn>
WHERE <condition>.

<dbtab>: database table


<itab>: internal table with the same line structure as <dbtab>.
<condition>: condition

UPDATE return code (SY-SUBRC):


0: At least one line has been changed.
4: No line was changed.

UPDATE using <itab> return code (SY-SUBRC):


0: Successful completely
4: At least one line cannot be changed (error)
SY-DBCNT contains the number updated rows.
Modifying Database Records

MODIFY command cover 2 commands:


UPDATE: If specified entry exists.
INSERT: If specified entry does not exist.

MODIFY can also be carried out on views. View's maintenance status must be "read and change" and
must view contains only fields from a table.

MODIFY return data (SY-SUBRC):


0: Specified record or all specified records were processed (updated/inserted).
4: The specified record or at least one could not be processed. For example, because the record
does not exist in the database and inserting it would destroy a unique secondary index. If you
have chosen mass data change, the other record were processed.
Deleting Database Records

Figure 9: Deleting a single record

DELETE <dbtab> [CLIENT SPECIFIED] FROM <wa>.


DELETE FROM <dbtab> [CLIENT SPECIFIED] WHERE <full_qualified_key>.

DELETE return data (SY-SUBRC):


0: Line was deleted
4: Line could not be deleted.
Figure 10: Deleting Several Records

DELETE <dbtab> [CLIENT SPECIFIED] FROM TABLE <itab>.


DELETE FROM <dbtab> [CLIENT SPECIFIED] WHERE <condition>.

You can use LIKE '%' in the <condition>.

DELETE using <itab> return code:


0: All lines in <itab> deleted.
4: At least one line could not be deleted. The other records were deleted.

SY-DBCNT shows number of lines deleted.

DELETE using <condition> return code:


0: At least one line was deleted.
4: No line was deleted.

SY-DBCNT shows number of lines deleted.


Restoring Previous Database
There are 2 ways of returning database rollback:
Sending a termination dialog message (message type A).
Using Rollback Work ABAP statement (doesn't terminate program).

UNIT 2: LUWs and Client/Server Architecture

Das könnte Ihnen auch gefallen