---
title: "ABAP--如何快速从BSEG读取数据"
author: "Jack Wu"
date: "2008-05-10T23:28:19Z"
tags: []
slug: "abap-bseg"
draft: false
lastmod: "2008-05-10T23:28:19Z"
id: 229282
url: "https://silverw0396.iteye.com/blog/229282"
---

由于bseg表很大，而且表的索引字段是：

> BUKRS - Company Code  
> BELNR - Accounting Document Number  
> GJAHR - Fiscal Year  
> BUZEI - Line Item Number

 访问此表最佳的是包含所有的索引字段，但在实际应用是很少能达到的。但要有好的性能根据经验必须要有公司和凭证号作为查询条件。为了达到这一点，我可以根据条件不同而使用不同ｓａｐ的其他表，先查出公司和凭证号，再去读取ＢＳＥＧ表**。**

**代码如下：**

**注意：该程序包含了各种条件的查询，程序员可以根据自己应用需求选择对应一个的子程序来读取公司和凭证号。**

REPORT ztest\_select.

\* Tables \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
TABLES: bkpf, bseg,  
        covp, csks,  
        glpca,  
        bsis, bsas, bsid, bsad, bsik, bsak,  
        ekbe, aufk,  
        vbfa, vbak,  
        vapma,  
        fmifiit,  
        payr.

\* Global Data \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**

TYPES: BEGIN OF doc,  
        bukrs TYPE bseg-bukrs,  
        belnr TYPE bseg-belnr,  
        gjahr TYPE bseg-gjahr,  
        buzei TYPE bseg-buzei,  
      END   OF doc.

DATA: doc\_int  TYPE TABLE OF doc,  
      doc\_wa   TYPE          doc,  
      w\_repid  TYPE sy-repid VALUE sy-repid,  
      no\_lines TYPE sy-tabix.

\* Selection Screen \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\**  
PARAMETERS: p\_gjahr TYPE covp-refgj OBLIGATORY.  
SELECTION-SCREEN SKIP.  
PARAMETERS: p\_kokrs TYPE csks-kokrs OBLIGATORY,  
            p\_kostl TYPE csks-kostl,  
            p\_prctr TYPE glpca-rprctr,  
            p\_aufnr TYPE aufk-aufnr.  
SELECTION-SCREEN SKIP.  
PARAMETERS: p\_bukrs TYPE bsis-bukrs OBLIGATORY,  
            p\_budat TYPE bkpf-budat,  
            p\_ebeln TYPE ekko-ebeln,  
            p\_hkont TYPE bsis-hkont,  
            p\_lifnr TYPE bsik-lifnr,  
            p\_kunnr TYPE bsid-kunnr.  
SELECTION-SCREEN SKIP.  
PARAMETERS: p\_vbeln TYPE vbak-vbeln.  
SELECTION-SCREEN SKIP.  
PARAMETERS: p\_matnr TYPE vapma-matnr.  
SELECTION-SCREEN SKIP.  
PARAMETERS: p\_fikrs TYPE fmifiit-fikrs,  
            p\_fistl TYPE fmifiit-fistl,  
            p\_fonds TYPE fmifiit-fonds.  
SELECTION-SCREEN ULINE.  
PARAMETERS: p\_hbkid TYPE payr-hbkid,  
            p\_hktid TYPE payr-hktid,  
            p\_rzawe TYPE payr-rzawe,  
            p\_chect TYPE payr-chect.

START-OF-SELECTION.

\* Retrieve document numbers based on different requirements

*** Posting Date　(用日期做查询条件)**  
  PERFORM posting\_date\_actuals  
    USING p\_bukrs  
          p\_budat.

*** Cost Center**  
  PERFORM cost\_center\_actuals  
    USING p\_kokrs  
          p\_kostl  
          p\_gjahr.

*** GL Account**  
  PERFORM gl\_actuals  
    USING p\_bukrs  
          p\_hkont  
          p\_gjahr.

*** Vendor**  
  PERFORM vendor\_actuals  
    USING p\_bukrs  
          p\_lifnr  
          p\_gjahr.

*** Customer**  
  PERFORM customer\_actuals  
    USING p\_bukrs  
          p\_kunnr  
          p\_gjahr.

