Home > SAP Tips > ABAP/Java developer tips > Use field symbols for better performance in internal tables
SAP Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

ABAP/JAVA DEVELOPER TIPS

Use field symbols for better performance in internal tables


James Vander Heyden
08.20.2003
Rating: -4.15- (out of 5) Hall of fame tip of the month winner


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


Do you want to squeeze more performance out of your Internal Table processing?The new ABAP object extensions in SAP require internal table definitions to look a little different. With this new style of definition comes many options for better performance like hash tables and sorted tables. The use of field symbols takes this performance concept one step further.

The new style of definition of internal tables usually doesn't include a header area for the internal table. Furthermore, ABAP Objects will not allow internal tables with header lines. For added performance, instead of declaring a work area--use a field symbol. The work area concept and header line format both require data to be moved from the internal table to the work area or header area. A field symbol is just a pointer that will point to the proper line of the itab. With field symbols, no data needs to be moved. The field symbol just stores the proper memory address to point at the right line. The field symbol can have structure and be made up of fields similar to a work area or header line. Because no data needs to be copied, processing is much faster.

The keys to the usage are:
1) Defining the table records and the field symbol in a similar type.
* just an ordinary standard table
TYPES: BEGIN OF it_vbak_line,
vbeln LIKE vbak-vbeln,
vkorg LIKE vbak-vkorg,
vtweg LIKE vbak-vtweg,
spart LIKE vbak-spart,
kunnr LIKE vbak-kunnr,
END OF it_vbak_line.

DATA: it_vbak TYPE TABLE OF it_vbak_line.
FIELD-SYMBOLS: <it_vbak_line> TYPE it_vbak_line.

* or as a screaming fast hash table for keyed reads
TYPES: BEGIN OF it_vbpa_line,
vbeln LIKE vbak-vbeln,
kunnr LIKE vbak-kunnr,
END OF it_vbpa_line.

DATA: it_vbpa TYPE HASHED TABLE OF it_vbpa_line
WITH UNIQUE KEY vbeln.
FIELD-SYMBOLS: <it_vbpa_line> TYPE it_vbpa_line.

2) In ITAB processing, utilize the ASSIGNING command.
* loop example
LOOP AT it_vbak ASSIGNING <it_vbak_line>.
* look at records--populate it_zpartner
* read example
READ TABLE it_vbpa ASSIGNING <it_vbpa_line>
WITH TABLE KEY vbeln = <it_vbak_line>-vbeln.

3) Refer to the field symbol's fields in the loop or after the read.
wa_zpartner-vkorg = <it_vbak_line>-vkorg.
wa_zpartner-vtweg = <it_vbak_line>-vtweg.
wa_zpartner-spart = <it_vbak_line>-spart.
wa_zpartner-kunag = <it_vbak_line>-kunnr.
wa_zpartner-kunwe = <it_vbpa_line>-kunnr.

See the code example below for further detail. The code was written in R/3 4.6C and should work for all 4.x versions.

Code

REPORT z_cnv_zshipto_from_hist          NO STANDARD PAGE HEADING
                                        LINE-SIZE 132
                                        LINE-COUNT 65
                                        MESSAGE-ID z1.

************************************************************************
* Program Name: ZSHIPTO from History             Creation: 07/23/2003  *
*                                                                      *
* SAP Name    : Z_CNV_ZSHIPTO_FROM_HIST           Application: SD      *
*                                                                      *
* Author      : James Vander Heyden               Type: 1              *
*______________________________________________________________________*
* Description :  This program reads tables VBAK & VBPA and populates   *
* ZPARTNER. ZPARTNER table is read in by ZINTSC06. ZINTSC06 updates    *
* the ZSHIPTO relationships.                                           *
*______________________________________________________________________*
* Inputs:  Selection Screen                                            *
*                                                                      *
* Outputs:                                                             *
*______________________________________________________________________*
* External Routines                                                    *
*   Function Modules:                                                  *
*   Transactions    :                                                  *
*   Programs        :                                                  *
*______________________________________________________________________*
* Return Codes:                                                        *
*                                                                      *
*______________________________________________________________________*
* Ammendments:                                                         *
*    Programmer        Date     Req. #            Action               *
* ================  ==========  ======  ===============================*
* J Vander Heyden   07/23/2003  PCR     Initial Build                  *
************************************************************************
*----------------------------------------------------------------------*
* DATA DICTIONARY TABLES                                               *
*----------------------------------------------------------------------*
TABLES:
  zpartner.

