Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
70Activity
0 of .
Results for:
No results containing your search query
P. 1
ABAP Programming : Tips & Tricks

ABAP Programming : Tips & Tricks

Ratings:

5.0

(1)
|Views: 15,485|Likes:
Published by kattak2k

More info:

Published by: kattak2k on Apr 08, 2009
Copyright:Traditional Copyright: All rights reserved

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less

05/27/2014

pdf

text

original

 
TIPS & TRICKS
NO
YES
Select using an aggregate function
 
DATA: MAX_MSGNR type t100-msgnr.MAX_MSGNR = '000'.SELECT * FROM T100 INTO T100_WAWHERE SPRSL = 'D' ANDARBGB = '00'.CHECK: T100_WA-MSGNR > MAX_MSGNR.MAX_MSGNR = T100_WA-MSGNR.ENDSELECT.DATA: MAX_MSGNR type t100-msgnr.SELECT MAX( MSGNR ) FROM T100 INTO max_msgnrWHERE SPRSL = 'D' ANDARBGB = '00'.
Select ... Up To 1 Rows
SELECT * FROM SBOOK INTO SBOOK_WAWHERE CARRID = 'LH'.EXIT.ENDSELECT.
If you are interested if there exists at least one row of adatabase table or view with a certain condition, use the Select... Up To 1 Rows statement instead of a Select-Endselect-loopwith an Exit.If all primary key fields are supplied in the Where conditions youcan even use Select Single.Select Single requires one communication with the databasesystem, whereas Select-Endselect needs two.
SELECT * FROM SBOOK INTO SBOOK_WAUP TO 1 ROWSWHERE CARRID = 'LH'.ENDSELECT.
Single-line Updates
SELECT * FROM SFLIGHT INTO SFLIGHT_WA.SFLIGHT_WA-SEATSOCC =SFLIGHT_WA-SEATSOCC - 1.UPDATE SFLIGHT FROM SFLIGHT_WA.ENDSELECT.
Whenever possible, use column updates instead of single-rowupdates to update your database tables.Network load is considerably less.
Column Update
UPDATE SFLIGHTSET SEATSOCC = SEATSOCC - 1.
Select without index support
SELECT * FROM SBOOK CLIENT SPECIFIED INTOSBOOK_WAWHERE CARRID = 'LH'AND CONNID = '0400'.ENDSELECT.
For all frequently used Select statements, try to use an index.You always use an index if you specify (a generic part of) theindex fields concatenated with logical Ands in the Selectstatement's Where clause. Note that complex Where clausesare poison for the statement optimizer in any database system.
Select with primary index support
SELECT * FROM SBOOK CLIENT SPECIFIED INTOSBOOK_WAWHERE MANDT IN ( SELECT MANDT FROM T000 )AND CARRID = 'LH'AND CONNID = '0400'.ENDSELECT.
Select without buffer support
SELECT SINGLE * FROM T100 INTO T100_WABYPASSING BUFFERWHERE SPRSL = 'D'AND ARBGB = '00'AND MSGNR = '999'.
For all frequently used, read-only tables, try to use SAPbuffering. Network load is considerably less.
Select with buffer support
SELECT SINGLE * FROM T100 INTO T100_WAWHERE SPRSL = 'D'AND ARBGB = '00'AND MSGNR = '999'.
Select + Append statement
DATA T006_WA TYPE T006.CLEAR X006.SELECT * FROM T006 INTO T006_WA.APPEND T006_WA TO X006.ENDSELECT.
Select Into Table
SELECT * FROM T006 INTO TABLE X006.
It is always faster to use the Into Table version of a Selectstatement than to use Append statements.
Single-line Inserts Array Insert
Ravi KPage 14/8/2009
 