*** Purchase Order**  
  PERFORM po\_actuals  
    USING p\_ebeln.

*** Sales Order**  
  PERFORM so\_actuals  
    USING p\_vbeln.

*** Order**  
  PERFORM order\_actuals  
    USING p\_aufnr  
          p\_gjahr.

*** Fund/Fund Center**  
  PERFORM fm\_actuals  
    USING p\_fikrs  
          p\_gjahr  
          p\_fistl  
          p\_fonds.

*** Profit Center**  
  PERFORM profit\_center\_actuals  
    USING p\_kokrs  
          p\_prctr  
          p\_gjahr.

*** Material**  
  PERFORM material\_actuals  
    USING p\_matnr  
          p\_gjahr.

*** Cheque number**  
  PERFORM cheque\_actuals  
    USING p\_hbkid  
          p\_hktid  
          p\_chect.

\*&---------------------------------------------------------------------*  
\*&      Form  posting\_date\_actuals  
\*&---------------------------------------------------------------------*  
*       Use one of the secondary indices of BKPF to retrieve the  
*       document number  
\*----------------------------------------------------------------------*  
FORM posting\_date\_actuals  
  USING    bukrs  
           budat.

  DATA: disp\_date(10).

  CHECK NOT budat IS INITIAL.

\* Uses index BKPF~2 (4.7)  
  SELECT bukrs belnr gjahr  
    INTO TABLE doc\_int  
    UP TO 100 ROWS  
    FROM bkpf  
    WHERE bukrs = bukrs  AND  
\* Normally, you would probably only want normal documents, that is  
\* documents with BSTAT = ' '. So you would change the next line.  
\* On the other hand, you might want documents for all values of BSTAT,  
\* but not want to hardcode the values. In that case, you can retrieve  
\* values from the domain of BSTAT and put them in a range table and  
\* use the range table in the next line.  
          bstat IN (' ', 'A', 'B', 'D', 'M', 'S', 'V', 'W', 'Z') AND  
          budat = budat.

  CHECK sy-subrc = 0.  
  WRITE budat TO disp\_date.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'Posting date'  
          disp\_date  
          space  
          space.

ENDFORM.                    " posting\_date\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  cost\_center\_actuals  
\*&---------------------------------------------------------------------*  
*       Retrieve documents for a cost center  
\*----------------------------------------------------------------------*  
FORM cost\_center\_actuals  
  USING    kokrs  
           kostl  
           gjahr.

  DATA: covp\_int TYPE TABLE OF covp,  
        disp\_cc(10).

  CHECK NOT kostl IS INITIAL.

\* Uses primary index (4.7)  
  SELECT SINGLE objnr  
    FROM csks  
    INTO csks-objnr  
    WHERE kokrs = kokrs  
      AND kostl = kostl.

  CHECK sy-subrc = 0.

\* COVP is a view. This uses index COEP~1 (4.7)  
  SELECT refbk refbn refgj refbz  
    FROM covp  
    INTO TABLE doc\_int  
    UP TO 100 ROWS  
    WHERE lednr = '00'  
      AND objnr = csks-objnr  
      AND gjahr = gjahr  
      AND wrttp IN ('04', '11')  
      AND versn = '000'.

  CHECK sy-subrc = 0.

  CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
    EXPORTING  
      input  = kostl  
    IMPORTING  
      output = disp\_cc.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'Cost Center'  
          disp\_cc  
          space  
          space.

ENDFORM.                    " cost\_center\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  gl\_actuals  
\*&---------------------------------------------------------------------*  
*       BKPF and BSEG have a number of secondary index tables. These are  
*       tables that are indexed by GL customer or vendor number and have  
*       data that is in both BKPF and BSEG. These secondary index tables  
*       that have an 'i' in the third character of the name contain open  
*       items. Those with an 'a' contain cleared items. In practice, you  
*       may only one or the other. In this program I am retrieving both.  
\*  
*       Here we get documents related to a GL.  
\*----------------------------------------------------------------------*  
FORM gl\_actuals  
  USING    bukrs  
           hkont  
           gjahr.

  DATA: disp\_gl(10).

  CHECK NOT hkont IS INITIAL.