*----------------------------------------------------------------------*
* SELECTION SCREEN LAYOUT                                              *
*----------------------------------------------------------------------*
PARAMETERS: p_load   RADIOBUTTON GROUP a1 DEFAULT 'X',
            p_deltab  RADIOBUTTON GROUP a1.

SELECTION-SCREEN SKIP 2.

SELECT-OPTIONS: s_vkorg  FOR zpartner-vkorg MEMORY ID vko OBLIGATORY
                             NO INTERVALS NO-EXTENSION,
                s_vtweg  FOR zpartner-vtweg MEMORY ID vtw OBLIGATORY
                             NO INTERVALS NO-EXTENSION,
                s_spart  FOR zpartner-spart MEMORY ID spa OBLIGATORY
                             NO INTERVALS NO-EXTENSION.

*----------------------------------------------------------------------*
* INTERNAL TABLES                                                      *
*----------------------------------------------------------------------*

TYPES: BEGIN OF it_vbak_line,
        vbeln LIKE vbak-vbeln,
        vkorg LIKE vbak-vkorg,
        vtweg LIKE vbak-vtweg,
        spart LIKE vbak-spart,
        kunnr LIKE vbak-kunnr,
      END OF it_vbak_line.

DATA: it_vbak TYPE TABLE OF it_vbak_line.
FIELD-SYMBOLS: <it_vbak_line>  TYPE it_vbak_line.

TYPES: BEGIN OF it_vbpa_line,
        vbeln LIKE vbak-vbeln,
        kunnr LIKE vbak-kunnr,
      END OF it_vbpa_line.

DATA: it_vbpa TYPE HASHED TABLE OF it_vbpa_line
                   WITH UNIQUE KEY vbeln.
FIELD-SYMBOLS: <it_vbpa_line>  TYPE it_vbpa_line.

DATA: it_zpartner TYPE TABLE OF zpartner.
DATA: wa_zpartner TYPE zpartner.

*----------------------------------------------------------------------*
* STRUCTURES                                                           *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* VARIABLES                                                            *
*----------------------------------------------------------------------*

DATA:  z_mode   VALUE 'N',
       z_commit TYPE i,
       z_good   TYPE i,
       z_bad    TYPE i.

*----------------------------------------------------------------------*
* CONSTANTS                                                            *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* SELECTION-SCREEN HELP                                                *
*----------------------------------------------------------------------*

*----------------------------------------------------------------------*
* INITIALIZATION                                                       *
*----------------------------------------------------------------------*
INITIALIZATION.
*----------------------------------------------------------------------*
* AT LINE SELECTION                                                    *
*----------------------------------------------------------------------*
AT LINE-SELECTION.
*----------------------------------------------------------------------*
* AT SELECTION SCREEN                                                  *
*----------------------------------------------------------------------*
AT SELECTION-SCREEN.
*----------------------------------------------------------------------*
* START-OF-SELECTION                                                   *
*----------------------------------------------------------------------*
START-OF-SELECTION.
  IF p_load = 'X'.
    PERFORM select_records.
    PERFORM process_itab.
  ELSE.
    PERFORM delete_table.
  ENDIF.
*----------------------------------------------------------------------*
* END-OF-SELECTION                                                     *
*----------------------------------------------------------------------*
END-OF-SELECTION.
**********************************************************************
**********************************************************************
*&---------------------------------------------------------------------*
*&      Form  DELETE_TABLE
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM delete_table.

  DATA:  w_answer.
  CLEAR: w_answer.
  CALL FUNCTION 'POPUP_TO_CONFIRM'
       EXPORTING
            titlebar              = text-002
*         DIAGNOSE_OBJECT       = ' '
            text_question         = text-003
            text_button_1         = text-004
            icon_button_1         = 'ICON_OKAY'
            text_button_2         = text-005
            icon_button_2         = 'ICON_CANCEL'
            default_button        = '2'
            display_cancel_button = ''
