Anda di halaman 1dari 11

SAP TECHED 04

ADVANCED AND GENERIC PROGRAMMING IN ABAP


ABAP351

Exercises / Solutions

GERD KLUGER, SAP AG BJRN MIELENHAUSEN, SAP AG

SAP TECHED 04

Generic Table List Writer 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. 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. SOLUTION
*&---------------------------------------------------------------------* *& Report ZABAP351_1 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT ZABAP351_1.

data: itab1 type sorted table of spfli with unique key carrid connid, itab2 type standard table of scarr. form write_table using itab type any table. field-symbols: <line> type any, <field> type any. loop at itab assigning <line>. do. assign component sy-index of structure <line> to <field>. if sy-subrc <> 0. exit. endif. write / <field>. enddo. write / sy-uline(50). endloop. endform. start-of-selection. select * from spfli into table itab1 where distance > 1000. perform write_table using itab1. select * from scarr into table itab2. perform write_table using itab2.

SAP TECHED 04

OCTOBER 2004

Database List Writer Define two PARAMETERs, one for a name of a database table and one for a WHERE-Condition. Use the CREATE DATA statement to create an appropriate internal table as a destination for a SELECT on the database table. Use the OpenSQL SELECT together with Dynamic Token Specification for the FROM and WHERE clause. 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. SOLUTION
*&---------------------------------------------------------------------* *& Report ZABAP351_2 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT ZABAP351_2.

parameters: table(30) type c, where(100) type c. data tabref type ref to data. field-symbols <table> type any table. create data tabref type table of (table). assign tabref->* to <table>. select * from (table) into table <table> where (where). perform WRITE_TABLE in program ZABAP351_1 using <table>.

SAP TECHED 04

OCTOBER 2004

Dynamic Table Creation Create a new table type which is able to store data records from a database join over tables 'SCARR' and 'SPFLI'. Although both tables have a lot of columns the new table should only contain the columns 'CARRNAME' , 'CONNID', 'CITYFROM', 'CITYTO' and 'DISTANCE'. The new table type should be sorted according to the distance. Once you have created the type, create a data object from it and fill it with all flights that have a distance > 1000. Tip: First get the component tables of 'SCARR' and 'SPFLI' and determine the types of the respective columns. Build a new component table from these columns and create the corresponding structure type. Once you have the structure type, create the required table type and dynamically create a data object of this type. Fill this object with an OpenSQL statement that is an inner join on 'SCARR' and 'SPFLI'. Hint: In order to fill the data object you will need a field symbol (ASSIGN dref->* to <t>). SOLUTION
*&---------------------------------------------------------------------* *& Report ZABAP351_3 *& *&---------------------------------------------------------------------* REPORT ZABAP351_3.

data: spfliType type ref to cl_abap_structdescr, scarrType type ref to cl_abap_structdescr, joinType type ref to cl_abap_structdescr, tableType type ref to cl_abap_tabledescr, comp_tab type cl_abap_structdescr=>component_table, comp like line of comp_tab, new_comp_tab like comp_tab, key type abap_keydescr_tab, dref type ref to data, from_clause type string, where_clause type string. field-symbols: <table> type any table. **** build new type(s) **** scarrType ?= cl_abap_typedescr=>describe_by_name('SCARR'). comp_tab = scarrType->get_components( ). read table comp_tab with table key name = 'CARRNAME' into comp. append comp to new_comp_tab. spfliType ?= cl_abap_typedescr=>describe_by_name('SPFLI'). comp_tab = spfliType->get_components( ). read table comp_tab with table key name = 'CONNID' into comp. append comp to new_comp_tab. read table comp_tab with table key name = 'CITYFROM' into comp. append comp to new_comp_tab. read table comp_tab with table key name = 'CITYTO' into comp. append comp to new_comp_tab. read table comp_tab with table key name = 'DISTANCE' into comp. append comp to new_comp_tab. joinType = cl_abap_structdescr=>create( new_comp_tab ). append 'DISTANCE' to key. tableType = cl_abap_tabledescr=>create( p_line_type = joinType p_table_kind = cl_abap_tabledescr=>tablekind_sorted p_key = key ).

SAP TECHED 04

OCTOBER 2004

**** create data ***** create data dref type handle tableType. assign dref->* to <table>. **** fill table and dump **** from_clause = 'scarr as c inner join spfli as p on c~carrid = p~carrid'. where_clause = 'distance > 1000'. select * from (from_clause) into corresponding fields of table <table> where (where_clause). perform WRITE_TABLE in program ZABAP351_1 using <table>.

SAP TECHED 04

OCTOBER 2004

Generic Component Access Write a form ACCESS_COMPONENT which accepts any structure and a string argument that denotes a component of this structure. If the structure indeed has this component, write its value, else write an error message. Extend your form in a way that it can also access subcomponents of substructures, e.g. if you pass structure 'foo' that has structure 'bar' as a component which again is a structure having 'toto' as a component, then a call of ACCESS_COMPONENT (foo, 'bar-toto') should print the corresponding value. Test your form with some nested structures, preferable with additional includes. SOLUTION
*&---------------------------------------------------------------------* *& Report ZABAP351_4 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT ZABAP351_4.

