0% found this document useful (0 votes)
21 views

ABAP T04-001 OpenSQL

hi

Uploaded by

nhinhlse173042
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

ABAP T04-001 OpenSQL

hi

Uploaded by

nhinhlse173042
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

ABAP OpenSQL

Agenda
1. Open SQL Part 1 – SELECT using obsolete TABLES
2. Open SQL Part 2 – SELECT using standard Internal
Table
3. OpenSQL to modify data (INSERT / UPDATE / MODIFY /
DELETE)
4. New OpenSQL syntaxes
Open SQL Part 1 –
SELECT using obsolete TABLES

Section 1
Topic Objectives
» What is Open SQL?
» Use the SELECT statement and the WHERE clause
» Describe the SELECT SINGLE statement
» Explain the ORDER BY addition
» Describe retrieving individual columns
» Exclude duplicate records during selection
» Describe the SELECT statement syntax
» Define Aggregate expressions
Introducing to Open SQL
»Open SQL statements are a subset of Standard SQL that is fully
integrated in the ABAP language.
»Open SQL statements allow uniform data access to the ABAP
programmer regardless of the database system installed.
»The database interface dynamically converts Open SQL
statements to database-specific SQL statements
Open SQL Architecture
SELECT Statement
• SELECT clause
• FROM clause
• INTO clause *** If you use obsolete TABLES keyword, there will be no INTO
• WHERE clause
SELECT using obsolete TABLES

Table
SPFLI

Table work area SPFLI

CARRID CONNID COUNTRYFR


TABLES: SPFLI.
SELECT * FROM SPFLI.
WRITE: / SPFLI-CARRID, SPFLI-CONNID,
SPFLI-CITYFROM, SPFLI-CITYTO.
ENDSELECT.
Restricting Data Selection -WHERE clause

TABLES: SPFLI.
SELECT * FROM SPFLI
WHERE COUNTRYFR = 'US’.
statements
ENDSELECT.
IF SY-SUBRC <> 0.
statements
ENDIF.

Allowable comparison operators for WHERE conditions :


Operator Meaning and Use
Comparison with a single value
=, <, >, <=, >=, <> In character-type fields, the result of size
EQ, LT, GT, LE, GE, NE comparisons may depend on the database
code page.
Restricting Data Selection -WHERE clause
Allowable comparison operators for WHERE conditions :

Operator Meaning and Use

IN (dobj1, dobj2, ...) Comparison with a list of single values

Comparison with an interval


BETWEEN dobj1 AND dobj2 In character-type fields, the result may de
pend on the database code page.

Comparison with character strings


LIKE dobj The _ and % placeholders let you define a
comparison pattern in DOBJ.

Evaluation of a selection table (select-op


IN seltab (Ex: Select-options)
tions)

Checks whether the database field has a initial


IS [NOT] INITIAL
value

AND , OR Link of logical expressions

NOT Negation of a logical expression


Single Record Access (SELECT SINGLE)

TABLES: SPFLI.
SELECT SINGLE * FROM SPFLI
WHERE CARRID = 'LH'
AND CONNID = '2407'.

ENDSELECT. No ENDSELECT
IF SY-SUBRC <> 0. required

statements
ENDIF.
Example: Single Access…UP TO <n> ROWS
TABLES: SPFLI.

SELECT * FROM SPFLI UP TO 1 ROWS


WHERE CITYFROM = 'NEW YORK'
AND CITYTO = 'SAN FRANCISCO'.
ENDSELECT.

IF SY-SUBRC = 0.
WRITE: / SPFLI-CARRID, SPFLI-CONNID,
SPFLI-CITYFROM, SPFLI-CITYTO.
ENDIF.
Retrieving Records in Sequence - ORDER BY
TABLES: SPFLI.
SELECT * FROM SPFLI
ORDER BY PRIMARY KEY.
statements

ENDSELECT.
TABLES: SPFLI.
SELECT * FROM SPFLI
WHERE COUNTRYFR = 'US'
ORDER BY ID CITYFROM CITYTO.
statements

ENDSELECT.
Retrieving Individual Columns
TABLES: SPFLI.
DATA: GD_COUNTRY LIKE SPFLI-COUNTRYFR.
SELECT COUNTRYFR INTO GD_COUNTRY
FROM SPFLI.
statements

ENDSELECT.
TABLES: SPFLI.
DATA: GD_COUNTRY LIKE SPFLI-COUNTRYFR,
GD_CITY LIKE SPFLI-CITYFROM.
SELECT COUNTRYFR CITYFROM INTO (GD_COUNTRY, GD_CITY)
FROM SPFLI.
statements
ENDSELECT.
Excluding Duplicates - SELECT DISTINCT
TABLES: SPFLI.
DATA: W_COUNTRY TYPE SPFLI-COUNTRYFR.
SELECT DISTINCT COUNTRYFR INTO W_COUNTRY TABNA
FROM SPFLI. CountryFr ConnID
statements 1st pass US 0017
ENDSELECT. US 2407
2nd pass JP 1689
JP 0254
TABLES: SPFLI. IT 0008
DATA: W_CONNID TYPE SPFLI-CONNID,
W_COUNTRY TYPE SPFLI-COUNTRYFR. TABNA
SELECT DISTINCT CONNID COUNTRYFR CountryFr ConnID
INTO (W_CONNID, W_COUNTRY)
1st pass US 0017
FROM SPFLI. US 2407
statements 2nd pass
JP 1689
ENDSELECT.
JP 0254
IT 0008
Aggregate Expressions