TIPS & TRICKS
LOOP AT TAB INTO TAB_WA.INSERT INTO CUSTOMERS VALUES TAB_WA.ENDLOOP.
Whenever possible, use array operations instead of single-rowoperations to modify your database tables. Frequentcommunication between the application program and databasesystem produces considerable overhead.
INSERT CUSTOMERS FROM TABLE TAB.
Select Into Table t + Loop at t.
SELECT * FROM T006INTO TABLE X006.LOOP AT X006 INTO X006_WA.ENDLOOP.
If you process your data only once, use a Select-Endselect-loopinstead of collecting data in an internal table with Select IntoTable. Internal table handling takes up much more space.
Select ... Endselect.
SELECT * FROM T006 INTO X006_WA.ENDSELECT.
Nested Select statements
SELECT * FROM DD01L INTO DD01L_WAWHERE DOMNAME LIKE 'CHAR%'AND AS4LOCAL = 'A'.SELECT SINGLE * FROM DD01T INTO DD01T_WAWHERE DOMNAME = DD01L_WA-DOMNAMEAND AS4LOCAL = 'A'AND AS4VERS = DD01L_WA-AS4VERSAND DDLANGUAGE = SY-LANGU.ENDSELECT.
Select with view
SELECT * FROM DD01V INTO DD01V_WAWHERE DOMNAME LIKE 'CHAR%'AND DDLANGUAGE = SY-LANGU.ENDSELECT.
To process a join, use a view instead of nested Selectstatements.Network load is considerably less.
Nested Select statements
SELECT * FROM SPFLI INTO SPFLI_WA.SELECT * FROM SFLIGHT INTO SFLIGHT_WAWHERE CARRID = SPFLI_WA-CARRIDAND CONNID = SPFLI_WA-CONNID.ENDSELECT.ENDSELECT.
To read data from several logically connected tables use a joininstead of nested Select statements. Network load isconsiderably less.
Select with join
SELECT * INTO WAFROM SPFLI AS P INNER JOIN SFLIGHT AS FON P~CARRID = F~CARRID ANDP~CONNID = F~CONNID.ENDSELECT.
Using Two Selects
SELECT * FROM SPFLIINTO TABLE T_SPFLIWHERE CITYFROM = 'FRANKFURT'AND CITYTO = 'NEW YORK'.SELECT * FROM SFLIGHT AS FINTO SFLIGHT_WAFOR ALL ENTRIES IN T_SPFLIWHERE SEATSOCC < F~SEATSMAXAND CARRID = T_SPFLI-CARRIDAND CONNID = T_SPFLI-CONNIDAND FLDATE BETWEEN '19990101' AND '19990331'.ENDSELECT.
Instead of using nested Select loops or FOR ALL ENTRIES it isoften possible to use sub queries.Network load is considerably less.
Using A Sub query
SELECT * FROM SFLIGHT AS F INTO SFLIGHT_WAWHERE SEATSOCC < F~SEATSMAXAND EXISTS ( SELECT * FROM SPFLIWHERE CARRID = F~CARRIDAND CONNID = F~CONNIDAND CITYFROM = 'FRANKFURT'AND CITYTO = 'NEW YORK' AND FLDATEBETWEEN'19990101' AND '19990331'.ENDSELECT.
SELECT
SELECT * FROM SBOOK INTO SBOOK_WA UP TO 10ROWS.SELECT SINGLE AIRPFROM AIRPTOINTO (AP1, AP2)FROM SPFLIWHERE CARRID = SBOOK_WA-CARRIDAND CONNID = SBOOK_WA-CONNID.SELECT SINGLE NAMEINTO NAME1FROM SAIRPORT
Context (for durable data)
SELECT * FROM SBOOK INTO SBOOK_WA UP TO 10ROWS.SUPPLY CARRID = SBOOK_WA-CARRIDCONNID = SBOOK_WA-CONNIDTO CONTEXT TRAV1.DEMAND AIRPFROM = AP1AIRPTO = AP2NAME_FROM = NAME1NAME_TO = NAME2FROM CONTEXT TRAV1.
Ravi KPage 24/8/2009
 
TIPS & TRICKS
WHERE ID = AP1.SELECT SINGLE NAMEINTO NAME2FROM SAIRPORTWHERE ID = AP2.ENDSELECT.ENDSELECT.
Context advantages:- no double fetch by DEMAND: 1. fetch, 2. get from buffer - more performance (best SELECT-statement)- better survey of code
Linear search in an internal table
* Entries: 1000, Line width: 100* Key width: 20* The READ ends with SY-SUBRC=4READ TABLE ITAB INTO WAWITH KEY K = 'X'.
If internal tables are assumed to have many (>20) entries, alinear search through all entries is very time-consuming.Try to keep the table ordered and use binary searchor used a table of type SORTED TABLE.If TAB has n entries, linear search runs in O( n ) time, whereasbinary search takes only O( log2( n ) ).
Binary search in an internal table
* Entries: 1000, Line width: 100* Key width: 20* The READ ends with SY-SUBRC=4READ TABLE ITAB INTO WAWITH KEY K = 'X'BINARY SEARCH.
Dynamically specified key
 
READ TABLE ITAB INTO WAWITH KEY (NAME) = 'X'.
A dynamic key access is slower than a static one, since the keyspecification must be evaluated at runtime. However, for largetables the costs are dominated by number of comparisonneeded to locate the entry.
Statically specified key
READ TABLE ITAB INTO WAWITH KEY K = 'X'.
No secondary index => linear search
 
READ TABLE ITAB INTO WAWITH KEY DATE = SY-DATUM.IF SY-SUBRC = 0." ...ENDIF.
If you need to access an internal table with different keysrepeatedly, keep your own secondary indices.With a secondary index, you can replace a linear search with abinary search plus an index access.
Binary search using secondary index
 
READ TABLE SEC_IDX INTO SEC_IDX_WAWITH KEY DATE = SY-DATUMBINARY SEARCH.IF SY-SUBRC = 0.READ TABLE ITAB INTO WAINDEX SEC_IDX_WA-INDX." ...ENDIF.
Key access with LOOP/CHECK
LOOP AT ITAB INTO WA.CHECK WA-K = 'X'." ...ENDLOOP.
LOOP ... WHERE is faster than LOOP/CHECK because LOOP... WHERE evaluates the specified condition internally.As with any logical expressions, the performance is better if theoperands of a comparison share a common type.The performance can be further enhanced if LOOP ... WHEREis combined with FROM i1 and/or TO i2, if possible.
Key access with LOOP ... WHERE
LOOP AT ITAB INTO WA WHERE K = 'X'." ...ENDLOOP.
READ on a unique sorted table
DO 250 TIMES.N = 4 * SY-INDEX.READ TABLE STAB INTO WA WITH TABLE KEY K = N.IF SY-SUBRC = 0." ...ENDIF.ENDDO.
Entries in a sorted table are located by binary search.The costs depend on the number of entries in the table (O (log
READ on a unique hashed table
DO 250 TIMES.N = 4 * SY-INDEX.READ TABLE HTAB INTO WA WITH TABLE KEY K = N.IF SY-SUBRC = 0." ...ENDIF.ENDDO.
Hashed tables are optimized for single entry access, whereassorted tables are optimized for partial sequential loop
Ravi KPage 34/8/2009

Activity (70)

You've already reviewed this. Edit your review.
kattak2k liked this
1 thousand reads
1 hundred reads
sirivirishi liked this
곽정호 liked this
alezhu liked this
Mirko liked this
Ahmed Hassan liked this

You're Reading a Free Preview

Download
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->