01-29-2010 9:24 AM
Hi Gurus,
Please find the below code segment.
LOOP AT itab_vbap INTO wa_vbap.
READ TABLE itab_vbak INTO wa_vbak
WITH KEY vbeln = wa_vbap-vbeln BINARY SEARCH.
IF wa_vbap-zshipdate NOT IN s_fsd.
DELETE itab_vbap WHERE vbeln = wa_vbak-vbeln.
ENDIF.
ENDLOOP.
The above code segment is working fine, but I need a better solution.
Reason: I feel the DELETE statement which is deleting entries from the ITAB vbap is inside the LOOP of vbap and thus this might result in some inconsistency.
The READ statement is written because to make sure those entries are deleted which are present in VBAK and VBAP and satisfies the given condition.
Please provide an alternative and better solution to this code.
Thanks !
01-29-2010 9:31 AM
Hi,
First of all where in delete is not good for peformance reasons. And use continue after delete so loop will continue with next pass. Check below code it should be ok.
data: lv_tabix type sy-tabix.
LOOP AT itab_vbap INTO wa_vbap.
lv_tabix = sy-tabix.
READ TABLE itab_vbak INTO wa_vbak
WITH KEY vbeln = wa_vbap-vbeln BINARY SEARCH.
IF wa_vbap-zshipdate NOT IN s_fsd.
DELETE itab_vbap index lv_tabix.
continue.
ENDIF.
ENDLOOP.
Regards,
Edited by: Gungor Ozcelebi on Jan 29, 2010 10:31 AM
01-29-2010 9:31 AM
Hi,
First of all where in delete is not good for peformance reasons. And use continue after delete so loop will continue with next pass. Check below code it should be ok.
data: lv_tabix type sy-tabix.
LOOP AT itab_vbap INTO wa_vbap.
lv_tabix = sy-tabix.
READ TABLE itab_vbak INTO wa_vbak
WITH KEY vbeln = wa_vbap-vbeln BINARY SEARCH.
IF wa_vbap-zshipdate NOT IN s_fsd.
DELETE itab_vbap index lv_tabix.
continue.
ENDIF.
ENDLOOP.
Regards,
Edited by: Gungor Ozcelebi on Jan 29, 2010 10:31 AM
01-29-2010 9:31 AM
hi,
LOOP AT itab_vbap INTO wa_vbap.
READ TABLE itab_vbak INTO wa_vbak
WITH KEY vbeln = wa_vbap-vbeln BINARY SEARCH.
if sy-subrc = 0.
IF wa_vbap-zshipdate NOT IN s_fsd.
DELETE itab_vbap WHERE vbeln = wa_vbak-vbeln.
ENDIF.
endif.
clear : wa_vbap,wa_vbak .
ENDLOOP.
regards
Gaurav
01-29-2010 9:42 AM
Hi
Declare the tmp date and delete it from the original internal table.
LOOP AT itab_vbap_tmp INTO wa_vbap_tmp.
IF wa_vbap_tmp-zshipdate NOT IN s_fsd.
DELETE itab_vbap WHERE vbeln = wa_vbap_tmp-vbeln.
ENDIF.
ENDLOOP.
Regards,
Bharani
01-29-2010 9:51 AM
Hi,
delete statement inside a loop should not lead to any inconsistency. You are not testing a sy-subrc after READ statement in you code. Is it necessary to have this statement there in such case? You can probably do it like this:
LOOP AT itab_vbap INTO wa_vbap WHERE zshipdate NOT IN s_fsd.
DELETE itab_vbap WHERE vbeln EQ wa_vbap-vbeln.
ENDLOOP.
This example is deleting all positions for VBELN if at least one of them have zshipdate NOT IN s_fsd as in your code. If You want to delete only positions with zshipdate NOT IN s_fsd, You can do it like this:
DELETE itab_vbap WHERE zshipdate NOT IN s_fsd.
Regards,
Adrian
01-29-2010 9:57 AM
Hi instead of deleting move the required entries into another itab.
Atleast we can git rid rebuilding the itab index during delete.
01-29-2010 10:00 AM
Hi Keshav,
I think you have learnt this technique from SDN
BR,
Suhas
01-29-2010 10:05 AM
Hi,
Suhas ... Its From Tips and tricks of henin frank
@chandan sinha - The read statemnt is hanging independently without a subrc check.
01-29-2010 10:11 AM
Hey buddy,
I think you asked a similar question y'day. Is the tips & tricks available on SDN ?
BR,
Suhas
01-29-2010 10:30 AM
That was a different one.
That code was not mine ...
link:[http://www.henrikfrank.dk/abaptips/]
Its explained somewhere in the link.
Keshu
01-29-2010 10:05 AM
Hi Chandan,
you can loop at a copy of itab_vbap and delete from the original table itab_vbap.
DATA: itab_vbap_cp LIKE itab_vbap.
itab_vbap_cp = itab_vbap.
LOOP AT itab_vbap_cp INTO wa_vbap.
READ TABLE itab_vbak INTO wa_vbak
WITH KEY vbeln = wa_vbap-vbeln BINARY SEARCH.
IF wa_vbap-zshipdate NOT IN s_fsd.
DELETE itab_vbap WHERE vbeln = wa_vbak-vbeln.
ENDIF.
ENDLOOP.
or you can collect the entiries to be deleted in the loop, and delete them after the loop is finished.
DATA: itab_vbap_todel LIKE itab_vbap.
LOOP AT itab_vbap INTO wa_vbap.
READ TABLE itab_vbak INTO wa_vbak
WITH KEY vbeln = wa_vbap-vbeln BINARY SEARCH.
IF wa_vbap-zshipdate NOT IN s_fsd.
APPEND wa_vbap TO itab_vbap_todel.
ENDIF.
ENDLOOP.
LOOP AT itab_vbap_todel INTO wa_vbap.
DELETE itab_vbap WHERE vbeln = wa_vbap-vbeln.
ENDLOOP.
Regards, Hubert
01-29-2010 10:13 AM
Hi Chandan Sinha,
After ovserving your code i suggest below way..
Please replace your code with below single statement... it will do the deletion task..
Only Single statement is enough for it..
DELETE itab_vbap WHERE zshipdate NOT IN s_fsd.
No need to use LOOP and rebuilding indexes and all..
Hope it will solve your problem..
Thanks & Regards
ilesh 24x7
ilesh Nandaniya
01-29-2010 10:32 AM
Hi,
I think he didnt use the read statement unnecessarily.
What if the records should be deleted only if the sy-subrc is true after read statement.
01-29-2010 10:43 AM
Hi Keshav T,
Thanks for the Suggession/Correction.. You may be true also.. Now Only Chandan can say what exaclty he needs..
But As I think He used READ statement unnecessorily as per the code snippet he has given...
And yes about READ Statement Condition..
He is checking existenance of VBELN in VBAK...
Simple Process ..
If item table (VBAP) has sale order then... it must be present in Header Table (VBAK).. So there is no need to check it...
Correct me If I am wrong..
But At last.. I would say it all depends on what is his final requirement.. and How he is filling Internal tables IT_VBAK and IT_VBAP
Thanks & Regards
ilesh 24x7
ilesh Nandaniya
01-29-2010 11:07 AM
Yeah its correct,
But chances are there that the table itab_vbak might have been populated based on some conditions..
If its just populated without any filtering then what you say is correct.
01-29-2010 11:47 AM
Keshav is right here.
VBAK and VBAP have mixed entries, some entries are not present in VBAP which are present in VBAK and vice versa.
READ is hence needed.
Anyways, Thanks to all the solution providers for giving so many alternatives. I have read all of them and will use the suitable one based on my requirement.
Thanks a lot to everyone once again.
01-29-2010 12:22 PM
STOP!.
It's obvious that both internal tables were sourced from SAP transparent tables at some point, so why on earth are you looping through and performing this riduculous process?.
Firstly, you should have performed one SQL extract and put the same logic into the WHERE clause. Let the database do the work, that's what it's built for.
For example:
SELECT ....
INTO itab_vbap
FROM vbap as a
INNER JOIN vbak as b ON avbeln EQ bvbeln
AND ....
WHERE zshiodate IN s_fsd.
Secondly, if you have to ignore this advice and continue with this LOOP then at least limit what's return by the LOOP and READ statement, using TRANSPORTING NO FIELDS, or transporting just the fields required. You don't need to use all fields, if you just intend to delete the record.
Jason
(With help from Dr Sidewalk.)
Edited by: Jason Stratham on Jan 29, 2010 1:43 PM
04-22-2010 2:25 PM