Sei sulla pagina 1di 14

IBM Global Services

Retrieving Data with SELECT Statements

Retrieving Data with the SELECT Sta


tement |

Dec-2008

2005 IBM Corporation

IBM Global Services

Objectives
The participants will be able to:
Retrieve information from the database using Open SQL.
Create basic Select statements for use in ABAP Code.
Describe the system field SY-SUBRC and properly use it in an ABAP Program.

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

Retrieving Information From the Database


Structured Query Language (SQL)
---------------------------------------1. Data Manipulation Language (DML)
2. Data Definition Language (DDL)
3. Data Control Language (DCL)

Jo
h
Sm n
ith

ry
Ma es
l
Sti

Data

DB

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

Open SQL

Open SQL - a subset of standard SQL

Portable across various databases

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

The Basic SELECT Statement


SELECT *FROM
<table name>
[..]
ENDSELECT.
Mary
Stiles

John Smith
Work Area

DB

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

Example Using the SELECT Statement

DATA: WA_KNA1 TYPE KNA1.


SELECT *
FROM

KNA1

INTO WA_KNA1.
WRITE: / WA_KNA1-KUNNR, WA_KNA1NAME1.
ENDSELECT.

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

The SELECT Statement with a WHERE Clause

DATA: WA_KNA1 TYPE KNA1.


SELECT *
FROM

KNA1

INTO WA_KNA1
WHERE KTOKD = 0001.
WRITE: / WA_KNA1-KUNNR, WA_KNA1-NAME1.
ENDSELECT.

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

SY-SUBRC

DATA: WA_KNA1 TYPE KNA1.


SELECT *
FROM KNA1
INTO WA_KNA1
WHERE KTOKD = 0001.
WRITE: / WA_KNA1-KUNNR, WA_KNA1-NAME1.
ENDSELECT.
IF SY-SUBRC <> 0.
WRITE: / No records found..
ENDIF.

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

The INTO Clause

DATA: WA_KNA1 TYPE KNA1.


SELECT KUNNR
NAME1
FROM KNA1
INTO
(WA_KNA1-KUNNR, WA_KNA1-NAME1).
WRITE : / WA_KNA1-KUNNR, WA_KNA1-NAME1.
ENDSELECT.
IF SY-SUBRC <> 0.
WRITE: / No records found..
ENDIF.

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

INTO CORRESPONDING-FIELDS

DATA: WA_KNA1.
SELECT KUNNR
NAME1
FROM KNA1
INTO CORRESPONDING-FIELDS
OF WA_KNA1.
WRITE : / WA_KNA1-KUNNR, WA_KNA1-NAME1.
ENDSELECT.
IF SY-SUBRC <> 0.
WRITE: / No records found..
ENDIF.

10

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

Demonstration
Selection of data from a database table, checking sy-subrc and displaying the
values in a report.

11

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

Practice
Selection of data from a database table, checking sy-subrc and displaying the
values in a report.

12

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

Summary
SQL has three main components:
Data Manipulation Language (DML):
Data Definition Language (DDL):
Data Control Language (DCL):

The DML provides the four basic data operations:


Select (retrieve)
Insert
Update
Delete

Open SQL is a subset of standard SQL with a specific syntax recognised by


ABAP.

13

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

IBM Global Services

Questions
What is SQL ? And What are different types of SQL ?
How values can be retrieved from a database table ?
Why SY-SUBRC check is required in the program ?

14

Retrieving Data with the SEL

Dec-2008

2005 IBM Corporation

Potrebbero piacerti anche