03 - ABAP Basics - Open SQL
03 - ABAP Basics - Open SQL
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
3
Overview
Communication Between Application Layer and Database Layer
Database Layer
Application Layer
Application Server
Application Program#1
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
Application Program#n
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
WHERE <Condition on Key Field> 2 CUST1 PRD2 200 EA 200 USD 01.08.2019
Syntax: Reading single Fields 5 CUST1 PRD4 400 EA 400 USD 01.31.2019
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
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’
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
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.
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
Group#3:
Group#4:
4 CUST3 PRD4 400 EA 400 USD 01.31.2019
4 CUST3 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 Server
Application Program
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
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
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
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
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
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