Sunteți pe pagina 1din 33

ECC 6.

0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 3,Exercise 1
REPORT YAPxx03_1 .
INCLUDE <ICON>.
ULINE.
WRITE: /'USER ADDRESS INFORMATION'.
ULINE.
SKIP 2.
ULINE 10(60).
WRITE: /15 ICON_DATE AS ICON, SY-DATUM,
/15 ICON_TIME AS ICON, SY-UZEIT,
/15 ICON_REPORT AS ICON, 'ABAP48', 30 TEXT-001,
/ TEXT-002 UNDER TEXT-001,
/ TEXT-003 UNDER TEXT-001.
ULINE /10(60).
Text-001: Scott Shore
Text-002: 2001 Hamilton Street. (Korman Suites)
Text-003: Philadelphia, PA 19103

Page 1

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 4, Exercise 1
REPORT YAPxx04_1 .
PARAMETERS: NUM1 TYPE I,
NUM2 LIKE NUM1.
DATA SUM LIKE NUM1.
SUM = NUM1 + NUM2.
WRITE: /10 TEXT-001 , 30 NUM1,
/10 '+',
/10 TEXT-002 , NUM2 UNDER NUM1.
ULINE /5(50).
WRITE: /10 TEXT-003 , SUM UNDER NUM1.
NB: goto->text element->text symbol->
TEXT-001->FIRST INTEGER
TEXT-002->SECOND INTEGER
TEXT-003->RESULT

Page 2

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 4, Exercise 2
REPORT YAPxx04_2 .
PARAMETERS: BIRTHDAY LIKE SY-DATUM.
DATA: DIFF TYPE I,
DT LIKE SY-DATUM.
DIFF = SY-DATUM - BIRTHDAY.
DT = BIRTHDAY + 10000.
WRITE: /10 'BIRTHDAY', 35 ':', BIRTHDAY,
/10 'CURRENTDATE' ,35 ':', SY-DATUM UNDER BIRTHDAY.
ULINE /10(40).
WRITE: /10 'DAYS OLD', 35 ':', DIFF UNDER BIRTHDAY,
/10 '10,000 DAYS', 35 ':', DT UNDER BIRTHDAY.

Page 3

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 4, Exercise 3
REPORT YAPxx04_3 .
TYPES DEG TYPE P DECIMALS 2.
PARAMETERS: FAHREN TYPE DEG.
DATA CELSIUS LIKE FAHREN.
CELSIUS = 5 / 9 * ( FAHREN - 32 ).
WRITE: /10 'FAHRENHEIT', 35 ':' , 50 FAHREN.
ULINE /10(65).
WRITE: /10 'CELSIUS' , 35 ':' , CELSIUS UNDER FAHREN.

Page 4

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 5, Exercise 1
REPORT YAPxx05_1
TYPES: BEGIN OF movie,
zyear TYPE zyear,
category TYPE zcategory,
name TYPE name1,
END OF movie.
DATA: movietab TYPE STANDARD TABLE OF movie INITIAL SIZE 0,
Wa_ movietab type movie.
Data: wa_ymovie type ymovie.
WRITE: /5 'YEAR' , 20 'CATEGORY' , 30 'WINNER'.
ULINE /5(31).
SELECT * FROM ymovie into wa_ymovie.
Wa_movietab-zyear = wa_ymovie-zyear.
Wa_movietab-category = wa_ymovie-category.
Wa_movietab-name = wa_ymovie-name.
APPEND Wa_movietab to movietab .
WRITE:/5 Wa_movietab-zyear, 20 Wa_movietab-category, 30 Wa_movietab-name.
ENDSELECT.

Page 5

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 5, Exercise 2
REPORT YAPxx05_2.
Data: wa_ YMOVIE type YMOVIE.
PARAMETERS:CATEGORY LIKE wa_YMOVIE-CATEGORY DEFAULT 'PIC'.
SELECT * FROM YMOVIE into WA_YMOVIE WHERE CATEGORY = CATEGORY.
WRITE:
/5 WA_YMOVIE-YEARC,
22 WA_YMOVIE-CATEGORY,
32 WA_YMOVIE-WINNER.
ENDSELECT.
IF SY-SUBRC <> 0.
WRITE: /5 'No records exist for category', CATEGORY.
ENDIF.
* remember that this SY-SUBRC check must occur after the ENDSELECT, not
* within the SELECT...ENDSELECT loop.