types: begin of nameType, first(10) type c, last(10) type c, end of nameType, begin of personType. include type nameType as name renaming with suffix _name. types: age(3) type n, end of personType, begin of employeeType. include type personType. include type personType as manager renaming with suffix _mg. types: end of employeeType. form access_component using any_struc type any comp_name type string. data: strucType type ref to cl_abap_structdescr, symb_tab type cl_abap_structdescr=>symbol_table, comp like line of symb_tab, part1 like comp_name, part2 like comp_name. field-symbols: <val> type any. try. strucType ?= cl_abap_typedescr=>describe_by_data( any_struc ). catch cx_sy_move_cast_error. write / 'Error: Argument was not a structure'. return. endtry. symb_tab = strucType->get_symbols( ). read table symb_tab with table key name = comp_name into comp. if sy-subrc <> 0. write: / 'Error: structure has no component ''' no-gap, comp_name no-gap, ''''. split comp_name at '-' into part1 part2. if part2 is not initial.

SAP TECHED 04

OCTOBER 2004

read table symb_tab with table key name = part1 into comp. if sy-subrc = 0. write: / '... trying subcomponent ''' no-gap, part1 no-gap, ''''. assign component part1 of structure any_struc to <val>. perform access_component using <val> part2. endif. endif. return. endif. assign component comp_name of structure any_struc to <val>. write: / 'Value of ''' no-gap, comp_name no-gap, ''':', <val>. endform. start-of-selection. data: emp type employeeType. emp-name-first = 'Little'. emp-name-last = 'Dilbert'. emp-age = 44. emp-manager-name-first = 'John'. emp-manager-last_name = 'Nerd'. emp-age_mg = 55. skip skip skip skip skip skip skip skip skip 1. 1. 1. 1. 1. 1. 1. 1. 1. perform perform perform perform perform perform perform perform perform access_component access_component access_component access_component access_component access_component access_component access_component access_component using using using using using using using using using 'a' emp emp emp emp emp emp emp emp 'test'. 'first_name'. 'FIRST_NAME'. 'FIRST_NAME_MG'. 'MANAGER'. 'NAME'. 'MANAGER-NAME-LAST'. 'MANAGER-NAME_LAST'. 'MANAGER-LAST_NAME'.

SAP TECHED 04

OCTOBER 2004

Understanding Included Views In order to get a better understanding of Included Views and the Symbol Table write forms that dump the component Table of a structure recursively also nested component tables the Included Views of the structure the symbol table of the structure Test your forms with some complicated structures. SOLUTION
*&---------------------------------------------------------------------* *& Report ZABAP351_5 *& *&---------------------------------------------------------------------* *& *& *&---------------------------------------------------------------------* REPORT ZABAP351_5.

types: begin of addressType, city type string, street type string, end of addressType. types: begin of nameType, first(30) type c, last(40) type c, end of nameType, begin of personType. include type nameType as name renaming with suffix _name. types: age(3) type n, end of personType, begin of employeeType. include type personType. include type personType as manager renaming with suffix _mg. types: end of employeeType.

form dump_comp_tab using struc_type type ref to cl_abap_structdescr. data: comp_tab type cl_abap_structdescr=>component_table, comp like line of comp_tab. comp_tab = struc_type->get_components( ). write / sy-uline(50). write at /10 'Component Description Table' color 2. write / sy-uline(50). loop at comp_tab into comp. write: / comp-name, at 20 comp-type->kind, at 25 comp-as_include, at 30 comp-suffix. endloop. endform.

SAP TECHED 04

OCTOBER 2004

form dump_comp_tab_deep using struc_type type ref to cl_abap_structdescr p_shift type i. data: comp_tab type cl_abap_structdescr=>component_table, comp like line of comp_tab, l_type like struc_type, l_shift like p_shift. comp_tab = struc_type->get_components( ). if p_shift = 0. write / sy-uline(50). write at /10 'Recursive Dump of Component Tables' color 2. write / sy-uline(50). endif. loop at comp_tab into comp. write: at /p_shift comp-name, at 20 comp-type->kind, at 25 comp-as_include, at 30 comp-suffix. if comp-type->kind = 'S'. l_shift = p_shift + 4. l_type ?= comp-type. perform dump_comp_tab_deep using l_type l_shift. endif. endloop. endform.

form dump_view_tab using struc_type type ref to cl_abap_structdescr level type i. data: view_tab type cl_abap_structdescr=>included_view, comp like line of view_tab. write / sy-uline(50). if level < 0. view_tab = struc_type->get_included_view( ). write at /10 'Included View (leaf view)' color 3. else. view_tab = struc_type->get_included_view( level ). write: at /10 'Included View for level' color 3, level color 3. endif. write / sy-uline(50). loop at view_tab into comp. write: / comp-name, at 20 comp-type->kind. endloop. endform.

form dump_symb_tab using struc_type type ref to cl_abap_structdescr. data: symb_tab type cl_abap_structdescr=>symbol_table, comp like line of symb_tab. symb_tab = struc_type->get_symbols( ). write / sy-uline(50).

SAP TECHED 04

OCTOBER 2004

write at /10 'Symbol Table' color 4. write / sy-uline(50). loop at symb_tab into comp. write: / comp-name, at 20 comp-type->kind. endloop. endform.

start-of-selection. data: strucType type ref to cl_abap_structdescr. strucType ?= cl_abap_structdescr=>describe_by_name( 'employeeType' ). perform dump_comp_tab using strucType. perform dump_comp_tab_deep using strucType 0. perform dump_view_tab using strucType -1. perform dump_view_tab using strucType 2. perform dump_view_tab using strucType 1. perform dump_view_tab using strucType 0. perform dump_symb_tab using strucType.

SAP TECHED 04

OCTOBER 2004

10

Copyright 2004 SAP AG. All Rights Reserved


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, Outlook, and PowerPoint 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, and Informix are trademarks or registered trademarks of IBM Corporation in the United States 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, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C 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. MaxDB is a trademark of MySQL AB, Sweden. SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver 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 the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty. SAP assumes no responsibility for errors or omissions in these materials.

Anda mungkin juga menyukai