Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 32

ABAP Database Access

Open/ABAP SQL

1
 ABAP SQL
 Database and SQL
 Open SQL Statements
 SELECT
 INSERT
 MODIFY
 UPDATE
 DELETE
 Transaction Handling
 Native SQL

2
Overview
ABAP SQL ( Structured Query Language ) is a subset of SQL statements included Query operations and modification Operations
 ABAP SQL statements can work on any database. ABAP SQL Statements converted to Database Specific SQL
 ABAP SQL Statements can be converted to Native SQL during execution
 ABAP SQL uses Automatic Client Handling to access data only in the Current Client
 ABAP SQL Statements can read the Buffers in the Application Server
 ABAP SQL statements tightly integrated with Dictionary hence Open SQL works only on those tables, views which are exists
in Data Dictionary

Open SQL Statements:


 SELECT
 INSERT
 UPDATE
 MODIFY
 DELETE

3
Overview
Communication Between Application Layer and Database Layer

Database Layer
Application Layer

Application Server

Application Program#1

ABAP SQL Commands


( SELECT | INSERT | MODIFY | DELETE )

Application Program#2
Amount Curr Delivery Date

DB Interface
Order Customer Product Quantity UoM
ABAP SQL Commands 1 CUST1 PRD1 100 EA 100 USD 01.01.2019
( SELECT | INSERT | MODIFY | DELETE ) 2 CUST1 PRD2 200 EA 200 USD 01.08.2019

3 CUST2 PRD3 100 EA 100 USD 01.20.2019


Application Program#3 400 USD 01.31.2019
4 CUST3 PRD4 400 EA

400 EA 400 USD 01.31.2019


ABAP SQL Commands 5 CUST1 PRD4
( SELECT | INSERT | MODIFY | DELETE ) 6 CUST4 PRD4 400 EA 400 USD 01.31.2019

Application Program#n

ABAP SQL Commands


( SELECT | INSERT | MODIFY | DELETE )

4
SELECT
SELECT command is used to get the Data from the Database Table. Selection can be done on Table in following ways
 Applies on Single Record
 Applies on Multiple Records
 Applies On groups

We can fetch single or multiple fields or all the fields from the database table for Single Record as well as multiple records

Syntax:
SELECT [SINGLE] DISTINCT * | <Fields List> | <Aggregate Functions>
FROM < Single Table/View | Multiple Tables/Views ON <Condition>
WHERE <Row Filter Condition>
GROUP BY <Fields List>
HAVING <Aggregate Condition>
INTO [TABLE] <Target Data Object>

5
SELECT -Single Record
SELECT SINGLE statement can be used get the single record. Filter condition on Primary key field has to be specified in the
WHERE Clause to get the unique record from Database Table

Syntax: Reading All fields or Multiple Felds


Reading Single Field
SELECT * | <List of Fields>
FROM <Database Table/View>
Order Customer Product Quantity UoM Amount Curr Delivery Date
INTO <Work Area> 1 CUST1 PRD1 100 EA 100 USD 01.01.2019

WHERE <Condition on Key Field> 2 CUST1 PRD2 200 EA 200 USD 01.08.2019

3 CUST2 PRD3 100 EA 100 USD 01.20.2019

4 CUST3 PRD4 400 EA 400 USD 01.31.2019

Syntax: Reading single Fields 5 CUST1 PRD4 400 EA 400 USD 01.31.2019

6 CUST4 PRD4 400 EA 400 USD 01.31.2019


SELCT <Single Fields>
FROM <Database Table/View>
INTO <Elementary Variable> Reading Some fields
Reading Complete Record
WHERE <Condition on Key Field>

6
SELECT-Single Record: Example
Select Single field from VBAK Table Select some fields from VBAK Table Select all fields from VBAK Table

7
SELECT-Multiple Records

SELECT statement can be used get the multiple records. Filter condition is optional and can be specified on any field in the database
table. Recommended to apply the Condition on Primary key fields or Secondary key fields

Syntax: Reading All fields or Multiple Felds


Reading Single Column
SELECT DISTINCT * | <List of Fields>
FROM <Database Table/View>
INTO TABLE<Internal Table> Order Customer Product Quantity UoM Amount Curr Delivery Date

1 CUST1 PRD1 100 EA 100 USD 01.01.2019


WHERE <Condition on Key Field> 2 CUST1 PRD2 200 EA 200 USD 01.08.2019

3 CUST2 PRD3 100 EA 100 USD 01.20.2019

4 CUST3 PRD4 400 EA 400 USD 01.31.2019

5 CUST1 PRD4 400 EA 400 USD 01.31.2019


 DISTINCT filter the result set and returns only unique 400 EA 400 USD 01.31.2019
6 CUST4 PRD4

records
 With out WHERE condition, Database will retrieve all the
Reading all Columns Reading Specific Columns
records from the table