Page 6

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 6, Exercise 1
REPORT YAPxx06_1.
PARAMETERS:NUM1 TYPE I,
ACTION,
NUM2 LIKE NUM1.
DATA: RESULT(7) TYPE P DECIMALS 2,
RESULT_OKAY VALUE 'Y'.
CASE ACTION.
WHEN '+'.
RESULT = NUM1 + NUM2.
WHEN '-'.
RESULT = NUM1 - NUM2.
WHEN '*'.
RESULT = NUM1 * NUM2.
WHEN '/'.
IF NUM2 = 0.
RESULT_OKAY = 'N'.
WRITE: /5 'Cannot divide by zero!'.
ELSE.
RESULT = NUM1 / NUM2.
ENDIF.
WHEN OTHERS.
RESULT_OKAY = 'N'.
WRITE: /5 ACTION, 'is an invalid operator.'.
ENDCASE.
CHECK RESULT_OKAY = 'Y'.
WRITE:

/7 NUM1,
/5 ACTION, NUM2 UNDER NUM1.
ULINE /3(20).
WRITE:
/6 RESULT.

Page 7

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 6, Exercise 2
REPORT YAPxx06_2 .
DATA: TOTALSEATS TYPE zocc.
DATA: IT_CONCERT TYPE STANDARD TABLE OF YCONCERT,
YCONCERT TYPE YCONCERT.
SELECT * FROM YCONCERT INTO TABLE IT_CONCERT ORDER BY ARTIST.
ON CHANGE OF YCONCERT-ARTIST.
IF SY-DBCNT <> 1. "total not displayed for 1st record
ULINE /50(12).
WRITE: / TOTALSEATS UNDER YCONCERT-OCCUPIED.
ENDIF.
ULINE.
WRITE: / YCONCERT-ARTIST.
TOTALSEATS = 0.
ENDON.
ADD YCONCERT-OCCUPIED TO TOTALSEATS.
WRITE:
/20 YCONCERT-CDATE,
35 YCONCERT-MAXSEATS,
50 YCONCERT-OCCUPIED,
65 YCONCERT-LOCATION.
ENDSELECT.
* need to print total for last artist
ULINE /50(12).
WRITE: / TOTALSEATS UNDER YCONCERT-OCCUPIED.

Page 8

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 7, Exercise 1
REPORT YAPXX07_1 .
TYPES: BEGIN OF ITAB_RECORD,
ARTIST TYPE ARTIST,
CDATE TYPE CDATE,
OCCUPIED TYPE OCCUPIED,
LOCATION
TYPE LOCATION,
END OF ITAB_RECORD.
* note:it is the NAME of these fields, not the "LIKE" addition,
* that is important for the MOVE-CORRESPONDING statement
DATA: CONCERT_ITAB TYPE STANDARD TABLE OF ITAB_RECORD,
WA_CONCERT TYPE ITAB_RECORD.
SELECT * FROM YCONCERT INTO TABLE CONCERT_ITAB.
SORT CONCERT_ITAB BY OCCUPIED.
IF SY-SUBRC <> 0.
WRITE: / 'No concerts exist in table.'.
ELSE.
WRITE: /6 'Attendance', 20 'Artist', 50 'Date', 65
'Location'.
ULINE.
LOOP AT CONCERT_ITAB INTO WA_CONCERT.
WRITE: /
WA_CONCERT-OCCUPIED,
20 WA_CONCERT-ARTIST,
50 WA_CONCERT-CDATE,
65 WA_CONCERT-LOCATION.
ULINE.
ENDLOOP.
ENDIF.

Page 9

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 7, Exercise 2
REPORT YAPxx07_2 .
TYPES: BEGIN OF TYPE CONCERT,
OCCUPIED TYPE OCCUPIED,
ARTIST TYPE ARTIST,
CDATE TYPE CDATE,
LOCATION TYPE LOCATION,
END OF TYPE CONCERT.
DATA: CONCERT TYPE STANDARD TABLE OF TYPE CONCERT.
DATA: SUM TYPE P.
SELECT * FROM YCONCERT INTO TABLE CONCERT WHERE OCCUPIED > 15000.
SORT CONCERT_ITAB BY OCCUPIED.
LOOP AT CONCERT INTO WA_CONCERT.
WRITE:/ WA_CONCERT-OCCUPIED, WA_CONCERT-ARTIST,WA_CONCERT-CDATE
,WA_CONCERT-LOCATION.
SUM = SUM + WA_CONCERT-OCCUPIED.
ENDLOOP.
ULINE.
WRITE SUM.