\* Uses primary index (4.7)  
  SELECT bukrs belnr gjahr buzei  
    FROM bsis  
    INTO TABLE doc\_int  
    UP TO 100 ROWS  
    WHERE bukrs = bukrs  
      AND hkont = hkont  
      AND gjahr = gjahr.

\* Uses primary index (4.7)  
  SELECT bukrs belnr gjahr buzei  
    FROM bsas  
    APPENDING TABLE doc\_int  
    UP TO 100 ROWS  
    WHERE bukrs = bukrs  
      AND hkont = hkont  
      AND gjahr = gjahr.

  DESCRIBE TABLE doc\_int LINES no\_lines.  
  CHECK no\_lines &gt; 0.

  CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
    EXPORTING  
      input  = hkont  
    IMPORTING  
      output = disp\_gl.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'GL Account'  
          disp\_gl  
          space  
          space.

ENDFORM.                    " gl\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  vendor\_actuals  
\*&---------------------------------------------------------------------*  
*       Here we get documents related to a vendor.  
\*----------------------------------------------------------------------*  
FORM vendor\_actuals  
  USING    bukrs  
           lifnr  
           gjahr.

  DATA: disp\_vendor(10).

  CHECK NOT lifnr IS INITIAL.

\* Uses primary index (4.7)  
  SELECT bukrs belnr gjahr buzei  
    FROM bsik  
    INTO TABLE doc\_int  
    UP TO 100 ROWS  
    WHERE bukrs = bukrs  
      AND lifnr = lifnr  
      AND gjahr = gjahr.

\* Uses primary index (4.7)  
  SELECT bukrs belnr gjahr buzei  
    FROM bsak  
    APPENDING TABLE doc\_int  
    UP TO 100 ROWS  
    WHERE bukrs = bukrs  
      AND lifnr = lifnr  
      AND gjahr = gjahr.

  DESCRIBE TABLE doc\_int LINES no\_lines.  
  CHECK no\_lines &gt; 0.

  CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
    EXPORTING  
      input  = lifnr  
    IMPORTING  
      output = disp\_vendor.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'Vendor'  
          disp\_vendor  
          space  
          space.

ENDFORM.                    " vendor\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  customer\_actuals  
\*&---------------------------------------------------------------------*  
*       Here we get documents related to a customer.  
\*----------------------------------------------------------------------*  
FORM customer\_actuals  
  USING  bukrs  
         kunnr  
         gjahr.

  DATA: disp\_customer(10).

  CHECK NOT kunnr IS INITIAL.

\* Uses primary index (4.7)  
  SELECT bukrs belnr gjahr buzei  
    FROM bsid  
    INTO TABLE doc\_int  
    UP TO 100 ROWS  
    WHERE bukrs = bukrs  
      AND kunnr = kunnr  
      AND gjahr = gjahr.

\* Uses primary index (4.7)  
  SELECT bukrs belnr gjahr buzei  
    FROM bsad  
    APPENDING TABLE doc\_int  
    UP TO 100 ROWS  
    WHERE bukrs = bukrs  
      AND kunnr = kunnr  
      AND gjahr = gjahr.

  DESCRIBE TABLE doc\_int LINES no\_lines.  
  CHECK no\_lines &gt; 0.

  CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
    EXPORTING  
      input  = kunnr  
    IMPORTING  
      output = disp\_customer.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'Customer'  
          disp\_customer  
          space  
          space.

ENDFORM.                    " customer\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  po\_actuals  
\*&---------------------------------------------------------------------*  
*       Table BKPF has a useful index on AWTYP and AWKEY. Here, we use  
*       this to retrieve documents for purchase orders.  
\*----------------------------------------------------------------------*  
FORM po\_actuals  
  USING    ebeln.

  TYPES: BEGIN OF ekbe\_type,  
           belnr TYPE ekbe-belnr,  
           gjahr TYPE ekbe-gjahr,  
           vgabe TYPE ekbe-vgabe,  
         END   OF ekbe\_type.

  DATA: ekbe\_int TYPE TABLE OF ekbe\_type,  
        ekbe\_wa  TYPE          ekbe\_type.

  DATA: v\_reference    TYPE bkpf-awtyp,           "Reference procedure  
        v\_objectkey    TYPE bkpf-awkey.           "Object key

  DATA:disp\_po(10).

  CHECK NOT ebeln IS INITIAL.

