Problem solve Get help with specific problems with your technologies, process and projects.

Looping through a structure

This tip is an efficient way to get the needed info by period from tables like COSB, COSS etc.

This tip is an efficient way to get the needed info by period from tables like COSB, COSS etc. These tables store...

data in single record for multiple periods with same object and category. Requirement: Fetch year-to-date totals Traditional coding style would be to: Select all columns (wog001-wog012) and use loop + case statement to get cumulative totals. The becomes lengthy and expensive. Efficient way: Select all columns (wog001-wog012) and use DO command with varying option, verify the code (Section 1) for clarity. This will eliminate the huge code otherwise necessary. If we need get even better and efficient, building dynamic column selection is way to go. Refer to code (section 2). This program was written in v.4.5B and will work on all versions from 3.1h up.


DATA : BEGIN OF i_cosb OCCURS 0,
       lednr LIKE cosb-lednr,
       objnr LIKE cosb-objnr,
       gjahr LIKE cosb-gjahr,
       abkat LIKE cosb-abkat,
       kstar LIKE cosb-kstar,
       wog001 LIKE cosb-wog001,
       wog002 LIKE cosb-wog002,
       wog003 LIKE cosb-wog003,
       wog004 LIKE cosb-wog004,
       wog005 LIKE cosb-wog005,
       wog006 LIKE cosb-wog006,
       wog007 LIKE cosb-wog007,
       wog008 LIKE cosb-wog008,
       wog009 LIKE cosb-wog009,
       wog010 LIKE cosb-wog010,
       wog011 LIKE cosb-wog011,
       wog012 LIKE cosb-wog012,
       END OF i_cosb.

* Dynamic Select list
DATA : BEGIN OF i_flist OCCURS 0,
       line(30),
       END OF i_flist.

****** Section 1. ******
* fetch the desired record, used primary key
SELECT lednr objnr gjahr abkat kstar
       wog001 wog002 wog003 wog004
       wog005 wog006 wog007 wog008
       wog009 wog010 wog011 wog012
INTO TABLE i_cosb
FROM cosb
WHERE lednr EQ '00'
AND objnr EQ 'PR1934567'
AND gjahr EQ '2002'
AND wrttp EQ '32'
AND versn EQ '000'
AND abkat EQ '51'.

IF NOT i_cosb[] IS INITIAL.

* Cumulate all record from previous period.

  LOOP AT i_cosb.

    DO p_period TIMES
    VARYING v_per_wip
    FROM i_cosb-wog001 NEXT i_cosb-wog002.

      v_total = v_total + v_per_wip.

    ENDDO.
  ENDLOOP.
  CLEAR i_cosb.
  FREE i_cosb.
ENDIF.

************ Section 2 ********
* Build column list

SELECT fieldname INTO TABLE i_flist
FROM dd03l
WHERE tabname EQ 'COSB'
AND fieldname LIKE 'WOG%'

* delete not required records
v_idx = p_period + 1.
DELETE i_flist FROM p_period TO 16.

IF NOT i_flist[] IS INITIAL.
  INSERT 'lednr objnr gjahr abkat kstar' INTO i_flist INDEX 1.

**/ fetch current year and current period
  SELECT (i_flist) FROM cosb
  INTO TABLE i_cosb
  WHERE lednr EQ '00'
    AND objnr EQ 'PR123456'
    AND gjahr EQ '2002'
    AND wrttp EQ '32'
    AND versn EQ '000'
    AND abkat IN '51'.

* since only the columns selected from above will have value and others
*  are blank
  LOOP AT i_cosb.
    v_total = v_total +
    i_cosb-wog001 + i_cosb-wog002 +
  i_cosb-wog003 + i_cosb-wog004 +
  i_cosb-wog005 + i_cosb-wog006 +
  i_cosb-wog007 + i_cosb-wog008 +
  i_cosb-wog009 + i_cosb-wog010 +
  i_cosb-wog011 + i_cosb-wog012 .
  ENDLOOP.
ENDIF.

This was last published in October 2002

Dig Deeper on SAP ABAP

PRO+

Content

Find more PRO+ content and other member only offers, here.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

-ADS BY GOOGLE

SearchManufacturingERP

SearchOracle

SearchDataManagement

SearchAWS

SearchBusinessAnalytics

SearchCRM

SearchContentManagement

SearchFinancialApplications

Close