Page 10

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 7, Exercise 3
REPORT YAPxx07_3 .
TYPES: BEGIN OF YCONTEMP_TABLE,
ARTIST LIKE YCONCERT-ARTIST,
CDATE LIKE YCONCERT-CDATE,
OCCUPIED LIKE YCONCERT-OCCUPIED,
LOCATION LIKE YCONCERT-LOCATION,
END OF YCONTEMP_TABLE.
DATA: CONCERT_ITAB TYPE STANDARD TABLE OF
YCONTEMP_TABLE,
CONCERT_WAV TYPE YCONTEMP_TABLE,
TOTALOCCUPIED LIKE YCONCERT-OCCUPIED.
SELECT * FROM YCONCERT INTO CONCERT_ITAB.
IF SY-SUBRC <> 0 .
WRITE:/ 'NO CONCERT FOUND'.
ELSE.
WRITE: /6 'ATTENDANCE', 20 'ARTIST', 50 'DATE', 65 'LOCATION'.
ULINE.
SORT CONCERT_ITAB BY OCCUPIED DESCENDING.
LOOP AT CONCERT_ITAB INTO CONCERT_WAV WHERE OCCUPIED > 1000.
WRITE: /5 CONCERT_WAV-OCCUPIED, 20 CONCERT_WAV-ARTIST,
50 CONCERT_WAV-CDATE, 65 CONCERT_WAV-LOCATION.
TOTALOCCUPIED = TOTALOCCUPIED + CONCERT_WAV-OCCUPIED.
ENDLOOP.
IF SY-SUBRC = 0.
ULINE /5(12).
WRITE: / TOTALOCCUPIED UNDER CONCERT_WAV-OCCUPIED.
ELSE.
WRITE: / 'NO CONCERT ABOVE 18000 IS FOUND'.
ENDIF.

Page 11

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 8, Exercise 1
REPORT YAPxx08_1 .
PARAMETERS: YEAR TYPE YEARC.
DATA: IT_MOVIE TYPE STANDARD TABLE OF YMOVIE,
WA_MOVIE TYPE YMOVIE.
DATA: TY_T_YMOVIE TYPE STANDARD TABLE OF YMOVIE.
WRITE:
/5 'Year',
15 'Cat.',
25 'Winner'.
ULINE.
PERFORM SELECT_RECORDS USING YEAR.
*&---------------------------------------------------------------------*
*&
Form SELECT_RECORDS
*&---------------------------------------------------------------------*
FORM SELECT_RECORDS USING YEAR TYPE YEARC.
SELECT * FROM YMOVIE INTO TABLE IT_MOVIE WHERE YEARC = YEAR.
PERFORM DISPLAY_RECORD USING IT_MOVIE[].
IF SY-SUBRC <> 0.
WRITE: /5 'No entries exist for', YR.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*&
Form DISPLAY_RECORD
*&---------------------------------------------------------------------*
FORM DISPLAY_RECORD USING IT_MOVIE TYPE TY_T_YMOVIE.
LOOP AT IT_MOVIE INTO WA_MOVIE.
WRITE:
/5 WA_MOVIE-YEARC,
15 WA_MOVIE-CATEGORY,
25 WA_MOVIE-WINNER.
ENDLOOP.
ENDFORM.

Page 12

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 8, Exercise 2
REPORT YAPxx08_2 .
DATA: WA_CONCERT TYPE YCONCERT.
DATA: SALES(7) TYPE P DECIMALS 2.
WRITE:

/5
35
51
67
82

'Artist',
'Date',
'Attendance',
'Price',
'Sales'.

ULINE.
SELECT * FROM YCONCERT INTO WA_CONCERT.
CALL FUNCTION 'Y_SALES_XX'
EXPORTING
ATTENDANCE = WA_CONCERT-OCCUPIED
TICKET_PRICE
= WA_CONCERT-PRICE
DATE
= WA_CONCERT-CDATE
IMPORTING
TOTAL_SALES = SALES
EXCEPTIONS
FUTURE_DATE
=1
OTHERS
= 2.
IF SY-SUBRC = 0.
WRITE:
/5
35
50
65
75
ENDIF.

check that no exceptions were raised in function module