\* Uses primary index (4.7)  
  SELECT belnr gjahr  
    FROM ekbe  
    INTO TABLE ekbe\_int  
    UP TO 100 ROWS  
    WHERE ebeln = ebeln  
    AND vgabe IN ('1', '2').                      "1 - GR, 2 - IR

  CHECK sy-subrc = 0.

  SORT ekbe\_int.  
  DELETE ADJACENT DUPLICATES FROM ekbe\_int.

  LOOP AT ekbe\_int INTO ekbe\_wa.  
    v\_objectkey+00(10) = ekbe\_wa-belnr.  
    v\_objectkey+10(10) = ekbe\_wa-gjahr.           "BELNR+YEAR

    IF ekbe\_wa-vgabe = '1'.  
      v\_reference = 'MKPF'.  
    ELSE.  
      v\_reference = 'RMRP'.  
    ENDIF.

\* Uses index BKPF~4 (4.7)  
    SELECT SINGLE bukrs belnr gjahr               "Accounting Doc Header  
      FROM bkpf  
      INTO doc\_wa  
      WHERE awtyp =  v\_reference  
        AND awkey =  v\_objectkey.  
    IF sy-subrc = 0.  
      APPEND doc\_wa TO doc\_int.  
    ENDIF.  
  ENDLOOP.

  CHECK no\_lines &gt; 0.

  CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
    EXPORTING  
      input  = ebeln  
    IMPORTING  
      output = disp\_po.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'Purchase Order'  
          disp\_po  
          space  
          space.

ENDFORM.                    " po\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  so\_actuals  
\*&---------------------------------------------------------------------*  
*       Use AWTYP and AWKEY to retrieve documents related to sales  
\*----------------------------------------------------------------------*  
FORM so\_actuals  USING    vbeln.

  TYPES: BEGIN OF vbfa\_type,  
           vbeln TYPE vbfa-vbeln,  
         END   OF vbfa\_type.

  DATA: vbfa\_int TYPE TABLE OF vbfa\_type,  
        vbfa\_wa  TYPE          vbfa\_type.

  DATA: v\_reference    TYPE bkpf-awtyp,           "Reference procedure  
        v\_objectkey    TYPE bkpf-awkey.           "Object key

  DATA:disp\_so(10).

  CHECK NOT vbeln IS INITIAL.

\* Uses primary index (4.7)  
  SELECT vbeln  
    FROM vbfa  
    INTO TABLE vbfa\_int  
    UP TO 100 ROWS  
    WHERE vbelv   = vbeln  
      AND vbtyp\_n = 'P'.                          "Debit memo

  CHECK sy-subrc = 0.

  SORT vbfa\_int.  
  DELETE ADJACENT DUPLICATES FROM vbfa\_int.

  LOOP AT vbfa\_int INTO vbfa\_wa.  
    v\_objectkey+00(10) = vbfa\_wa-vbeln.           "BELNR  
    v\_reference        = 'VBRK'.

\* Uses index BKPF~4 (4.7)  
    SELECT SINGLE bukrs belnr gjahr               "Accounting Doc Header  
      FROM bkpf  
      INTO doc\_wa  
      WHERE awtyp =  v\_reference  
        AND awkey =  v\_objectkey.  
    IF sy-subrc = 0.  
      APPEND doc\_wa TO doc\_int.  
    ENDIF.  
  ENDLOOP.

  DESCRIBE TABLE doc\_int LINES no\_lines.  
  CHECK no\_lines &gt; 0.

  CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
    EXPORTING  
      input  = vbeln  
    IMPORTING  
      output = disp\_so.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'Sales Document'  
          disp\_so  
          space  
          space.

ENDFORM.                    " so\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  order\_actuals  
\*&---------------------------------------------------------------------*  
*       Retrieve documents related to an order  
\*----------------------------------------------------------------------*  
FORM order\_actuals  
  USING    aufnr  
           gjahr.

  DATA: disp\_order(10).

  CHECK NOT aufnr IS INITIAL.

\* Uses primary index (4.7)  
  SELECT SINGLE objnr  
    FROM aufk  
    INTO aufk-objnr  
    WHERE aufnr = aufnr.

  CHECK sy-subrc = 0.

