Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Jusitification of IF statement in code

Former Member
0 Likes
1,442

Hi,

I am working on a code win which i have to display that if the storage location is scrap i.e.(SC01,SC02,SC03,SC04) then it will display the data other wise it will make MGEIG EQ 0.

i am facing the problem at if statement where i had mentioned that if this storage location is not of scrap then it should be zero but it is nod ing so.

Please provide me guidelines as how do i achieve this functionality in my code.

here's the code:-



SELECT A~PRUEFLOS B~MBLNR C~LGORT FROM QALS AS A
INNER JOIN QAMB AS B ON B~PRUEFLOS = A~PRUEFLOS "AND B~ZEILE = A~ZEILE
INNER JOIN MSEG AS C ON C~MBLNR = B~MBLNR AND C~MJAHR = B~MJAHR
INTO  TABLE ITSC "(l_PRUEFLOS , l_MBLNR, l_LGORT)
FOR ALL ENTRIES IN STIT
WHERE A~PRUEFLOS EQ STIT-PRUEFLOS." AND ( LGORT = 'SC01' OR LGORT = 'SC02' OR LGORT = 'SC03' OR LGORT = 'SC04' )." AND A~ZEILE EQ STIT-ZEILE. AND ( LGORT = 'SC01' OR LGORT = 'SC02' OR LGORT = 'SC03' OR LGORT = 'SC04' ).

SORT ITSC BY PRUEFLOS.
ITSC1[] = ITSC[].
SORT ITSC1 BY LGORT.

LOOP AT STIT.

 IF STIT-MGEIG GE 0.
*LOOP AT ITSC1 WHERE PRUEFLOS EQ STIT-PRUEFLOS.

 READ TABLE ITSC1 WITH KEY PRUEFLOS = STIT-PRUEFLOS.

  w_tabx = sy-tabix.
  L_lgort = ITSC1-LGORT.

IF L_LGORT NE 'SC01' OR L_LGORT NE 'SC02' OR L_LGORT NE 'SC03' OR l_LGORT NE 'SC04' .
* IF ( L_LGORT NE  'SC01' OR 'SC02' OR 'SC03' OR 'SC04' ).

  STIT-MGEIG = 0.

  MODIFY stit INDEX w_tabx  TRANSPORTING mgeig.

endif.

*ENDLOOP.
ENDIF.
modify stit.
clear w_tabx.
clear l_LGORT.
ENDLOOP.

SORT STIT BY MATNR MGEIG.

LOOP AT STIT.
  WRITE:/ STIT-PRUEFLOS,STIT-CHARG,STIT-LGORTCHARG,STIT-MGEIG.
ENDLOOP.

1 ACCEPTED SOLUTION
Read only

Former Member
0 Likes
1,385

Hi Rick

It seems you've post the same questions twice.

Anyway your code is good but u need to use AND instead of OR in your IF condition, because u want to your storage location is not equal to SC01 AND not equal to SC02.....

If you use OR the IF condition will be alway true, infact for example:

IF LOGRT <> 'SC01' OR LGORT <> 'SC02' OR LGORT <> 'SC03' OR LGORT <'SC04>.

IF lgort is SC01, it's not equal to SC02 so IF condition is true.

So your code has to be:

LOOP AT STIT.
 
 IF STIT-MGEIG GE 0.
*LOOP AT ITSC1 WHERE PRUEFLOS EQ STIT-PRUEFLOS.
 
 READ TABLE ITSC1 WITH KEY PRUEFLOS = STIT-PRUEFLOS.
 
  w_tabx = sy-tabix.
  L_lgort = ITSC1-LGORT.
 
*IF L_LGORT NE 'SC01' OR L_LGORT NE 'SC02' OR L_LGORT NE 'SC03' OR l_LGORT NE 'SC04' .
    IF L_LGORT NE 'SC01' AND L_LGORT NE 'SC02' AND L_LGORT NE 'SC03' AND l_LGORT NE 'SC04' .
    
       STIT-MGEIG = 0.
       MODIFY stit INDEX w_tabx  TRANSPORTING mgeig.
    endif.
  ENDIF.
