Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 19

IBM Global Services

SELECT Statements

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

Objectives
The participants will be able to:
Select data from a database using a restrictive WHERE clause
Describe how the following ABAP commands effect a Select statement:
SELECT SINGLE, LIKE, IN, ORDER BY, BYPASSING BUFFER
Describe when to use a WHERE clause vs. a CHECK statement in an ABAP Program
Describe when to use SELECT <field list> vs. SELECT * in an ABAP Program
Use Aggregate Functions in an ABAP Program

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

Database Access I
Physical
Database

Logical
Database
REPORT: ZRACER01.
NODES: LFA1, LFB1,
LFC3.

Authorisation
Checking

GET LFA1.

No
Authorisation
Checking

GET LFB1.
GET LFB3.
ABAP Open SQL
ABAP Native SQL
3

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

SQL (SELECT)
ABAP Programs

Database

REPORT: YNEWAPPL.
Data:wa_ytabna type ytabna
SELECT * FROM ytabna into
wa_ytabna

ytabna
LFA1

ENDSELECT.

LFC1

Data

ABAP
Reports

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

WHERE Clause: Single Access


In the return-code field
*&
*
(SY-SUBRC),
* & ------------------------------------------------------------------------------------ *
0 = successful and
report
YAP00001. 4 = entry does not exist.
The SELECT SINGLE* statement
Data:wa_tabna type requires the full primary key of the table
ytabna.
in the WHERE clause.
* Syntax of a select single * statement for single access.
select single * from ytabna into wa_tabna
where country eq D
and id
eq 00000002.
if sy-subrc = 0.
write: wa_ytabna-country, wa_ytabna-id,
wa_ytabna-name1.
endif.
5

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

Syntax: Restricted Loop Processing


SELECT * FROM <table>
WHERE
AND
OR
AND

<table field 1> <relational operator> <field 1>


<table field 2> <relational operator> <field 2>
<table field 3> <relational operator> <field 3>
<table field 4> <relational operator> <field 4>
:
:
OR<table field n> <relational operator> <field n>.

ENDSELECT.

EQ
GE
LE
NE
GT
LT
6

SELECT Statement |

=
>=
<=
<>
>
<

=>
=<
><

Dec-2008

Relational
Operators

2005 IBM Corporation

IBM Global Services

Syntax: Restricted Loop Processing


Types:Begin of ty_tabna.
Include structure ytabna.
Types:end of ty_tabna.
Data: it_tabna type standard table of ty_tabna,
Wa_tabna type ty_tabna.
SELECT * FROM <table> into table it_tabna
WHERE
AND
OR
AND

<table field 1> <relational operator> <field 1>


<table field 2> <relational operator> <field 2>
<table field 3> <relational operator> <field 3>
<table field 4> <relational operator> <field 4>
:
:
OR<table field n> <relational operator> <field n>.
Loop at it_tabna into wa_tabna.
Write:/ wa_tabna-field1 , wa_tabna-field1,
Endloop.

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

Syntax: Between Values, Templates and Lists

SELECT * FROM <table> WHERE <table field>...

BETWEEN <field 1> AND <field 2>

LIKE <with % and _ masked literal>

IN (<field 1, field 2,......field n>)

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

WHERE Clause: Loop Processing with Restricted Selection

report

YAP00002.

Data: wa_ytabna type ytabna.


* The result of the select statement is limited by the conditions of
* the where clause.
select * from ytabna into wa_ytabna
where country eq USA
and id between 0000001
and 0000002 .
write: / wa_ytabna-country, wa_ytabna-id.
wa_ytabna-name1.
endselect.
if sy-subrc ne 0.
write: / The conditions cannot be satisfied.
endif.

SELECT Statement |

The result set is limited by the


conditions in the
WHERE clause.

Dec-2008

2005 IBM Corporation

IBM Global Services

LIKE: Example of a Template (%_)


*&
*
*&
*
The _ is a single character
* &-----------------------------------------------------------------------------------------------*

positional parameter.

report YAP00003.

The % allows for any string


of any length.

Data: wa_t001 type t001.


* Using a template during selection.----------------------------------------------select * from t001 into wa_t001
where bukrs like __01
and butxt like SAP%.
write: / wa_t001-bukrs, wa_t001-butxt.
endselect.
if sy-subrc ne 0.
write: / The conditions cannot be satisfied.
endif.

10

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

IN: Example of a Comparison Value List


