Vous êtes sur la page 1sur 5

SELECT Statements in SAP ABAP

There are different types of Select Statements in ABAP. Below are some
select statements for frequently used cases.
<FLD_LST! "! List of Fields from Data Base ta#le for selection
<DB_TABL$! "! Source Data Base Ta#le
<%A! "! Tar&et %or' Area(Structure
<TAB! "! Tar&et nternal Ta#le
<)*+DT*+! "! %here condition in the select statement #ased on DB
Ta#le Fields.
1.
S$L$)T ,-<FLD_LST! .%e can select all fields or
the fields in &i/en order of the internal ta#le0
F1*2 <DB_TABL$!
+T* <%A!
%3$1$ <)*+DT*+!.
$+DS$L$)T.
Ex: S$L$)T , F1*2 4+A5
+T* %_4+A5
%3$1$ 46++1 7T 899995999998. .%here %_4+A5 is a ST16)T61$
similar to 4+A50
%1T$: ( %_4+A5"46++1; %_4+A5"+A2$5.
$+DS$L$)T.
PS: This is a loop on data #ase ta#le. This Select"$ndSelect loop will
retrie/e records one after the other from data #ase and places it in the
structure(%or' Area. Pro&rammer can use the structure data for their
pro&rammin& purpose within the Select < $ndSelect Statements.
=. S$L$)T ,-<FLD_LST!
F1*2 <DB_TABL$!
+T* TABL$ <TAB!
%3$1$ <)*+DT*+!.
$>: S$L$)T , F1*2 4+A5
+T* TABL$ T_4+A5
%3$1$ 46++1 7T 899995999998. .%here T_4+A5 is an internal
ta#le with structure similar to 4+A50
L**P AT T_4+A5.
%1T$: ( T_4+A5"46++1; T_4+A5"+A2$5.
$+DL**P.

PS: %hen INTO TABLE is used; it will no more #e a Loop. $+DS$L$)T
is not needed here. This is a sin&le statement to &et all the required records
from the Data Base ta#le into our internal ta#le in a sin&le step; which will
#e /ery faster as compared with earlier process as well as reduces num#er of
hits to the DB and thus optimi?in& our pro&rams performance.
Also; here we will ha/e all the records for processin& at a later sta&e in our
internal ta#le.
@. S$L$)T S+7L$ ,-<FLD_LST! .%e can select all fields or the
fields
in &i/en order of the
internal ta#le0
F1*2 <DB_TABL$!
+T* <%A!
%3$1$ <)*+DT*+!.
Ex: S$L$)T S+7L$ , F1*2 4+A5
+T* %_4+A5
%3$1$ 46++1 7T 899995999998. .%here %_4+A5 is a
ST16)T61$ similar to 4+A50
PS: n this case also we don8t need $+DS$L$)T. This is a statement which
will #rin& the first selected record from the Data Base Ta#le and place it in
our structure(%A. This will #e useful if we need only one record from the
DB ta#le thus not loopin& o/er the DB ta#le num#er of times.
NOTE: One important thing to remember for all the above cases (hen
!sing the "#L$%LIST& in the Select statement' is that( the or)er of the
fiel)s in the "#L$%LIST& sho!l) be same as the or)er of the fiel)s
)efine) in the *or+ Area,Internal Table-
S.stem ill tr. to place the selecte) fiel)s of each recor) in the or)er
(i-e-( first fiel) into first fiel) etc-'- So( if the fiel)s )efine) in the internal
table an) fiel)s selecte) in Select statement are not in same or)er( there
ill be errors in the )ata selecte) (sometimes r!nning into /!n0Time
errors hen there is a )ata t.pe conflict'- The sol!tion to this scenario is
belo-
A. S$L$)T <FLD_LST!
F1*2 <DB_TABL$!
+T* )*11$SP*+D+7 F$LDS *F < TAB!
%3$1$ <)*+DT*+!.
By 6sin& the INTO 1O//ESPON$IN2 #IEL$S O# addition; the select
statement will try to identify the correspondin& field of the nternal Ta#le
which has the same name as the Selected field from the Data Base ta#le and
updates it in that field. Thus e/en if the order of selection for the fields in the
Select statement and in the nternal ta#le do not match; the data is populated
into the correspondin& fields.
The same functionality applies to all the three scenarios e>plained a#o/e.
NOTE: One Limitation here hoever is that( the Names of the #iel)s
in the Internal Table sho!l) be the same as the names of the $ata Base
Table- Or else( s.stem cannot fin) an. s!ch fiel) an) so the fiel)s ill
not be !p)ate)-
Belo is a )etaile) )escription of the 2eneric Select Statement S.ntax
an) its )ifferent parts-
S.ntax:
SELE1T "res!lt&
INTO "target&
#/O3 "so!rce&
4*5E/E "con)ition&6
42/O7P B8 "fiel)s&6
45A9IN2 "con)&6
4O/$E/ B8 "fiel)s&6-
The S$L$)T statement consists of a series of clauses; each of which fulfils
a certain tas':
SELE1T cla!se: Defines the structure of the selection.
S.ntax
S$L$)T BS+7L$C-BDST+)TC
, - <si!...
<a&&!. BDST+)TC <sD!0 BAS <aD!C...
The selection can #e a sin&le line S+7L$ or a series of lines. Eou can
eliminate duplicate lines usin& the DST+)T addition. To select the entire
line; use ,; otherwise; you can specify indi/idual columns <si!. For
indi/idual columns; you can use a&&re&ate functions <a&&!; and assi&n
alternati/e column names <ai!.
INTO cla!se: Defines the tar&et area into which the selection from the
S$L$)T clause is to #e placed.
S.ntax
... +T* B)*11$SP*+D+7 F$LDS *FC <wa!
- +T*-APP$+D+7 B)*11$SP*+D+7 F$LDS *FC TABL$ <ita#!
BPA)4A7$ SF$ <n!C
- +T* .<f5!; <f=!;...0
The tar&et area can #e a flat wor' area <wa!; an internal ta#le <ita#!; or a
list of fields <fi!. f you use the )*11$SP*+D+7 F$LDS addition; data
is only selected if there is an identicallynamed field in the tar&et area. f you
use APP$+D+7 instead of +T*; the data is appended to an internal ta#le
instead of o/erwritin& the e>istin& contents. PA)4A7$ SF$ allows you to
o/erwrite or e>tend the internal ta#le in a series of pac'a&es. The data type
of the tar&et area must #e appropriate for the selection in the S$L$)T
clause.
#/O3 cla!se: Specifies the data#ase ta#les from which the data in the
selection in the S$L$)T clause is to #e read.
S.ntax
--- #/O3 4"tab& 4INNE/6:LE#T 4O7TE/6 ;OIN6 ")btab& 4AS
"alias&6
4ON "con)&6
41LIENT SPE1I#IE$6
4B8PASSIN2 B7##E/6
47P TO "n& /O*S6
Eou can read a sin&le ta#le <d#ta#! or more than one ta#le; usin& inner and
outer Doins to lin' ta#les with conditions <cond!; where <ta#! is a sin&le
ta#le or itself a Doin condition. Eou can specify indi/idual data#ase ta#les
either statically or dynamically; and you can replace their names with an
alternati/e <alias!. Eou can #ypass automatic client handlin& with the
)L$+T SP$)F$D addition; and SAP #ufferin& with BEPASS+7
B6FF$1. Eou can also restrict the num#er of lines read from the ta#le usin&
the 6P T* <n! 1*%S addition.
*5E/E cla!se: 1estricts the num#er of lines selected.
S.ntax
--- 4#O/ ALL ENT/IES IN "itab&6 *5E/E "con)&
The condition <cond! may contain one or more comparisons; tests for
#elon&in& to inter/als; /alue list chec's; su#queries; selection ta#le queries
or null /alue chec's; all lin'ed with A+D; *1; and +*T. f you use the
F*1 ALL $+T1$S addition; the condition <cond! is chec'ed for each line
of the internal ta#le <ita#! as lon& as <cond! contains a field of the internal
ta#le as an operand. For each line of the internal ta#le; the lines from the
data#ase ta#le meetin& the condition are selected. The result set is the union
of the indi/idual selections resultin& from each line.
2/O7P B8 cla!se: 7roups lines in the selection
S.ntax
--- 2/O7P B8 "s<& "s=&
7roups lines with the same contents in the specified columns. 6ses
a&&re&ate functions for all other columns in each &roup. All columns listed
in the S$L$)T clause that do not appear in the 71*6P BE addition must
#e specified in a&&re&ate e>pressions.
5A9IN2 cla!se: 1estricts the num#er of line &roups.
S.ntax
--- 5A9IN2 "con)&
Li'e the %3$1$ clause; #ut can only #e used in conDunction with a
71*6P BE clause. Applies conditions to a&&re&ate e>pressions to reduce
the num#er of &roups selected.
O/$E/ B8 cla!se: Sorts the lines in the selection.
S.ntax
--- O/$E/ B8 P/I3A/8 >E8 :--- "si& 4AS1EN$IN2:
$ES1EN$IN26---
Sorts the selection in ascendin& or descendin& order accordin& to the
primary 'ey or the contents of the fields listed.

Vous aimerez peut-être aussi