0% found this document useful (0 votes)
93 views12 pages

ABAP For Power Users E&S

ABAP for power users

Uploaded by

hymarao
Copyright
© © All Rights Reserved
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)
93 views12 pages

ABAP For Power Users E&S

ABAP for power users

Uploaded by

hymarao
Copyright
© © All Rights Reserved
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/ 12

SAP TECHNICAL EDUCATION CONFERENCE 2003

Web Application Development

ABAP for Power Users


Exercises / Solutions
Bjrn Mielenhausen

SAP TECHNICAL EDUCATION CONFERENCE 2003

Copyright

No part of this publication may be reproduced or transmitted in any form or for any purpose
without the express permission of SAP AG. The information contained herein may be
changed without prior notice.
Some software products marketed by SAP AG and its distributors contain proprietary
software components of other software vendors.
Microsoft, WINDOWS, NT, EXCEL, Word, PowerPoint and SQL Server are
registered trademarks of Microsoft Corporation.
IBM, DB2, DB2 Universal Database, OS/2, Parallel Sysplex, MVS/ESA, AIX,
S/390, AS/400, OS/390, OS/400, iSeries, pSeries, xSeries, zSeries, z/OS, AFP,
Intelligent Miner, WebSphere, Netfinity, Tivoli, Informix and Informix Dynamic
ServerTM are trademarks of IBM Corporation in USA and/or other countries.
ORACLE is a registered trademark of ORACLE Corporation.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
Citrix, the Citrix logo, ICA, Program Neighborhood, MetaFrame, WinFrame,
VideoFrame, MultiWin and other Citrix product names referenced herein are trademarks
of Citrix Systems, Inc.
HTML, DHTML, XML, XHTML are trademarks or registered trademarks of W3C, World
Wide Web Consortium, Massachusetts Institute of Technology.
JAVA is a registered trademark of Sun Microsystems, Inc.
JAVASCRIPT is a registered trademark of Sun Microsystems, Inc., used under license for
technology invented and implemented by Netscape.
MarketSet and Enterprise Buyer are jointly owned trademarks of SAP Markets and
Commerce One.
SAP, SAP Logo, R/2, R/3, mySAP, mySAP.com and other SAP products and services
mentioned herein as well as their respective logos are trademarks or registered trademarks of
SAP AG in Germany and in several other countries all over the world. All other product and
service names mentioned are trademarks of their respective companies.

SAP TECHNICAL EDUCATION CONFERENCE 2003

Contents
Exercise description: Performance Analysis of Internal Tables ......................................... 4
Exercise Tasks: Performance Analysis of Internal Tables ................................................. 4
Source Example: Performance Analysis of Internal Tables ............................................... 5
Exercise description: Internal Table List Writer.................................................................. 7
Exercise Tasks: Internal Table List Writer ......................................................................... 7
Source Example: Internal Table List Writer ....................................................................... 8
Exercise description: Database List Writer ........................................................................ 9
Exercise Tasks: Database List Writer ................................................................................ 9
Source Example: Database List Writer ............................................................................ 10
Exercise description: Performance of Generating Programs ........................................... 11
Exercise Tasks: Performance of Generating Programs ................................................... 11
Source Example: Performance of Generating Programs ................................................. 12

SAP TECHNICAL EDUCATION CONFERENCE 2003

Exercise description: Performance


Analysis of Internal Tables
This exercise shows the advantages and disadvantages of the different kinds of internal
tables. Standrad, sorted, and index tables are investigated. The operation INSERT, READ
WITH KEY and READ INDEX are compared by there time costs.

Exercise Tasks: Performance Analysis of


Internal Tables
Task 1:
Define a PARAMETER to specify the count of entries for the internal tables to be
investigated -default value 100.

Task 2:
Define a STANDARD, SORTED and HASHED TABLE with line type Integer and the key
TABLE_LINE.

Task 3:
Fill the three tables using three DO loops with the number of entries specified by the
PARAMETER. Calculate the run time of all three loops and write the result devided by the
count of entries to the ABAP list for every table.

Task 4:
Now use a DO loop to READ WITH KEY each entry of the three tables and calculate the
run time for every table.

Task 5:
At the end, use a DO loop to READ with INDEX each entry of the standard and sorted table
and calculate the run time.
TIP: Use GET RUN TIME FIELD to get run time.
Use SY-INDEX to fill and read the tables.
SY_INDEX contains the actual loop count within DO and ENDDO.

SAP TECHNICAL EDUCATION CONFERENCE 2003

Source Example: Performance Analysis of


Internal Tables
PROGRAM table_performance.
PARAMETER count TYPE i DEFAULT 100.
DATA:wa type i, t1 TYPE
std_tab TYPE STANDARD
srt_tab TYPE SORTED
hsh_tab TYPE HASHED

