You are on page 1of 29

DATABASE ACCESS

Kaavian Systems

Agenda
What to access
Why to access
How do we access

Business Data Record from RDBMS


Increasing System Performance , By accessing
required set of data Records in an internal table
for manipulation from the RDBMS
Avoid Data Loss
Using SQL Statements - Open SQL / Native
Kaavian Systems
SQL Statement

Accessing the Database


Open SQL /
Native SQL
Reading data
Changing data
Performance

Kaavian Systems

Open SQL / Native


SQL

02-Sep-09

Kaavian Systems

Open SQL Statements


DML Statements

Keyword

Function

SELECT
INSERT

Reads data from database tables


Adds lines to database tables
Changes the contents of lines of
database tables

UPDATE
MODIFY

Inserts lines into database tables or


changes the contents of existing lines

DELETE

Deletes lines from database tables

OPEN
CURSOR,
FETCH,
CLOSE
CURSOR

Reads lines of database table using the


cursor
Kaavian Systems

Reading Data From Database


SELECT <result>
INTO
FROM

<target>
<source>

[WHERE <condition>]
[GROUP BY <fields>]
[HAVING

<cond>]

[ORDER BY <fields>]

Kaavian Systems

Reading Data

Kaavian Systems

Defining Selection
Reading a Single Line :
SELECT SINGLE <cols> WHERE
Reading Several Lines :
SELECT [DISTINCT] <cols> WHERE
Reading the Whole Line :
SELECT <lines> *

Kaavian Systems

Defining Selection
Reading Aggregate Data for Columns :
SELECT <lines> <agg> ([DISTINCT] <S1>

Aggregate functions:
MAX: returns the Maximum value of the Column<Si>
MIN : returns the Minimum value of the Column <Si>
AVG : returns the average value of the Column <Si>
SUM : returns the sum value of the Column <Si>
COUNT : returns the total number of lines in the selection
Kaavian Systems

Defining Selection
INTO clause
INTO wa
INTO CORRESPONDING FIELDS OF wa
INTO (f1,,fn)
INTO TABLE itab
INTO CORRESPONDING FIELDS OF TABLE itab
APPENDING TABLE itab
APPENDING CORRESPONDING FIELDS OF
TABLE itab

Kaavian Systems

10

Defining Selection
FROM Clause
FROM dbtab [AS alias]
FROM (dbtabname)
FROM <tab> [INNER] JOIN <dbtab> ON <cond>
<options>
FROM <tab> LEFT [OUTER] JOIN <dbtab> [AS
<alias>]
ON <cond> <options>
CLIENT SPECIFIED
BYPASSING BUFFER
UPTO n ROWS

Kaavian Systems

11

Inner Join

02-Sep-09

Kaavian Systems

12

Example

SELECT a~matnr a~mtart a~matkl


b~werks b~ekgrp b~pstat
INTO CORRESPONDING FIELDS OF TABLE itab
FROM mara as a INNER JOIN marc as b
ON a~matnr = b~matnr
UPTO 7 rows

Kaavian Systems

13

Defining Selection
FOR ALL ENTRIES IN itab WHERE cond
The comparison is performed for each line of the internal
table. For each line, the system selects the lines from the
database table that satisfy the condition. If <itab> is
empty, the addition FOR ALL ENTRIES is disregarded,
and all entries are read.

FOR ALL ENTRIES can be used to replace nested select


loops on internal tables to improve the performance for
large sets of selected data.
Kaavian Systems

14

Subqueries
SELECTSINGLE city latitude longitude

INTO(city, lati, longi)


FROMsgeocity.
SELECT DISTINCT matnr mtart
from MARA
INTO CORRESPONDING FIELDS FOR ITAB.

Kaavian Systems

15

Inserting Lines Into Table


Inserting a Single Line
<wa>

INSERT INTO dbtab [CLIENT SPECIFIED] VALUES


( or )

INSERT <dbtab> FROM <wa>


INSERT <dbtab>
( or )
INSERT (dbtabname)

Inserting Several Lines


INSERT <dbtab> FROM TABLE itab
[ACCEPTING DUPLICATE KEYS]
Kaavian Systems

16

Changing Lines

Updating individual line


UPDATE <dbtab> FROM <wa>
UPDATE <dbtab>

Changing Lines Column by Column

<cond>]

UPDATE dbtab SET f1 ... fn

[WHERE

Updating several lines


UPDATE dbtab FROM TABLE itab or
UPDATE (dbtabname) FROM TABLE itab
Kaavian Systems

17

Modifying Lines

Modifying single line


MODIFY dbtab FROM <wa>
MODIFY dbtab

Modifying several lines


MODIFY dbtab FROM TABLE itab
MODIFY (dbtabname) FROM TABLE itab

Kaavian Systems

18

Deleting Lines
Deleting single line
DELETE <dbtab> FROM <wa>
DELETE dbtab

Deleting several lines


DELETE dbtab FROM TABLE itab
DELETE (dbtabname) FROM TABLE itab
DELETE FROM dbtab WHERE <cond>

Kaavian Systems

19

Committing Database Changes


COMMIT WORK
Concludes a database LUW and begins a new one

ROLLBACK WORK
Undoes all changes back to the start of the database LUW

Kaavian Systems

20

Performance
Keep the Result Set Small
Minimize the Amount of Data Transferred
Minimize the Search Overhead
Reduce the Database Load

Kaavian Systems

21

Minimal Data Transfer


Restrict number of lines
Restrict number of columns
Use Aggregate Functions
Specific update for certain columns

Kaavian Systems

22

Minimal Number of Transfers


Multiple operations
Avoid nested select
Use joins

Kaavian Systems

23

Minimizing Search Overhead


Primary Index
Secondary Index

Kaavian Systems

24

Database Load Reduction


Use Buffer
Minimize the use of statements that bypasses buffer
Sort in ABAP program

Kaavian Systems

25

Statements that Bypasses Buffer


The BYPASSING BUFFER addition in the FROM
clause
The DISTINCT addition in the SELECT clause
Aggregate expressions in the SELECT clause
Joins in the FROM clause
The IS NULL condition in the WHERE clause
Subqueries in the WHERE clause
The ORDER BY clause
The GROUP BY clause
The FOR UPDATE addition
Kaavian Systems

26

Scenario
Retrieve all the Sales orders in the current
year (eg 2004) where
Sales order is of type OR
The value of the order is more than $1000.00
The number of line items are more than 2
And the sales order does not contain material M-11

Kaavian Systems

27

Reference
The learning material contained in this PowerPoint Presentation is
prepared with the help of http://help.sap.com/ and sap press book. All
rights reserved by SAP AG.
Unless otherwise specifically stated, this learning material is intended
for the sole purpose of class room session, internal use and for
knowledge transfer to the Consultants.
However, for detailed information on this learning
http://help.sap.com / sap press book may be referred.

material,

Kaavian is not liable for any legal liability or responsibility for the
accuracy, completeness or usefulness of any information disclosed in
these learning materials.
No portion of the learning materials shall be modified, reproduced or
transmitted in any form by any means, whether by electronic,
photocopier or otherwise without the written permission of Kaavian. In
no event shall Kaavian be liable for any damage whatsoever resulting in
any action arising in connection with the use of learning material.
Kaavian Systems

28

Discussion

Kaavian Systems

You might also like