WA_CONCERT-ARTIST,
WA_CONCERT-CDATE,
WA_CONCERT-OCCUPIED,
WA_CONCERT-PRICE,
SALES.

ENDSELECT.
IF SY-SUBRC <> 0.
WRITE: / 'No concerts exist in table.'.
ENDIF.

Page 13

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 8, Exercise 3
The program code will be identical to the previous exercise except your function module will be
used instead of Y_SALES_XX.
The function module should have three importing parameters. One for attendance, one for ticket
price, and one for the concert date.
The function module should have one exporting parameter for the total sales calculated.
The function module should have one exception for the case when the concert has not yet
occurred.
Here is the source code for the function module:
FUNCTION Y_SALES_XX.
*---------------------------------------------------------------------*Local interface:
*
IMPORTING
*
VALUE(ATTENDANCE)
*
VALUE(TICKET_PRICE)
*
VALUE(DATE) LIKE SY-DATUM
*
EXPORTING
*
VALUE(TOTAL_SALES)
*
EXCEPTIONS
*
FUTURE_DATE
*---------------------------------------------------------------------IF DATE > SY-DATUM.
RAISE FUTURE_DATE.
ENDIF.
TOTAL_SALES = ATTENDANCE * TICKET_PRICE.
ENDFUNCTION.

Page 14

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 9, Exercise 1
REPORT YAPxx09_1 .
PARAMETERS:YEAR TYPE YMOVIE-AAYEAR,
CATEGORY TYPE YMOVIE-CATEGORY.
DATA: IT_MOVIE TYPE STANDARD TABLE OF YMOVIE,
WA_MOVIE TYPE YMOVIE.
SELECT SINGLE * FROM YMOVIE INTO WA_MOVIE
WHERE AAYEAR = YEAR
AND CATEGORY = CATEGORY.
IF SY-SUBRC <> 0.
WRITE: /5 'No entry exists for', YEAR, 'and', CATEGORY.
ELSE.
SKIP.
WRITE:
/5 'The winner for', YEAR, 'and',
CATEGORY, 'was', WA_MOVIE-WINNER.
SKIP.
WRITE:
/5 'The nominees were:',
/10 WA_MOVIE-NAME1,
/
WA_MOVIE-NAME2 UNDER WA_MOVIE-NAME1,
/
WA_MOVIE-NAME3 UNDER WA_MOVIE-NAME1,
/
WA_MOVIE-NAME4 UNDER WA_MOVIE-NAME1,
/
WA_MOVIE-NAME5 UNDER WA_MOVIE-NAME1.
ENDIF.

Page 15

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 10, Exercise 1


REPORT YAPxx10_1.
DATA: ENTRIES TYPE I,
TOTAL_ATTENDANCE TYPE I,
MAX_ATTENDANCE TYPE I,
AVG_ATTENDANCE TYPE I.
SELECT

COUNT( ARTIST )
SUM( SEATSOCC )
MAX( SEATSOCC )
AVG( SEATSOCC )
FROM YCONCERT
INTO (ENTRIES, TOTAL_ATTENDANCE, MAX_ATTENDANCE, AVG_ATTENDANCE).
IF SY-SUBRC = 0.
SKIP.
WRITE:
/5
/5
/5
/5
ELSE.
WRITE:
/5
ENDIF.

'Number of entries:', ENTRIES,


'Total attendance :', TOTAL_ATTENDANCE,
'Max. attendance :', MAX_ATTENDANCE,
'Avg. attendance :', AVG_ATTENDANCE.
'No concerts exist in table.'.

Page 16

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 10, Exercise 2


REPORT YAPxx10_2.
TYPES:

BEGIN OF MOVIE_REC,
AAYEAR
TYPE AAYEAR,
CATEGORY
TYPE CATEGORY,
WINNER
TYPE WINNER,
END OF MOVIE_REC.

DATA: WA_MOVIE TYPE MOVIE_REC.


DATA: MOVIE_CURSOR TYPE CURSOR.
OPEN CURSOR MOVIE_CURSOR FOR
SELECT AAYEAR CATEGORY WINNER FROM YMOVIE
ORDER BY CATEGORY
DESCENDING
AAYEAR
DESCENDING.
WRITE:

/5 'Year',
15 'Cat.',
25 'Winner'.

