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: 

SAP QUERY LOOPS AND INTERNAL TABLE

adeel_sarwar
Contributor
0 Kudos

Hi All, I have a query which i have made. It runs from Table EKPO which has PO details and what I want to do is now via ABAP Code pull through the total of goods receipt for the PO and Line Item into a field. Sounds Easy enough..Problem now,

The table which contains the GR data is EKBE which agains a PO and Line Item can have many 101 movements and 102 movements so what I want is an ABAP Statent to basically sum up the total of 101 for the PO & LINE ITEMS and then minus this from the total of 102 for the PO & LINE ITEMS and post the result in to this new field I have created.

I am pretty decent with ABAP Code in Querys I.e Select statements etc but from what I can see i need to create an internal table and do a loop and collect statement but I keep on failing due to not enough knowledge. Please can some one help me with this and provide me with the code and explanation as i would like to understand,

POINTS WILL BE REWARDED

Thanks

Kind Regards

Adeel Sarwar

7 REPLIES 7

naimesh_patel
Active Contributor
0 Kudos

Ok.. for that you can use the collect statement.

1. Select your data from EKBE into IT_EKBE.

2. Now, collect it into one table

DATA:  BEGIN OF IT_SUM OCCURS 0,
EBELN TYPE EBELN,
EBELP TYPE EBELP,
DMBTR TYPE DMBTR,
MENGE TYPE MENGE,
END OF IT_SUM.

LOOP AT IT_EKBE.
  MOVE CORRESPOING IT_EKBE TO IT_SUM.
  IF IT_EKBE-BWART = '102'.
    IT_SUM-DMBTR = IT_SUM-DMBTR * -1.
    IT_SUM-MENGE = IT_SUM-MENGE * -1.
  ENIDF.
  COLLECT IT_SUM.
  CLEAR IT_SUM.
ENDLOOP.

3. In this table IT_SUM you will have the total for PO and ITEM.

Regards,

Naimesh Patel

0 Kudos

Hi Naimesh,

Sorry to be a pain but how do you select the data from EKBE in to IT_EKBE.

This is my current code and I have no Idea how to do the select statement with into, I would really appreciate it if y ou could give me the full code to put in the query. The new field I have created is called QTYD. I Understand your code but am struggling to do the 1st part "Select your data from EKBE into IT_EKBE"

Thanks Again

TABLES: EKBE.

CLEAR: QTYD.

SELECT * FROM EKBE

WHERE EBELN = EKPO-EBELN

AND EBELP = EKPO-EBELP

AND BEWTP = 'E'

AND BWART = '101'.

If sy-subrc = 0.

QTYD = EKBE-MENGE.

ELSE.

QTYD = '0'.

ENDIF.

ENDSELECT.

0 Kudos

Hi,

This is the full code i have entered but its not working. Any help would be appreciated. If you could rectify the code and internal tables that would be great.

Thanks

TABLES: EKBE.

DATA: PurO LIKE EKPO-EBELN,

POLI LIKE EKPO-EBELP.

*New Table and Vars defined

DATA: BEGIN OF IT_EKBE,

IT_EKBE LIKE EKBE,

END OF IT_EKBE.

DATA: BEGIN OF IT_SUM OCCURS 0,

EBELN TYPE EBELN,

EBELP TYPE EBELP,

DMBTR TYPE DMBTR,

MENGE TYPE MENGE,

END OF IT_SUM.

CLEAR: QTYD.

MOVE: EKPO-EBELN TO PurO,

EKPO-EBELP TO POLI.

SELECT * FROM EKBE INTO IT_EKBE

WHERE EBELN = PurO

AND EBELP = POLI

AND BEWTP = 'E'

LOOP AT IT_EKBE.

MOVE CORRESPOING IT_EKBE TO IT_SUM.

IF IT_EKBE-BWART = '102'.

IT_SUM-DMBTR = IT_SUM-DMBTR * -1.

IT_SUM-MENGE = IT_SUM-MENGE * -1.

ENIDF.

COLLECT IT_SUM.

CLEAR IT_SUM.

ENDLOOP.

ENDSELECT.

If sy-subrc = 0.

QTYD = IT_SUM.

ELSE.

QTYD = 0.

ENDIF.

0 Kudos

hey,

You can use the following query to select data.



select ebeln ebelp bwart dmbtr menge from ekbe into table it_ekbe  for all entries in it_ekpo where ebeln = it_ekpo-ebeln
            and    ebelp = it_ekpo-ebelp.

Assuming you already have the range of PO's for which the processing has to be done in it_ekpo table.

Do the processing as suggested by Naimesh.

regards,

Anubhav

adeel_sarwar
Contributor

still not working

0 Kudos

Change the declaration of the internal table

DATA: BEGIN OF IT_EKBE,

IT_EKBE LIKE EKBE,

END OF IT_EKBE.

TO

DATA:IT_EKBE type standard table of EKBE with header line.

adeel_sarwar
Contributor
0 Kudos

Got it working from reading other threads and help from you guys thanks