ENDLOOP.

Max

Hi,

I am working on a code win which i have to display that if the storage location is scrap i.e.(SC01,SC02,SC03,SC04) then it will display the data other wise it will make MGEIG EQ 0.

i am facing the problem at if statement where i had mentioned that if this storage location is not of scrap then it should be zero but it is nod ing so.

Please provide me guidelines as how do i achieve this functionality in my code.

here's the code:-



SELECT A~PRUEFLOS B~MBLNR C~LGORT FROM QALS AS A
INNER JOIN QAMB AS B ON B~PRUEFLOS = A~PRUEFLOS "AND B~ZEILE = A~ZEILE
INNER JOIN MSEG AS C ON C~MBLNR = B~MBLNR AND C~MJAHR = B~MJAHR
INTO  TABLE ITSC "(l_PRUEFLOS , l_MBLNR, l_LGORT)
FOR ALL ENTRIES IN STIT
WHERE A~PRUEFLOS EQ STIT-PRUEFLOS." AND ( LGORT = 'SC01' OR LGORT = 'SC02' OR LGORT = 'SC03' OR LGORT = 'SC04' )." AND A~ZEILE EQ STIT-ZEILE. AND ( LGORT = 'SC01' OR LGORT = 'SC02' OR LGORT = 'SC03' OR LGORT = 'SC04' ).

SORT ITSC BY PRUEFLOS.
ITSC1[] = ITSC[].
SORT ITSC1 BY LGORT.

LOOP AT STIT.

 IF STIT-MGEIG GE 0.
*LOOP AT ITSC1 WHERE PRUEFLOS EQ STIT-PRUEFLOS.

 READ TABLE ITSC1 WITH KEY PRUEFLOS = STIT-PRUEFLOS.

  w_tabx = sy-tabix.
  L_lgort = ITSC1-LGORT.

IF L_LGORT NE 'SC01' OR L_LGORT NE 'SC02' OR L_LGORT NE 'SC03' OR l_LGORT NE 'SC04' .
* IF ( L_LGORT NE  'SC01' OR 'SC02' OR 'SC03' OR 'SC04' ).

  STIT-MGEIG = 0.

  MODIFY stit INDEX w_tabx  TRANSPORTING mgeig.

endif.

*ENDLOOP.
ENDIF.
modify stit.
clear w_tabx.
clear l_LGORT.
ENDLOOP.

SORT STIT BY MATNR MGEIG.

LOOP AT STIT.
  WRITE:/ STIT-PRUEFLOS,STIT-CHARG,STIT-LGORTCHARG,STIT-MGEIG.
ENDLOOP.

13 REPLIES 13
Read only

Former Member
0 Likes
1,385

Write :

IF L_LGORT EQ 'SC01' OR L_LGORT EQ 'SC02' OR L_LGORT EQ 'SC03' OR l_LGORT EQ 'SC04' .

"do nothing" or "Display data"

ELSE.

STIT-MGEIG = 0.

MODIFY stit INDEX w_tabx TRANSPORTING mgeig.

ENDIF.

IN PLACE OF BELOW CODE:

IF L_LGORT NE 'SC01' OR L_LGORT NE 'SC02' OR L_LGORT NE 'SC03' OR l_LGORT NE 'SC04' .

  • IF ( L_LGORT NE 'SC01' OR 'SC02' OR 'SC03' OR 'SC04' ).

STIT-MGEIG = 0.

MODIFY stit INDEX w_tabx TRANSPORTING mgeig.

endif.

Read only

Former Member
0 Likes
1,385

Hi,

Always Check Sy-Subrc after a Read statement, proceed only if it is 0(zero).

Regards

Karthik D

Read only

Former Member
0 Likes
1,385

Hi,

You are modifying stit with index w_tabx if the storage loc is not scrap.