TABLES: SPFLI. SPFLI


DATA: GD_TOTAL TYPE I, AirPort Flight Time
GD_HIGH_TIME TYPE SPFLI-FLTIME, JFK 7:20
GD_LOW_TIME TYPE SPFLI-FLTIME. JFK 5:21
SELECT COUNT( DISTINCT CONNID ) FRA 12:55
MAX( FLTIME ) FRA 16:15
MIN( FLTIME ) FRA 10:30
FROM SPFLI INTO (GD_TOTAL, GD_HIGH_TIME, After SELECT :
GD_LOW_TIME).
GD_TOTAL
statements 5
ENDSELECT. GD_HIGH_TIME
16:15
GD_LOW_TIME
5:21
SELECT Part 2 –
using standard Internal Table
Section 2
Topic Objectives
» How to define simple internal table / work area?
» Differences when SELECT … INTO TABLE …
» Automatically mapping with INTO CORESPONDING FIELD OF
TABLE
» SELECT … JOIN …
» Use Internal table as filter conditions: SELECT … FOR ALL
ENTRIES …
Declaring Work Area (Structure)
TYPE: BEGIN OF <name>,
<field1>, ..., <fieldn>,
END OF <name>.

PROGRAM <name>.
TYPE: BEGIN OF TY_ADDRESS,
FLAG TYPE C,
NAME TYPE C LEGNTH 25,
Declaration of a
CITY TYPE C LEGNTH 20,
SALES TYPE EKPO-NETPR, structure.
END OF TY_ADDRESS.
DATA: WA_ADDRESS TYPE TY_ADDRESS.

MOVE ‘X’ TO WA_ADDRESS-FLAG.


ADD AMOUNT TO WA_ADDRESS-SALES.

WRITE: WA_ADDRESS-FLAG, WA_ADDRESS-NAME,


WA_ADDRESS-CITY, WA_ADDRESS-SALES.
Internal Tables
Internal tables
ADDRESS_LIST

Name First Name City Street


Name1 First name 1 City 1 Street 1
Name2 First name 2 City 2 Street 2
Name3 First name 3 City 3 Street 3
Name4 First name 4 City 4 Street 4
Name5 First name 5 City 5 Street 5
Declaring Simple Internal Table
PROGRAM ZEXAMPLE.
TYPE: BEGIN OF TY_EMPLOYEE,
PERSNBR(6) TYPE N, Line Type
LASTNAME(20) TYPE C,
END OF TY_EMPLOYEE.
Table
DATA: GT_EMPLTAB TYPE STANDARD TABLE Type
OF TY_EMPLOYEE,
GS_EMPLTAB TYPE TY_EMPLOYEE.

* All of the above syntax will be explained in detail later in this section!
Differences when SELECT … INTO TABLE …
SELECT * FROM <db_table> • No TABLES declaration
• We need to specify <internal_table> to store data that
INTO TABLE <internal_table> SELECTed from DB.
… • Usable in ABAP OOP programming
ENDSELECT.

• No TABLES declaration
SELECT SINGLE * UP TO <n> ROWS • We need to specify <work_area> to store data that
FROM <db_table> SELECTed from DB
• Usable in ABAP OOP programming
INTO <work_area>

ENDSELECT.

• No TABLES declaration
SELECT * UP TO <n> ROWS • We need to specify <internal_table> to store data that
FROM <db_table> SELECTed from DB
• Usable in ABAP OOP programming
INTO TABLE <internal_table>

ENDSELECT.
Automatically mapping with
INTO CORESPONDING FIELD OF TABLE

SELECT SINGLE * UP TO <n> ROWS


FROM <db_table>
INTO CORESPONDING FIELD OF <work_area>

ENDSELECT.

SELECT * FROM <db_table>


INTO CORESPONDING FIELD OF TABLE <internal_table>

ENDSELECT.
SELECT … INNER JOIN Statement
TYPES: BEGIN OF TY_CONN,
CONNID TYPE SPFLI-CONNID,
COUNTRYFR TYPE T005T-LANDX,
COUNTRYTO TYPE T005T-LANDX,
END OF TY_CONN.
DATA: GT_CONN TYPE STANDARD TABLE OF TY_CONN.

