11
 August-2003 SELECT Statement | Document 2.09 1 REPORT: ZRACER01. T ABLES: LFA1, LFB1, LFC3. GET LFA1. GET LFB1. G ET LFB3. Database Access I Authorisation Checking Logical Database ABAP Native SQL ABAP Open SQL No Authorisation Checking Physical Database

Intro to ABAP - Chapter 09_v1

Embed Size (px)

Citation preview

Page 1: Intro to ABAP - Chapter 09_v1

7/30/2019 Intro to ABAP - Chapter 09_v1

http://slidepdf.com/reader/full/intro-to-abap-chapter-09v1 1/11

 August-2003 SELECT Statement | Document2.09

1

REPORT: ZRACER01.

TABLES: LFA1, LFB1,

LFC3.

GET LFA1.

GET LFB1.

G

ET LFB3.

Database Access I

Authorisation

Checking

Logical

Database

ABAP Native SQL

ABAP Open SQL

No

Authorisation

Checking

Physical

Database

Page 2: Intro to ABAP - Chapter 09_v1

7/30/2019 Intro to ABAP - Chapter 09_v1

http://slidepdf.com/reader/full/intro-to-abap-chapter-09v1 2/11

 August-2003 SELECT Statement | Document2.09

2

SQL (SELECT)

ABAP Programs

REPORT: YNEWAPPL.

TABLES...

SELECT * FROM TABNA.

ENDSELECT.

ABAP

Reports

Data

Database

TABNA

LFA1 LFC1

Page 3: Intro to ABAP - Chapter 09_v1

7/30/2019 Intro to ABAP - Chapter 09_v1

http://slidepdf.com/reader/full/intro-to-abap-chapter-09v1 3/11

 August-2003 SELECT Statement | Document2.09

3

WHERE Clause: Single Access

&

& ------------------------------------------------------------------------------------------

report y170dm64.

tables: tabna.

Syntax of a select single statement for single access. ----------

select single from tabna

where country eq ’D’ 

and

id eq ’00000002’. 

if sy-subrc = 0.

write: tabna-country, tabna-id,

tab

na-name1.

endif.

In the return-code field

(SY-SUBRC),

0 = successful and

4 = entry does not exist.

The SELECT SINGLE* statement

requires the full primary key of the table in the WHERE clause.

*

* *

*

* *

*

Page 4: Intro to ABAP - Chapter 09_v1

7/30/2019 Intro to ABAP - Chapter 09_v1

http://slidepdf.com/reader/full/intro-to-abap-chapter-09v1 4/11

 August-2003 SELECT Statement | Document2.09

4

Syntax: Restricted Loop

ProcessingSELECT * FROM <table> 

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

AND <table field 2> <relational operator> <field 2>

OR <table field 3> <relational operator> <field 3>

AND <table field 4> <relational operator> <field 4>::

OR<table field n> <relational operator> <field n>.

ENDSELECT.

EQ =

GE >= =>

LE <= =<

NE <> ><

GT >

LT <

Relational

Operators

Page 5: Intro to ABAP - Chapter 09_v1

7/30/2019 Intro to ABAP - Chapter 09_v1

http://slidepdf.com/reader/full/intro-to-abap-chapter-09v1 5/11

 August-2003 SELECT Statement | Document2.09

5

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

BETWEEN <field 1> AND <field 2>

LIKE <with ‘%’ and ‘_’ masked literal> 

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

Syntax: Between Values,

Templates and Lists

Page 6: Intro to ABAP - Chapter 09_v1

7/30/2019 Intro to ABAP - Chapter 09_v1

http://slidepdf.com/reader/full/intro-to-abap-chapter-09v1 6/11

 August-2003 SELECT Statement | Document2.09

6

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.

ause: oopProcessing with Restricted

Selection

The result set is limited by

the conditions in the

WHERE clause.

Page 7: Intro to ABAP - Chapter 09_v1

7/30/2019 Intro to ABAP - Chapter 09_v1

http://slidepdf.com/reader/full/intro-to-abap-chapter-09v1 7/11

 August-2003 SELECT Statement | Document2.09

7

LIKE: Example of a Template

(%_)& *

& *

&-----------------------------------------------------------------------------------------------*

report y170dm66.

tables: t001.

Using a template during selection.-----------------------------------------------

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.

*

*

*

*

*

*

The ‘_’ is an any character 

positional parameter.

The ‘%’ allows for any

string of any length.

Page 8: Intro to ABAP - Chapter 09_v1

7/30/2019 Intro to ABAP - Chapter 09_v1

http://slidepdf.com/reader/full/intro-to-abap-chapter-09v1 8/11

 August-2003 SELECT Statement | Document2.09

8

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.

*

*

IN: Example of a Comparison

Value List

Page 9: Intro to ABAP - Chapter 09_v1

7/30/2019 Intro to ABAP - Chapter 09_v1

http://slidepdf.com/reader/full/intro-to-abap-chapter-09v1 9/11

 August-2003 SELECT Statement | Document2.09

9

IN: Example of a Range

tables: lfa1.

data: begin of tab occurs 10.sign (1),

option (2),

low like lfa1-lifnr,

high like lfa1-lifnr,end of tab.

move: ‘I’ to tab-sign,‘BT’ to tab-option,‘0000000001’ to tab-low,

‘9999999999’ to tab-high.

append tab.

select from lfa1

where lifnr in tab.

write: / lfa1-lifnr, lfa1-name1.

endselect.

This internal table is the same one

that is created automatically when

a selection screen is processed.

The RANGES statement builds the

structure of the table automatically.

Ranges: tab for lfa1-lifnr.

move: ‘I’ to tab-sign,

‘BT’ to tab-option,

‘0000000001’ to tab-low,

‘9999999999’ to tab-high.

append tab.

select from lfa1

where lifnr in tab.

write: / lfa1-lifnr, lfa1-name1.

endselect.

*

tables: lfa1.

DATA: BEGIN OF <name>

OCCURS <n>,

SIGN(1),

OPTION(2),

LOW LIKE <field>,HIGH LIKE <field>,

END OF <name>.

RANGES <name> FOR <field>.

Page 10: Intro to ABAP - Chapter 09_v1

7/30/2019 Intro to ABAP - Chapter 09_v1

http://slidepdf.com/reader/full/intro-to-abap-chapter-09v1 10/11

 August-2003 SELECT Statement | Document2.09

10

&-----------------------------------------------------------------------------------------------

& Report Y170DM71

&

&-----------------------------------------------------------------------------------------------

&

&

&-----------------------------------------------------------------------------------------------

report y170dm71.

tables: lfa1.

Notice the order by clause below.----------------------------------------------

select from lfa1

order by name1.

write: / lfa1-lifnr, lfa1-name1.

endselect.

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

**

The ORDER BY Clause

SELECT * FROM <table>

WHERE <condition>

ORDER BY <table field 1>

<table field 2>

<table field 3>:

:

<table field n>.

PRIMARY KEY.

ENDSELECT.

Page 11: Intro to ABAP - Chapter 09_v1

7/30/2019 Intro to ABAP - Chapter 09_v1

http://slidepdf.com/reader/full/intro-to-abap-chapter-09v1 11/11

 August-2003 SELECT Statement | Document2.09

11

BYPASSING BUFFER

SELECT * FROM <table>

BYPASSING BUFFER

WHERE <condition>.

:

:

:

ENDSELECT.