Data: wa_ytabna type ytabna.
parameters: country1 type wa_ytabna-country.
country2 type wa_ytabna-country.
select * from ytabna into wa_ytabna
where country in (country1, country2).
* where country = country1 or country = country2.
write: / wa_ytabna-country, wa_ytabna-id,
wa_ytabna-name1.
endselect.
if sy-subrc ne 0.
write: / The conditions cannot be satisfied.
endif.

11

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

IN: Example of a Range


Data:wa_lfa1 type lfa1.
This internal table is the same one
Types: begin of ty_tab ,
that is created automatically when
sign (1),
a selection screen is processed.
option (2),
The RANGES statement builds the
low type lifnr,
structure of the table automatically.
high type lifnr,
end of ty_tab.
Data: tab type standard table of ty_tab,
Wa_tab type tab.
TYPES <range variable> TYPE
move: I
to wa_tab-sign,
RANGE OF <data element name>.
BT
to wa_tab-option,
Data:wa_lfa1 type lfa1.
0000000001 to wa_tab-low,
Data: tab type range of lifnr,
9999999999 to wa_tab-high.
Wa_tab like line of tab.
append wa_tab to tab.
select
from lfa1 into wa_lfa1 move: I
to wa_tab-sign, DATA: BEGIN OF
where lifnr in tab.
<name>
BT
to wa_tab-option,
write: / wa_lfa1-lifnr, wa_lfa1-name1. 0000000001 to wa_tab-low, OCCURS <n>,
endselect.
9999999999 to wa_tab-high. SIGN(1),
Append wa_tab to tab.
OPTION(2),
select* from lfa1 into wa_lfa1
LOW LIKE <field>,
where lifnr in tab.
HIGH LIKE <field>,
write: / wa_lfa1-lifnr, wa_lfa1-name1.
END OF <name>.
endselect.
12

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

The ORDER BY Clause

SELECT * FROM <table>


WHERE <condition>

*&----------------------------------------------------------------------------------------------- *
*& Report YAP00006
ORDER
* BY <table field 1>
&
*
* <table field 2>
*&----------------------------------------------------------------------------------------------- * <table field 3>
*&
*
:
*&
*
:
*&----------------------------------------------------------------------------------------------- *
<table field n>.
report YAP00006.

PRIMARY KEY.

Data:wa_lfa1 type lfa1.


ENDSELECT.
Data: wa_lfa1 type
Notice the order by clause below.---------------------------------------------*
*
select * ffrom lfa1 into wa_lfa1
order by name1.
write: / wa_lfa1-lifnr, wa_lfa1-name1.
endselect.

13

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

BYPASSING BUFFER
SELECT * FROM <table>
BYPASSING BUFFER
WHERE <condition>.
:
:
:
ENDSELECT.

14

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

Demonstration
Creation a program to retrieve specific records from a SAP standard database
table and displaying those in a report.

15

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

Practice
Creation a program to retrieve specific records from a SAP standard database
table and displaying those in a report.

16

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

Summary
Accessing the database can be accomplished with two methods GET Event,
SELECT Statement.
The SELECT statement can access data by three methods: Single access with
fully qualified key, Loop processing with restriction, Loop processing without
restriction.
SELECT SINGLE statement allows accessing a single table entry.
The return code (SY-SUBRC) will be set to either of the following values after the
SELECT statement finishes: 0 = Read was successful,4 = Record does not exist.
WHERE clause is to specify selection condition.
Several type of comparisons (BETWEEN, LIKE, IN) can be made in the WHERE
clause to specify ranges, masked literals/templates and field lists.
A masked literal contains the underscore _ for a single character and the
percentage character % for a character string.

17

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

Summary (Contd.)
IN operator of the WHERE clause allows to compare a table field with a list of
single values.
RANGES statement creates the identical internal table that is created in the DATA
statement with the columns SIGN, OPTION, LOW, and HIGH.
Additional specification ORDER BY PRIMARY KEY to sort the table entries by the
primary key field(s).

18

SELECT Statement |

Dec-2008

2005 IBM Corporation

IBM Global Services

Questions
What are the different types of methods for accessing database records?
How you will understand that a select is successful or not?
What is the significance of SELECT SINGLE statement?
How you can restrict your selection while retrieving records from a database
table?
What is a range table and what are the fields in the range table?
What is the significance of using a range table?.

19

SELECT Statement |

Dec-2008

2005 IBM Corporation

You might also like