Wednesday, May 23, 2012

Difference between CONTINUE, CHECK and EXIT statement in ABAP/4

  • CONTINUEWhen you apply this statement, system will pass a current loop. System  ignores any statements after CONTINUE statement and start to the next loop.
Example
DO 4 TIMES.
  WRITE:/ sy-index.
  IF sy-index <= 2.
    CONTINUE.

  ENDIF.
  WRITE: ’After’.
ENDDO.
The output is
1  
2  
3   After
4   After
  • CHECK
    This statement will apply if you want to check condition before go to next statement. If the result of checking is ’TRUE’, system will go to next step as normally. If the result of checking is ’FALSE’, system will operate look like CONTINUE statement.
Example
DO 4 TIMES.
  WRITE:/ sy-index.
  CHECK sy-index > 2.

  WRITE: ’After’.
ENDDO.
The output is
1  
2  
3   After
4   After
  • EXITWhen you apply this statement, system will exit from loop. System  ignores any statements after EXIT statement and go to after LOOP command.
Example
DO 4 TIMES.
  WRITE:/ sy-index.
  IF sy-index > 2.
    EXIT.

  ENDIF.
  WRITE: ’After’.
ENDDO.
The output is
1  After
2  After

Control Break Processing in ABAP Internal Tables Continued

In AT FIRST and AT LAST event blocks the numeric values in the work area contains zeros. SUM statement calculates the totals of numeric fields and places the totals in the corresponding fields of work area.
In AT NEW and AT END OF event blocks SUM statement finds all the rows within the control level and calculates the totals of numeric fields that are right to the control level and places the totals in the corresponding fields of work area.
*————————————————————–* *Data Declaration *————————————————————–*
DATA: gwa_spfli TYPE spfli.
DATA: gt_spfli  TYPE TABLE OF spfli.

SELECT * UP TO 5 ROWS FROM spfli INTO TABLE gt_spfli.

LOOP AT gt_spfli INTO gwa_spfli.
  AT FIRST.
    WRITE:/ 'Flight Details'.
    WRITE:/ 'Airline Code' COLOR 5,14 'Connection No.' COLOR 5,
         29 'Departure City' COLOR 5, 44 'Arival City' COLOR 5,
         58 'Distance' COLOR 5.
    ULINE.
  ENDAT.
  AT NEW carrid.
    WRITE:/ gwa_spfli-carrid, ' : New Airline'.
    ULINE.
  ENDAT.
  WRITE:/14 gwa_spfli-connid,29 gwa_spfli-cityfrom,
         44 gwa_spfli-cityto,58 gwa_spfli-distance.
  AT END OF carrid.
    ULINE.
SUM.
    WRITE:/ gwa_spfli-carrid,58 gwa_spfli-distance.
    ULINE.
  ENDAT.
  AT LAST.
SUM.
    WRITE:/ 'Total',58 gwa_spfli-distance.
    WRITE:/ 'End of Loop'.
  ENDAT.
ENDLOOP.
Output
control-break-sum
ON CHANGE OF behaves similar to AT NEW. The syntax is as follows.
ON CHANGE OF <control level1> [or <control level2> . .].
[ELSE.]
ENDON.
*————————————————————–* *Data Declaration *————————————————————–*
DATA: gwa_spfli TYPE spfli.
DATA: gt_spfli  TYPE TABLE OF spfli.

SELECT * UP TO 5 ROWS FROM spfli INTO TABLE gt_spfli.

LOOP AT gt_spfli INTO gwa_spfli.
  AT FIRST.
    WRITE:/ 'Flight Details'.
    WRITE:/ 'Airline Code' COLOR 5,14 'Connection No.' COLOR 5,
         29 'Departure City' COLOR 5, 44 'Arival City' COLOR 5,
         58 'Distance' COLOR 5.
    ULINE.
  ENDAT.
ON CHANGE OF gwa_spfli-carrid. WRITE:/ gwa_spfli-carrid, ‘ : New Airline’. ULINE. ENDON.
  WRITE:/14 gwa_spfli-connid,29 gwa_spfli-cityfrom,
         44 gwa_spfli-cityto,58 gwa_spfli-distance.

ENDLOOP.
Output
control-break-on-change-of
Below table summarizes the differences between AT NEW and ON CHANGE OF statements.
AT NEW ON CHANGE OF
It can be used only in AT LOOP statement. It can be used in any loop like SELECT, DO etc..
Only one control field can be used. Multiple control fields separated by OR can be used.
AT NEW is triggered when a field left to control level changes. ON CHANGE OF is not triggered when a field left to control level changes.
Values in the fields to the right of control level contains asterisks and zeros. Values in the fields to the right of control level contains original values.
ELSE addition cannot be used. ELSE addition can be used.
Changes to work area with AT NEW will be lost. Changes to work area with ON CHANGE OF will not be lost.

