Chapter 10 - ABAP Open SQL Extensions
Chapter 10 - ABAP Open SQL Extensions
Dec-2008
Objectives
The participants will be able to:
Describe how to use the following in an ABAP Program:
SELECT DISTINCT Statement Dynamic WHERE Clause Concatenate Statement Join (Inner vs. Left Outer)
Aliases
Dec-2008
REPORT YAP00007. DATA: CNTRY TYPE YTABNA-COUNTRY. SELECT DISTINCT COUNTRY FROM Ytabna INTO (CNTRY).
WRITE: / CNTRY.
ENDSELECT. Using SELECT DISTINCT, it is possible to eliminate duplicate rows from the result set.
Dec-2008
WRITE: / Table ytabna Statistics, / TOTAL, Different Countries, /8 MIN_SALES, Lowest Sales, /8 AVG_SALES, Average Sales, /8 MAX_SALES, Highest Sales.
Dec-2008
The parameters entered make up the contents of the WHERE clause. The user has the option of changing the conditions and dynamically effecting which way the program will execute.
An internal table is created which holds the WHERE clause until it is used in the SELECT statement.
Dec-2008
CONCATENATE Statement
REPORT YAP00010. PARAMETERS: WHERECL1(72) DEFAULT COUNTRY = USA , WHERECL2(3) DEFAULT OR, WHERECL3(72) DEFAULT COUNTRY = GB .
DATA: WHERE_ITAB TYPE STANDARD TABLE OF ITAB_RECORD INITIAL SIZE 3, WA_WHERE_ITAB TYPE ITAB_RECORD, WA_YTABNA TYPE YTABNA, IT_YTABNA TYPE STANDARD TABLE OF YTABNA. CONCATENATE WHERECL1 WHERECL2 WHERECL3 INTO WA_WHERE_ITAB-TEXT SEPARATED BY SPACE. APPEND WA_WHERE_ITAB TO WHERE_ITAB. SELECT * FROM ytabna INTO TABLE IT_YTABNA WHERE (WHERE_ITAB). LOOP AT IT_YTABNA INTO WA_YTABNA. WRITE: / WA_YTABNA-COUNTRY, WA_YTABNA-ID ENDLOOP.
CONCATENATE <source field 1> <source field 2> <source field 3> : : <source field n> INTO <target field> SEPARATED BY <constant>.
Dec-2008
Dec-2008
DATA: TABLE_LINE(240).
SELECT * FROM (TAB_NAME) INTO TABLE_LINE. WRITE: / TABLE_LINE.
ENDSELECT.
Dec-2008
Demonstration
Using a dynamic select statement in a custom ABAP program.
Dec-2008
Practice
Using a dynamic select statement in a custom ABAP program.
10
Dec-2008
CURSOR Processing
OPEN CURSOR [WITH HOLD] <cursor name> FOR <SELECT statement>.
11
Dec-2008
12
Dec-2008
The FETCH statement retrieves the rows from the CURSOR one by one until there are no more rows left.
DO. FETCH NEXT CURSOR TABCUR INTO YTABNA_WA. IF SY-SUBRC <> 0. CLOSE CURSOR TABCUR. After the FETCH is executed, the SY-SUBRC is EXIT. tested. Zero indicates a successful retrieval; ELSE. indicates there are no more rows. WRITE: / YTABNA_WA-COUNTRY,Four ytabna-NAME1. ENDIF. ENDDO.
13
Dec-2008
When CURSOR processing is completed, the CURSOR should be closed with the CLOSE CURSOR statement.
DO. FETCH NEXT CURSOR TABCUR INTO YTABNA_WA. IF SY-SUBRC <> 0. CLOSE CURSOR TABCUR. EXIT. ELSE. WRITE: / YTABNA_WA-COUNTRY, ytabna-NAME1. ENDIF. ENDDO.
14
Dec-2008
Demonstration
Using cursor in a program to retrieve data from a database table .
15
Dec-2008
Practice
Using cursor in a program to retrieve data from a database table .
16
Dec-2008
Joins are more efficient than logical databases and nested selects. They access multiple tables with one select statement.
17 ABAP Open SQL Extensions | Dec-2008
Inner Joins
SCARR SFLIGHT
18
Dec-2008
19
Dec-2008
20
Dec-2008
SFLIGHT
21
Dec-2008
SELECT <table1~field1 table1~field2 table2~field3. . . > INTO (<target >) FROM <table1 > LEFT OUTER JOIN <table2 > ON <table1~keyfield1 > = <table2~keyfield1 > AND <table1~keyfield2 > = <table2~keyfield2 > AND . . . WHERE . . . ENDSELECT.
22
Dec-2008
The syntax for joins have been given certain restrictions in order to ensure that they produce the same results for all SAP supported databases.
23
Dec-2008
Redundancy
LFA1
BSIK
24
Dec-2008
25
Dec-2008
Aliases
SELECT A~carrname B~carrid B~connid B~fldate INTO (carrid, connid, date, carrname) FROM scarr AS A INNER JOIN sflight AS B ON scarr~carrid = sflight~carrid. WRITE: / carrid, connid, date, carrname. ENDSELECT.
26
Dec-2008
Subquery
SCARR SFLIGHT
27
Dec-2008
Subquery Syntax/Example
SELECT * FROM scarr WHERE NOT carrid IN ( SELECT carrid FROM sflight ). WRITE:/ scarr-carrid, scarr-carrname. ENDSELECT.
28
Dec-2008
Having Clause
List all Luftansa CONNIDs where the sum of LUGGWEIGHT is < 20,000.
29
Dec-2008
SELECT carrid connid COUNT( *) SUM( luggweight ) INTO (carrid, connid, count, sum_weight) FROM sbook WHERE carrid = 'LH' GROUP BY carrid connid HAVING SUM( luggweight ) > 25. WRITE:/ carrid, connid, count, sum_weight. ENDSELECT.
30
Dec-2008
Demonstration
Using JOIN in select statement of a custom ABAP program retrieve data from two or more related database tables.
31
Dec-2008
Practice
Using JOIN in select statement of a custom ABAP program retrieve data from two or more related database tables.
32
Dec-2008
Summary
SELECT DISTINCT option is used to return only one record for each unique occurrence of data in a column based on a field name. Aggregate functions MIN, MAX, AVG, SUM, and COUNT are a valuable tool for accumulating values across the entire table or a subset of the table rows based on the conditions in the WHERE clause. WHERE clause can be created at runtime and can be created by the user using parameters. It is possible to use the ORDER BY PRIMARY KEY, but not ORDER BY any other field. CURSOR processing enables the programmer to execute SELECT statements that produce multiple row result sets then read/process each row of the result set sequentially.
33
Dec-2008
Summary (Contd.)
The CURSOR data type is then associated with a SELECT from a database table in the OPEN statement. Individual rows of the result set are processed using a FETCH statement sequentially until the end of the table is reached. CURSOR processing is terminated using the CLOSE CURSOR statement. At that point the result set is destroyed. Joins have the advantage of accessing multiple tables with one Select statement, thereby reducing the amount of server overhead. Sub queries are a more efficient method of performing complex select statements.
34
Dec-2008
Questions
What are the aggregate functions? And why these are used in the select statement? What is the purpose of using CONCATENATE statement? What are the different steps of cursor processing? How data can be retrieved from two or multiple related database table in single select?
35
Dec-2008