*         USERDEFINED_F1_HELP   = ' '
*         START_COLUMN          = 25
*         START_ROW             = 6
*         POPUP_TYPE            =
      IMPORTING
           answer                = w_answer.
*    TABLES
*         PARAMETER             =
*    EXCEPTIONS
*         TEXT_NOT_FOUND        = 1
*         OTHERS                = 2
  .
  IF w_answer = 1.
    DELETE FROM zpartner
   WHERE vkorg IN s_vkorg
     AND vtweg IN s_vtweg
     AND spart IN s_spart.
    MESSAGE i398(00) WITH 'Records deleted from table: '
                          sy-dbcnt
                          ' '
                          ' '.
  ENDIF.


ENDFORM.                    " DELETE_TABLE
*&---------------------------------------------------------------------*
*&      Form  SELECT_RECORDS
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
FORM select_records.

* get vbaks
  CLEAR: it_vbak.
  SELECT vbeln vkorg vtweg spart kunnr
    FROM vbak
    INTO CORRESPONDING FIELDS OF TABLE it_vbak
   WHERE vkorg IN s_vkorg
     AND vtweg IN s_vtweg
     AND spart IN s_spart
     AND auart = 'TA'.

* get vbpa we's
  CLEAR: it_vbpa.
  SELECT *
    FROM vbpa
    INTO CORRESPONDING FIELDS OF TABLE it_vbpa
    FOR ALL ENTRIES IN it_vbak
   WHERE vbeln = it_vbak-vbeln
     AND posnr = '000000'
     AND parvw = 'WE'.

ENDFORM.                    " SELECT_RECORDS

*&---------------------------------------------------------------------*
*&      Form  PROCESS_ITAB
*&---------------------------------------------------------------------*
*  attempt post goods issue for all entries.
*----------------------------------------------------------------------*
FORM process_itab.

  LOOP AT it_vbak ASSIGNING <it_vbak_line>.
* look at records--populate it_zpartner

    READ TABLE it_vbpa ASSIGNING <it_vbpa_line>
                WITH TABLE KEY  vbeln   = <it_vbak_line>-vbeln.

    IF sy-subrc = 0.
      CLEAR: wa_zpartner.
      wa_zpartner-mandt = sy-mandt.
      wa_zpartner-vkorg = <it_vbak_line>-vkorg.
      wa_zpartner-vtweg = <it_vbak_line>-vtweg.
      wa_zpartner-spart = <it_vbak_line>-spart.
      wa_zpartner-kunag = <it_vbak_line>-kunnr.
      wa_zpartner-kunwe = <it_vbpa_line>-kunnr.
      APPEND wa_zpartner TO it_zpartner.
    ENDIF.
*
  ENDLOOP.

  SORT it_zpartner BY mandt vkorg vtweg spart kunag kunwe..
  DELETE ADJACENT DUPLICATES FROM it_zpartner.
* do a mass table update.
  INSERT zpartner FROM TABLE it_zpartner.
  IF sy-subrc = 0.
    MESSAGE s398(00) WITH 'Inserted records: ' sy-dbcnt.
  ENDIF.

ENDFORM.                    " PROCESS_ITAB

Rate this Tip
To rate tips, you must be a member of SearchSAP.com.
Register now to start rating these tips. Log in if you are already a member.


Submit a Tip




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
ABAP/Java developer tips
Select Text fields: Case-insensitive
Finding BADIs you can use
Is this the quickest way to find a BADI?
ABAP Objects in SAP Workflow to provide improved performance
Easily debug error messages in SAP processes
Accessing private attributes in ABAP Objects
Find a BADI in a minute
Top 10 SAP tips of 2007
How to transport an SAP query in R/3 4.6x
How to switch off message determination in BAPI_PO_CREATE1

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.

HomeNewsTopicsBlogsTipsAsk the ExpertsMultimediaWhite PapersProducts
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2000 - 2008, TechTarget | Read our Privacy Policy
SearchSAP.com is a search service provided by TechTarget and is completely
independent of and not affiliated with SAP AG.
  TechTarget - The IT Media ROI Experts