Friday, May 18, 2012

Inserting Lines into ABAP Internal Tables

We can insert one or more lines to ABAP internal tables using the INSERT statement. To insert a single line, first place the values we want to insert in a work area and use the INSERT statement to insert the values in the work area to internal table.

Syntax to insert a line to internal table
INSERT <work area> INTO TABLE <internal table>.
                       OR
INSERT <work area> INTO <internal table> INDEX <index>.
The first INSERT statement without INDEX addition will simply add the record to the end of the internal table. But if we want to insert the line to specific location i.e. if we want to insert it as second record then we need to specify 2 as the index in the INSERT statement.
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
       id(5)    TYPE n,
       name(10) TYPE c,
       END OF ty_student.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it TYPE TABLE OF ty_student.

gwa_student-id    = 1.
gwa_student-name  = 'JOHN'.
INSERT gwa_student INTO TABLE it.

gwa_student-id    = 2.
gwa_student-name  = 'JIM'.
INSERT gwa_student INTO TABLE it.

gwa_student-id    = 3.
gwa_student-name  = 'JACK'.
INSERT gwa_student INTO TABLE it.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name.
ENDLOOP.

SKIP.
WRITE:/ 'After using Index addition' COLOR 4.

gwa_student-id    = 4.
gwa_student-name  = 'RAM'.
INSERT gwa_student INTO it INDEX 2.

WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5.
LOOP AT it INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name.
ENDLOOP.
Output
insert-it-1
We can also insert multiple lines to an internal table with a single INSERT statement i.e. we can insert the lines of one internal table to another internal table.
Syntax to insert multiple lines to internal table
INSERT LINES OF <itab1> [FROM <index 1>] [TO <index 2>] INTO TABLE <itab2>.
                                        OR
INSERT LINES OF <itab1> [FROM <index 1>] [TO <index 2>] INTO
<itab2> INDEX <index>.
*--------------------------------------------------------------*
*Data Types
*--------------------------------------------------------------*
TYPES: BEGIN OF ty_student,
       id(5)    TYPE n,
       name(10) TYPE c,
       END OF ty_student.

*--------------------------------------------------------------*
*Data Declaration
*--------------------------------------------------------------*
DATA: gwa_student TYPE ty_student.
DATA: it  TYPE TABLE OF ty_student,
      it2 TYPE TABLE OF ty_student,
      it3 TYPE TABLE OF ty_student,
      it4 TYPE TABLE OF ty_student.

gwa_student-id    = 1.
gwa_student-name  = 'JOHN'.
INSERT gwa_student INTO TABLE it.

gwa_student-id    = 2.
gwa_student-name  = 'JIM'.
INSERT gwa_student INTO TABLE it.

gwa_student-id    = 3.
gwa_student-name  = 'JACK'.
INSERT gwa_student INTO TABLE it.

gwa_student-id    = 4.
gwa_student-name  = 'ROB'.
INSERT gwa_student INTO TABLE it.

WRITE:/ 'Inserting all the lines of IT to IT2' COLOR 4.

INSERT LINES OF it INTO TABLE it2.

WRITE:/ 'Display values of IT2' COLOR 1.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5.
LOOP AT it2 INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name.
ENDLOOP.

SKIP.
WRITE:/ 'Inserting only lines 2 & 3 of IT to IT3' COLOR 4.

INSERT LINES OF it FROM 2 TO 3 INTO TABLE it3.

WRITE:/ 'Display values of IT3' COLOR 1.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5.
LOOP AT it3 INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name.
ENDLOOP.

gwa_student-id    = 1.
gwa_student-name  = 'RAM'.
INSERT gwa_student INTO TABLE it4.

gwa_student-id    = 4.
gwa_student-name  = 'RAJ'.
INSERT gwa_student INTO TABLE it4.

SKIP.
WRITE:/ 'Inserting only lines 2 & 3 of IT to IT4 at 2' COLOR 4.

INSERT LINES OF it FROM 2 TO 3 INTO it4 INDEX 2.

WRITE:/ 'Display values of it4' COLOR 1.
WRITE:/ 'ID' COLOR 5,7 'Name' COLOR 5.
LOOP AT it4 INTO gwa_student.
  WRITE:/ gwa_student-id, gwa_student-name.
ENDLOOP.
The last INSERT statement in the above program inserts the 2nd and 3rd line from IT at index 2 in IT4, so the new lines inserted becomes the 2nd and 3rd line in IT4.
Output
insert-it-2