SAP 内表 结构 综合练习 abap,一个需求,三种方式实现,注释 部分可以单独运行,带程序 效率 分析。
程序员文章站
2022-05-18 08:08:26
...
* 需求:查询采购订单项目,选择条件:订单号,输出结果,行项目,物料号,公司代码,库存地点。
三种方式实现,注释 部分可以单独运行,带程序 效率 分析。
abap 代码:
*&---------------------------------------------------------------------*
*& Report ZYHJ20200217
*&---------------------------------------------------------------------*
*&
*&---------------------------------------------------------------------*
*REPORT zyhj20200217.
*TABLES:ekpo.
*DATA: BEGIN OF t_write OCCURS 0,
* ebeln LIKE ekpo-ebeln,
* ebelp LIKE ekpo-ebelp,
* matnr LIKE ekpo-matnr,
* bukrs LIKE ekpo-bukrs,
* lgort LIKE ekpo-lgort,
* END OF t_write.
*
*SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE TEXT-001.
*
* SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln.
*
*SELECTION-SCREEN END OF BLOCK blk1.
*
*SELECT ebeln ebelp matnr bukrs lgort INTO CORRESPONDING FIELDS OF TABLE t_write
* FROM ekpo WHERE ebeln IN s_ebeln .
*LOOP AT t_write.
* WRITE:/ t_write-ebeln,t_write-ebelp,t_write-matnr,t_write-bukrs,t_write-lgort.
*ENDLOOP.
REPORT zyhj20200217 NO STANDARD PAGE HEADING LINE-SIZE 120 LINE-COUNT 65(2).
* 需求:查询采购订单项目,选择条件:订单号,输出结果,行项目,物料号,公司代码,库存地点。
TABLES:ekpo,makt.
DATA: BEGIN OF i_data OCCURS 0 , "with header line
ebelp LIKE ekpo-ebelp,
matnr LIKE ekpo-matnr,
bukrs LIKE ekpo-bukrs,
lgort LIKE ekpo-lgort,
maktx LIKE makt-maktx,
END OF i_data,
t1 type i, t2 type i, t3 type i, time_1 TYPE TIMESTAMP,time_2 TYPE TIMESTAMP,time_3 TYPE TIMESTAMP.
SELECTION-SCREEN BEGIN OF BLOCK blk1 WITH FRAME TITLE text001.
SELECT-OPTIONS: s_ebeln FOR ekpo-ebeln. "项目号 aaa
SELECTION-SCREEN END OF BLOCK blk1.
* PARAMETERS Company AS CHECKBOX.
* 检查屏幕
*AT SELECTION-SCREEN.
* 程序的初始化
*INITIALIZATION.
* 开始取数据 别名
*START-OF-SELECTION.
DATA:
wcl_container type ref to cl_gui_custom_container, "存放ALV的容器
wcl_alv type ref to cl_gui_alv_grid, "ALV的网络
gt_fieldcat type lvc_t_fcat, "存放字段目录的内表
gs_layout type lvc_s_layo. "布局结构
DATA:lt_sflight type table of sflight,wa_sflight type sflight.
**** 第一种分两次单表查询取数,第一次取完数,存在内表中,凭此数据,在循环中内表数据(物料描述),再去修改。效率比下面的方法快10倍
*get run time field t1.
* SELECT A~EBELP A~MATNR A~BUKRS A~LGORT INTO
* CORRESPONDING FIELDS OF TABLE I_DATA " 高效一点的做法 SELECT INTO CORRESPONDING TABLE
* FROM EKPO AS A WHERE A~EBELN IN s_ebeln."S_EBELN.
*
*
* SELECT MAKTX INTO I_DATA-MAKTX FROM MAKT WHERE MATNR = I_DATA-MATNR ." AND SPRAS ='E'. "加了single ,select 后面的 ENDSELECT ,就可以去掉了。
* APPEND I_DATA.
* CLEAR I_DATA.
* ENDSELECT.
*
*GET RUN TIME FIELD t2.
*T3 = T2 - T1. " TOTAL TIME
*WRITE t3.
**** 第一种分两次单表查询取数,第一次取数在结构中,然后在内表中执行循环,并插入,然后APPEND,完成里层循环,开始二次外场循环
**低效一点的做法 SELECT INTO CORRESPONDING 结构 而不是 TABLE,效率会低一点。
* GET TIME STAMP FIELD time_1.
* SELECT A~EBELP A~MATNR A~BUKRS A~LGORT INTO
* CORRESPONDING FIELDS OF I_DATA "低效一点的做法 SELECT INTO CORRESPONDING 结构I_DATA 而不是 TABLE,效率会低一点。
* FROM EKPO AS A WHERE A~EBELN IN s_ebeln."S_EBELN.
*
* SELECT MAKTX INTO I_DATA-MAKTX FROM MAKT WHERE MATNR = I_DATA-MATNR ." AND SPRAS ='E'. "加了single ,select 后面的 ENDSELECT ,就可以去掉了。
* APPEND I_DATA.
* CLEAR I_DATA.
* ENDSELECT.
* ENDSELECT.
*
* GET TIME STAMP FIELD time_2.
*time_3 = time_2 - time_1. " TOTAL TIME 秒内计数不起作用。
*Write: / 'The short time stamp is:', time_1 . "TIME ZONE SY-ZONLO.
*Write: / 'The short time stamp is:', time_2 . "TIME ZONE SY-ZONLO.
*WRITE time_1.
*WRITE time_2.
*WRITE time_3.
*WRITE /.
*第二种两个表关联取出得数据到结构,然后循环写到内表中,不是最好的办法,已经无问题,除分页,,
*get run time field t1.
* SELECT a~ebelp "采购凭证号
* a~matnr "物料
* a~bukrs "工厂
* a~lgort "库位
* b~maktx "物料描述
* INTO
* (i_data-ebelp,i_data-matnr,i_data-bukrs,i_data-lgort,i_data-maktx) FROM ekpo AS a
* JOIN makt AS b ON a~matnr = b~matnr WHERE a~ebeln IN s_ebeln . " AND b~spras = 'E'.
* APPEND i_data.
* CLEAR i_data.
* ENDSELECT.
* GET RUN TIME FIELD t2.
*T3 = T2 - T1. " TOTAL TIME
*WRITE t3.
* 第二种方法两个表关联的第2种方法,用:CORRESPONDING FIELDS OF 带 TABLE I_DATA
*******说明: select into corespond 不带 TABLE ,采购订单号:3100000003 有结果,且物料描述有值,达到目的,除了分页问题 和 物料描述换行问题外,没有问题。
*get run time field t1.
* SELECT A~EBELP A~MATNR A~BUKRS A~LGORT b~maktx INTO
* CORRESPONDING FIELDS OF TABLE I_DATA "CORRESPONDING FIELDS OF TABLE 这个table 必须去掉
* FROM EKPO AS A JOIN makt AS b ON a~matnr = b~matnr WHERE A~EBELN IN S_EBELN.
*GET RUN TIME FIELD t2.
*T3 = T2 - T1. " TOTAL TIME
*WRITE t3.
****** ENDSELECT. " 不能使用 ENDSELECT,1是 语法不行,二、这样效率低,需要长时间和数据库保持连接状态。
**** 第三种方法 单表取出数据放入内表 select into corespond TABLE ,再用LOOP 循环 开启 查询 ,查到数据,再借用循环修改数据,耗时貌似最长时间
**** 第一个表取值不直接放入TABLE,放入结构后用另外的SQL语句取物料描述,最后append到内表。
get run time field t1.
SELECT A~EBELP A~MATNR A~BUKRS A~LGORT INTO
CORRESPONDING FIELDS OF TABLE I_DATA "CORRESPONDING FIELDS OF TABLE 这个table 必须去掉
FROM EKPO AS A WHERE A~EBELN IN S_EBELN.
* *** 修改数据
LOOP AT i_data.
SELECT single maktx into i_data-maktx
from makt where matnr = i_data-matnr .
modify i_data.
clear i_data. " 加了保险一些。
endloop.
GET RUN TIME FIELD t2.
T3 = T2 - T1. " TOTAL TIME
WRITE t3.
***** 第三种方法end
loop at i_data.
NEW-LINE.
write : at 1 '|' no-gap,i_data-ebelp left-justified,
at 21 '|' no-gap,i_data-MATNR left-justified,
at 30 '|' no-gap,i_data-BUKRS left-justified,
at 41 '|' no-gap,i_data-LGORT left-justified,
at 51 '|' no-gap,i_data-maktx left-justified,
at 90 '|' no-gap.
NEW-LINE.
uline at 1(90).
endloop.
* 定义表头
TOP-OF-PAGE.
WRITE:50 '测试程序表头'.
NEW-LINE.
NEW-LINE.
WRITE: AT 1 '|' NO-GAP,'项目号' LEFT-JUSTIFIED,
AT 21 '|' NO-GAP,'物料编码' LEFT-JUSTIFIED,
AT 30 '|' NO-GAP,'工厂' LEFT-JUSTIFIED,
AT 41 '|' NO-GAP,'库位' LEFT-JUSTIFIED,
AT 51 '|' NO-GAP,'物料描述' LEFT-JUSTIFIED ,
AT 90 '|' NO-GAP.
NEW-LINE.
ULINE AT 1(90).
INCLUDE zyhj20200217_status_9000o01.