0% found this document useful (0 votes)
173 views14 pages

Abap Tricks and Tips: Select Where VS Select + Check

The document provides tips for writing efficient ABAP code when working with SQL statements and strings. Some key recommendations include using WHERE clauses with indexes instead of CHECK statements, SELECT SINGLE instead of SELECT-ENDSELECT loops, aggregates instead of self-computing values, joins instead of nested SELECTs, and built-in string functions instead of custom string manipulation code. Using the techniques described can significantly reduce network load and improve performance.

Uploaded by

Gourab Banik
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
173 views14 pages

Abap Tricks and Tips: Select Where VS Select + Check

The document provides tips for writing efficient ABAP code when working with SQL statements and strings. Some key recommendations include using WHERE clauses with indexes instead of CHECK statements, SELECT SINGLE instead of SELECT-ENDSELECT loops, aggregates instead of self-computing values, joins instead of nested SELECTs, and built-in string functions instead of custom string manipulation code. Using the techniques described can significantly reduce network load and improve performance.

Uploaded by

Gourab Banik
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

ABAPLOVERS.BLOGSPOT.

COM ABAP TRICKS and TIPS


SQL INTERFACE Select where VS Select + Check
SELECT * FROM SBOOK INTO SBOOK_WA. CHECK: SBOOK_WA-CARRID = 'LH' AND SBOOK_WA-CONNID = '0400'. ENDSELECT. VS

SELECT * FROM SBOOK INTO SBOOK_WA


WHERE CARRID = 'LH' AND CONNID = '0400'. ENDSELECT. Always specify your conditions in the Where-clause instead of checking them yourself with check statements. The database system can then use an index (if possible) and the network load is considerably less.

Select with Index Support


SELECT * FROM SBOOK CLIENT SPECIFIED INTO SBOOK_WA WHERE CARRID = 'LH' AND CONNID = '0400'. ENDSELECT. VS SELECT * FROM SBOOK CLIENT SPECIFIED INTO SBOOK_WA WHERE MANDT IN ( SELECT MANDT FROM T000 ) AND 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) the index fields concatenated with logical Ands in the Select statement's Where clause. Note that complex Where clauses are poison for the statement optimizer in any database system.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS


Select Single VS Select-Endselect
SELECT * FROM SBOOK INTO SBOOK_WA WHERE CARRID = 'LH'. EXIT. ENDSELECT. VS

SELECT SINGLE * FROM SBOOK INTO SBOOK_WA WHERE CARRID = 'LH'.

If you are interested if there exists at least one row of a database table or view with a certain condition, use the Select Single statement instead of a Select-Endselect-loop. Select Single requires one communication with the database system, whereas Select-Endselect needs two.

Select into Table t


DATA T006_WA TYPE T006. CLEAR X006. SELECT * FROM T006 INTO T006_WA. APPEND T006_WA TO X006. ENDSELECT. VS

SELECT * FROM T006 INTO TABLE X006.

It is always faster to use the Into Table version of a Select statement than to use Append statements.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS


Select aggregates
C4A = '000'. SELECT * FROM T100 INTO T100_WA WHERE SPRSL = 'D' AND ARBGB = '00'. CHECK: T100_WA-MSGNR > C4A. C4A = T100_WA-MSGNR. ENDSELECT. VS SELECT MAX( MSGNR ) FROM T100 INTO C4A WHERE SPRSL = 'D' AND ARBGB = '00'. If you want to find the maximum, minimum, sum and average value or the count of a database column, use a select list with aggregate functions instead of computing the aggregates yourself. Network load is considerably less.

Select Endselect VS Array Select


SELECT * FROM T006 INTO TABLE X006. LOOP AT X006 INTO X006_WA. ENDLOOP. VS SELECT * FROM T006 INTO X006_WA. ENDSELECT. If you process your data only once, use a Select-Endselect-loop instead of collecting data in an internal table with Select Into Table. Internal table handling takes up much more space.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS


Select with View
SELECT * FROM DD01L INTO DD01L_WA WHERE DOMNAME LIKE 'CHAR%' AND AS4LOCAL = 'A'. SELECT SINGLE * FROM DD01T INTO DD01T_WA WHERE DOMNAME = DD01L_WA-DOMNAME AND AS4LOCAL = 'A' AND AS4VERS = DD01L_WA-AS4VERS AND DDLANGUAGE = SY-LANGU. ENDSELECT. VS SELECT * FROM DD01V INTO DD01V_WA WHERE DOMNAME LIKE 'CHAR%' AND DDLANGUAGE = SY-LANGU. ENDSELECT. To process a join, use a view instead of nested Select statements. Network load is considerably less.

Select With Join


SELECT * FROM SPFLI INTO SPFLI_WA. SELECT * FROM SFLIGHT INTO SFLIGHT_WA WHERE CARRID = SPFLI_WA-CARRID AND CONNID = SPFLI_WA-CONNID. ENDSELECT. ENDSELECT. VS SELECT * INTO WA FROM SPFLI AS P INNER JOIN SFLIGHT AS F ON P~CARRID = F~CARRID AND P~CONNID = F~CONNID. ENDSELECT. To read data from several logically connected tables use a join instead of nested Select statements. Network load is considerably less.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS


Select with Select list
SELECT * FROM DD01L INTO DD01L_WA WHERE DOMNAME LIKE 'CHAR%' AND AS4LOCAL = 'A'. ENDSELECT. VS SELECT DOMNAME FROM DD01L INTO DD01L_WA-DOMNAME WHERE DOMNAME LIKE 'CHAR%' AND AS4LOCAL = 'A'. ENDSELECT. Use a select list or a view instead of Select * , if you are only interested in specific columns of the table. Network load is considerably less.

Select with Buffer Support


SELECT SINGLE * FROM T100 INTO T100_WA BYPASSING BUFFER WHERE SPRSL = 'D' AND ARBGB = '00' AND MSGNR = '999'. VS SELECT SINGLE * FROM T100 INTO T100_WA WHERE SPRSL = 'D' AND ARBGB = '00' AND MSGNR = '999'.

For all frequently used, read-only tables, try to use SAP buffering. Network load is considerably less.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS


Array Insert VS Single row Insert
LOOP AT TAB INTO TAB_WA. INSERT INTO CUSTOMERS VALUES TAB_WA. ENDLOOP. VS INSERT CUSTOMERS FROM TABLE TAB. Whenever possible, use array operations instead of single-row operations to modify your database tables. Frequent communication between the application program and database system produces considerable overhead.

Column Update
SELECT * FROM SFLIGHT INTO SFLIGHT_WA. SFLIGHT_WA-SEATSOCC = SFLIGHT_WA-SEATSOCC - 1. UPDATE SFLIGHT FROM SFLIGHT_WA. ENDSELECT. VS

UPDATE SFLIGHT SET SEATSOCC = SEATSOCC 1.

Whenever possible, use column updates instead of single-row updates to update your database tables. Network load is considerably less.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS


Using Subqueries
SELECT * FROM SPFLI INTO TABLE T_SPFLI WHERE CITYFROM = 'FRANKFURT' AND CITYTO = 'NEW YORK'. SELECT * FROM SFLIGHT AS F INTO SFLIGHT_WA FOR ALL ENTRIES IN T_SPFLI WHERE SEATSOCC < F~SEATSMAX AND CARRID = T_SPFLI-CARRID AND CONNID = T_SPFLI-CONNID AND FLDATE BETWEEN '19990101' AND '19990331'. ENDSELECT. VS SELECT * FROM SFLIGHT AS F INTO SFLIGHT_WA WHERE SEATSOCC < F~SEATSMAX AND EXISTS ( SELECT * FROM SPFLI WHERE CARRID = F~CARRID AND CONNID = F~CONNID AND CITYFROM = 'FRANKFURT' AND CITYTO = 'NEW YORK' ) AND FLDATE BETWEEN '19990101' AND '19990331'. ENDSELECT. Instead of using nested Select loops or FOR ALL ENTRIES it is often possible to use subqueries. Network load is considerably less.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS


String Manupulations
Special Operators in If (CA,.....
ASSIGN CHA(1) TO <C1A>. DO 200 TIMES. IF <C1A> = '(' OR <C1A> = ')'. "...any actions EXIT. ENDIF. ASSIGN <C1A>+1 TO <C1A>. ENDDO. VS IF CHA(200) CA '()'. "...any actions ENDIF.

Use the special operators CO, CA, CS, instead of programming the operations yourself. If ABAP/4 statements are executed per character on long strings, CPU consumption can rise substantially.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS


String Concatenation
CALL FUNCTION 'STRING_CONCATENATE_3' EXPORTING STRING1 = T100_WA-ARBGB STRING2 = T100_WA-MSGNR STRING3 = T100_WA-TEXT IMPORTING STRING = CLA EXCEPTIONS TOO_SMALL = 01.

VS
CONCATENATE T100_WA-ARBGB T100_WA-MSGNR T100_WA-TEXT INTO CLA.

Some function modules for string manipulation have become obsolete and should be replaced by ABAP/4 statements or functions: STRING_CONCATENATE... -> CONCATENATE, STRING_SPLIT... -> SPLIT, STRING_LENGTH -> strlen(), STRING_CENTER -> WRITE...TO...CENTERED, STRING_MOVE_RIGHT -> WRITE...TO...RIGHT-JUSTIFIED ________________________________________________________________________

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS


String Concatenation
* MOVE 'Jane' TO CMA. * MOVE 'Miller' TO CMB. * MOVE 'New York City' TO CMC. I1 = STRLEN( CMA ). I2 = STRLEN( CMB ). MOVE 'Mrs. ' TO CHA. MOVE CMA TO CHA+5. I1 = I1 + 6. MOVE CMB TO CHA+I1. I1 = I1 + I2 + 1. MOVE 'from ' TO CHA+I1. I1 = I1 + 5. MOVE CMC TO CHA+I1. * Mrs. Jane Miller from New York City" is the final value of CHA.

VS
* MOVE 'Jane' TO CMA. * MOVE 'Miller' TO CMB. * MOVE 'New York City' TO CMC. CONCATENATE 'Mrs.' CMA CMB 'from' CMC INTO CHA SEPARATED BY SPACE. * Mrs. Jane Miller from New York City" is the final value of CHA.

Use the CONCATENATE statement instead of programming a string concatenation of your own.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS

* CLA contains the string * ' "Editor line n'. IF CLA CN SPACE. ENDIF. SHIFT CLA BY SY-FDPOS PLACES LEFT. VS * CLA contains the string * ' "Editor line n'.

SHIFT CLA LEFT DELETING LEADING SPACE. If you want to delete the leading spaces in a string, use the ABAP/4 statement SHIFT...LEFT DELETING LEADING... .Other constructions (with CN and SHIFT...BY SY-FDPOS PLACES, with CONDENSE if possible, with CN and ASSIGN CLA+SY-FDPOS(LEN) ...) are not as fast. In any case, avoid using SHIFT inside a WHILE-loop!

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS


String Split
* CMA contains '(410)-45174-66354312' and shall be * split into AREA_CODE, * TEL_NO1, * TEL_NO2. SEARCH CMA FOR '-'. MOVE CMA(SY-FDPOS) TO AREA_CODE. I1 = SY-FDPOS + 2. SEARCH CMA FOR '-' STARTING AT I1. I1 = I1 - 1. MOVE CMA+I1(SY-FDPOS) TO TEL_NO1. I1 = I1 + SY-FDPOS + 1. MOVE CMA+I1 TO TEL_NO2.

VS
* CMA contains '(410)-45174-66354312' and shall be * split into AREA_CODE, * TEL_NO1, * TEL_NO2. SPLIT CMA AT '-' INTO AREA_CODE TEL_NO1 TEL_NO2.

Use the SPLIT statement instead of programming a string split yourself.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS

* DATA: BEGIN OF STR, LINE TYPE X, END OF STR, * CHECK_SUM TYPE I. * MOVE 'KALEBVPQDSCFG' TO CLA.

DO 64 TIMES VARYING STR FROM CLA NEXT CLA+1. CHECK STR NE SPACE. ADD STR-LINE TO CHECK_SUM. ENDDO.

VS
* DATA: BEGIN OF STR, LINE TYPE X, END OF STR, * CHECK_SUM TYPE I. * MOVE 'KALEBVPQDSCFG' TO CLA. I1 = STRLEN( CLA ). DO I1 TIMES VARYING STR FROM CLA NEXT CLA+1. CHECK STR NE SPACE. ADD STR-LINE TO CHECK_SUM. ENDDO. Use the strlen( ) function to restrict the DO loop to the relevant part of the field, e.g. when determinating a check-sum.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

ABAPLOVERS.BLOGSPOT.COM ABAP TRICKS and TIPS


* STRING is a 255 byte character field CLEAR STRING. TRANSLATE STRING USING ' *'. VS * STRING is a 255 byte character field CLEAR STRING WITH '*'.

Use "CLEAR f WITH val" whenever you want to initialize a field with a value different from the field's typespecific initial value.

LOGON TO ABAPLOVERS.BLOGSPOT.COM

You might also like