ULINE.
DO.
FETCH NEXT CURSOR MOVIE_CURSOR INTO WA_MOVIE.
IF SY-SUBRC <> 0.
CLOSE CURSOR MOVIE_CURSOR.
EXIT.
ENDIF.
WRITE:
/5 WA_MOVIE-AAYEAR,
15 WA_MOVIE-CATEGORY,
25 WA_MOVIE-WINNER.
ENDDO.

Page 17

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 10, Exercise 3


REPORT YAPxx10_3

TYPES: BEGIN OF V_RECORD,


VBELN TYPE VBELN,
ERDAT TYPE ERDAT,
ERNAM TYPE ERNAM,
ARKTX TYPE ARKTX,
PSTYV TYPE PSTYV,
END OF V_RECORD.
DATA: VSTRUC TYPE V_RECORD.
*----------------------------------------------------------------------TOP-OF-PAGE.
*----------------------------------------------------------------------FORMAT COLOR 4.
WRITE: / 'Sales Doc.',
15 'Created on',
30 'Created by',
43 'Description',
65 'Category '.
*----------------------------------------------------------------------START-OF-SELECTION.
*----------------------------------------------------------------------* Inner Join *
SELECT VBAK~VBELN VBAK~ERDAT VBAK~ERNAM VBAP~ARKTX VBAP~PSTYV
INTO VSTRUC
FROM VBAK JOIN VBAP
ON VBAK~VBELN = VBAP~VBELN
WHERE VBAK~ERNAM = 'THOMPSON'.
WRITE: / VSTRUC-VBELN, 15 VSTRUC-ERDAT, 30 VSTRUC-ERNAM,
43 VSTRUC-ARKTX, 65 VSTRUC-PSTYV.
ENDSELECT.
* Outer Join *
SELECT VBAK~VBELN VBAK~ERDAT VBAK~ERNAM VBAP~ARKTX VBAP~PSTYV
INTO VSTRUC
FROM VBAK LEFT JOIN VBAP
ON VBAK~VBELN = VBAP~VBELN
WHERE VBAK~ERNAM = 'THOMPSON'.
WRITE: / VSTRUC-VBELN, 15 VSTRUC-ERDAT, 30 VSTRUC-ERNAM,
43 VSTRUC-ARKTX, 65 VSTRUC-PSTYV.

Page 18

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

ENDSELECT.

Questions:
Which table is the driving table, and why?
The driving table is VBAK since it contain header information for sales documents. I.e., sales
document header in VBAK refers to many sales documents in VBAP.
What do you notice about you OUTER JOIN?
The outer join should have returned the same records as the inner join. This is because all of the
records in the VBAK table had records that corresponded to them in the VBAP table.

Chapter 10, Exercise 4


REPORT YAPXX10_4

PARAMETERS: YEAR(4),
MONTH(2),
DAY(2).
SELECTION-SCREEN SKIP.
PARAMETERS: AMOUNT TYPE I.
DATA: COUNTER TYPE I,
ERNAM TYPE ERNAM,
DATE_HOLD(8) TYPE C.
IF YEAR IS INITIAL. YEAR = '____'. ENDIF.
IF MONTH IS INITIAL. MONTH = '__'. ENDIF.
IF DAY IS INITIAL. DAY = '__'.
ENDIF.
CONCATENATE YEAR MONTH DAY INTO DATE_HOLD.
SELECT COUNT( * ) ERNAM
INTO (COUNTER, ERNAM) FROM VBAK

Page 19

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

WHERE ERDAT LIKE DATE_HOLD


GROUP BY ERNAM
HAVING COUNT( * ) >= AMOUNT.
WRITE: / ERNAM, COUNTER.
ENDSELECT.

Chapter 10, Exercise 5


REPORT YAPXX10_5.
DATA: WA_SCARR TYPE SCARR,
WA_SFLIGHT TYPE SFLIGHT.
SELECT * FROM SCARR INTO WA_SCARR
WHERE NOT CARRID IN
( SELECT CARRID FROM SFLIGHT ).
WRITE:/ WA_SCARR-CARRID, WA_SCARR-CARRNAME.
ENDSELECT.

Question:
How is your subquery related to the inner join found in chapter 11?
Your subquery will return the non-corresponding records that were not returned by the
inner join statement in the chapter 11.

Page 20

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 12, Exercise 1


