Internal Table Operations & Declarations.


Hello SAP Viewers,

:-) Welcome to SAPWorldPark Blog. :-).

This blog is completely useful for those who would like to learn/revise SAP ABAP basics. And content covers completely about Multiple ways to declare Internal table and their operations. 
All most, all the required actions/operations are covered here.

**... User defined datatypes (structure)
TYPES : BEGIN OF ty_t001, "Customized Company Code Structure
               bukrs TYPE bukrs,   "Company Code
               butxt TYPE butxt,    "Company Name
               ort01 TYPE ort01,    "City
               land1 TYPE land1,   "Country Key
               waers TYPE waers,  "Currency Key
               END OF ty_t001.
**...
DATA : lv_count TYPE sytabix.
* Declaration of Internal Table

**... 1st Method.
    TYPES: tt_ty_t001 TYPE STANDARD TABLE OF ty_t001.
                                                                               "Types don't create memory but it assign type to it.
    DATA  : gt_t001     TYPE tt_ty_t001.
                            "Data allocates memory at run-time & Creates the memory at this point for gt_t001.
**...
**...
**... 2nd Method.
*  Simple and directly you can use it.
DATA : gt1_t001 TYPE STANDARD TABLE OF ty_t001.
                                         "Similar allocates memory for gt1_t001 at run-time due to DATA statement.

**... 3. Declare using LIKE type.
  DATA : tt_ty_t001   TYPE STANDARD TABLE OF ty_t001.
  DATA : gt2_t001      LIKE tt_ty_t001.
                  "Created with already existing internal table data type by reference type LIKE statement.
  DATA : gs_t001       LIKE LINE OF gt2_t001.
                                                                                          "Creates a single line structure/ Work Area.
**...
**... 4. Declaring using Headline Line Concept & Currently it is obsolete.
DATA : gt3_t001 TYPE ty_t001 OCCURS 0 WITH HEADER LINE.
                                                                                      "Both Internal table and Work Area same name.

**... 5. Sorted table declaration.
DATA : gt4_t001 TYPE SORTED TABLE OF ty_t001 WITH UNIQUE KEY bukrs.

**... 6. Hashed table declaration.
DATA : gt5_t001 TYPE HASHED TABLE OF ty_t001 WITH UNIQUE KEY bukrs.
                                                      "The Fetching response time is constant regardless of no of records.

START-OF-SELECTION.
**... Fetch records from T001
  SELECT bukrs butxt ort01 land1 waers FROM t001 INTO TABLE gt_t001 UP TO 10 ROWS.

**... Operations on Internal Table (Standard Table)
**... 7. Count No of records in gt1_t001 (Internal Table).
   DESCRIBE TABLE gt_t001 LINES lv_count. "lv_count = data type.
   gt1_t001 = gt_t001.
**... 8. Adding Single record at end/required position.
  gs_t001-bukrs = '1245'.
  gs_t001-butxt = 'Temporary Solutions'.
  gs_t001-ort01 = 'New York'.
  gs_t001-land1 = 'US'.
  gs_t001-waers = 'USD'.
  APPEND gs_t001 TO gt1_t001.
                                   "Append statement add records at the end of the internal table through WA.
  APPEND gs_t001 TO gt2_t001.
                                   "Add at given location.
  INSERT gs_t001 INTO gt1_t001 INDEX 2.
                                   "Here N = 2 can be any number < lv_count.
**... 9. Adding Multiple records at end/required position.
  APPEND LINES OF gt2_t001 TO gt1_t001.

                                                                                    "For only required N no of records to append
                                                                                    "Use addition - FROM <N1> to <N2> addition. 
                                                                                    "N1 and N2 are index numbers.
**...
**... 10. Adding Multiple records at required position.
  INSERT LINES OF gt2_t001 INTO gt1_t001 INDEX 6.
**... 
**...  Modify Single/Multiple Records
**... 12. Using WorkArea.
  gs_t001-ort01 = 'IOWA'.
  MODIFY gt1_t001 FROM gs_t001 TRANSPORTING ort01 WHERE bukrs = '1245'.
                                                     "Use Transporting Keyword only if particular fields to be modified.
**...
**...
  SORT gt1_t001 BY bukrs.                                                                   
  CLEAR gs_t001.                                                                                             "Clears WA data.
  REFRESH gt2_t001.                                                                          "Clear internal table data.
*  FREE gt2_t001.                                                                                          "Releases memory.
**... 13. Reading a single record.
  READ TABLE gt1_t001 INTO gs_t001 WITH KEY bukrs = '1245' BINARY SEARCH.
                                                                                          "Prerequisite for binary search - SORT
  IF sy-subrc = 0.
    CLEAR gs_t001.
  ENDIF.
**...
**... 14. Read data Using Index.
  READ TABLE gt1_t001 INTO gs_t001 INDEX 5.
  IF sy-subrc EQ 0.
    CLEAR gs_t001.
  ENDIF.
**...
**... 15.Iterate record by record
  LOOP AT gt1_t001 INTO gs_t001.
* Iterates record by record
    CLEAR gs_t001.
  ENDLOOP.
**...
**... 16. Iterate record by record for satisfied condition.
  LOOP AT gt1_t001 INTO gs_t001 WHERE bukrs = '1245'.
* Iterates only record with bukrs 1245.
    CLEAR gs_t001.
  ENDLOOP.
**...
**... 17. Iterate record using <index>
  LOOP AT gt1_t001 INTO gs_t001 FROM 3 TO 5. "3 & 5 = Index
* Iterates only from 3 to 5 records, sy-tabix.
    CLEAR gs_t001.
  ENDLOOP.
**...
**...   18.Removing duplicate data
                        "prerequisite - i) SORT ii) Delete adjacent duplicates
  SORT gt1_t001 BY bukrs.
  DELETE ADJACENT DUPLICATES FROM gt1_t001 COMPARING bukrs.
**...
* Multiple ways to delete records.
**...
* Single record.
    DELETE gt1_t001 WHERE bukrs = '1245'.                                    "Delete using condition
*  DELETE gt1_t001 FROM 2 TO 5.                                       "Delete records based on index.
*  DELETE gt1_t001 INDEX 3.                                                 "Delete record based on index.
**...
**...
**...19. Sorted Internal Table.
* Inserting/Appending is Similar as Standard Table. Only difference is Sorted table would contains
* records in sorted order
* And table contains only <unique/non unique> declaration for fields.
  gt4_t001 = gt1_t001.
  gt5_t001 = gt1_t001.
**...

Thanks.

Comments

Popular posts from this blog

SAP Selection Screens

SAP - Write Statemets Additions.

SAP String Operations