i, t2
TABLE
TABLE
TABLE

TYPE
OF i
OF i
OF i

i, t
WITH
WITH
WITH

TYPE p DECIMALS 2,
NON-UNIQUE KEY table_line,
UNIQUE KEY
table_line,
UNIQUE KEY
table_line.

GET RUN TIME FIELD t1.


DO count TIMES.
INSERT sy-index INTO TABLE std_tab.
ENDDO.
GET RUN TIME FIELD t2. t = ( t2 - t1 ) / count.
WRITE: / 'insert standard table:', 30 t.
GET RUN TIME FIELD t1.
DO count TIMES.
INSERT sy-index INTO TABLE srt_tab.
ENDDO.
GET RUN TIME FIELD t2. t = ( t2 - t1 ) / count.
WRITE: / 'insert sorted table:', 30 t.
GET RUN TIME FIELD t1.
DO count TIMES.
INSERT sy-index INTO TABLE hsh_tab.
ENDDO.
GET RUN TIME FIELD t2. t = ( t2 - t1 ) / count.
WRITE: / 'insert hashed table:', 30 t.
ULINE.
GET RUN TIME FIELD t1.
DO count TIMES.
READ TABLE std_tab INTO WA WITH TABLE KEY table_line = sy-index.
ENDDO.
GET RUN TIME FIELD t2. t = ( t2 - t1 ) / count.
WRITE: / 'read with key standard table:', 30 t.
GET RUN TIME FIELD t1.
DO count TIMES.
READ TABLE srt_tab INTO WA WITH TABLE KEY table_line = sy-index.
ENDDO.
GET RUN TIME FIELD t2. t = ( t2 - t1 ) / count.
WRITE: / 'read with key sorted table:', 30 t.
GET RUN TIME FIELD t1.
DO count TIMES.
READ TABLE hsh_tab INTO wa WITH TABLE KEY table_line = sy-index.
ENDDO.
GET RUN TIME FIELD t2.t = ( t2 - t1 ) / count.

SAP TECHNICAL EDUCATION CONFERENCE 2003

WRITE: / 'read with key hashed table:', 30 t.


ULINE.
GET RUN TIME FIELD t1.
DO count TIMES.
READ TABLE std_tab TRANSPORTING NO FIELDS index sy-index.
ENDDO.
GET RUN TIME FIELD t2. t = ( t2 - t1 ) / count.
WRITE: / 'read index standard table:', 30 t.
GET RUN TIME FIELD t1.
DO count TIMES.
READ TABLE srt_tab TRANSPORTING NO FIELDS index sy-index.
ENDDO.
GET RUN TIME FIELD t2. t = ( t2 - t1 ) / count.
WRITE: / 'read index sorted table:', 30 t.

SAP TECHNICAL EDUCATION CONFERENCE 2003

Exercise description: Internal Table List


Writer
This exercise shows how to implement a form that is able to write the contents of any
internal table to the ABAP List.

Exercise Tasks: Internal Table List Writer


Task 1
Write a form WRITE_TABLE which accepts any internal table. Write the contents of the
internal table to the ABAP list, line by line and field by field.

Task 2
Define and fill an internal table with a line type of your choice to test the form
WRITE_TABLE.
TIP: Use LOOP ASSIGNING nested with ASSIGN COMPONENT compindex OF
STRUCTURE and DO loop to access each field of every line.
TIP: If you need more information about the syntax and the semantics of an ABAP
statement use Online-Help by pressing F1 and entering the first word of the statement.

SAP TECHNICAL EDUCATION CONFERENCE 2003

Source Example: Internal Table List Writer


PROGRAM write_table.
FORM write_table USING p_table TYPE ANY TABLE.
FIELD-SYMBOLS:
<line> TYPE DATA,
<field> TYPE DATA.
WRITE / '{'.
LOOP AT p_table ASSIGNING <line>.
WRITE /4 '('.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <line> TO <field>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
WRITE /8 <field>.
ENDDO.
"loop at component
WRITE /4 ')'.
ENDLOOP.
"loop at table
WRITE / '}'.
ENDFORM.
"write_table
TYPES:
BEGIN OF my_line_type,
name TYPE string,
birth_date TYPE d,
salary TYPE p DECIMALS 2,
END OF my_line_type,
my_table_type TYPE HASHED TABLE OF my_line_type
WITH UNIQUE KEY name birth_date.
DATA:
my_wa TYPE my_line_type,
my_table TYPE my_table_type.
START-OF-SELECTION.
my_wa-name = 'Peter Smith'.
my_wa-birth_date = '19640303'.
my_wa-salary = '20000.00'.
INSERT my_wa INTO TABLE my_table.
my_wa-name = 'April May June'.
my_wa-birth_date = '19660401'.
my_wa-salary = '1000000.00'.
INSERT my_wa INTO TABLE my_table.
my_wa-name = 'Mary McWeather'.
my_wa-birth_date = '19710711'.
my_wa-salary = '1.00'.
INSERT my_wa INTO TABLE my_table.
PERFORM write_table USING my_table.

