现在的位置: 首页 > 综合 > 正文

ABAP–如何快速从BSEG读取数据

2013年08月20日 ⁄ 综合 ⁄ 共 5138字 ⁄ 字号 评论关闭

由于bseg表很大,而且表的索引字段是:


BUKRS - Company Code

BELNR - Accounting Document Number

GJAHR - Fiscal Year

BUZEI - Line Item Number


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


代码如下:


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


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)

抱歉!评论已关闭.