SELECT CONNID
CTRY1~LANDX AS COUNTRYFR "First join to get Country Name of CountryFr
CTRY2~LANDX AS COUNTRYTO "Second join to get Country Name of CountryTo
INTO TABLE GT_CONN
FROM SPFLI INNER JOIN T005T AS CTRY1 "First join
ON SPFLI~COUNTRYFR = CTRY1~LAND1
AND CTRY1~SPRAS = 'EN'
INNER JOIN T005T AS CTRY2 "Second join
ON SPFLI~COUNTRYTO = CTRY2~LAND1
AND CTRY2~SPRAS = 'EN'.
Use Internal table as filter conditions:
SELECT … FOR ALL ENTRIES
» 'FOR ALL ENTRIES' statement allows you to build an internal
table “itab” and then restrict the database select to only get
entries contained in that internal table “itab”.
DATA: IT_DOCTYPE TYPE STANDARD TABLE OF TY_DOCTYPE.

IT_DOCTYPE = VALUE #( ( BLART = 'KG' )


( BLART = 'KZ' ) ).
SELECT BELNR,
BUDAT
INTO TABLE @DATA(IT_ACCT)
FROM BKPF " Accounting Docs Header
FOR ALL ENTRIES IN @IT_DOCTYPE
WHERE BKPF~BLART = @IT_DOCTYPE-BLART.
OpenSQL to modify data
Section 3
Write Access to
the Database
• In addition to the SELECT statement,
Open SQL contains the UPDATE,
INSERT, DELETE, and MODIFY
statements.
• However, it is important to understand
the SAP transaction concept associated
with these database change accesses
to avoid causing data inconsistencies.
INSERT
*-- Method 1 - TABLES
TABLES: YTXXX_EMPLOYEE.
YTXXX_EMPLOYEE-EMPLOYEE = '2000000001'.
YTXXX_EMPLOYEE-LASTNAME = 'Jenny'.
YTXXX_EMPLOYEE-FIRSTNAME = 'Carl'.
YTXXX_EMPLOYEE-GENDER = 'F'.

INSERT YTXXX_EMPLOYEE.
IF SY-SUBRC <> 0.
MESSAGE 'Insert error!' TYPE 'E'.
ENDIF.

*-- Method 2 - Work area


DATA: GS_EMP TYPE YTXXX_EMPLOYEE.

CLEAR: GS_EMP.
GS_EMP-EMPLOYEE = '3000000004'.
GS_EMP-LASTNAME = 'Employee 1'.
GS_EMP-FIRSTNAME = 'First Name'.
GS_EMP-GENDER = 'M'.
INSERT YTXXX_EMPLOYEE FROM GS_EMP.
INSERT
*-- Method 3 - Internal Table
DATA: GS_EMP TYPE YTXXX_EMPLOYEE.
DATA: GT_EMP TYPE STANDARD TABLE OF YTXXX_EMPLOYEE.
GS_EMP-EMPLOYEE = '3000000004'.
GS_EMP-LASTNAME = 'James'.
GS_EMP-FIRSTNAME = 'Hornor'.
GS_EMP-GENDER = 'F'.
APPEND GS_EMP TO GT_EMP.

INSERT YTXXX_EMPLOYEE FROM TABLE GT_EMP


ACCEPTING DUPLICATE KEYS. " Prevent Short-Dump

IF SY-SUBRC <> 0.
MESSAGE 'Insert error!' TYPE 'E'.
ENDIF.
UPDATE
*-- Method 1 - Update by Work area
DATA: GS_EMP TYPE YTXXX_EMPLOYEE.

CLEAR: GS_EMP.
GS_EMP-EMPLOYEE = '3000000004'.
GS_EMP-LASTNAME = 'Updated Employee 1'.
GS_EMP-FIRSTNAME = 'Updated First Name'.
GS_EMP-GENDER = 'M’.
UPDATE YTXXX_EMPLOYEE FROM GS_EMP.

*-- Method 2 - Update by SET (update all data by conditions)


UPDATE YTXXX_EMPLOYEE
SET DEPARTMENT = 'FA2101'
WHERE EMPLOYEE LIKE '300%'.
IF SY-SUBRC = 0.
MESSAGE 'Update successfuly!' TYPE 'S'.
ELSE.
MESSAGE 'Update error!' TYPE 'E'.
ENDIF.
DELETE
*-- Method 1 - Logical DELETE (Update Delete Flag = ‘X’)
DATA: GS_EMP TYPE YT21024_EMPLOYEE.
GS_EMP-EMPLOYEE = '3000000004'. " Key required
GS_EMP-DEL_FLAG = 'X'.
UPDATE YT21024_EMPLOYEE FROM GS_EMP.
IF SY-SUBRC = 0.
MESSAGE 'Delete successfuly!' TYPE 'S'.
ELSE.
MESSAGE 'Delete error!' TYPE 'E'.
ENDIF.

*-- Method 2 - Physically delete data

GS_EMP-EMPLOYEE = '3000000004'. " Key required

DELETE YT21024_EMPLOYEE FROM GS_EMP.


New OpenSQL (>7.4)
Section 4
New OpenSQL (>7.4)
LIVE DEMO / VIDEO DEMO
Agenda
1. Open SQL Part 1 – SELECT using obsolete TABLES
2. Open SQL Part 2 – SELECT using standard Internal
Table
3. OpenSQL to modify data (INSERT / UPDATE / MODIFY /
DELETE)
4. New OpenSQL syntaxes
Thank you
Q&A

You might also like