REPORT YAPXX12_1 .
PARAMETERS: ACTIVITY(2) TYPE N.
SKIP.
AUTHORITY-CHECK OBJECT 'F_LFA1_APP'
ID 'ACTVT'
FIELD ACTIVITY
ID 'APPKZ'
DUMMY.
IF SY-SUBRC <> 0. failed authority check
WRITE:
/5 'You do NOT have the authority for activity', ACTIVITY NO-GAP, '!!!'.
ELSE.
WRITE: /5 'You have the authority for activity', ACTIVITY.
ENDIF.

Page 21

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 13, Exercise 1


REPORT YAPXX13_1 .
DATA: WA_LFA1 TYPE LFA1,
WA_LFB1 TYPE LFB1.
WRITE: / 'VENDOR INFORMATION'.
SELECT * FROM LFA1 INTO WA_LFA1 WHERE LIFNR BETWEEN 'VEND012' AND
'VEND100'.
ULINE.
WRITE: /5 'Vendor:', WA_LFA1-LIFNR, 30 Name:, WA_LFA1-NAME1.
SELECT * FROM LFB1 INTO WA_LFB1 WHERE LIFNR = WA_LFA1-LIFNR.
WRITE: /10 'Company Code:', WA_LFB1-BUKRS.
ENDSELECT.
ENDSELECT.
ULINE.
SKIP.
WRITE: /5 'Total vendors processed:', SY-DBCNT LEFT-JUSTIFIED.

Page 22

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 13, Exercise 2


REPORT YAPXX13_2 .
* this program uses the logical database KDF
NODES: LFA1, LFB1.
DATA: VENDOR_COUNT TYPE I.
*--begin of START-OF-SELECTION event
START-OF-SELECTION.
WRITE: / 'VENDOR INFORMATION'.
*--end of START-OF-SELECTION event
*--begin of GET LFA1 event
GET LFA1.
ULINE.
WRITE: /5 'Vendor:', LFA1-LIFNR, 30 Name:, LFA1-NAME1.
ADD 1 TO VENDOR_COUNT.
*--end of GET LFA1 event
*--begin of GET LFB1 event
GET LFB1.
WRITE: /10 'Company Code:', LFB1-BUKRS.
*--end of GET LFB1 event
*--begin of END-OF-SELECTION event
END-OF-SELECTION.
ULINE.
WRITE: /5 'Total vendors processed:', VENDOR_COUNT LEFT-JUSTIFIED.
*--end of END-OF-SELECTION event

Page 23

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 13, Exercise 3


REPORT YAPXX13_3 .
* this program uses the logical database KDF
NODES: LFA1, LFB1.
DATA: VENDOR_COUNT TYPE I,
COMPANY_COUNT TYPE I.
*--begin of START-OF-SELECTION event
START-OF-SELECTION.
WRITE: / 'VENDOR INFORMATION'.
*--end of START-OF-SELECTION event
*--begin of GET LFA1 event
GET LFA1.
ULINE.
WRITE: /5 'Vendor:', LFA1-LIFNR, 30 Name:, LFA1-NAME1.
ADD 1 TO VENDOR_COUNT.
COMPANY_COUNT = 0.
*--end of GET LFA1 event
*--begin of GET LFB1 event
GET LFB1.
WRITE: /10 'Company Code:', 24 LFB1-BUKRS.
ADD 1 TO COMPANY_COUNT.
*--end of GET LFB1 event
* continued on next page

Page 24

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

*--begin of GET LFA1 LATE event


GET LFA1 LATE.
ULINE /22(8).
WRITE:
/10 'Total Codes:',
25 COMPANY_COUNT LEFT-JUSTIFIED.
*--end of GET LFA1 LATE event
*--begin of END-OF-SELECTION event
END-OF-SELECTION.
ULINE.
SKIP.
WRITE: /5 'Total vendors processed:', VENDOR_COUNT LEFT-JUSTIFIED.
*--end of END-OF-SELECTION event

Page 25

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 13, Exercise 4


REPORT YAPXX13_4 .
* this program uses the logical database KDF
NODES: LFA1, LFB1, BSIK.
DATA: VENDOR_COUNT TYPE I,
TOTAL_PER_COMPANY LIKE BSIK-DMBTR,
TOTAL_PER_VENDOR LIKE BSIK-DMBTR.
CONSTANTS: OPEN_INVOICES LIKE BSIK-BSCHL VALUE '31'.
*--begin of START-OF-SELECTION event
START-OF-SELECTION.
WRITE: / 'VENDOR INFORMATION'.
*--end of START-OF-SELECTION event
*--begin of GET LFA1 event
GET LFA1.
ULINE.
WRITE: /5 'Vendor:', LFA1-LIFNR, 30 'Name:', LFA1-NAME1.
ADD 1 TO VENDOR_COUNT.
TOTAL_PER_VENDOR = 0.
*--end of GET LFA1 event
*--begin of GET LFB1 event
GET LFB1.
SKIP.
WRITE: /10 'Company Code:', 24 LFB1-BUKRS.
TOTAL_PER_COMPANY = 0.
*--end of GET LFB1 event
* continued on next page