Howvere W_tabx will not contain the current index of STIT.

This is because you are moving sy-tabix after the READ statement on ITSC1 into W_TABX.

So there is a possibility that the wrong record in STIT will be modified.

Also I feel the OR condition with negation is actually a tricky condition.

The result is difficult to interpret.

Because if L_LGORT is SC01

then the outcome of your condition will be true and it will display 0 which is incorrect.

So instead use like this.

IF ( L_LGORT NE 'SC01'

AND L_LGORT NE 'SC02'

AND L_LGORT NE 'SC03'

AND l_LGORT NE 'SC04' .)

  • display 0

else.

  • display record

endif.

Because as you said that you want 0 when storage location are not scrap.

Regards,

Ankur Parab

Read only

Former Member
0 Likes
1,386

Hi Rick

It seems you've post the same questions twice.

Anyway your code is good but u need to use AND instead of OR in your IF condition, because u want to your storage location is not equal to SC01 AND not equal to SC02.....

If you use OR the IF condition will be alway true, infact for example:

IF LOGRT <> 'SC01' OR LGORT <> 'SC02' OR LGORT <> 'SC03' OR LGORT <'SC04>.

IF lgort is SC01, it's not equal to SC02 so IF condition is true.

So your code has to be:

LOOP AT STIT.
 
 IF STIT-MGEIG GE 0.
*LOOP AT ITSC1 WHERE PRUEFLOS EQ STIT-PRUEFLOS.
 
 READ TABLE ITSC1 WITH KEY PRUEFLOS = STIT-PRUEFLOS.
 
  w_tabx = sy-tabix.
  L_lgort = ITSC1-LGORT.
 
*IF L_LGORT NE 'SC01' OR L_LGORT NE 'SC02' OR L_LGORT NE 'SC03' OR l_LGORT NE 'SC04' .
    IF L_LGORT NE 'SC01' AND L_LGORT NE 'SC02' AND L_LGORT NE 'SC03' AND l_LGORT NE 'SC04' .
    
       STIT-MGEIG = 0.
       MODIFY stit INDEX w_tabx  TRANSPORTING mgeig.
    endif.
  ENDIF.
ENDLOOP.

Max

Read only

0 Likes
1,385

Hi,

I had tried a lot but still it is not giving the desiered output i.e. if there are any storage locations such as SC01,SC02,SC03,SC04 then it should display the value otherwise if data present except these locations it should be displayed zero.I had checked in the debug mode it is not able to modify the value of MGEIG where the storage location is EQ above mentioned. Please provide me guidelines to solve this problem.




LOOP AT STIT.
  IF STIT-MGEIG GE 0.
    LOOP AT ITSC1 WHERE PRUEFLOS EQ STIT-PRUEFLOS.
  w_tabx = sy-tabix.
  L_lgort = ITSC1-LGORT.
IF  L_LGORT EQ 'SC01' OR L_LGORT EQ 'SC02' OR L_LGORT EQ 'SC03' OR l_LGORT EQ 'SC04' .
  STIT-MGEIG =   STIT-MGEIG.
  modify stit INDEX w_tabx TRANSPORTING MGEIG.
ELSE.
  STIT-MGEIG = 0.
  modify stit INDEX w_tabx TRANSPORTING MGEIG.
endif.

CLEAR: L_lgort , w_tabx.
ENDLOOP.
ENDIF.
ENDLOOP.

SORT STIT BY MATNR MGEIG.
LOOP AT STIT.
WRITE:/ STIT-PRUEFLOS,STIT-CHARG,STIT-LGORTCHARG,STIT-MGEIG.
ENDLOOP.

Edited by: ricx .s on Jun 8, 2009 9:17 AM

Read only

0 Likes
1,385

Use AND instead of OR


IF  L_LGORT ne 'SC01' and L_LGORT nE 'SC02' AND L_LGORT nE 'SC03' AND l_LGORT nE 'SC04' .
  STIT-MGEIG =   STIT-MGEIG.
  modify stit INDEX w_tabx TRANSPORTING MGEIG.
