Вы находитесь на странице: 1из 11

Database Access I

Physical Database

Logical Database
REPORT: ZRACER01. TABLES: LFA1, LFB1, LFC3. GET LFA1. GET LFB1.

Authorisation Checking

No Authorisation Checking

GET LFB3.

ABAP Open SQL


August-2003 SELECT Statement | Document 2.09

ABAP Native SQL

SQL (SELECT)
ABAP Programs
REPORT: YNEWAPPL. TABLES...

Database TABNA

SELECT * FROM TABNA.

LFA1
ENDSELECT.

LFC1

Data

ABAP Reports
August-2003 SELECT Statement | Document 2.09 2

WHERE Clause: Single Access


*& * * & -----------------------------------------------------------------------------------------* In the return-code field (SY-SUBRC), report y170dm64. 0 = successful and 4 = entry does not exist.
tables: tabna. statement for single access. ----------

* Syntax of a select single * select single* from tabna

The SELECT SINGLE* statement where country eq D requires the full primary key of and id eq 00000002. the table in the WHERE clause.

if sy-subrc = 0. write: tabna-country, tabna-id, tabna-name1. endif. August-2003 SELECT Statement | Document
2.09

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.

August-2003

SELECT Statement | Document 2.09

EQ GE LE NE GT LT

= >= <= <> > <

=> =< ><

Relational Operators
4

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>)


August-2003 SELECT Statement | Document 2.09 5

WHERE Clause: Loop Processing with Restricted Selection


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

The result set is limited by the conditions in the WHERE clause.

August-2003

SELECT Statement | Document 2.09

LIKE: Example of a Template (%_)


* *& * *& * &-----------------------------------------------------------------------------------------------* report y170dm66.

tables: t001. * Using a template during selection.----------------------------------------------The % allows


select * from t001 where bukrs like __01 and butxt like SAP%. write: / t001-bukrs, t001-butxt. endselect. if sy-subrc ne 0. write: / The conditions cannot be satisfied. endif. * August-2003 SELECT Statement | Document 2.09

The _ is an any character positional parameter. for any string of any length.

IN: Example of a Comparison Value List


tables: tabna. parameters: country1 like tabna-country. country2 like tabna-country. select * from tabna where country in (country1, country2). * where country = country1 or country = country2.

write: / tabna-country, tabna-id, tabna-name1. endselect. if sy-subrc ne 0. write: / The conditions cannot be satisfied. endif. August-2003 SELECT Statement | Document 2.09 8

IN: Example of a Range


tables: lfa1. This internal table is the same one data: begin of tab occurs 10. that is created automatically when sign (1), option (2), a selection screen is processed. low like lfa1-lifnr, The RANGES statement builds the high like lfa1-lifnr, structure of the table automatically. end of tab. move: I to tab-sign, BT to tab-option, RANGES <name> FOR <field>. 0000000001 to tab-low, 9999999999 to tab-high. DATA: BEGIN OF <name> append tab. OCCURS <n>, select from lfa1 tables: lfa1. SIGN(1), where lifnr in tab. Ranges: tab for lfa1-lifnr. OPTION(2), write: / lfa1-lifnr, lfa1-name1. move: I to tab-sign, LOW LIKE <field>, endselect. BT to tab-option, HIGH LIKE <field>, 0000000001 to tab-low, END OF <name>. 9999999999 to tab-high. append tab. select* from lfa1 where lifnr in tab. write: / lfa1-lifnr, lfa1-name1. August-2003 SELECT Statement | Document 9 endselect. 2.09

The ORDER BY Clause


SELECT * FROM <table> WHERE <condition>
*&-----------------------------------------------------------------------------------------------

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

PRIMARY KEY.
tables: lfa1.

ENDSELECT.
* Notice the order by clause below.---------------------------------------------- * select * from lfa1 order by name1. write: / lfa1-lifnr, lfa1-name1. endselect. August-2003 SELECT Statement | Document 2.09 10

BYPASSING BUFFER
SELECT * FROM <table>
BYPASSING BUFFER WHERE <condition>. : : : ENDSELECT.
August-2003 SELECT Statement | Document 2.09 11

Вам также может понравиться