8
SELECT-Multiple Records: Example – Single Column
Select Gross Amount from the all orders of the Customer ‘1390’

Number of records retrieved

9
SELECT-Multiple Records: Example – Multiple Columns
Select orders for the Customer ‘1390’

Output

10
SELECT-Multiple Records: Example – All Columns
Select orders for the Customer ‘1390’

Output

11
SELECT-WHERE Condition
WHERE Clause in the SELECT statement allows to filter the rows when selecting data from database table. We can pass the filter
condition for single field or multiple fields. Conditions on multiple field can be combined either by using AND or OR operator

 Simple WHERE Clause: Constants and Variables can be used to pass data to Database fields.
 Using Select-Options and Range Tables: To allow Multiple values to the same fields
 Using FOR ALL ENTRIES: Internal table can be used for filtering data on the database table

Syntax: Syntax: Syntax:


SELECT.. SELECT.. SELECT..
WHERE <field1> = <Value> WHERE <field1> IN <Range Table| Selct-Options> FOR ALL ENTRIES IN <Internal Table>
AND <Field2> = <Elementary variable>… AND <Field2> = <Elementary variable>… WHERE <field1> EQ <Internal Table>-<Field>
AND <Field2> = <Elementary variable>…

12
SELECT-WHERE Condition: Example – Multiple Elementary Fields
Get the list of Sales Orders from the VBAK table where Sales Organization is ‘1000’ and Currency is ‘EUR’ in the year ‘2018’ and Amount
should not be zero

13
SELECT-WHERE Condition: Example – Range Table
Get the list of Sales Orders from the VBAK table where Sales Organization is ‘1000’ and Currency is ‘EUR’ in the year ‘2018’ and Amount should not
be zero for the customers 1177 and 1000

14
SELECT-WHERE Condition: Example – FOR ALL ENTRIES
Get the list of Sales Orders for the customers who are from ‘US’

15
SELECT-JOIN:
JOIN Clause in the SELECT statement allows to combine and return the result from multiple database tables/views. Generally data
from the tables already linked with foreign key relationship.

Ex#1: Sales Order header and Sales Order items


VBAK an VBAP tables joined with Foreign key relationship with Sales Order number VBELN
Ex#2:Customer and Sales Order Header
KNA1 and VBAK table joined with Foreign key relationship with Sales Order number VBELN

Following are the Different Join Types


INNER JOIN: Inner join allows to read only those records from the joined table if join condition is fulfilled.

OUTER JOIN: LEFT OUTER: LEFT Outer join allows to read records from the joined table if join condition is fulfilled and remaining
records from the Left Table.

OUTER JOIN: RIGHT OUTER: RIGHT Outer join allows to read records from the joined table if join condition is fulfilled and
remaining records from the RIGHT Table

16
SELECT-JOIN: Example
Syntax:
SLEECT
Table1~Field1
Table1~Field2
..
Table2~Field1
Table2~Field2
..
FROM Table1 INNER JOIN Table2
ON Table1~Fieldn = Table2~Fieldm
..
Note:
 If same field name exists in the both the tables then
need to qualify the field with Table Name or Alias Name
 ON Condition on Single field or Multiple Fields

17
SELECT-JOIN: Example
Get Customer details who are from ‘US’ along with Sales Order details

18
SELECT-GROUP BY and HAVING
GROUP BY Clause allows to process the records of the Table as Group and applies Aggregate Functions to get the result at certain
granularity levels
Table Data

Order Customer Product Quantity UoM Amount Curr Delivery Date

1 CUST1 PRD1 100 EA 100 USD 01.01.2019

2 CUST1 PRD2 200 EA 200 USD 01.08.2019

3 CUST2 PRD3 100 EA 100 USD 01.20.2019

4 CUST3 PRD4 400 EA 400 USD 01.31.2019

5 CUST1 PRD4 400 EA 400 USD 01.31.2019

6 CUST4 PRD4 400 EA 400 USD 01.31.2019

Customer Wise Product Wise


Grouping Grouping
Group#1: Group#1:
1 CUST1 PRD1 100 EA 100 USD 01.01.2019
1 CUST1 PRD1 100 EA 100 USD 01.01.2019

2 CUST1 PRD2 200 EA 200 USD 01.08.2019


Group#2:
5 CUST1 PRD4 400 EA 400 USD 01.31.2019
2 CUST1 PRD2 200 EA 200 USD 01.08.2019
Group#2:
Group#3:
3 CUST2 PRD3 100 EA 100 USD 01.20.2019 100 USD 01.20.2019
3 CUST2 PRD3 100 EA

Group#3:
Group#4:
4 CUST3 PRD4 400 EA 400 USD 01.31.2019
4 CUST3 PRD4 400 EA 400 USD 01.31.2019

Group#4: 5 CUST1 PRD4 400 EA 400 USD 01.31.2019

