working on all events

Upload: raj-rajesh

Post on 14-Apr-2018

217 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 Working on All Events

    1/13

    Native and Open SQL (Lesson - 4)

    The goal of this tutorial is not to teach you SQL or database concepts but to introduce you to the SQL

    diversity in ABAP

    In ABAP/4 programming language, there are two types of SQL being used.

    1. NATIVE SQL

    2. OPEN SQL.

    Open SQL allows you to access the database tables declared in the ABAP dictionary regardless of the

    database platform that the R/3 system is using.

    Native SQL allows you to use database-specific SQL statements in an ABAP/4 program. This means that

    you can use database tables that are not administered by ABAP dictionary, and therefore integrate data

    that is not part of the R/3 system.

    Open SQL consists of a set of ABAP statements that perform operations on the central database in the

    R/3 system. The results of the operations and any error messages are independent of the database

    system in use. Open SQL thus provides a uniform syntax and semantics for all of the database systemssupported by SAP. ABAP programs that only use Open SQL statements will work in any R/3 system,

    regardless of the database system in use. Open SQL statements can only work with database tables that

    have been been created in the ABAP dictionary.

    BASIC OPEN SQL COMMANDS

    SELECT

    INSERT

    UPDATE

    MODIFY

    DELETE

    OPEN CURSOR, FETCH, CLOSE CURSORExample

    TABLES SBOOK.

    DATA C TYPE CURSOR,

    WA LIKE SBOOK.

    OPEN CURSOR C FOR SELECT * FROM SBOOK WHERE CARRID = LH

    AND CONNID = 0400

    AND FLDATE = 19950228

    ORDER BY PRIMARY KEY.

    DO.

    FETCH NEXT CURSOR C INTO WA.

    IF SY-SUBRC 0.

  • 7/27/2019 Working on All Events

    2/13

    CLOSE CURSOR C.

    EXIT.

    ENDIF.

    WRITE: / WA-BOOKID, WA-CUSTOMID, WA-CUSTTYPE,

    WA-SMOKER, WA-LUGGWEIGHT, WA-WUNIT,

    WA-INVOICE.

    ENDDO.

    Output the passenger list for the Lufthansa flight 0400 on 28-02.1995:

    OPEN SQL RETURN CODES

    All Open SQL statements fill the following two system fields with return codes.SY-SUBRC

    After every Open SQL statement, the system field SY-SUBRC contains the value 0 if the operation was

    successful, a value other than 0 if not.

    SY-DBCNT

    After an Open SQL statement, the system field SY-DBCNT contains the number of database lines

    processed.

    NATIVE SQL

    As already mentioned, Native SQL allows you to use database-specific SQL statements in an ABAP

    program.

    To use Native SQL statement, you must precede it with the EXEC SQL statement, and follow it with the

    ENDEXEC statement.

    Syntax

    EXEC SQL [PERFORMING ].

    ENDEXEC.

    There is no period after Native SQL statements. Furthermore, using inverted commas () or an asterisk (*)

    at the beginning of a line in a native SQL statement does not introduce a comment as it would in normal

    ABAP syntax. You need to know whether table and field names are case-sensitive in your chosen

    database.

    In Native SQL statements, the data is transported between the database table and the ABAP program

    using host variables. These are declared in the ABAP program, and preceded in the Native SQL

    statement by a colon (:). You can use elementary structures as host variables. Exceptionally, structures in

    an INTO clause are treated as though all of their fields were listed individually.

    As in Open SQL, after the ENDEXEC statement, SY-DBCNT contains the number of lines processed. In

    nearly all cases, SY-SUBRC contains the value 0 after the ENDEXEC statement.

  • 7/27/2019 Working on All Events

    3/13

    OPEN SQL PERFORMANCE RULES

    To improve the performance of the SQL and in turn of the ABAP program, one should take care of the

    following rules-

    Keep the Result Set Small

    Using the where clause

    If only one record is required from the database, use SELECT SINGLE whenever possible .

    Minimize the Amount of Data Transferred

    Restrict the number of lines

    If only certain fields are required from a table, use the SELECT INTO statement

    Restrict no of columns

    Use aggregate functions

    Minimize the Number of Data Transfers

    Avoid nested select loops

    An alternative option is to use the SELECT .. FOR ALL ENTRIES statement. This statement canoften be a lot more efficient than performing a large number of SELECT or SELECT SINGLE statementsduring a LOOP of an internal table.

    Use dictionary views

    Use Joins in the FROM clause

    Use subqueries in the where clause

    Minimize the Search Overhead

    Use index fields in the where clause

    When accessing databases, always ensure that the correct index is being used .

    Reduce the Database Load

    Buffering

    Logical databases Avoid repeated database access

    Using Internal Tables to Buffer Records

    To avoid executing the same SELECT multiple times (and therefore have duplicate selects), aninternal table of type HASHED can be used to improve performance.

    Report using all the events:

    REPORT ZKA_REPORT MESSAGE-ID ZKA LINE-SIZE 255 LINE-COUNT 10(2).

    TABLES: ZKA_EMP,ZKA_COM.

    DATA: ITAB LIKE ZKA_EMP OCCURS 0 WITH HEADER LINE.

    DATA: JTAB LIKE ZKA_COM OCCURS 0 WITH HEADER LINE.

    SELECT-OPTIONS: EMP_NO FOR ZKA_EMP-EMPNO.

  • 7/27/2019 Working on All Events

    4/13

    SET PF-STATUS 'ZKA_MENU'.

    INITIALIZATION.

    EMP_NO-LOW = '1'.

    EMP_NO-HIGH = '10'.

    EMP_NO-SIGN = 'I'.

    EMP_NO-OPTION = 'BT'.

    APPEND EMP_NO.

    CLEAR EMP_NO.

    AT SELECTION-SCREEN.

    IF EMP_NO-LOW < '1'. MESSAGE S000(ZKA). ELSEIF EMP_NO-HIGH > '10'.

    MESSAGE S001.

    ENDIF.

    START-OF-SELECTION.

    SELECT * FROM ZKA_EMP INTO TABLE ITAB WHERE EMPNO IN EMP_NO.

    LOOP AT ITAB.

    WRITE:/5 SY-VLINE,6 ITAB-EMPNO,17 SY-VLINE,18 ITAB-EMPNAME,28 SY-VLINE,

    29 ITAB-EMPPHONE,39 SY-VLINE.

    HIDE: ITAB-EMPNO.

    ENDLOOP.

    WRITE:/5 SY-ULINE(35).

    TOP-OF-PAGE.

    WRITE:/5 SY-ULINE(35).

    WRITE:/5 SY-VLINE,6 'EMPNO',17 SY-VLINE,18 'EMPNAME',28 SY-VLINE,

    29 'EMPPHONE',39 SY-VLINE.

  • 7/27/2019 Working on All Events

    5/13

    WRITE:/5 SY-ULINE(35).

    END-OF-PAGE.

    WRITE:/5 SY-ULINE(35).

    WRITE:/ 'THE PAGE NO IS',SY-PAGNO.

    END-OF-SELECTION.

    WRITE:/ 'THE RECORD IS CLOSED'.

    AT LINE-SELECTION.

    IF SY-LSIND = 1.

    SELECT * FROM ZKA_EMP INTO TABLE ITAB WHERE EMPNO = ITAB-EMPNO.

    LOOP AT ITAB.

    WRITE:/5 SY-VLINE,6 ITAB-EMPNO,17 SY-VLINE,18 ITAB-EMPNAME,28 SY-VLINE,

    29 ITAB-EMPPHONE,39 SY-VLINE.

    ENDLOOP.

    WRITE:/5 SY-ULINE(35).

    ELSEIF SY-LSIND = 2.

    SELECT * FROM ZKA_COM INTO TABLE JTAB WHERE EMPNO = ITAB-EMPNO.

    LOOP AT JTAB.

    WRITE:/5 SY-VLINE,6 JTAB-COMNO,17 SY-VLINE,18 JTAB-COMNAME,28 SY-VLINE,

    29 JTAB-COMPHONE,39 SY-VLINE.

    ENDLOOP.

    WRITE:/5 SY-ULINE(35).

    ENDIF.

    AT PF7.

    IF SY-LSIND = 1.

    SELECT * FROM ZKA_EMP INTO TABLE ITAB WHERE EMPNO = ITAB-EMPNO.

  • 7/27/2019 Working on All Events

    6/13

    LOOP AT ITAB.

    WRITE:/5 SY-VLINE,6 ITAB-EMPNO,17 SY-VLINE,18 ITAB-EMPNAME,28 SY-VLINE,

    29 ITAB-EMPPHONE,39 SY-VLINE.

    ENDLOOP.

    WRITE:/5 SY-ULINE(35).

    ELSEIF SY-LSIND = 2.

    SELECT * FROM ZKA_COM INTO TABLE JTAB WHERE EMPNO = ITAB-EMPNO.

    LOOP AT JTAB.

    WRITE:/5 SY-VLINE,6 JTAB-COMNO,17 SY-VLINE,18 JTAB-COMNAME,28 SY-VLINE,

    29 JTAB-COMPHONE,39 SY-VLINE.

    ENDLOOP.

    WRITE:/5 SY-ULINE(35).

    ENDIF.

    AT USER-COMMAND.

    IF SY-UCOMM = '0001'.

    IF SY-LSIND = 1.

    SELECT * FROM ZKA_EMP INTO TABLE ITAB WHERE EMPNO = ITAB-EMPNO.

    LOOP AT ITAB.

    WRITE:/5 SY-VLINE,6 ITAB-EMPNO,17 SY-VLINE,18 ITAB-EMPNAME,28 SY-VLINE,

    29 ITAB-EMPPHONE,39 SY-VLINE.

    ENDLOOP.

    WRITE:/5 SY-ULINE(35).

    ELSEIF SY-LSIND = 2.

    SELECT * FROM ZKA_COM INTO TABLE JTAB WHERE EMPNO = ITAB-EMPNO.

    LOOP AT JTAB.

  • 7/27/2019 Working on All Events

    7/13

    WRITE:/5 SY-VLINE,6 JTAB-COMNO,17 SY-VLINE,18 JTAB-COMNAME,28 SY-VLINE,

    29 JTAB-COMPHONE,39 SY-VLINE.

    ENDLOOP.

    WRITE:/5 SY-ULINE(35).

    ENDIF.

    ENDIF.

    Output is Given Below :

    Performance Tools

    http://2.bp.blogspot.com/__aUD213pS6o/SCx1pVAqq6I/AAAAAAAAAbE/d3mHQ5IbwQY/s1600-h/sap1.JPG
  • 7/27/2019 Working on All Events

    8/13

    SQL TraceSQL Trace

    SQL trace(ST05) provides the developer with the ability to analyse database select statements. Simply

    execute ST05to turn on SQL trace, then execute the statement/program you want to analyse. Now turn off SQL traceusing ST05and click on list trace to view the details.

    You can also perform traces on other items such as authorisation objects.

    Runtime Analysis*To turn runtim analysis on within ABAP code insert the following codeSET RUN TIME ANALYZER ON.

    *To turn runtim analysis off within ABAP code insert the following code

    SET RUN TIME ANALYZER OFF.

    Performance Tuning using Parallel cursor

    ************************************************************************* Performance Tuning using parallel cursor*

    * Extracts from program ZFAL2002************************************************************************

    ************************************************************************* START-OF-SELECTIONSELECT *INTO TABLE I_KEPH FROM KEPHWHERE KADKY

  • 7/27/2019 Working on All Events

    9/13

    *----------------------------------------------------------------------*FORM GET_COST_VALUES.* Determine start position and then process all records for given key* from that starting point* i_keph is sorted on kalnr kalka bwvar kadky.READ TABLE I_KEPH WITH KEY KALNR = W_KEKO-KALNRKALKA = W_KEKO-KALKABWVAR = W_KEKO-BWVARKADKY = W_KEKO-KADKY BINARY SEARCH.IF SY-SUBRC = 0.* Loop at itab from first record found (sy-tabix) until record* no-longer matches your criteria.LOOP AT I_KEPH FROM SY-TABIX.IF I_KEPH-KALNR = W_KEKO-KALNR AND I_KEPH-KALKA = W_KEKO-KALKAAND I_KEPH-BWVAR = W_KEKO-BWVAR AND I_KEPH-KADKY = W_KEKO-KADKY.* Key matchD_MAT_COST = D_MAT_COST + I_KEPH-KST001.D_LAB_COST = D_LAB_COST + I_KEPH-KST004.

    D_OVER_HEAD = D_OVER_HEAD + I_KEPH-KST010.D_EXT_PURCH = D_EXT_PURCH + I_KEPH-KST014.D_MISC_COST = D_MISC_COST + I_KEPH-KST002 + I_KEPH-KST003+ I_KEPH-KST005 + I_KEPH-KST006 + I_KEPH-KST007+ I_KEPH-KST008 + I_KEPH-KST009 + I_KEPH-KST011+ I_KEPH-KST012 + I_KEPH-KST013 + I_KEPH-KST015+ I_KEPH-KST016 + I_KEPH-KST017 + I_KEPH-KST018+ I_KEPH-KST019 + I_KEPH-KST020 + I_KEPH-KST021+ I_KEPH-KST022 + I_KEPH-KST023 + I_KEPH-KST024+ I_KEPH-KST025 + I_KEPH-KST026 + I_KEPH-KST027+ I_KEPH-KST028 + I_KEPH-KST029 + I_KEPH-KST030+ I_KEPH-KST031 + I_KEPH-KST032 + I_KEPH-KST033+ I_KEPH-KST034 + I_KEPH-KST035 + I_KEPH-KST036

    + I_KEPH-KST037 + I_KEPH-KST038 + I_KEPH-KST039+ I_KEPH-KST040.ELSE.* Key greater - can't be lessEXIT. " Exit loopENDIF.ENDLOOP.ENDIF.

    D_MAT_COST = D_MAT_COST / W_KEKO-LOSGR.D_LAB_COST = D_LAB_COST / W_KEKO-LOSGR.D_OVER_HEAD = D_OVER_HEAD / W_KEKO-LOSGR.D_EXT_PURCH = D_EXT_PURCH / W_KEKO-LOSGR.

    D_MISC_COST = D_MISC_COST / W_KEKO-LOSGR.ENDFORM. " GET_COST_VALUES

    Performance tuning using GROUPBY************************************************************************* Performance tuning using GROUPBY*

    http://abap.sdn-sap.com/2013/01/performance-tuning-using-groupby.htmlhttp://abap.sdn-sap.com/2013/01/performance-tuning-using-groupby.html
  • 7/27/2019 Working on All Events

    10/13

    * Extracts from program ZFAL2002************************************************************************

    ************************************************************************* START-OF-SELECTION

    SELECT KALNR KALKA BWVAR MEEHT SUM( MENGE )INTO TABLE I_CKIS FROM CKISWHERE KADKY

  • 7/27/2019 Working on All Events

    11/13

    1. ABAP/4 programs can take a very long time to execute , and can make other

    processes have to wait before executing. Here are some tips to speed up your programs and

    reduce the load your programs put on the system:

    2. Use the GET RUN TIME command to help evaluate performance . It's hard to know

    whether that optimization technique REALLY helps unless you test it out. Using this tool can help

    you know what is effective, under what kinds of conditions. The GET RUN TIME has problemsunder multiple CPUs, so you should use it to test small pieces of your program, rather than the

    whole program.

    3.

    Generally, try to reduce I/O first, then memory, then CPU activity. I/O operations that read/write to

    hard disk are always the most expensive operations. Memory, if not controlled, may have to be

    written to swap space on the hard disk, which therefore increases your I/O read/writes to disk.

    CPU activity can be reduced by careful program design, and by using commands such as SUM

    (SQL) and COLLECT (ABAP/4).

    4. Avoid 'SELECT *', especially in tables that have a lot of fields. Use SELECT A B C INTO

    instead, so that fields are only read if they are used. This can make a very big difference.

    Field-groups can be useful for multi-level sorting and displaying. However, they write their data tothe system's paging space, rather than to memory (internal tables use memory). For this reason,

    field-groups are only appropriate for processing large lists (e.g. over 50,000 records). If you have

    large lists, you should work with the systems administrator to decide the maximum amount of

    RAM your program should use, and from that, calculate how much space your lists will use. Then

    you can decide whether to write the data to memory or swap space. See the Fieldgroups ABAP

    example.

    5. Use as many table keys as possible in the WHERE part of your select statements.

    Whenever possible, design the program to access a relatively constant number of records (for

    instance, if you only access the transactions for one month, then there probably will be a

    reasonable range, like 1200-1800, for the number of transactions inputted within that month).

    Then use a SELECT A B C INTO TABLE ITAB statement.6. Get a good idea of how many records you will be accessing. Log into your productive

    system, and use SE80 -> Dictionary Objects (press Edit), enter the table name you want to see,

    and press Display. Go To Utilities -> Table Contents to query the table contents and see the

    number of records. This is extremely useful in optimizing a program's memory allocation.

    7. Try to make the user interface such that the program gradually unfolds more information

    to the user, rather than giving a huge list of information all at once to the user.

    8. Declare your internal tables using OCCURS NUM_RECS, where NUM_RECS is the

    number of records you expect to be accessing. If the number of records exceeds NUM_RECS,

    the data will be kept in swap space (not memory).

    9. Use SELECT A B C INTO TABLE ITAB whenever possible. This will read all of the

    records into the itab in one operation, rather than repeated operations that result from a SELECT

    A B C INTO ITAB... ENDSELECT statement. Make sure that ITAB is declared with OCCURS

    NUM_RECS, where NUM_RECS is the number of records you expect to access.

    10. If the number of records you are reading is constantly growing, you may be able to break

    it into chunks of relatively constant size. For instance, if you have to read all records from 1991 to

    present, you can break it into quarters, and read all records one quarter at a time. This will reduce

    I/O operations. Test extensively with GET RUN TIME when using this method.

    http://www.geocities.com/SiliconValley/Grid/4858/sap/ABAPCode/Fieldgroups.htmhttp://www.geocities.com/SiliconValley/Grid/4858/sap/ABAPCode/Fieldgroups.htmhttp://www.geocities.com/SiliconValley/Grid/4858/sap/ABAPCode/Fieldgroups.htmhttp://www.geocities.com/SiliconValley/Grid/4858/sap/ABAPCode/Fieldgroups.htmhttp://www.geocities.com/SiliconValley/Grid/4858/sap/ABAPCode/Fieldgroups.htmhttp://www.geocities.com/SiliconValley/Grid/4858/sap/ABAPCode/Fieldgroups.htm
  • 7/27/2019 Working on All Events

    12/13

    11. Know how to use the 'collect' command. It can be very efficient.

    12. Use the SELECT SINGLE command whenever possible.

    13. Many tables contain totals fields (such as monthly expense totals). Use these avoid

    wasting resources by calculating a total that has already been calculated and stored.

    Workload analysis ( ST03 ) is used to

    determine system performance

    You should check statistics and record trends to get a feel for the systems behavior and

    performance. Understanding the system when it is running well helps you determine what

    changes may need to be made when it is running poorly.

    1. In the Command field, enter transaction ST03 and choose Enter (or from the SAP standard menu,choose Tools -Administration --Monitor - Performance - Workload -ST03-Analysis).2. Choose Data base server or This application server. (In this example, we chose Thisapplication server, pal101003_SAS_00.)

    3. Select a time period to analyze. (Inthis example, we chose Last minute load.)

    4. Enter how many minutes back to analyze, or choose Other selection to specify a date and time periodto analyze. In this example, we chose Other selection.

    5. Under Time interval to be analyzed is, enter the Date and time range to be analyzed.

    6. Choose

    7. Check the Current value under Task types (for example, Total).The task types are:Note : Judgment must be applied when reviewing statistical values. If you just started the R/3System, the buffers will be empty and many of the statistics will be unfavorable. Once thebuffers are loaded, values can be properly evaluated. In this example, the Av. response time of almost 4

    http://abap.sdn-sap.com/2013/01/workload-analysis-st03-is-used-to.htmlhttp://abap.sdn-sap.com/2013/01/workload-analysis-st03-is-used-to.htmlhttp://3.bp.blogspot.com/__aUD213pS6o/Rs3EMHqbLoI/AAAAAAAAAMA/VvLKsaQm3-s/s1600-h/sap.JPGhttp://abap.sdn-sap.com/2013/01/workload-analysis-st03-is-used-to.htmlhttp://abap.sdn-sap.com/2013/01/workload-analysis-st03-is-used-to.html
  • 7/27/2019 Working on All Events

    13/13

    seconds must be evaluated with otherfactors in mind.Note : The R/3 user default for a decimal point is a comma. If your default profile for decimal point, (pointor comma) is not appropriately set, the display may be misread. For example, rather than 3,888 ms, itwould read 3.888 ms. Quite a difference!

    11. Click on any cell in the Response time avg column.

    12. Choose .

    Analysis of transaction ST03 is covered in BC315 (the Workload Analysis and Tuning class).We recommend you take this class.

    13. The programs and transactions are now sorted in average response time order.

    A few standard functional transactions will exceed the one-second guideline. They include,but are not limited to the following:

    Type Transaction

    Create Sales Order VA01Change Sales Order VA02Display Sales Order VA03Create Billing Document VF01Create Delivery VL01Maintain Master HR data PA30