background image

BC170_2.11.1

ABAP Open SQL Extensions

background image

BC170_2.11.2

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

background image

BC170_2.11.3

The SELECT DISTINCT Statement

Using SELECT 

DISTINCT, it 

is possible 

to eliminate  

duplicate rows 

from the 

result set.

Expected 
Results

background image

BC170_2.11.4

SELECT Using Aggregate Functions

Expected 
Results

background image

BC170_2.11.5

The Dynamic WHERE Clause

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.

background image

BC170_2.11.6

CONCATENATE Statement

CONCATENATE <source field 1>

<source field 2>
<source field 3>

 

:

 

:
<source field n>
INTO

<target field>

SEPARATED BY <constant>.

background image

BC170_2.11.7

Variations on the INTO Clause

SELECT <FIELD> FROM  <dbtable>
                  INTO TABLE <internal table>
                  PACKAGE SIZE <n>.
ENDSELECT. 

background image

BC170_2.11.8

Specifying the Table Name at 

Runtime

background image

BC170_2.11.9

Joins: Why We Should Use Them

Joins are more efficient than logical databases and nested selects.

They access multiple tables with one select statement.

background image

BC170_2.11.10

Inner Joins

SFLIGHT

SCARR

background image

BC170_2.11.11

Inner Joins Syntax 

SELECT 

<table1~field1 table1~field2 table2~field3. 

. . >

 

    INTO 

(<target >)

       

    FROM 

<table1 >

 INNER JOIN 

<table2 >

               

    ON 

<table1~keyfield1 >

 = 

<table2~keyfield1 >

        AND 

<table1~keyfield2 >

 = 

<table2~keyfield2 

>

        AND . . .
    WHERE . . .  
ENDSELECT.

background image

BC170_2.11.12

The Driving Table

SELECT 

scarr~carrname sflight~carrid 

sflight~connid sflight~fldate

    INTO (

carrname, carrid, connid, date)

       

    FROM 

scarr

 INNER JOIN 

sflight

               

    ON 

scarr~carrid

 = 

sflight~carrid

.           

          WRITE: / 

carrid, connid, date, 

carrname

.  

ENDSELECT.

background image

BC170_2.11.13

Left Outer Joins

SFLIGHT

SCARR

background image

BC170_2.11.14

Left Outer Join Syntax

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.

background image

BC170_2.11.15

Open SQL Syntax Restrictions

The syntax for joins have been given certain restrictions in 

order to insure that they produce the same results for all 

SAP supported databases. 

background image

BC170_2.11.16

Redundancy

LFA1

BSIK

background image

BC170_2.11.17

Joins Accessing More than 

Two Tables

SELECT 

<table1~field1 table1~field2 

table2~field3. . . >

 

    INTO 

(<target >)

       

    FROM (

<table1 >

 INNER JOIN 

<table2 >

               

    ON 

<table1~keyfield1 >

 = 

<table2~keyfield1 >

        AND 

<table1~keyfield2 >

 = 

<table2~keyfield2 >

        AND . . .) 
       INNER JOIN 

<table3 >

    

   ON

 <table1~keyfield > 

=

 <table3~keyfield >

   AND . . .

    WHERE . . .  
ENDSELECT.

background image

BC170_2.11.18

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.

background image

BC170_2.11.19

Subquery

SFLIGHT

SCARR

Which airlines are not found in the sflight 
table?

background image

BC170_2.11.20

Subquery Syntax/Example

SELECT 

carrid carrname

 FROM 

scarr

 

INTO (

scarr-carrid, scarr-carrname

)                            

   WHERE NOT 

carrid

 IN                          

        (  SELECT 

carrid

 FROM 

sflight 

  ).    

   WRITE:/ 

scarr-carrid, scarr-carrname

.        

ENDSELECT.                                      

background image

BC170_2.11.21

Having Clause

List all Luftansa CONNIDs where the sum of 

LUGGWEIGHT is > 20,000.

background image

BC170_2.11.22

Having Clause Syntax/Example

SELECT 

carrid connid

 COUNT( 

*

) SUM(  

luggweight

  

)          
  INTO (

carrid, connid, count, sum_weight

) FROM 

sbook

    

  WHERE

 carrid

 = 

'LH'    

                                

  GROUP BY 

carrid connid

                                 

  HAVING SUM( 

luggweight

 ) > 

20000

.                      

   
  WRITE:/  

carrid, connid, count, sum_weight.

           

 
ENDSELECT.                                               

background image

BC170_2.11.23

Summary

• The participants should 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

background image

BC170_2.11.24


Document Outline