6 CUST4 PRD4 400 EA 400 USD 01.31.2019


6 CUST4 PRD4 400 EA 400 USD 01.31.2019

19
SELECT-GROUP BY and HAVING
Aggregate Functions
 SUM: Calculates sum for the Specified in the function for individual Groups
 MAX: Calculates Maximum for the Specified in the function for individual Groups
 MIN: Calculates Minimum for the Specified in the function for individual Groups
 COUNT: Calculates Count for the Specified in the function for individual Groups
 AVG: Calculates Average for the Specified in the function for individual Groups

HAVING Clause:
HAVING Clause is filter the result after the Grouping Operation and applying the Aggregate Operation

SYNTAX
SELECT <Column>…Agr_fun(Column)…
FROM <Table>

GROUP BY Column1 ….
HAVING Agr_fun <Operator> <Value>

20
SELECT-GROUP BY and HAVING: Example
Get Customer wise Gross amount from the Sales Orders

21
SELECT-GROUP BY and HAVING: Example
Get Customer wise Gross Amount where sum is greater than 500000

22
SELECT-ORDER BY: Example
Get Customer wise Gross Amount where sum is greater than 500000

23
INSERT
INSERT SQL Command allows to insert record to the database table. Using Open SQL INSERT statement, application can send one
or many records at a time to the database.

Application Layer Database Layer

Application Server

Application Program

Open SQL Commands Amount Curr Delivery Date


Order Customer Product Quantity UoM
INSERT
1 CUST1 PRD1 100 EA 100 USD 01.01.2019

7 CUST6 PRD4 400 EA 400 USD 02.31.2019 2 CUST1 PRD2 200 EA 200 USD 01.08.2019

8 CUST5 PRD4 400 EA 400 USD 02.31.2019 3 CUST2 PRD3 100 EA 100 USD 01.20.2019

4 CUST3 PRD4 400 EA 400 USD 01.31.2019

5 CUST1 PRD4 400 EA 400 USD 01.31.2019

6 CUST4 PRD4 400 EA 400 USD 01.31.2019

Syntax:
INSERT INTO <Database Table> FROM <Work Area>
INSERT INTO <Database Table> FROM TABLE <Internal Table>
INSERT INTO <Database Table> VALUES < Individual Fields >
INSERT INTO <Database Table> FROM SELECT <Query>

24
INSERT- Single Record: An Example
System will set the SY-SUBRC value when executed in the INSERT command
 0 – Record successfully inserted
 4 – Row with same key can exists in the Database Table

Insert single record using Work Area.


Step#1: Fill work area
Step#2: Insert Work area to Database table using INSERT Command

25
INSERT- Multiple Records: An Example
Insert multiple records using Internal Table.
Step#1: Fill Internal Table
Step#2: Insert multiple records to Database table using INSERT Command

26
UPDATE
UPDATE SQL Command allows to update the content of the existing record to the database table.

Syntax:
UPDATE <Database Table> <Source>
<Source> determines the entries to be updated and value to be updated

UPDATE <Database Table> SET <Expression> WHERE <Condition>


UPDATE <Database Table> FROM <Work Area>
UPDATE <Database Table> FROM TABLE <Internal Table>

System will set the SY-SUBRC value when executed the UPDATE statement.
0 – At least one row updated
4 – No row changed

27
UPDATE – An Example
Update the Records using SET operation

Entries from the Database table Before executing the Entries from the Database table After executing the
Update Statement Update Statement

28
MODIFY
MODIFY SQL Command allows to either
 Insert record to the table if no record found in the database table for given key
 Updates the existing record if record found in the database table for given key

Syntax:
MODIFY <Database Table> <Source>
<Source> determines the entries to be updated and value to be updated

MODIFY <Database Table> SET <Expression> WHERE <Condition>


MODIFY <Database Table> FROM <Work Area>
MODIFY <Database Table> FROM TABLE <Internal Table>

System will set the SY-SUBRC value when executed the UPDATE statement.
0 – If record is updated from the Work Area or if all records processed from Internal Table
4 – No row changed

29
MODIFY – An Example
Modifying multiple records using MODIFY statement
Entries from the Database table Before executing the
MODIFY Statement

Entries from the Database table After executing the


MODIFY Statement

30
DELETE
DELETE SQL Command deletes one or more number of records from Database Table

Syntax:
DELETE FROM <Database Table> WHERE <Condition>
DELETE <Database Table> FROM <Work Area>
DELETE <Database Table> FROM TABLE <Internal Table>

System will set the SY-SUBRC value when executed the UPDATE statement.
0 – If at least one record deleted from the Database table for given condition
4 – No row deleted from Database Table

31
DELETE – An Example

Delete the records from the table where Gross Amount > 100

Entries from the Database table Before executing the Entries from the Database table Before executing the
DELETE Statement DELETE Statement

32

You might also like