Page 26

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

*--begin of GET BSIK event


GET BSIK.
CHECK BSIK-BSCHL = OPEN_INVOICES.
WRITE:
/15 'Year:', BSIK-GJAHR,
30 'Doc. #:', BSIK-BELNR,
55 'Amount:', BSIK-DMBTR.
ADD BSIK-DMBTR TO TOTAL_PER_VENDOR.
ADD BSIK-DMBTR TO TOTAL_PER_COMPANY.
*--end of GET BSIK event
*--begin of GET LFB1 LATE event
GET LFB1 LATE.
CHECK TOTAL_PER_COMPANY > 0.
SKIP.
WRITE:
/39 'Total Amount for Company:',
63 TOTAL_PER_COMPANY.
*--end of GET LFB1 LATE event
*--begin of GET LFA1 LATE event
GET LFA1 LATE.
SKIP.
WRITE:
/5 'Total Amount for Vendor:',
TOTAL_PER_VENDOR LEFT-JUSTIFIED.
*--end of GET LFA1 LATE event
*--begin of END-OF-SELECTION event
END-OF-SELECTION.
ULINE.
SKIP.
WRITE: /5 'Total vendors processed:', VENDOR_COUNT LEFT-JUSTIFIED.
*--end of END-OF-SELECTION event

Page 27

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 14, Exercise 1


REPORT YAPXX14_1 . new lines of code are in bold
* this program uses the logical database KDF
NODES: LFA1, LFB1, BSIK.
SELECT-OPTIONS: AMOUNT FOR BSIK-DMBTR.
PARAMETERS: NAME(20) LOWER CASE.
DATA: VENDOR_COUNT TYPE I,
TOTAL_PER_COMPANY LIKE BSIK-DMBTR,
TOTAL_PER_VENDOR LIKE BSIK-DMBTR.
CONSTANTS: OPEN_INVOICES LIKE BSIK-BSCHL VALUE '31'.
*--begin of INITIALIZATION event
INITIALIZATION.
NAME = '<Your Name>'.
*--end of INITIALIZATION event
*--begin of START-OF-SELECTION event
START-OF-SELECTION.
WRITE: / 'VENDOR INFORMATION Report by:', NAME.
*--end of START-OF-SELECTION event
*--begin of GET LFA1 event
GET LFA1.
ULINE.
WRITE: /5 'Vendor:', LFA1-LIFNR, 30 'Name:', LFA1-NAME1.
ADD 1 TO VENDOR_COUNT.
TOTAL_PER_VENDOR = 0.
*--end of GET LFA1 event
* continued on next page

Page 28

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

*--begin of GET LFB1 event


GET LFB1.
SKIP.
WRITE:
/10 'Company Code:', 24 LFB1-BUKRS.
TOTAL_PER_COMPANY = 0.
*--end of GET LFB1 event
*--begin of GET BSIK event
GET BSIK.
CHECK BSIK-BSCHL = OPEN_INVOICES. "only display open invoices
CHECK AMOUNT. make sure amount of invoice is in selection table values
WRITE:
/15 'Year:', BSIK-GJAHR,
30 'Doc. #:', BSIK-BELNR,
55 'Amount:', BSIK-DMBTR.
ADD BSIK-DMBTR TO TOTAL_PER_VENDOR.
ADD BSIK-DMBTR TO TOTAL_PER_COMPANY.
*--end of GET BSIK event
*--begin of GET LFB1 LATE event
GET LFB1 LATE.
CHECK TOTAL_PER_COMPANY > 0.
SKIP.
WRITE:
/39 'Total Amount for Company:',
63 TOTAL_PER_COMPANY.
*--end of GET LFB1 LATE event
*--begin of GET LFA1 LATE event
GET LFA1 LATE.
SKIP.
WRITE:
/5 'Total Amount for Vendor:',
TOTAL_PER_VENDOR LEFT-JUSTIFIED.
*--end of GET LFA1 LATE event
*--begin of END-OF-SELECTION event
END-OF-SELECTION.
ULINE.
SKIP.
WRITE: /5 'Total vendors processed:', VENDOR_COUNT LEFT-JUSTIFIED.
*--end of END-OF-SELECTION event