\* COVP is a view. This uses index COEP~1 (4.7)  
  SELECT refbk refbn refgj refbz  
    FROM covp  
    INTO TABLE doc\_int  
    UP TO 100 ROWS  
    WHERE lednr = '00'  
      AND objnr = aufk-objnr  
      AND gjahr = gjahr  
      AND wrttp IN ('04', '11')  
      AND versn = '000'.

  CHECK sy-subrc = 0.

  CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
    EXPORTING  
      input  = aufnr  
    IMPORTING  
      output = disp\_order.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'Order'  
          disp\_order  
          space  
          space.

ENDFORM.                    " order\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  FM\_actuals  
\*&---------------------------------------------------------------------*  
*       Not many institutions use Funds Management, but if you do, this  
*       is how to relate funds management documents to FI documents.  
\*----------------------------------------------------------------------*  
FORM fm\_actuals  
  USING    fikrs  
           gjahr  
           fistl  
           fonds.

  DATA: disp\_cfc(10),  
        disp\_fund(10).

  CHECK NOT fikrs IS INITIAL AND  
        NOT fistl IS INITIAL.

\* Uses index FMIFIIT~3 (4.7)  
  SELECT bukrs knbelnr kngjahr knbuzei  
    FROM  fmifiit  
    INTO TABLE doc\_int  
    UP TO 100 ROWS  
         WHERE  fistl  = fistl  
         AND    fonds  = fonds.

  CHECK sy-subrc = 0.

  CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
    EXPORTING  
      input  = fistl  
    IMPORTING  
      output = disp\_cfc.

  IF NOT fonds IS INITIAL.  
    CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
      EXPORTING  
        input  = fonds  
      IMPORTING  
        output = disp\_fund.  
  ENDIF.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'Fund Center'  
          disp\_cfc  
          'Fund'  
          disp\_fund.

ENDFORM.                    " FM\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  profit\_center\_actuals  
\*&---------------------------------------------------------------------*  
*       Retrieve documents related to a profit center  
\*----------------------------------------------------------------------*  
FORM profit\_center\_actuals  
  USING    kokrs  
           prctr  
           gjahr.

  DATA: disp\_pc(10).

  CHECK NOT prctr IS INITIAL.

\* This uses index GLPCA~1 (4.7)  
  SELECT rbukrs refdocnr refryear refdocln  
    FROM glpca  
    INTO TABLE doc\_int  
    UP TO 100 ROWS  
    WHERE kokrs  = kokrs  
      AND ryear  = gjahr  
      AND rprctr = prctr  
      AND awtyp  = 'BKPF'.

  CHECK sy-subrc = 0.

  CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
    EXPORTING  
      input  = prctr  
    IMPORTING  
      output = disp\_pc.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'Profit Center'  
          disp\_pc  
          space  
          space.

ENDFORM.                    " profit\_center\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  material\_actuals  
\*&---------------------------------------------------------------------*  
*       Get FI documents for a material.  
*       For purchase orders get:  
*         goods receipts  
*         invoice receipts  
*       For sales orders get:  
*         debit memos  
\*----------------------------------------------------------------------*  
FORM material\_actuals  
  USING    matnr  
           gjahr.

  TYPES: BEGIN OF ekpo\_type,  
           ebeln TYPE ekpo-ebeln,  
           ebelp TYPE ekpo-ebelp,  
         END   OF ekpo\_type.

  TYPES: BEGIN OF ekbe\_type,  
           belnr TYPE ekbe-belnr,  
           gjahr TYPE ekbe-gjahr,  
           vgabe TYPE ekbe-vgabe,  
         END   OF ekbe\_type.

  TYPES: BEGIN OF vapma\_type,  
           vbeln TYPE vapma-vbeln,  
           posnr TYPE vapma-posnr,  
         END   OF vapma\_type.

  TYPES: BEGIN OF vbfa\_type,  
           vbeln TYPE vbfa-vbeln,  
           posnv TYPE vbfa-posnv,  
         END   OF vbfa\_type.

  DATA: ekpo\_int TYPE TABLE OF ekpo\_type,  
        ekpo\_wa  TYPE          ekpo\_type.

  DATA: ekbe\_int TYPE TABLE OF ekbe\_type,  
        ekbe\_wa  TYPE          ekbe\_type.

  DATA: vapma\_int TYPE TABLE OF vapma\_type,  
        vapma\_wa  TYPE          vapma\_type.

  DATA: v\_reference    TYPE bkpf-awtyp,           "Reference procedure  
        v\_objectkey    TYPE bkpf-awkey.           "Object key

  DATA: vbfa\_int TYPE TABLE OF vbfa\_type,  
        vbfa\_wa  TYPE          vbfa\_type.

  DATA: disp\_mat(10).

  CHECK NOT matnr IS INITIAL.