ELSE.
  STIT-MGEIG = 0.
  modify stit INDEX w_tabx TRANSPORTING MGEIG.
endif.

Edited by: Tripat Pal Singh on Jun 8, 2009 1:39 PM

Read only

0 Likes
1,385

oh i got it, u r using sy-tabix value at wrong place, see the code below and check the difference from ur original code


LOOP AT STIT.
  w_tabx = sy-tabix.
  IF STIT-MGEIG GE 0.
    LOOP AT ITSC1 WHERE PRUEFLOS EQ STIT-PRUEFLOS.
*  w_tabx = sy-tabix.
  L_lgort = ITSC1-LGORT.
IF  L_LGORT EQ 'SC01' OR L_LGORT EQ 'SC02' OR L_LGORT EQ 'SC03' OR l_LGORT EQ 'SC04' .
  STIT-MGEIG =   STIT-MGEIG.
  modify stit INDEX w_tabx TRANSPORTING MGEIG.
ELSE.
  STIT-MGEIG = 0.
  modify stit INDEX w_tabx TRANSPORTING MGEIG.
endif.
 
CLEAR: L_lgort , w_tabx.
ENDLOOP.
ENDIF.
ENDLOOP.
 
SORT STIT BY MATNR MGEIG.
LOOP AT STIT.
WRITE:/ STIT-PRUEFLOS,STIT-CHARG,STIT-LGORTCHARG,STIT-MGEIG.
ENDLOOP.
 

Read only

0 Likes
1,385

Hello ricx,

Have you got the solution or not???

Read only

0 Likes
1,385

hi,

yes,i got the solution ,actaully the problem is the placing of the sy-tabix which is present in the 2 nd loop and it is overwriting the value of it,i had moved that line in the loop of STIT and there it allows to modify the field which is being transported through it.

I really thanks all for thier help and closing this thread.

Read only

0 Likes
1,385

Ricx,

Somewhere you have wriiten code:

SELECT aprueflos bmblnr clgort cmenge FROM qals AS a

INNER JOIN qamb AS b ON bprueflos = aprueflos "AND BZEILE = AZEILE

INNER JOIN mseg AS c ON cmblnr = bmblnr AND cmjahr = bmjahr AND czeile = bzeile

INTO TABLE itsc "(l_PRUEFLOS , l_MBLNR, l_LGORT)

FOR ALL ENTRIES IN stit

WHERE aprueflos EQ stit-prueflos AND amatnr EQ stit-matnr.

when you write "for all entries in internal table" without writing "if internal table is not initial" will create the proble.See for ex:

If stit[] is initial no data is there in internal table it will fetch all the entries and will take database time too much and sometimes may get you dump error!

So try to write always :

If stit[] is not initial.

SELECT aprueflos bmblnr clgort cmenge FROM qals AS a

INNER JOIN qamb AS b ON bprueflos = aprueflos "AND BZEILE = AZEILE

INNER JOIN mseg AS c ON cmblnr = bmblnr AND cmjahr = bmjahr AND czeile = bzeile

INTO TABLE itsc "(l_PRUEFLOS , l_MBLNR, l_LGORT)

FOR ALL ENTRIES IN stit

WHERE aprueflos EQ stit-prueflos AND amatnr EQ stit-matnr.

endif.

Read only

0 Likes
1,385

Hi Supriya Bhatt,

Thanks a lot for your help,yeah it is point which is to be taken in considerataion and i had modified the code and debugged it is the right way to do it,thanks for your help. Hey, do you have material regarding OOPs concept?

Read only

0 Likes
1,385

Sorry I dont have ...You may get lots of stuff from sdn or other site

try sap-img.com.

Read only

0 Likes
1,385

hi,

its ok, actually i was looking for some godd pdf or zip files for it,anyways thanks for your help.:-)