Friday, March 20, 2015



CHECK STATEMENT IN ABAP



CHECK statement in ABAP is used for leaving a loop or processing block with some conditional check. If the conditional check with the CHECK statement is true, the process will continue the execution with the next statement. If the conditional check is false, the process will quit the current loop pass and will process the next loop. If CHECK statement is not within a loop, the false condition will terminate the current event.
Syntax Usages
Inside loops /bloks
CHECK <expr>

With Reports with logical databse
CHECK sel.
CHECK SELECT-OPTIONS

You will get clearer picture by seeing the following sample scenarios.

Check statement in a loop
We can use check statement within a loops like
  • DO … ENDDO
  • WHILE … ENDWHILE
  • LOOP … ENDLOOP
  • SELECT … ENDSELECT

In these loops, if the check statement returns the false, it will stop the current loop pass and start the next loop. Here is an example.
DATA: I =0,
J=0.
WHILE  J < 5.
J = J + 1.
CHECK J NE 2.
WRITE J.
ENDWHILE.

Its output will be
1345

Tip: You can see that check statement terminates the loop with value 2.In that loop check conditional statement return false and the loop pass terminated.

CHECK statement with logical databases

CHECK sel: Checks the selection criterion requested by the statement SELECT-OPTIONS sel. If the result of this check is negative, the processing in this event is terminated and the GET events for any subordinate database tables are not processed either.
CHECK SELECT-OPTIONS: checks all the selections for SELECT-OPTIONS where the reference field after FOR belongs to the current table dbtab. And it is calling only after GET event.




IF, CHECK & WHEN ABAP commends example source code and information.


The IF and the CHECK statements perform similar functionality but whereas The IF statement gives you the option to run different code based on if the condition is true or not, the CHECK just stops the current unit of processing code (i.e. LOOP, PERFORM, FM etc) if the condition is false. See example ABAP code below:


data: it_ekko type STANDARD TABLE OF ekko,
      wa_ekko like line of it_ekko.

SELECT *
from ekko
into table it_ekko
where ebeln eq '1111'.
if sy-subrc = 0.
* code for if sy-subrc = 0


if sy-subrc = 0.
* code for if sy-subrc = 0
elseif sy-subrc = 4.
* code for if sy-subrc = 4
else.
* code for if sy-subrc not = 0 or 4
endif.

SELECT *
  from ekko
  into table it_ekko
 where ebeln eq '1111'.
check sy-subrc = 0.
*only progresses further of sy-subrc = 0
The check statement kind of acts like a combination of the IF & EXIT commands. So when you want to stop processing within something like a loop, perform or function module etc. Instead of using the IF/EXIT commands you can just use the CHECK.
SELECT *
  from ekko
  into table it_ekko.
loop at it_ekko into wa_ekko.
  if sy-tabix eq 14.
    EXIT.
  endif.
*At line 14 the loop processing would stop
endloop.

SELECT *
  from ekko
  into table it_ekko.
loop at it_ekko into wa_ekko.
  check sy-tabix lt 14.
*At line 14 the loop processing would stop
endloop.
In conclusion there isn't really anything the check can do that you can't do with the IF and EXIT commands, just provides a slightly neater way of doing a true/fasle check using a few less lines of ABAP code. The IF on the other hand allows for further conditions other than true or false via the ELSEIF and ELSE statements.


WHEN statement
It's probably at this point I should mention the CASE, WHEN statements, which can be used in the same way as the IF. i.e.
CASE sy-subrc.
  When 0.
*            code for if sy-subrc = 0
  When 4.
*           code for if sy-subrc = 4
  When others.
*           code for if sy-subrc not = 0 or 4
endifcase.
Like the CHECK there is nothing really a when can do that can't be achieved by an IF ELSE but does look a bit neater and is potential more efficient. You can also do something like this if you want to find out which flag has been checked (i.e. contains the value X). For some reason it looks cleverer than it actually is, just seems to switch around what you would expect in the When and case sections.
CASE 'X'.
  When ld_flag1.
  When ld_flag2.
  When ld_flag3.
ENDCASE.
Again the IF does have a further trick up its sleeve that makes it the most comprehensive of the options discussed here and that is the ability to use operators such as AND, OR, NOT etc. for example you could have something like
SELECT *
  from ekko
  into table it_ekko
 where ebeln eq '1111'.
if sy-subrc = 0 or sy-subrc = 4.
* code for if sy-subrc = 0 or 4
elseif sy-subrc = 8 and
       sy-datum gt '20120101' .
* code for if sy-subrc = 8 and date is greater than 01.01.2012
else.
* code for if sy-subrc not = 0 or 4
endif.

No comments:

Post a Comment