\* First, find purchase orders for the material  
\* This uses index EKPO~1 (4.7)  
  SELECT ebeln ebelp  
    FROM ekpo  
    INTO TABLE ekpo\_int  
    UP TO 100 ROWS  
    WHERE matnr = matnr.

\* Now get the FI document numbers as above  
\* Uses primary index (4.7)  
  SELECT belnr gjahr vgabe  
    FROM ekbe  
    INTO TABLE ekbe\_int  
    UP TO 100 ROWS  
    FOR ALL ENTRIES IN ekpo\_int  
    WHERE ebeln = ekpo\_int-ebeln  
      AND ebelp = ekpo\_int-ebelp  
      AND vgabe IN ('1', '2').                      "1 - GR, 2 - IR

  CHECK sy-subrc = 0.

  SORT ekbe\_int.  
  DELETE ADJACENT DUPLICATES FROM ekbe\_int.

  LOOP AT ekbe\_int INTO ekbe\_wa.  
    v\_objectkey+00(10) = ekbe\_wa-belnr.  
    v\_objectkey+10(10) = ekbe\_wa-gjahr.           "BELNR+YEAR

    IF ekbe\_wa-vgabe = '1'.  
      v\_reference = 'MKPF'.  
    ELSE.  
      v\_reference = 'RMRP'.  
    ENDIF.

\* Uses index BKPF~4 (4.7)  
    SELECT SINGLE bukrs belnr gjahr               "Accounting Doc Header  
      FROM bkpf  
      INTO doc\_wa  
      WHERE awtyp =  v\_reference  
        AND awkey =  v\_objectkey.  
    IF sy-subrc = 0.  
      APPEND doc\_wa TO doc\_int.  
    ENDIF.  
  ENDLOOP.

\* Next get the sales orders for the material

\* Note - I am using an artificial date range here because of archiving  
\* in our system. Feel free to remove it.  
  SELECT vbeln posnr  
    FROM vapma  
    INTO TABLE vapma\_int  
    UP TO 100 ROWS  
    WHERE matnr = matnr  
      AND audat BETWEEN '20070101' AND '20071231'.

\* Uses primary index (4.7)  
  SELECT vbeln posnv  
    FROM vbfa  
    INTO TABLE vbfa\_int  
    FOR ALL ENTRIES IN vapma\_int  
    WHERE vbelv   = vapma\_int-vbeln  
      AND posnv   = vapma\_int-posnr  
      AND vbtyp\_n = 'P'.                          "Debit memo

  CHECK sy-subrc = 0.

  SORT vbfa\_int.  
  DELETE ADJACENT DUPLICATES FROM vbfa\_int.

  LOOP AT vbfa\_int INTO vbfa\_wa.  
    CLEAR: v\_objectkey,  
           v\_reference.  
    v\_objectkey+00(10) = vbfa\_wa-vbeln.           "BELNR  
    v\_reference        = 'VBRK'.

\* Uses index BKPF~4 (4.7)  
    SELECT SINGLE bukrs belnr gjahr               "Accounting Doc Header  
      FROM bkpf  
      INTO doc\_wa  
      WHERE awtyp =  v\_reference  
        AND awkey =  v\_objectkey.  
    IF sy-subrc = 0.  
      APPEND doc\_wa TO doc\_int.  
    ENDIF.  
  ENDLOOP.

  DESCRIBE TABLE doc\_int LINES no\_lines.  
  CHECK no\_lines &gt; 0.

  CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
    EXPORTING  
      input  = matnr  
    IMPORTING  
      output = disp\_mat.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'Material'  
          disp\_mat  
          space  
          space.

