*&---------------------------------------------------------------------* *& Report ZABAPS4 *&---------------------------------------------------------------------* *& *&---------------------------------------------------------------------* REPORT ZABAPS4. TYPES: BEGIN OF TY_OUT, MATNR TYPE MARA-MATNR, MAKTX TYPE MAKT-MAKTX, END OF TY_OUT. DATA: GT_OUT TYPE TABLE OF TY_OUT, GS_OUT TYPE TY_OUT, GT_TMP TYPE TABLE OF TY_OUT, GS_TMP TYPE TY_OUT. **********************************内嵌声明 *构建工作区 GS_OUT = VALUE #( MATNR = '1000001' MAKTX = '物料描述' ). *构建内表 GT_OUT = VALUE #( ( MATNR = '1000001' MAKTX = '物料描述1' ) ( MATNR = '1000002' MAKTX = '物料描述2' ) ). *内表追加数据 GT_OUT = VALUE #( BASE GT_OUT ( MATNR = '1000003' MAKTX = '物料描述3' ) ). *定义变量 DATA(LV_DATUM) = SY-DATUM. *定义内表和工作区 DATA(LS_OUT) = GS_OUT. DATA(LT_OUT) = GT_OUT. **********************************SELECT新特性 DATA LV_MATNR TYPE MARA-MATNR. *直接取数定义局部内表 SELECT MATNR,ERSDA,ERNAM,MTART,MATKL FROM MARA INTO TABLE @DATA(LT_MARA) WHERE MATNR = @LV_MATNR. IF LT_MARA IS NOT INITIAL. SELECT MATNR, MAKTX FROM MAKT INTO TABLE @DATA(LT_MAKT) FOR ALL ENTRIES IN @LT_MARA WHERE MATNR = @LT_MARA-MATNR. ENDIF. *WITH 替换 FOR ALL ENTRY IN CLEAR LT_MAKT. WITH +DATA AS ( SELECT FROM MARA AS A FIELDS A~MATNR WHERE MATNR = '000000000000000104' ) SELECT FROM +DATA INNER JOIN MAKT AS B ON +DATA~MATNR = B~MATNR FIELDS B~MATNR,B~MAKTX INTO TABLE @LT_MAKT . *SUBSTRING SQL中截取字段任意长度 注意某些情况INTO需要写在SQL最后面 否则会报错 CLEAR LT_MARA. SELECT SUBSTRING( MATNR,1,5 ) AS MATNR,ERSDA,ERNAM,MTART FROM MARA WHERE MATNR = @LV_MATNR INTO TABLE @LT_MARA. *SQL中默认值 CLEAR LT_MARA. SELECT MATNR, ERSDA, 'Yangchao'AS ERNAM, MTART, MATKL FROM MARA INTO TABLE @LT_MARA WHERE MATNR = '000000000000000104' . **********************************内表操作 *字段字段相同 GS_TMP = CORRESPONDING #( GS_OUT ). GT_TMP = CORRESPONDING #( GT_OUT ). *字段名相同赋值 并且指定字段复制到特定字段上 CLEAR GS_TMP. GS_TMP = CORRESPONDING #( GS_OUT MAPPING MATNR = MAKTX ). *EXCEPT时是将EXCEPT的字段置为空而不是不赋值 CLEAR GS_TMP. GS_TMP = CORRESPONDING #( GS_OUT EXCEPT MAKTX ). CLEAR GT_TMP. GT_TMP = CORRESPONDING #( GT_OUT EXCEPT MAKTX ). *找出满足条件的数据 使用操作符FILTER过滤 DATA GT_FILTER TYPE HASHED TABLE OF TY_OUT WITH UNIQUE KEY MATNR. GT_FILTER = VALUE #( ( MATNR = '1000001' MAKTX = '物料描述1') ( MATNR = '1000005' MAKTX = '物料描述5') ). *找出GT_OUT在GT_FILTER里的 CLEAR GT_TMP. GT_TMP = FILTER #( GT_OUT IN GT_FILTER WHERE MATNR = MATNR ) . "找出GT_OUT不在GT_FILTER里的 CLEAR GT_TMP. GT_TMP = FILTER #( GT_OUT EXCEPT IN GT_FILTER WHERE MATNR = MATNR ) . *判断某一行数据是否存在 IF LINE_EXISTS( GT_OUT[ 2 ] ). MESSAGE 'OK' TYPE 'S'. ENDIF. *获取第N行的记录 CLEAR GS_OUT. GS_OUT = GT_OUT[ 2 ]. *互获取第N行的某个字段 DATA(LV_MAKTX) = GT_OUT[ 2 ]-MAKTX."标签类型 "内表中符合条件的数据有几条 DATA(LV_LINE) = REDUCE I( INIT X = 0 FOR GS_LINES IN GT_OUT WHERE ( MATNR = '1000001' ) NEXT X = X + 1 ). *计算内表行数 DATA(LV_LINES) = LINES( GT_OUT ). *增加前导0 删除前导0 DATA LV_VBELN TYPE VBAK-VBELN VALUE '000010001'. LV_VBELN = |{ LV_VBELN ALPHA = OUT }| . LV_VBELN = |{ LV_VBELN ALPHA = IN }| . *拼接 DATA LV_STR TYPE STRING. LV_STR = |{ '系统日期'}{ SY-DATUM }{ '系统时间'}{ SY-UZEIT }| . *通过REDUCE汇总 TYPES:BEGIN OF TY_DATA, NUMBER TYPE C, PRICE TYPE FINS_VOCUR12, CATEGORY(20) TYPE C, END OF TY_DATA. DATA: LT_DATA TYPE TABLE OF TY_DATA, LT_SUM1 TYPE TABLE OF TY_DATA. LT_DATA = VALUE #( ( NUMBER = 'a' PRICE = 2 CATEGORY = 'xxx') ( NUMBER = 'a' PRICE = 2 CATEGORY = 'xxx') ( NUMBER = 'a' PRICE = 2 CATEGORY = 'xxx') ( NUMBER = 'b' PRICE = 3 CATEGORY = 'xxx') ( NUMBER = 'b' PRICE = 3 CATEGORY = 'AAA') ( NUMBER = 'b' PRICE = 3 CATEGORY = 'AAA') ( NUMBER = 'b' PRICE = 3 CATEGORY = 'AAA') ( NUMBER = 'c' PRICE = 4 CATEGORY = 'AAA') ( NUMBER = 'c' PRICE = 4 CATEGORY = 'AAA') ( NUMBER = 'c' PRICE = 4 CATEGORY = 'AAA') ( NUMBER = 'c' PRICE = 4 CATEGORY = 'AAA') ). SELECT NUMBER,SUM( PRICE ) AS PRICE FROM @LT_DATA AS A GROUP BY NUMBER ORDER BY NUMBER ASCENDING INTO TABLE @DATA(LT_SUM2). LT_SUM2 = VALUE #( ( NUMBER = 'a') ( NUMBER = 'b' ) ( NUMBER = 'c' ) ). LOOP AT LT_SUM2 ASSIGNING FIELD-SYMBOL(<FS_SUM2>). <FS_SUM2>-PRICE = REDUCE #( INIT LV_PRICE TYPE FINS_VOCUR12 FOR LS_DATA IN LT_DATA WHERE ( NUMBER = <FS_SUM2>-NUMBER ) NEXT LV_PRICE = LV_PRICE + LS_DATA-PRICE ). ENDLOOP. *LOOP AT GROUP替代AT NEW. DATA: LT_COLLECT TYPE TABLE OF TY_DATA, LS_COLLECT TYPE TY_DATA. *根据NUMBER分组跑逻辑 LOOP AT LT_DATA ASSIGNING FIELD-SYMBOL(<FS_DATA>) GROUP BY ( NUMBER = <FS_DATA>-NUMBER * SIZE = GROUP SIZE"分组组数" * INDEX = GROUP INDEX "分组的组序号" ) ASCENDING ASSIGNING FIELD-SYMBOL(<GROUP>). LOOP AT GROUP <GROUP> ASSIGNING FIELD-SYMBOL(<FS_GROUP>). ENDLOOP. ENDLOOP. **********************************指针 *循环中直接定义工作区/指针 LOOP AT GT_OUT INTO DATA(LW_OUT). ENDLOOP. LOOP AT GT_OUT ASSIGNING FIELD-SYMBOL(<FS_OUT>). ENDLOOP. READ TABLE GT_OUT INTO DATA(LW_OUT1) INDEX 1. READ TABLE GT_OUT ASSIGNING FIELD-SYMBOL(<FS_OUT1>) INDEX 1. BREAK-POINT.
标签:GT,汇总,NUMBER,S4,语法,LT,MATNR,DATA,OUT From: https://www.cnblogs.com/Anyohh233/p/18131759