Page 29

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 14, Exercise 2


REPORT YAPXX14_2 MESSAGE-ID ZA.
DATA: WA_MOVIE TYPE YMOVIE.
PARAMETERS:YEAR TYPE AAYEAR,
CATEGORY
TYPE CATEGORY.
SELECT SINGLE * FROM YMOVIE INTO WA_MOVIE
WHERE AAYEAR = YEAR AND CATEGORY = CATEGORY.
IF SY-SUBRC <> 0.
MESSAGE I030 WITH YEAR CATEGORY.
ELSE.
SKIP.
WRITE:
/5 'The winner for', YEAR, 'and', CATEGORY, 'was', WA_MOVIE-WINNER.
SKIP.
WRITE:
/5 'The nominees were:',
/10 WA_MOVIE-NAME1,
/
WA_MOVIE-NAME2 UNDER WA_MOVIE-NAME1,
/
WA_MOVIE-NAME3 UNDER WA_MOVIE-NAME1,
/
WA_MOVIE-NAME4 UNDER WA_MOVIE-NAME1,
/ WA_MOVIE-NAME5 UNDER WA_MOVIE-NAME1.
CLEAR WA_MOVIE.
ENDIF.

Chapter 15, Exercise 1


Page 30

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

REPORT YAPXX15_1

SAP Development ABAP Training

DATA: WA_VBAK TYPE VBAK,


WA_VBAP TYPE VBAP.
DATA: V_VBELN TYPE VBELN.
FIELD-GROUPS: HEADER, GENERAL, DETAIL.
SELECT-OPTIONS: SALES_NO FOR V_VBELN.
INSERT: WA_VBAK-VBELN INTO HEADER,
WA_VBAK-KUNNR
WA_VBAK-ERDAT INTO GENERAL,
WA_VBAP-POSNR
WA_VBAP-MATNR INTO DETAIL.
SELECT * FROM VBAK INTO WA_VBAK WHERE VBELN IN SALES_NO.
EXTRACT HEADER.
EXTRACT GENERAL.
SELECT * FROM VBAP INTO WA_VBAP WHERE VBELN = WA_VBAK-VBELN.
EXTRACT DETAIL.
ENDSELECT.
ENDSELECT.
SORT.
LOOP.
AT NEW WA_VBAK-VBELN.
WRITE: / 'doc no:', WA_VBAK-VBELN,
20 'Customer No:', WA_VBAK-KUNNR,
40 'Creation Date:', WA_VBAK-ERDAT.
ENDAT.
AT DETAIL.
WRITE: / 'doc no:', WA_VBAK-VBELN,
20 'item no:', WA_VBAP-POSNR,
40 'Material:', WA_VBAP-MATNR.
ENDAT.
AT END OF WA_VBAK-VBELN.
SKIP.
ENDAT.
Page 31

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

ENDLOOP.

Page 32

Dec-2008

ECC 6.0
Exercise Solutions: Introduction to
ABAP Programming

SAP Development ABAP Training

Chapter 16, Exercise 1


Program Name:

YAPxx16_1

Syntax Problems
Extra spaces in INTO clause of SELECT statement. Open and close parentheses should not
have spaces after and before them.
WHERE clause of SELECT statement not written properly. Should be WHERE CATEGORY
= CATEGORY.
Logical Errors
SY-SUBRC check should be for not being zero. Change = to <> or NE.

Exercise 2
Program Name:

YAPxx16_2

Syntax Problems
Change comma to period before the ENDSELECT.
Logical Errors
YCONT_WA contains values for internal table, but YCONT is being appended. Either
append from the separate work area, or dont use it at all.
Initial size should be 5, not 0. Otherwise the APPEND SORTED BY clause loses all the
incoming records.
Writing out the table work area while looping at the internal table.

Exercise 3
Program Name:

YAPxx16_3

Syntax Problems
Import parameter TICKET_PRICE should be of type P.
Logical Errors
ATTENDANCE parameter under EXPORTING is misspelled.
IMPORTING statement commented out.
Logic to do totaling buried in the IF statement under the logic to detect FUTURE_DATE.

Page 33

Dec-2008

S-ar putea să vă placă și