ENDFORM.                    " material\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  cheque\_actuals  
\*&---------------------------------------------------------------------*  
*       Retrieve FI documents for a cheque. We are assuming Accounts  
*       Payable related.  
\*----------------------------------------------------------------------*  
FORM cheque\_actuals  
  USING    hbkid  
           hktid  
           chect.  
  DATA: belnr TYPE payr-vblnr,  
        gjahr TYPE payr-gjahr,  
        lifnr TYPE payr-lifnr,  
        laufd TYPE payr-laufd,  
        disp\_cheque(13).

  CHECK NOT p\_chect IS INITIAL.

\* Retrieve the cheque data using the primary key (4.7)  
\* We are assuming a vendor payment here  
  SELECT SINGLE vblnr gjahr lifnr laufd  
    FROM  payr  
    INTO (belnr, gjahr, lifnr, laufd )  
    WHERE  zbukr  = p\_bukrs  
      AND  hbkid  = p\_hbkid  
      AND  hktid  = p\_hktid  
      AND  rzawe  = p\_rzawe  
      AND  chect  = p\_chect.

\* Now get the accounting documents  
  SELECT bukrs belnr gjahr  
    FROM  bsak  
    INTO TABLE doc\_int  
         WHERE  bukrs  = p\_bukrs  
         AND    lifnr  = lifnr  
         AND    umsks  = ' '  
         AND    umskz  = ' '  
         AND    augdt  = laufd  
         AND    augbl  = belnr  
         AND    gjahr  = gjahr.

  DESCRIBE TABLE doc\_int LINES no\_lines.  
  CHECK no\_lines &gt; 0.

  CALL FUNCTION 'CONVERSION\_EXIT\_ALPHA\_OUTPUT'  
    EXPORTING  
      input  = chect  
    IMPORTING  
      output = disp\_cheque.

  PERFORM display\_documents  
    TABLES doc\_int  
    USING 'Cheque number'  
          disp\_cheque  
          space  
          space.

ENDFORM.                    " cheque\_actuals

\*&---------------------------------------------------------------------*  
\*&      Form  display\_documents  
\*&---------------------------------------------------------------------*  
*       At this point, you have the company code, document number and  
*       fiscal year in table DOC\_INT. Here, I just use a standard SAP  
*       function module and transaction to display documents and  
*       details, but you could use the same information to retrieve  
*       document data from BKPF and/or BSEG.  
\*----------------------------------------------------------------------*  
FORM display\_documents  
  TABLES   doc\_int STRUCTURE doc\_wa  
  USING    doc\_source\_1  
           source\_value\_1  
           doc\_source\_2  
           source\_value\_2.

  TYPE-POOLS: slis.

  DATA: sel     TYPE  slis\_selfield.  
  DATA: title   TYPE string.

  CONCATENATE 'FI line items for' doc\_source\_1 source\_value\_1  
               INTO title SEPARATED BY space.

  IF NOT source\_value\_2 IS INITIAL.  
    CONCATENATE title doc\_source\_2 source\_value\_2  
                INTO title SEPARATED BY space.  
  ENDIF.

  CALL FUNCTION 'REUSE\_ALV\_POPUP\_TO\_SELECT'  
    EXPORTING  
      i\_title                 = title  
      i\_selection             = 'X'  
      i\_tabname               = 'BSEG'  
      i\_structure\_name        = 'BSEG'  
      i\_callback\_user\_command = 'USER\_COMMAND'  
      i\_callback\_program      = w\_repid  
    IMPORTING  
      es\_selfield             = sel  
    TABLES  
      t\_outtab                = doc\_int.

  IF NOT sel IS INITIAL.  
    READ TABLE doc\_int INDEX sel-tabindex.  
    SET PARAMETER ID 'BUK' FIELD doc\_int-bukrs.  
    SET PARAMETER ID 'BLN' FIELD doc\_int-belnr.  
    SET PARAMETER ID 'GJR' FIELD doc\_int-gjahr.  
    CALL TRANSACTION 'FB03' AND SKIP FIRST SCREEN.  
  ENDIF.

ENDFORM.                    " display\_documents