Application Development 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: 

Filter data using loop

Former Member
0 Kudos

I am new to abap and I need your help. I am basically creating a function module with import parameter the customer number(KUNNR). and returning 8 tables .

This is the source code of the function module.

*"*"Local Interface:
*" IMPORTING
*" VALUE(CUSTOMER) TYPE KUNNR
*" TABLES
*" GT_VBAK STRUCTURE VBAK
*" GT_VBAP STRUCTURE VBAP
*" GT_LIKP STRUCTURE LIKP
*" GT_LIPS STRUCTURE LIPS
*" GT_VBRK STRUCTURE VBRK
*" GT_VBRP STRUCTURE VBRP
*" GT_VBAK2 STRUCTURE VBAK
*" GT_VBAP2 STRUCTURE VBAP
*" RETURN STRUCTURE BAPIRET2
*"----------------------------------------------------------------------
data: wa_return type bapiret2 .
data gs_vbak type vbak .
data gs_vbap type vbap .
data gs_vbak2 type vbak .
data gs_vbap2 type vbap .
data: gs_likp type likp.
data gs_lips type lips .
data: gs_vbrk type vbrk ,
gs_vbrp type vbrp .
data w_tabix type sy-tabix .

select * from vbak
into corresponding fields of table gt_vbak
where vbak~vbtyp = 'B' or vbak~vbtyp = 'C'
and
vbak~kunnr = customer .


select * from vbap
into corresponding fields of table gt_vbap.
*this works
loop at gt_vbak into gs_vbak.
append gs_vbak to gt_vbak2 .
if gs_vbak-vbtyp = 'B' .
delete gt_vbak where vbtyp = 'C' .
endif.
loop at gt_vbak2 into gs_vbak2.
if gs_vbak2-vbtyp = 'C' .
delete gt_vbak2 where vbtyp = 'B'.
endif.
endloop.

endloop.

*filter vbap-vbeln must be eq to vbak-vbeln. It doesn't work it shows me all vbeln that are *not the same
loop at gt_vbak into gs_vbak .
loop at gt_vbap into gs_vbap .
W_TABIX = SY-TABIX.
if gs_vbap-vbeln ne gs_vbak-vbeln .
delete gt_vbap index w_tabix .
endif.

endloop.
endloop.



select * from vbap
into corresponding fields of table gt_vbap2 .


loop at gt_vbak2 into gs_vbak2 .
loop at gt_vbap2 into gs_vbap2 ."where vbeln eq gs_vbak2-vbeln .
read table gt_vbap2 into gs_vbap2 with key vbeln = gs_vbak-vbeln.
if sy-subrc ne 0.
delete gt_vbap2 .
endif.
W_TABIX = SY-TABIX.

modify gt_vbap2 from gs_vbap2 index w_tabix.

endloop.
endloop.


select * from likp
into corresponding fields of table gt_likp
where kunnr = customer .

select * from lips
into corresponding fields of table gt_lips.

loop at gt_likp into gs_likp .
loop at gt_lips into gs_lips .
if gs_lips-vbeln ne gs_likp-vbeln.
delete gt_lips .
endif.
endloop.
endloop.


select * from vbrk
into corresponding fields of table gt_vbrk
where kunag = customer .


select * from vbrp
into corresponding fields of table gt_vbrp.

loop at gt_vbrk into gs_vbrk.
loop at gt_vbrp into gs_vbrp.
if gs_vbrp-vbeln ne gs_vbrk-vbeln.
delete gt_vbrp .
endif.
endloop.
endloop.

Basically I need a way to filter the records which the vbeln in vbap is equal to the vbeln in vbak.

How can I achieve this ?

2 REPLIES 2

srikanthnalluri
Active Participant
0 Kudos

Hi Rachid, You have to put where condition for the second loop. But from the above code i dont think you are retrieving item data based on header data.

1. corresponding is not necessary and use for all entries to get data from VBAP data

select * from vbak
into of table gt_vbak
where ( vbtyp = 'B' or vbtyp = 'C' ) and kunnr = customer .
if sy-subrc = 0.
* Delete the order which are not required for further processing.
  delete gt_vbak where vbtyp = 'C' or vbtyp = 'B'.
* Get Sales Item data
  if gt_vbak is not initial.
    select * from vbap 
    into of table gt_vbap
    for all entries in gt_vbak
    where vbeln = vbak-vbeln.
  endif.
endif.

2.Not sure why did you take another internal table - GT_VBAK2.

And get the delivery data based on the above sales order.Here you no need to have LOOP statement.

DoanManhQuynh
Active Contributor
0 Kudos

you could have performance issue with select *, into corresponding, select no where conditions...etc so I suggest you re-check your code, requirement first.

Second, your thinking is too complicate. I suppose your requirement is get data from vbap and vbak with each vbtyp = B, vbtyp = C to corresponding output table. I can suggest you doing like this:

SELECT VBAK~*, VBAP~* FROM vbak
 INNER JOIN vbap ON ( vbak~vbeln = vbap~vbeln )
 WHERE vbak~vbtyp IN ( 'B' , 'C' ) INTO TABLE @DATA(lt_all).

LOOP AT lt_all INTO DATA(ls_all).
 IF ls_all-vbak-vbtyp = 'C'.
 APPEND ls_all-vbak TO gt_vbak.
 APPEND ls_all-vbap TO gt_vbap.
 ENDIF.

ENDLOOP.
SORT gt_vbak BY vbeln.
DELETE ADJACENT DUPLICATES FROM lt_vbak.