63
Day 2 Reports

2 Reports

Embed Size (px)

DESCRIPTION

abap

Citation preview

  • Day 2

    Reports

  • Basic Functions of the ABAP Editor

  • DATA DefinitionsDATA StatementDATA TYPE or LIKE VALUE DECIMALS

    - All variables used within the ABAP/4 program must be declared with DATA statements- up to 30 characters in length, containing any characters other than (, ), +, ., : Indicates the variable type

    Example:DATA: p_bukrs LIKE bkpf-bukrs.DATA i_val TYPE i VALUE 99.

  • DATA DefinitionsTYPES Statement TYPES TYPE or LIKE DECIMALSSAP allows the creation of new user defined data types. And this does not create a variable, BUT just a new type that can be used in creating a variable. Example : TYPES : cc LIKE bkpf-bukrs DATA : c_cc TYPE cc.

  • TYPESField String Type TYPES: BEGIN OF ... END OF .ExampleTYPES: flight(25) TYPE C.

    TYPES: BEGIN OF flightrec1_type, flag TYPE C, carrid LIKE spfli_carrid, name TYPE flight, sum_field TYPE sum_field_type, END OF flightrec1_type.

    TYPES: f_type TYPE flightrec1_type.

  • Selection screen ElementsParameters cannot have data type F. The data type F is not supported in the Selection ScreenTo suppress the display use NO-DISPLAY option PARAMTER P_TELNO NO-DISPLAY.To make a parameter a required input field, the OBLIGATORY option of the PARAMETERS statement is used.

    REPORT ztraining.PARAMETERS: value TYPE i DEFAULT 100, name LIKE sy-uname DEFAULT sy-uname , date LIKE sy-datum DEFAULT sy-datum.

  • Selection screenTo define a checkbox for parameter input, the option AS CHECKBOX of the PARAMETERS statement is used.Syntax PARAMETERS ...... AS CHECKBOX.To define groups of radio buttons for parameter input, the RADIOBUTTON GROUP option of the PARAMETERS statement is used. Syntax PARAMETERS ...... RADIOBUTTON GROUP .Example

    PARAMETERS: yes AS CHECKBOX, no AS CHECKBOX DEFAULT 'X'

  • Program SelectionsSELECT-OPTIONS StatementSELECT-OPTIONS FOR NO EXTENSION OBLIGATORY LOWER CASE

    SELECT-OPTIONS allows specification of multiple values and ranges. This can only be declared for fields within tables defined in the TABLES statement.Example SELECT-OPTIONS: s_kunnr FOR kna1-kunnr.

  • Specifying Blank LinesTo produce blank lines, the SKIP option is used. Syntax SELECTION-SCREEN SKIP [].To underline a line or part of a line, the ULINE option is used.Syntax SELECTION-SCREEN ULINE [[/]]To write text on the selection screen, the COMMENT option is usedSyntax SELECTION-SCREEN COMMENT [/] [FOR FIELD ]

  • Elements on a Single LineTo position a set of parameters or comments on a single line on the selection screen, the elements are declared in a block enclosed by the following two statements:SELECTION-SCREEN BEGIN OF LINE.SELECTION-SCREEN END OF LINE.

    ExampleSELECTION-SCREEN BEGIN OF LINE.SELECTION-SCREEN COMMENT 1(10) text-001. Text Symbol for TitlePARAMETERS: p1(3), p2(5), p3(1).SELECTION-SCREEN END OF LINE.

  • Positioning in the Selection ScreenTo position the next parameter or comment on the selection screen, the POSITION option is used. Syntax SELECTION-SCREEN POSITION .For , you can specify a number, POS_LOW, or POS_HIGH.To create a logical block of elements on the selection screen, mark the beginning of the block with the BEGIN OF BLOCK option of the SELECTION-SCREEN statement, then define the individual elements and mark the end of the block with the END OF BLOCK option as shown below:SELECTION-SCREEN BEGIN OF BLOCK [WITH FRAME [TITLE ]][NO INTERVALS].SELECTION-SCREEN END OF BLOCK .Blocks can be nested.

  • Blocking Selection ScreenExample SELECTION-SCREEN BEGIN OF BLOCK rad1 WITH FRAME TITLE text-002.PARAMETERS vendor RADIOBUTTON GROUP gr1.PARAMETERS customer RADIOBUTTON GROUP gr1.PARAMETERS material RADIOBUTTON GROUP gr1.SELECTION-SCREEN END OF BLOCK rad1.

  • Data DefinitionsInternal TablesDATA : BEGIN OF OCCURS x, (variable definitions) END OF . Internal Tables are defined as an extension of a structure, with the addition of an OCCURS clause. Internal Tables can be created with or without header lines.

    Example 1 (with header line)DATA : BEGIN OF t_wrk OCCURS 0 WITH HEADER LINE, t_kunnr LIKE kna1-kunnr, sw TYPE C, END OF t_wrk.

  • Data DefinitionsExample 2 (without header line)DATA : BEGIN OF t_wrk OCCURS 0, t_kunnr LIKE kna1-kunnr, sw TYPE c, END OF t_wrk.

    Internal Table records are added by a number of statements, including INSERT & APPEND. Only one line can be referenced at a time in the program via the header line. Lines to be referenced must be loaded into the header line via statements such as READ and LOOP.

  • Data DefinitionsYou can Include another structure into Internal table.

    DATA : BEGIN OF t_tab1, field1 LIKE bkpf-belnr, field2 LIKE bseg-buzei, END OF T_TAB1.

    DATA : BEGIN OF t_tab2 OCCURS 10. INCLUDE STRUCTURE t_tab1.DATA : END OF t_tab2.

    In this example, t_tab2 will contain the fields field1 & field2.

  • Program Level StatementsCLEAR StatementCLEAR .Initializes the var1 to Zero

    REFRESH StatementREFRESH .Deletes and Initializes the var1 to Zero

    This has differences only in Internal Tables with header line and without header line.

    CLEAR will initialize the header line, if the Internal table is with header line otherwise it is same as REFRESH.

  • Data DefinitionsAppending Internal TableDATA : BEGIN OF t_tab1 OCCURS 0, field1 TYPE C, field2 TYPE C, END OF t_tab1.t_tab1-field1 = A.t_tab1-field2 = B.APPEND t_tab1.CLEAR t_tab1.

    t_tab1-field1 = C.t_tab1-field2 = D.APPEND t_tab1.CLEAR t_tab1.

  • Internal TablesModifying Internal Table MODIFY t_tab1-field1 = Y.t_tab1-field2 = Z.

    MODIFY t_tab1 INDEX 1. This will modify the Internal Table of first row. MODIFY t_tab1. This will modify the entire Internal Table. MODIFY t_tab1 INDEX sy-index. This will modify the Internal Table where the current index pointer is pointing.

  • Internal TablesDeleting Internal Table DELETE DELETE t_tab1 INDEX 1. This will delete the Internal Table of first row. DELETE t_tab1 FROM 1 TO 4. This will deletes from 1 to 4 lines in Internal Table. DELETE t_tab1 INDEX sy-index. This will delete the Internal Table where the current index pointer is pointing. DELETE t_tab1 WHERE t_tab1-field1 = C. This will delete the satisfied records of the above condition.

  • Internal TablesReading Internal TableREAD TABLE INDEX . READ TABLE INTO INDEX . READ TABLE WITH KEY [BINARY SEARCH]ExampleREAD TABLE itab INDEX 1.This will read the first line of Internal TableREAD TABLE itab INTO wrk_tab INDEX sy-tabix.This will copy the line into another work area where current index is pointer is pointingREAD TABLE itab WITH KEY lifnr eq V001 BINARY SEARCH This will read the internal table with specified key in binary search mode

  • Internal TablesThe SORT Statement

    SORT BY .. []

    The internal table is sorted by its standard key if the BY option isnot used. If BY option is used, it will be sorted by the order of the fields , ,.. By default, it will sort by ASCENDING, To sort in the descending order we have to specify as DESCENDING.

  • ReportsCreating an ABAP/4 ProgramAny customer-developed program should begin with Y or Z as a first character.A statement is a sequence of words that ends with a period . .A word in a statement always begins with an ABAP/4 keyword. A literal is enclosed by single quotation marks* at the first column denotes the entire line is commented can be inserted at any place in line, after this double quotes, everything will be treated as comments.Transaction Code : SE38.Menu Path: Tools>ABAP Workbench>ABAP Editor

  • Types of programsType 1 run on its own Can be started it in the R/3 system without a transaction codeCan be executed in background

    Type M ( Module pool)Program cannot run on its own and can be called via a transaction code

  • Types of programsType I ( Include program )Contains the program code that can be used by different programsIt modularizes the source code which consists of several different, logically related partsReadability is improved and thus easy maintenance

  • Sample ProgramREPORT ZFIRSPRG.WRITE This is the First Sample ABAP Program.WRITE / This is in Second Line..WRITE: / This is in Third Line., I am also in Third Line.

    / - Line feed: - Chain declaration.

  • REPORT StatementREPORT StatementLINE-SIZE - Specifies, in columns, the width of the list to be displayed.LINE-COUNT - Specifies the no. of lines per pageMESSAGE-ID - Allows the use of the Message statement without explicitly specifying the message id.NO STANDARD PAGE HEADING - Builds a header for the list displayed from your report by default.

    Example :REPORT ZTEST LINE-SIZE 250 LINE-COUNT 65.WRITE / Width of the Line Statement.

  • WRITE StatementWRITE Output Format specificationBeing with a / to indicate a new lineWRITE 9 means on the current line, begin in column 9WRITE /03(5) means begin a new line, begin in column 3, for a length of 5 Can be a data variable, text literal, or numbered text Specify a number of formatting options like NO-ZERO, NO-SIGN, CURRENCY w, DECIMALS d, ROUND r, DD/MM/YYYY, NO-GAPExampleWrite / p_text NO-GAP

  • ReportsREPORT ZTEST NO STANDARD PAGE HEADINGWRITE 1,2,3 NO-GAP.WRITE : / 3 Column 1, 15 Column 2, 25(7) ------, 35 Column 3.SKIP.WRITE End of Line.

    SKIP - Will leave a blank line.

  • Events in ABAPABAP is a event driven language. The different events in an ABAP report areInitialisationAt Selection ScreenAt Selection Screen OutputStart of SelectionEnd of SelectionTop of PageEnd of Page

    At Line SelectionAt User Command

  • Program Level EventsINITIALIZATION.This event is triggered prior to the first display of the selection screen.Example INITIALIZATION.CLEAR s_blart.s_blart-sign = I.s_blart-option = EQ.s_blart-low = WA.APPEND s_blart.CLEAR s_blart.

  • Initialisation

  • Program Level EventsAT SELECTION-SCREENThis event is processed after user presses Enter on the selection screen. Its main purpose is to verify user input prior to program execution. Used during validating the data entered by the user

    AT SELECTION-SCREEN OUTPUTThis event is processed before display of the selection screen.ExampleAT SELECTION SCREEN.IF p_wrbtr < 1. MESSAGE e999 WITH Greater than 1. ENDIF.

  • Program Level EventsSTART-OF-SELECTION.This event begins the main processing of the program. The event is triggered upon the user pressing Execute on the selection screen. Note : An implicit START-OF-SELECTION is defined by the REPORT statement. Any code placed between the REPORT statement and the first event declaration is executed during the START-OF-SELECTION event.

    END-OF-SELECTIONThis event is triggered following the execution of the last statement in the START-OF-SELECTION. Note : STOP Statement causes an automatic branch to END-OF-SELECTION.

  • Program Level EventsTOP-OF-PAGE.This event is triggered by the first WRITE statement to display data on a new page; it is not triggered by the NEW-PAGE statement, but the first WRITE statement following a NEW-PAGE.

    END-OF-PAGE.This event is triggered as soon as the LINE-COUNT reached. NEW-PAGE statement causes this event to be ignored.

  • Program Level EventAT LINE-SELECTION.This event is activated from the displayed list when the user selects Choose or double-clicks on a line.

    AT USER-COMMAND.This event gets activated when the user executes a function defined within the menu for the displayed list.

  • Sample Report

    Sample Report

    REPORT ZSAMPLE NO STANDARD PAGE HEADING

    LINE-SIZE 133

    LINE-COUNT 65.

    *---------------------------------------------------------------------------------*

    * This program is used to explain the various events in *

    * a report. This program is intended only for training purpose *

    *----------------------------------------------------------------------------------*

    Tables: MARA.

    data: p_bukrs like bkpf-bukrs.

    data : int_mara like MARA occurs 10 with header line.

    WRITE :/ 'All Report Events'.

    SELECTION-SCREEN BEGIN OF BLOCK b1 with frame title text-001.

    SELECT-OPTIONS: smatnr FOR mara-matnr.

    SELECTION-SCREEN END OF BLOCK b1.

    *=====================================

    * Initializing the Report values

    *=====================================

    INITIALIZATION.

    smatnr-sign = 'I'.

    smatnr-option = 'EQ'.

    smatnr-low = 1001.

    APPEND smatnr.

    smatnr-sign = 'I'.

    smatnr-option = 'EQ'.

    smatnr-low = 1002.

    APPEND smatnr.

    smatnr-sign = 'I'.

    smatnr-option = 'EQ'.

    smatnr-high = 1003.

    APPEND smatnr.

    *======================================

    * Selection screen output - format of screen

    *======================================

    AT SELECTION-SCREEN OUTPUT.

    LOOP at screen.

    if screen-name = '%_smatnr_%_APP_%-TEXT'.

    screen-intensified = '1'.

    modify screen.

    endif.

    ENDLOOP.

    *============================================

    * At selction screen - side events on screen

    *============================================

    AT SELECTION-SCREEN on value-request for smatnr-LOW.

    FORM get_file_name.

    CALL FUNCTION 'F4_FILENAME'

    EXPORTING

    PROGRAM_NAME = 'ZALL_EVENTS'

    DYNPRO_NUMBER = '1000'

    FIELD_NAME =

    IMPORTING

    FILE_NAME = fnam.

    ENDFORM. " get_file_name

    *============================================

    * Start of Selection

    *============================================

    START-OF-SELECTION.

    select * from MARA into table int_MARA.

    and other statements

    *============================================

    * Top of page

    *============================================

    TOP-OF-PAGE.

    Write:/ This is top of page event.

  • Events in ABAP Runtime Environment

  • Control StatementsIFELSEENDIFCASE.ENDCASELOOP.ENDLOOPDO..ENDDOWHILE.ENDWHILE

  • IF StatementOption 3 :IF .

    ELSEIF .

    .....ELSE.

    ENDIF.Option 1 :IF

    ENDIF Option 2 :IF .

    ELSE.

    ENDIF.

  • IF IF is one of the following :F1 F2 Any logical or relational operators can be used.F1 BETWEEN F2 AND F3Field F1 is checked for the value between F2 and F3 F1 IS INITIALField F1 is Initial I.e. Value is equals ZeroF1 IN is an internal table of Select-Options.NOT ( F1 IS INITIAL)Field F1 is not Initial I.e. Value is not equals Zero

  • OPERANDS= , EQ Equal to., >, GTGreater than.=, =>, GEGreater than or equal to
  • CASE . WHEN . WHEN . ...... WHEN OTHERS. ENDCASE.

    CASE Statement

  • CASE..Example DATA: txt1 VALUE 'X', txt2 VALUE 'Y', txt3 VALUE 'Z', strng VALUE 'A'. CASE strng. WHEN text1. WRITE: / 'String is', txt1. WHEN text2. WRITE: / String is, txt2. WHEN text3. WRITE: / String is, txt3. WHEN OTHERS. WRITE: / String is not, txt1, txt2, txt3. ENDCASE.The output appears as follows: String is not X Y Z

  • LOOP AT FROM TO WHERE ENDLOOP. Internal Table within the program If specified, the LOOP begins with record number n1. If specified, the LOOP ends with record number n2.WHERE Comparison to be performed before processing the statements.

    System fields : sy-index, sy-tabix.LOOP Statement

  • LOOPNested Loops are also possibleLOOP AT ITAB1. LOOP AT ITAB2. . ENDLOOP. ENDLOOP.

    Within the loop, the statements CHECK and EXIT can also be used; afailed CHECK statement skips the processing of the current record andreturns to the top of the LOOP. EXIT resumes processing with thestatement immediately following the ENDLOOP.

  • Control Breaks in LoopFour forms of the AT statement exist for processing within a LOOP.

    1. AT FIRST ENDAT for Statements to be executed before any records are processed.

    ExampleLOOP AT itab. AT FIRST.WRITE : SY-ULINE. ENDAT..ENDLOOP.

  • Control Breaks2. AT LAST ENDAT for Statements to be executed after all records are processed. For both AT FIRST and AT LAST, all fields in the header line of the internal table will contain an *

    ExampleLOOP AT itab.AT LAST.WRITE : SY-ULINE.ENDAT.ENDLOOP.

  • Control Breaks3. AT NEW ENDAT for Statements to be executed at the beginning of a group of records containing the same value for . All fields in the internal table header line defined AFTER will contain an *

    Example AT NEW I_LIFNR.WRITE : SY-ULINEENDAT.

  • Control Breaks4. AT END OF . Statements to be executed at the end of a group of records containing the same value for . All fields in the internal table header line defined AFTER will contain an *.

    ExampleAT END OF I_LIFNR. SUM. WRITE : SY-ULINE.ENDAT.

    Note : AT NEW and AT END OF only make sense for a sorted table.

  • DO [ TIMES] [VARYING FROM NEXT ]. ENDDO.

    Do LoopExample

    DO 2 TIMES. WRITE SY-INDEX. SKIP. DO 3 TIMES. WRITE SY-INDEX. ENDDO. SKIP.ENDDO.

    Output11 2 321 2 3

  • WHILE [VARY FROM NEXT ].ENDWHILE.ExampleDATA: length TYPE I VALUE 0, strl TYPE I VALUE 0, string(30) TYPE C VALUE 'Test String'.strl = STRLEN( string ).WHILE string NE SPACE. WRITE string(1). length = sy-index. SHIFT string.ENDWHILE.WRITE: / 'STRLEN: ', strl.WRITE: / 'Length of string:', length.WHILE LoopOutput

    T e s t S t r i n gSTRLEN: 11Length of string: 11

  • LOOPTo terminate the processing of a loop, one of the following keywords is used.CONTINUE -Terminating the current Loop Pass UnconditionallyCHECK- Terminating the current Loop Pass Conditionally EXIT -Terminating a Loop Entirely

  • Other eventsNEW-PAGE.This will trigger a new page to be displayed. This can be used to skip from the current page.

  • Data RetrievalSELECT * FROM WHERE ENDSELECT.

    is table defined within the TABLES statement.WHERE clause identifies which records to retrieve.

    Note : SY-SUBRC, 0 If records are retrieved, 4 if none are found. SY-DBCNT, Number of database records retrieved.

  • Data RetrievalSELECT * FROM INTO . must be defined in your program, and be at least as wide as the record length of the . In this case, the record is read into the and not into the database table buffer.

    SELECT * FROM INTO TABLE must be defined as above. The contents of the internal table are replaced with the records retrieved. This form does not require an ENDSELECT, as no loop processing is performed.

    SELECT * FROM APPENDING TABLE same as INTO TABLE, but the records are added to the end of , leaving the original contents.

  • Data RetrievalSELECT * FROM ORDER BY specifies a sort order for records retrieved. By default is ascending order; if the field should be in descending order use DESCENDING

    SELECT * FROM CLIENT SPECIFIED For client-dependent tables, the first field is always the client or MANDT. This does not normally need to be specified; SAP will automatically only retrieve records from the client from which the report is being executed.

  • Data RetrievalSELECT SINGLE * FROM WHERE This statement retrieves one and only one record from the database. This form does not require an ENDSELECT, as no loop processing is performed.

    ABAP/4 is more like SQL. In addition to SELECT * form, individual fields or columns can be selected. In this case, the INTO clause must be specified: SELECT belnr blart INTO t_bkpf-belnr t_bkpf-blart FROM bkpf .

    The addition INTO CORRESPONDING FIELDS OF can also be used. Also, the GROUP BY clause has also been added, with the following aggregate functions available :MIN, MAX, AVG, SUM, COUNT

    Finally, the table name in the FROM clause can now be a variable; thusSELECT * FROM (var1)

  • JoinsJoins are more efficient than logical database and nested selects.

    They access multiple tables with one select statement.

  • Inner JoinsInner Joins allow access to multiple tables with a single select statement by creating a temporary table based on the conditions in the ON Statement. Multiple tables are joined based on the key fields specified by the join condition.

    Inner Joins are equivalent to views created in the Dictionary.

    Syntax for inner join SELECT scarr~carrname sflight~carrid sflight~connid sflight~fldate INTO (carrname,carrid,connid, date) FROM scarr INNER JOIN sflight ON scarr~carrid = sflight~carrid. WRITE:/ carrname,carrid,connid,date. ENDSELECT.

  • Inner Joins Syntax.SELECT into () FROM INNER JOIN ON = AND = AND WHERE.ENDSELECT.

  • Left Outer JoinsLike inner joins, left outer joins create a temporary table based on the conditions specified in the ON clause

    Unlike inner joins: - Based on conditions expressed in the ON statement, fields in the driving (left-hand) table that do not correspond to fields in the right-hand table are still added to temporary table.There they are populated with initial values

    SELECT into () FROM LEFT OUTER JOIN ON = AND = AND WHERE.ENDSELECT.

  • Joins Accessing More than Two TablesSELECT into () FROM ( INNER JOIN ON = AND = AND) INNER JOIN ON = AND WHERE.ENDSELECT.

  • System Variables - ListThe system fields used in interactive reporting

    SY-CUROW - Cursor position (line)SY-CUCOL - Cursor position (column)SY-CPAGE - Number of the current pageSY-STACO - First displayed column of the list on displaySY-STARO - First displayed line of the list on displaySY-LSIND - Index of the displayed list levelSY-LISTI - Index of the selected list levelSY-LILLI - Number of the selected line SY-LISEL - Contents of the selected line

    The ABAP program editor provides many functions through the use of graphical push-buttons. Among these functions are: -Toggle from display mode to edit mode -Syntax check with limited auto correction -Activation activates the program. -Execution- Executes the program -Where used list- to find the tables, statements, data elements used in the programs. -Find and repeat find. -ABAP help