Wednesday, January 7, 2009

Internal Table and its operations, Control Statements, Selection Query

Dataobject declaration in program:

DATA: str_NAME TYPE/LIKE DDIC STRUCTURE / PREDEFINED.
example:
DATA X_MARA TYPE MARA. “MARA is a DDIC table
DATA X_NAME LIKE NAME.
“NAME is a dataobject declared in the current “program.

INTERNAL TABLES:

They are temporary memory buffers in the application server which can hold multiple records.
Internal tables are active till the program is running. Once the program ends, memory will be released / internal tables are erased.


Internal Tables are of two types:
1. Index
- Standard
- Sorted
2. Hashed (One which follows the hash algo)

In order to work with individual records of the internal table, we need to have a structure of same size and type as internal table and that structure is called as field string or work are of header line.

DATA: BEGIN OF IT_SO OCCURS 0,
SONO TYPE VBAP_VBELN,
ITM TYPE VBAP-POSNR,
MATERIAL TYPE VBAP-MATNR,
QTY TYPE VBAP-KWMENG,
END OF IT_SO.

The above code will create:
1. a work area having four fields: SONO, ITM, MATERIAL, QTY
2. an internal table similar to work area.

At any point IT_SO will point to work are and not internal table because we can not point to internal table.

IT_SO-SONO = ‘5000’.
IT_SO-ITM = ‘10’.
IT_SO-MATERIAL = ‘M-09’.
IT_SO-QTY = ‘7’.
APPEND IT_SO.

IT_SO-ITM = ‘20’.
IT_SO-MATERIAL = ‘M-02’.
IT_SO-QTY = ‘11’.
APPEND IT_SO.
WRITE: IT_SO-SONO,
IT_SO-ITM,
IT_SO-MATERIAL,
IT_SO-QTY.
ENDLOOP.

Filling the Internal Table

Syntax: APPEND WA TO ITAB.
In earlier example :
APPEND IT_SO TO IT_SO.
OR
APPEND IT_SO.

Looping through internal table.
LOOP AT IT_SO INTO WA [where condition].
….

….
ENDLOOP.

In place of above, we can write:
LOOP AT IT_SO.
….


ENDLOOP.
“Above can be done provided the internal table has OCCURS 0 (or any other number in place of 0) as addition while declaration.

OCCURS 0. This addition allots 8KB of memory
OCCURS 10. Memory for 10 records is allocated.
OCCURS 5. Memory for 5 records is allocated.
SAP says in ECC 6.0 occurs has become obsolete as SAP system needs to take overhead of memory allocation.

DATA: ITAB type/like table of x [WITH HEADER LINE].


TYPES: BEGIN OF TY_SO, “User defined structure type
POSNR TYPE VBAP-POSNR,
MATNR TYPE VBAP-MATNR,
KWMENG TYPE VBAP-KWMENG,
END OF TY_SO,
TY_T_SO TYPE TABLE OF TY_SO. “User defined table type
DATA: IT_SO TYPE TY_T_SO,
X_SO TYPE TY_SO.

Fetching data from database.

SELECT *
INTO TABLE ITAB
FROM DATABASE_TABLE
WHERE CONDITION.

Structure of the internal table should be same or higher than that of fields specified in the select statement.
The sequence of fields should be same otherwise use:
1. SELECT * INTO…
2. SELECT F1 F2 INTO CORRESPONDING FIELDS OF TABLE ITAB…
But both of the above options should be avoided due to performance reasons.

SELECT A~F1 A~F2 A~F3
B~C1 B~C2 B~C3
INTO TABLE ITAB
FROM DBTAB1
AS A INNER JOIN DBTAB2 AS B
ON A ~ CF = B ~ CF
WHERE CONDITION…
*DBTAB1 and DBTAB2 are database tables
*A is alias name for DBTAB1
*B is alias name for DBTAB2
*F1, F2, F3 are fields of DBTAB1
*C1, C2, C3 are fields of DBTAB2
* CF is the common field in DBTAB1 and DBTAB2

One can bring all fields of structure together through PATTERN button.

Selection Table: Its an internal table with the fields:
SIGN – Include / Exclude
OPTION – BT NB GT LT GE
LOW – Low Value
HIGH -- High Value

Declaring selection fields table:

SELECT-OPTIONS: S_VBAK FOR VBAK-VBELN.
Syntax: SELECT-OPTIONS VAR FOR X_VAR
[OBLIGATORY] [LOWER_CASE]
[DEFAULT ‘’ TO ‘’]
[NO-EXTENSION]
[NO-INTERVALS]

We need to add TABLES: VBAL only for the tables used in SELECTOPTIONS
(in earlier versions, it used to be for all the tables used in the program).

Operations with internal table:
Reading single Records:
READ TABLE ITAB INTO WA
[INDEX|WITH KEY CONDITION]

READ TABLE IT_VBAKVBAP INTO X_VBAKVBAP INDEX 7.
IF SY-SUBRC = 0.
….
..

ENDIF.
SY-SUBRC is a system field which contains return code of previously executed statement.
If 0, it means atleast one record was read for the selection query.

READ TABLE IT_VBAKVBAP INTO X_VBAKVBAP
WITH KEY VBELN = ‘5006’
POSNR = ‘10’.

MODIFY TABLE
MODIFY ITAB FROM WA
[INDEX I | WHERE CONDITION]

READ TABLE IT_VBAKVBAP INTO X_VBAKVBAP INDEX 4.
IF SY-SUBRC = 0.
X_VBAKVBAP-KWMENG = X_VBAKVBAP-KWMENG + 8.
MODIFY IT_VBAKVBAP FORM X_VBAKVBAP INDEX Y
TRANSPORTING KWMENG.
Sorting Internal Table
SORT ITAB BY F1 [ASCENDING / DESCENDING]
F2 [ASCENDING / DESCENDING]
….


[AS TEXT]

By default it is ASCII , AS TEXT addition makes it otherwise.

DELETING INTERNAL TABLE
1. FREE - FREE ITAB. “deletes contents of internal table and releases memory
2. REFRESH – REFRESH INTERNAL TABLE. “Deletes content only, memory remains as it is.
In both above cases, work area will not be affected( in case there is a header line).
3. CLEAR – CLEAR ITAB. “Clears only work area
-- CLEAR ITAB[]. “ clears only the internal table

CONTROL BREAKS

1. AT FIRST….ENDAT
LOOP AT ITAB.
AT FIRST.

….
..
ENDAT.
ENDLOOP.
This will be triggered for the very first loop pass.
We can use for table headers.
Inside AT FIRST, ENDAT – character values will be changed to **, rather numeric values will be changed to 0.
2. AT LAST….ENDAT.
LOOP AT ITAB.
AT LAST.

….
..
ENDAT.
ENDLOOP.

Used for last loop pass. Used for table footers.
3. AT NEW…ENDAT.
LOOP AT ITAB.
AT NEW.

….
..
ENDAT.
ENDLOOP.

AT NEW will be triggered:
- for very first loop pass.
- when the content of control label changes.
- when left side fields of control label changes.
- inside AT NEW and ENDAT, the contents of work area will be changed as follows:
Left side fields and control labels remain as it is rather the right side field will be change to * and 0s based on character and numeric.
4. AT END OF ..ENDAT.
- This will trigger when the control label value changes.
- For the last loop pass.
- When the left side field of control label changes.
Values of work area will be changed same as AT NEW.
5. SUM.
The statement will sum the quantity field value for the corresponding control label and the summed value will be stored in the work area’s quantity fields only.
6. ON CHANGE OF... ENDON.
Triggers on change of the particular control label.

No comments: