Open nangnangchuai opened 5 years ago
se16
实现单表查询
DATA: wa_EKPO TYPE EKPO, "定义工作区
itab_EKPO TYPE STANDARD TABLE OF EKPO. "定义内表
SELECT-OPTIONS :
S_EBELN FOR wa_EKPO-EBELN. "定义画面中的变量名称
SELECT EBELN MATNR TXZ01 BUKRS LGORT "查询后台数据表的中字段
INTO CORRESPONDING FIELDS OF TABLE itab_EKPO"将查询结果插入到内表中,如果内表的字段不一致就无法插入结果
FROM EKPO "数据来源于那个表
WHERE EBELN IN S_EBELN."条件为屏幕输入的条件与数据标准的条件一致
LOOP AT itab_EKPO INTO wa_EKPO ."读取内表
WRITE:/ wa_EKPO-EBELN,wa_EKPO-MATNR,wa_EKPO-TXZ01,wa_EKPO-BUKRS,wa_EKPO-LGORT."打印出内表字段的结果
ENDLOOP.
实现三个表之间用 inner join 连接
DATA: wa_flights TYPE sv_flights, "定义工作区间
itab_flights TYPE STANDARD TABLE OF sv_flights. "定义内表
SELECT-OPTIONS :
SO_CITY FOR wa_flights-CITYFROM. "画面输入框
SELECT C~CARRID C~CARRNAME P~CONNID P~CITYFROM P~CITYTO
F~FLDATE F~SEATSMAX F~SEATSOCC
INTO CORRESPONDING FIELDS OF TABLE ITAB_FLIGHTS "取到的数据放到内表中
FROM ( SCARR AS C INNER JOIN SPFLI AS P
ON C~CARRID = P~CARRID )
INNER JOIN SFLIGHT AS F
ON F~CARRID = P~CARRID
AND F~CONNID = P~CONNID
WHERE CITYFROM IN SO_CITY. "查询CITYFRO是输入的字段
LOOP AT itab_flights INTO wa_flights . "读取内表中的数据放到工作区
WRITE:/ wa_flights-CARRID,wa_flights-CARRNAME,wa_flights-CONNID,wa_flights-CITYFROM,wa_flights-FLDATE." "读取工作区中的字段的内容
ENDLOOP.
REPORT YY_LTT_SCOTT_JOIN3.
DATA : BEGIN OF gs_mara , "定义结构体=工作区
matnr LIKE mara-matnr,
mtart LIKE mara-mtart,
END OF gs_mara.
DATA gt_mara LIKE TABLE OF gs_mara WITH HEADER LINE . "定义跟结构体结构一样的内表
DATA : BEGIN OF gs_makt ,
matnr LIKE makt-matnr,
maktx LIKE makt-maktx,
END OF gs_makt.
DATA gt_makt LIKE TABLE OF gs_makt WITH HEADER LINE.
DATA : BEGIN OF gs_total ,
matnr LIKE mara-matnr,
maktx LIKE makt-maktx,
mtart LIKE mara-mtart,
END OF gs_total.
DATA gt_total LIKE TABLE OF gs_total WITH HEADER LINE.
SELECT-OPTIONS:
S_MATNR FOR gs_mara-matnr.
SELECT matnr mtart "第一步:从数据库db中取得数据放到内表
FROM mara
INTO CORRESPONDING FIELDS OF TABLE gt_mara
where matnr IN S_MATNR.
IF gt_mara[] IS NOT INITIAL. "检查内表gt_mara不是空
SELECT maktx matnr
FROM makt
INTO CORRESPONDING FIELDS OF TABLE gt_makt
FOR ALL ENTRIES IN gt_mara "FOR ALL ENTRIES IN代表根据内表gt_mara中的内容进行限制
WHERE matnr IN S_MATNR
AND matnr = gt_mara-matnr.
ENDIF.
SORT gt_mara BY matnr. "设置排序使用matnr关键字
LOOP AT gt_mara .
CLEAR gt_total .
MOVE-CORRESPONDING gt_mara TO gt_total.
READ TABLE gt_makt WITH KEY matnr = gt_mara-matnr
BINARY SEARCH.
IF sy-subrc = 0 .
gt_total-maktx = gt_makt-maktx.
ENDIF.
APPEND gt_total.
ENDLOOP.
LOOP AT gt_total INTO gs_total.
WRITE:/ gs_total-matnr, gs_total-maktx,gs_total-mtart.
ENDLOOP.
报表中如果想固定表头-是表头一直显示和固定并且在每一页中都显示 使用TOP-OF-PAGE.
LOOP AT gt_total INTO gs_total.
WRITE:/ SY-ULINE(35).
WRITE:/1 SY-VLINE NO-GAP,
2 gs_total-matnr NO-GAP,
10 SY-VLINE NO-GAP ,
11 gs_total-maktx NO-GAP,
21 SY-VLINE NO-GAP,
22 gs_total-mtart NO-GAP,
35 SY-VLINE NO-GAP.
ENDLOOP.
TOP-OF-PAGE.
WRITE:/ SY-ULINE(35).
WRITE:/1 SY-VLINE NO-GAP,2 'gs_total-matnr' NO-GAP, 10 SY-VLINE NO-GAP,
11 'gs_total-maktx' NO-GAP,21 SY-VLINE NO-GAP,22 'gs_total-maktx' NO-GAP,35 SY-VLINE NO-GAP.
YY_LTT_SCOTT_JOIN3
1.取数据库表A,放到内表A中 2,取数据库表B,放到内表B中 3,将内表A中的数据循环放到汇总的总表中 4,根据A表中的数据去内表B中去取数据。放在汇总的总表中。