21
Satyam Computer Services Ltd, Pune, India 1999 Processing Database Tables Satyam Computer Services Ltd, Pune, India 1999

SAP DataBase Concepts New

Embed Size (px)

DESCRIPTION

SAP

Citation preview

Satyam Computer Services Ltd, Pune, India 1999

Processing Database Tables

Satyam Computer Services Ltd, Pune, India 1999

Satyam Computer Services Ltd, Pune, India 1999

Database Tables and SQL Concepts

To avoid incompatibilities between different database tables and also to make ABAP/4 programs independent of the database system in use, SAP has created a set of separate SQL statements called Open SQL.

Open SQL contains a subset of standard SQL statements as well as some enhancements which are specific to SAP.

Satyam Computer Services Ltd, Pune, India 1999

Difference between Open SQL & Native SQL

ABAP/4Interpreter

Select *From….

EXEC SQLSelect ...Endselect

DB interfaceSQL

Database

Native SQL

DB Data

SAP SQL

DB SQL

DB Data

DB

Data

Satyam Computer Services Ltd, Pune, India 1999

Difference between Open & Native SQL

A database interface translates SAP's Open SQL statements into SQL commands specific to the database in use. Native SQL statements access the database directly.

Satyam Computer Services Ltd, Pune, India 1999

Open SQL Keywords

Keyword Used for

SELECT Reading Data from Database Tables

INSERT Adding Lines to Database Tables

UPDATE Changing Lines in Database Tables

MODIFY Adding or Changing Lines

DELETE Deleting Lines from Database Tables

Satyam Computer Services Ltd, Pune, India 1999

Open SQL Keywords

Keyword Used For

OPEN CURSOR,

FETCH,

CLOSE CURSOR Reading Lines from Database Tables with a Cursor

COMMIT WORK,

ROLLBACK WORK Confirming or Reversing Changes to

Database Tables

Satyam Computer Services Ltd, Pune, India 1999

Important points

When using Open SQL statements in an ABAP/4 program, you must ensure the following:

1) The database system being addressed must be supported by SAP.

2) The database tables being addressed must be defined in the ABAP/4 Dictionary.

Satyam Computer Services Ltd, Pune, India 1999

Important Points

The following system fields play an important role in Open SQL operations:

SY-SUBRC

As with other ABAP/4 statements, the return code value in the system field SY-SUBRC indicates after each Open SQL operation whether or not the operation was successful. If an operation is successful, SY-SUBRC = 0. If an operation is unsuccessful, SY-SUBRC <> 0.

Satyam Computer Services Ltd, Pune, India 1999

Important Points

SY-DBCNT

The value in the SY-DBCNT field indicates how many lines were affected by the operation or how many lines have already been processed.

Satyam Computer Services Ltd, Pune, India 1999

Data Retrieval

Reading data from database tables :

SELECT <result> FROM <source>

[INTO <target>]

[WHERE <condition>]

[GROUP BY <fields>]

[ORDER BY <sort_order>]

Satyam Computer Services Ltd, Pune, India 1999

Data Retrieval

TABLES tablename. table declaration

SELECT * FROM tablename . read all records

Statements.ENDSELECT.

SELECT SINGLE * FROM tablename.Statements. read

single record (Endselect NOT required)

Satyam Computer Services Ltd, Pune, India 1999

Data Retrieval contd.

SELECT * FROM table WHERE conditionStatements.

ENDSELECT.

SELECT * FROM table INTOinternal_table.

Statements(Endselect NOT required)

Satyam Computer Services Ltd, Pune, India 1999

Data Retrieval contd.

SELECT * FROM table WHERE conditionORDER BY field1 field2.

Statements.ENDSELECT.

SELECT COUNT(*) FROM table WHERE condition.

(Gives # of records)

Satyam Computer Services Ltd, Pune, India 1999

Dynamic Selection

SELECT-OPTIONS sname FORcustomers_name.

SELECT * FROM customersINTO all_customers

WHERE name IN sname.(Program offers the user a screen to enter selection

criteria into the select-options, and select statement reads set of data as specified by the user)

Satyam Computer Services Ltd, Pune, India 1999

Dynamic Selection contd.

DATA : tname(4),

no_of_rows TYPE I.

MOVE ‘CUST’ TO tname.

SELECT COUNT(*) FROM (tname) INTO no_of_rows.

WRITE : tname, no_of_rows.(Data is read from table CUST)

Satyam Computer Services Ltd, Pune, India 1999

Nested Select

SELECT * FROM customers.

SELECT * FROM bookings WHERE

custid = customers-id.

WRITE : / customers-name,

bookings-fldate.

ENDSELECT.

ENDSELECT.

Satyam Computer Services Ltd, Pune, India 1999

DATABASE UPDATION

Changing the contents of the database tables :

You can change the contents of the database table by using the following commands :

• INSERT :

Adding a single line:

INSERT <dbtab> FROM <wa>.

Adding a several lines from an internal table:

INSERT <dbtab> FROM TABLE <itab>.

Satyam Computer Services Ltd, Pune, India 1999

DATABASE UPDATION

• UPDATE :

To change single line -

UPDATE <dbtab> FROM <wa>.

To change several lines -

UPDATE <dbtab> SET <s1>....<sn>

[WHERE <condition>].

To change several lines using internal tables -

UPDATE <dbtab> FROM TABLE <itab>.

Satyam Computer Services Ltd, Pune, India 1999

Accessing Database Tables• MODIFY

Adding or changing single line :

MODIFY <dbtab> [FROM <wa>].

Adding or changing multiple lines:

MODIFY <dbtab> FROM TABLE <itab>.

Satyam Computer Services Ltd, Pune, India 1999

Accessing Database Tables

• DELETE

Deleting a single line-

DELETE <dbtab> FROM <wa>.

Deleting several lines-

DELETE FROM <dbtab>

WHERE <conditions>.

Deleting several lines using internal tables -

DELETE <dbtab> FROM TABLE <itab>.

Satyam Computer Services Ltd, Pune, India 1999

Accessing Database Tables

• Using Native SQL -

To use a native SQL statement , it must be preceded by EXEC SQL statement and concluded by END SQL statement .

EXEC SQL

< native SQL statement>

END EXEC.