SAP TECHNICAL EDUCATION CONFERENCE 2003

Exercise description: Database List Writer


This exercise shows how to implement program that is able to select any data from any
database table and displays the contents .

Exercise Tasks: Database List Writer


Task 1
Define two PARAMETERs, one for a name of a database table and one for a WHERECondition.

Task 2
Use the CREATE DATA statement to create an appropriate internal table as a destination
for a SELECT on the database table.

Task 3
Use the OpenSQL SELECT together with Dynamic Token Specification for the FROM and
WHERE clause.

Task 4
At the end, take the form WRITE_TABLE from Exercise: Generic Internal Table List
Writer to write the result of the SELECT to the ABAP list.

SAP TECHNICAL EDUCATION CONFERENCE 2003

Source Example: Database List Writer


PROGRAM generic_database_writer.
PARAMETER:
table TYPE c LENGTH 30 DEFAULT 'SPFLI',
where TYPE c LENGTH 70 DEFAULT 'carrid = ''AA'''.
DATA:
data_ref TYPE REF TO data.
FIELD-SYMBOLS:
<tab> TYPE ANY TABLE.
FORM write_table USING p_table TYPE ANY TABLE.
FIELD-SYMBOLS:
<line> TYPE DATA,
<field> TYPE DATA.
WRITE / '{'.
LOOP AT p_table ASSIGNING <line>.
WRITE /4 '('.
DO.
ASSIGN COMPONENT sy-index OF STRUCTURE <line> TO <field>.
IF sy-subrc <> 0.
EXIT.
ENDIF.
WRITE /8 <field>.
ENDDO.
"loop at component
WRITE /4 ')'.
ENDLOOP.
"loop at table
WRITE / '}'.
ENDFORM.
"write_table
START-OF-SELECTION.
CREATE DATA data_ref TYPE TABLE OF (table).
ASSIGN data_ref->* TO <tab>.
SELECT * FROM (table) INTO TABLE <tab> WHERE (where).
PERFORM write_table USING <tab>.

SAP TECHNICAL EDUCATION CONFERENCE 2003

Exercise description: Performance of


Generating Programs
This exercise shows the advantages and disadvantages of generating programs and
subroutine pools.

Exercise Tasks: Performance of


Generating Programs
Task 1
Write a program that creates at run time an empty program and a subroutine pool with one
empty form.

Task 2
Define a PARAMETER for a counter with default value 100.

Task 3
Perform the form and submit the program counter times and calculate the runtime for one
SUBMIT and one PERFORM IN PROGRAM.

TIP: Use GET RUN TIME FIELD to get relative runtime (see Exercise: Performance
Analysis of Internal Tables).

SAP TECHNICAL EDUCATION CONFERENCE 2003

Source Example: Performance of


Generating Programs
REPORT performace_generate_program.
PARAMETER:
count TYPE i DEFAULT 100.
DATA:
t1 TYPE i,
t2 TYPE i,
t TYPE p DECIMALS 2,
sbp_src TYPE TABLE OF string,
prg_src TYPE TABLE OF string,
sbp_name TYPE c LENGTH 8,
prg_name TYPE c LENGTH 30 VALUE 'Z_ABAP_POWER_USER_XX'.
APPEND 'PROGRAM test.' TO sbp_src.
APPEND 'FORM test.'
TO sbp_src.
APPEND 'ENDFORM.'
TO sbp_src.
GENERATE SUBROUTINE POOL sbp_src NAME sbp_name.
IF sy-subrc <> 0.
WRITE / 'error generating subroutine'.
EXIT.
ENDIF.
APPEND 'PROGRAM test.' TO prg_src.
INSERT REPORT prg_name FROM prg_src.
GENERATE REPORT prg_name.
IF sy-subrc <> 0.
WRITE / 'error generating report'.
EXIT.
ENDIF.
GET RUN TIME FIELD t1.
DO count TIMES.
PERFORM test IN PROGRAM (sbp_name).
ENDDO.
GET RUN TIME FIELD t2.
t = ( t2 - t1 ) / count.
WRITE: / 'perform in program', 30 t.
GET RUN TIME FIELD t1.
DO count TIMES.
SUBMIT (prg_name) AND RETURN.
ENDDO.
GET RUN TIME FIELD t2.
t = ( t2 - t1 ) / count.
WRITE: / 'submit program', 30 t.

You might also like