Difference Between Internal Table and a Work Area

What is the difference between internal table and a work area and how do they work?

Internal table is a temporary two dimensional memory structure similar to database table.  We can store multiple records in the internal table and also using record pointers we can do the activities such as reading, appending, deleting, modifying etc.

Whereas work area is a variable declared with the TYPE of an internal table or a database table.  It can store only one record at a time.  It is like a structure declaration in C.  You can refer individual columns in the work area with the names.

If you declare an internal table with "WITH HEADER LINE" clause the internal table itself acts as a work area.  For example, your ITAB is a work area and ITAB[] is the internal table.

If you are familiar with ORACLE PLSQL, work area is similar to %ROW_TYPE and internal is similar to TABLE TYPE.

A work area is nothing more than a structure which serves as a header line for an internal table.  When you read an internal table (without header line) the line which you read must be moved somewhere, it is moved into a work area. 

While adding or retrieving records to / from internal table we have to keep the record temporarily. 

The area where this record is kept is called as work area for the internal table. The area must have the same structure as that of internal table. An internal table consists of a body and an optional header line. 

Header line is a implicit work area for the internal table. It depends on how the internal table is declared that the itab will have the header line or not. 

Work areas are used for processing data from n into internal table. as work area is a structure which can hold only one record at a time i.e to be inserted into internal table populated from internal table.

Work areas generally used for reusability purpose.  i.e a internal table with header line is only used for storing data from a single data base table where as internal table without header line can be used in other internal table. for this purpose we'll create a explicit work area for processing data.

e.g. 
data: begin of itab occurs 10,
ab type c,
cd type i,
end of itab. " this table will have the header line. 

data: wa_itab like itab. " explicit work area for itab

data: itab1 like itab occurs 10. " table is without header line. 

The header line is a field string with the same structure as a row of the body, but it can only hold a single row.  It is a buffer used to hold each record before it is added or each record as it is retrieved from the internal table. It is the default work area for the internal table.

Sample code:

TYPES : BEGIN OF ty_test,
        code TYPE i,
        name(10) TYPE c,
        amount TYPE p DECIMALS 2,
       END OF ty_test.
 
DATA : it_test TYPE STANDARD TABLE OF ty_test WITH HEADER LINE INITIAL SIZE 10.
 
DATA : wa TYPE ty_test,
       chk1 TYPE c,
       fldname(30), fldval(50).
 
*set pf-status 'PF01'.
*set titlebar 'PF01'.
*
 
INITIALIZATION.
  it_test-code = 300.
  it_test-name = 'Ramesh'.
  it_test-amount = 5500.
  APPEND it_test.
 
  wa-code = 207.
  wa-name = 'Prem'.
  wa-amount = 5000.
  APPEND wa TO it_test.
 
  it_test-code = 117.
  it_test-name = 'James Bond'.
  it_test-amount = 9900.
  INSERT it_test INDEX 3.
 
  it_test-code = 217.
  it_test-name = 'Sivaraman'.
  it_test-amount = 9900.
  INSERT it_test INDEX 3.
 
  it_test-code = 201.
  it_test-name = 'Saravanan'.
  it_test-amount = 1000.
  APPEND it_test.
 
  it_test-code = 210.
  it_test-name = 'Shanmugam'.
  it_test-amount = 6000.
  APPEND it_test.
 
  WRITE : / 'Loop Display ( Appended rows ) :-'.
  LOOP AT it_test.
    WRITE : / chk1 AS CHECKBOX,
    sy-tabix, sy-vline, it_test-code, it_test-name, it_test-amount.
    HIDE : it_test-code, it_test-name.
  ENDLOOP.
  SKIP.
 
END-OF-SELECTION.
  CLEAR : it_test-code, it_test-name.
  WRITE : / 'this from end of selection'.
 
 
*&--------------------------------------------------------------------*
*&      Form  DISP1
*&--------------------------------------------------------------------*
*       text
*---------------------------------------------------------------------*
FORM disp1.
  WINDOW STARTING AT 15 10
         ENDING AT 80 15.
  DO.
    CLEAR chk1.
    READ LINE sy-index FIELD VALUE chk1.
    IF sy-subrc NE 0.
      EXIT.
    ELSE.
      CHECK chk1 NE space.
      WRITE : / it_test-code, it_test-name.
      MODIFY CURRENT LINE :
        FIELD VALUE chk1 FROM ' '
        FIELD FORMAT chk1 INPUT OFF.
    ENDIF.
  ENDDO.
ENDFORM.                                              
     "DISP1
 
***line double click ****
AT LINE-SELECTION.
  CHECK sy-lsind = 1.
  WINDOW STARTING AT 5 4
         ENDING AT 85 20.
  WRITE: /  'THE USER DOUBLE-CLICKED A LINE IN THE REPORT'.
  WRITE: /  sy-lisel.
  WRITE : / 'Sometime ',it_test-name, ' is good '.
  WRITE : / 'Sometime ',it_test-name, ' is bad  '.
  WRITE : / 'Sometime ',it_test-name, ' is rich '.
  WRITE : / 'Sometime ',it_test-name, ' is poor '.
  WRITE : / 'Who knows, who is ',it_test-name, ' ? '.
  WRITE : /, / 'we can also use this in SELECT statement'.
  CLEAR : it_test-code, it_test-name.
 
        .
IF SY-SUBRC <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
ENDIF.
 
  ULINE.
  SKIP.
  SKIP.
  WRITE : / 'Below from Get Cursor Field...'.
  GET CURSOR FIELD fldname VALUE fldval.
  CONDENSE fldname.
  CONDENSE fldval.
  WRITE : / 'You have clicked ', fldname, ' & its value is ', fldval.
 
***function key press F6 ****
AT PF06.
  PERFORM disp1.
 
*AT USER-COMMAND.
*  CASE SY-UCOMM.
*    WHEN 'STOP' OR 'CANCEL'.
*      LEAVE TO SCREEN 0.
*    WHEN 'TESTME'.
*      PERFORM DISP1.
*  ENDCASE.

Get help for your ABAP problems
Do you have a ABAP Question?

ABAP Books
ABAP Certification, BAPI, Java, Web Programming, Smart Forms, Sapscripts Reference Books

More ABAP Tips

Best regards,
SAP Basis, ABAP Programming and Other IMG Stuff
http://www.erpgreat.com

All the site contents are Copyright © www.erpgreat.com and the content authors. All rights reserved.
All product names are trademarks of their respective companies.  The site www.erpgreat.com is in no way affiliated with SAP AG. 
Every effort is made to ensure the content integrity.  Information used on this site is at your own risk. 
 The content on this site may not be reproduced or redistributed without the express written permission of 
www.erpgreat.com or the content authors.