2019-12-27 查询PIR - 2 GETDATA

处理数据先检查再取数据

START-OF-SELECTION .
PERFORM CHECK_DATA.
PERFORM GET_DATA.

仅做简单检查

FORM CHECK_DATA.
  IF S_LIFNR IS INITIAL AND S_MATNR IS INITIAL AND S_EKORG IS INITIAL AND S_WERKS IS INITIAL AND S_MATKL IS INITIAL AND S_EKGRP IS INITIAL.
    MESSAGE S208(00) WITH 'Please input query parameters'.
    EXIT.
  ENDIF.
ENDFORM.

取数据部分:GET_DATA. 最先大条件, 再做筛选。
FROM table 与INNER JOIN必须连在一起写,不然会报错。

SELECT
      A~LIFNR,
      A~MATNR,
      E~EKORG,
      E~WERKS,
      E~ESOKZ,
      A~MEINS,
      A~UMREZ,
      A~UMREN,
      A~VABME,
      E~NETPR,
      E~WAERS,
      E~PEINH,
      E~BPRME,
      B~PEINH AS MM_PEINH,
      A~LMEIN,
      E~MWSKZ,
      A~MFRNR,
      E~EKGRP,
      E~APLFZ,
      E~MINBM,
      E~NORBM,
      E~INCO1,
      E~INCO2,
      A~IDNLF,
      A~RELIF,
      A~INFNR,
      A~URZLA,
      A~TELF1,
      M~MTART,
      E~LOEKZ,
      C~MMSTA
      INTO CORRESPONDING FIELDS OF TABLE @GT_ITAB
      FROM EINA AS A "PIR:General Data
      INNER JOIN EINE AS E ON A~INFNR = E~INFNR "PIR -Purchasing Organization Data
      INNER JOIN MARA AS M ON A~MATNR = M~MATNR "General Material Data
      LEFT  JOIN MARC AS C ON A~MATNR = C~MATNR AND E~WERKS = C~WERKS "Plant Data for Material
      LEFT  JOIN MBEW AS B ON A~MATNR = B~MATNR AND E~WERKS = B~BWKEY "Material Valuation
        WHERE A~MATNR IN @S_MATNR
        AND  E~WERKS IN @S_WERKS
        AND  A~LIFNR IN @S_LIFNR
        AND  E~EKORG IN @S_EKORG
        AND  E~ESOKZ IN @S_ESOKZ
        AND  M~MATKL IN @S_MATKL
        AND  E~EKGRP IN @S_EKGRP
        AND  M~MTART IN @S_MTART

再检查:CHECK语句基于条件终止循环传递。 如果CHECK语句中的条件被计算为false,则忽略CHECK语句之后的语句块中的所有剩余语句,并且下一个循环开始。 CHECK语句中的条件可以是任何逻辑表达式。
CHECK跳转的前提是为假时。
l CHECK只是跳出当前事件块,继续下一个事件块的处理,相当于方法的return;
l 另外,如果CHECK在循环(DO、WHILE、LOOP)里,只是跳出当前循环而已;
l 如果CHECK出现在循环以外,退出的是当前执行的程序块(processing block),例如一个FORM,METHOD,或EVENT。
详细参考 https://www.cnblogs.com/jiangzhengjun/p/4265443.html

  CHECK SY-SUBRC = 0 .

  SELECT * INTO TABLE gt_a017 FROM a017
           WHERE  matnr IN s_matnr
             AND  werks IN s_werks
             AND  lifnr IN s_lifnr
             AND  ekorg IN s_ekorg
             AND  esokz IN s_esokz.
  IF sy-subrc = 0.

  IF C_DEL = ' '.
    DELETE GT_ITAB WHERE LOEKZ = 'X'.  "Purch. info: Purch. organization data flagged for deletion
  ENDIF.

   SELECT * INTO TABLE GT_KONP FROM KONP
             FOR ALL ENTRIES IN GT_A017
             WHERE  KNUMH = GT_A017-KNUMH.

    SELECT * INTO TABLE GT_KONM FROM KONM
             FOR ALL ENTRIES IN GT_A017
             WHERE  KNUMH = GT_A017-KNUMH.

  ENDIF.

  SORT gt_a017 BY matnr werks matnr ekorg esokz .

  IF c_due = ' '.
    DELETE gt_a017 WHERE datab > sy-datum OR datbi < sy-datum.
  ELSE.

    IF p_datab IS NOT INITIAL.
      DELETE gt_a017 WHERE datab < p_datab.
    ENDIF.
    IF p_datbi IS NOT INITIAL.
      DELETE gt_a017 WHERE datbi > p_datbi.
    ENDIF.
  ENDIF.

  IF c_del = ' '.
    DELETE gt_konp WHERE loevm_ko = 'X'.
  ENDIF.

对表中数据进行转换,取值为内部形式(物料号有前导零的时候被认为是内部形式;单位一般也有内部形式外部形式之分)。在se11浏览数据的时候,显示数据的画面是使用内部形式,当detail overview的时候,则是外部形式。显示前转换成外部形式。

在SAP系统里面很多地方都用到了长文本,这些长文本的值以其他字符的形式存在表STXL和STXH里面,不能直接去这两个表里面去取值,而是要通过函数READ_TEXT去取. 很多新手不了解的地方可能就是怎么取到READ_TEXT函数里面的那些参数值,我这边介绍两种方法,通过这两种方法都可以找到那几个参数:
一、第一种利用跟踪查询,不是用ST05跟踪,而是去表里面跟踪查询,比如你刚刚创建了一个销售订单的长文本,这时候你可以去表STXH输入日期和刚刚的时间,然后查看在这个表里刚刚产生的这些数据,依据此项就可以找到对应的参数了,这个方法非常实用
二、第二种前台查看,进入长文本编辑界面以后(记得是双击长文本编辑器进入),点击菜单转到—->表头,里面出现的就是对应的那些参数值,如下图:


image.png

SE37: READ_TEXT


image.png
  LOOP AT gt_itab.

    CLEAR gt_itab-potxt.

    CONCATENATE gt_itab-infnr gt_itab-ekorg '0' gt_itab-werks INTO tdname.

    CALL FUNCTION 'READ_TEXT'
      EXPORTING
        client          = sy-mandt
        id              = 'BT'
        language        = 'E'
        name            = tdname
        object          = 'EINE'
*       archive_handle  = 0
*   IMPORTING
*       HEADER          = GW_THEAD    " Change 1 (need a header)
      TABLES
        lines           = t_line
      EXCEPTIONS
        id              = 1
        language        = 2
        name            = 3
        not_found       = 4
        object          = 5
        reference_check = 6.

    IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    IF sy-subrc = 0.
*      read table t_table into wa_tline index 1.
*      I_ITAB-ZTEXT = wa_tline-tdline.
      LOOP AT t_line INTO wa_tline.
        CONCATENATE gt_itab-potxt wa_tline-tdline INTO gt_itab-potxt.
      ENDLOOP.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.

    CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
      EXPORTING
        input  = gt_itab-meins
*       LANGUAGE             = SY-LANGU
      IMPORTING
*       LONG_TEXT            =
        output = gt_itab-meins
*       SHORT_TEXT           =
*      EXCEPTIONS
*       UNIT_NOT_FOUND       = 1
*       OTHERS = 2
      .
    LOOP AT gt_a017  WHERE lifnr = gt_itab-lifnr
                       AND matnr = gt_itab-matnr
                       AND ekorg = gt_itab-ekorg
                       AND werks = gt_itab-werks
                       AND esokz = gt_itab-esokz.
*        AND DATAB <= SY-DATUM
*        AND DATBI >= SY-DATUM.

      gt_itab-datab = gt_a017-datab.
      gt_itab-datbi = gt_a017-datbi.
      gt_itab-knumh = gt_a017-knumh.

      LOOP AT gt_konp WHERE knumh = gt_a017-knumh.
        IF gt_konp-kzbzg <> ''.
          LOOP AT gt_konm WHERE knumh = gt_konp-knumh AND kopos = gt_konp-kopos.
            CLEAR gt_out_tmp.
            MOVE-CORRESPONDING  gt_itab TO gt_out_tmp.
            gt_out_tmp-kstbm = gt_konm-kstbm.
*            IF gt_konm-kstbm <> 0.
            gt_out_tmp-kbetr = gt_konm-kbetr.
*            ENDIF.
            gt_out_tmp-peinh = gt_konp-kpein.
            gt_out_tmp-waers = gt_konp-konwa.
            gt_out_tmp-bprme = gt_konp-kmein.
            CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
              EXPORTING
                input  = gt_out_tmp-bprme
*               LANGUAGE             = SY-LANGU
              IMPORTING
*               LONG_TEXT            =
                output = gt_out_tmp-bprme
*               SHORT_TEXT           =
*      EXCEPTIONS
*               UNIT_NOT_FOUND       = 1
*               OTHERS = 2
              .
            gt_out_tmp-netpr    = gt_konp-kbetr.
            gt_out_tmp-loevm_ko = gt_konp-loevm_ko.
* Begin of change SAPCI#2690
            IF c_cur IS NOT INITIAL.
              PERFORM base_currency USING gt_out_tmp-netpr
                                          gt_out_tmp-waers
                                          gt_out_tmp-peinh
                                    CHANGING gt_out_tmp-b_netpr
                                             gt_out_tmp-b_waers
                                             gt_out_tmp-b_netpr1.
            ENDIF.
* End of change SAPCI#2690
            APPEND gt_out_tmp.
          ENDLOOP.
        ELSE.
*          CLEAR gt_out_tmp.
          MOVE-CORRESPONDING  gt_itab TO gt_out_tmp.
          gt_out_tmp-peinh = gt_konp-kpein.
          gt_out_tmp-waers = gt_konp-konwa.
          gt_out_tmp-bprme = gt_konp-kmein.
          CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
            EXPORTING
              input  = gt_out_tmp-bprme
*             LANGUAGE             = SY-LANGU
            IMPORTING
*             LONG_TEXT            =
              output = gt_out_tmp-bprme
*             SHORT_TEXT           =
*      EXCEPTIONS
*             UNIT_NOT_FOUND       = 1
*             OTHERS = 2
            .
          gt_out_tmp-netpr    = gt_konp-kbetr.
          gt_out_tmp-loevm_ko = gt_konp-loevm_ko.
* Begin of change SAPCI#2690
          IF c_cur IS NOT INITIAL.
            PERFORM base_currency USING gt_out_tmp-netpr
                                        gt_out_tmp-waers
                                        gt_out_tmp-peinh
                                  CHANGING gt_out_tmp-b_netpr
                                           gt_out_tmp-b_waers
                                           gt_out_tmp-b_netpr1.
          ENDIF.
* End of change SAPCI#2690
          APPEND gt_out_tmp.
        ENDIF.
      ENDLOOP.
    ENDLOOP.

    IF sy-subrc <> 0 .
      IF gt_itab-mtart = 'ZSV'.
*      DATA:PV_MTART TYPE MARA-MTART.
*      SELECT SINGLE MTART INTO PV_MTART FROM MARA WHERE MATNR = GT_ITAB-MATNR.
*      IF PV_MTART = 'ZSV'.
        MOVE-CORRESPONDING  gt_itab TO gt_out_tmp.
        APPEND gt_out_tmp.
      ENDIF.
    ENDIF.
  ENDLOOP.

***{ D01K932831;CN0778;02/28/2019
  LOOP AT gt_out_tmp.

*{  SAPCI-2801 Start
    CALL FUNCTION 'CONVERSION_EXIT_CUNIT_OUTPUT'
      EXPORTING
        input          = gt_out_tmp-lmein
      IMPORTING
        output         = gt_out_tmp-lmein
      EXCEPTIONS
        unit_not_found = 1
        OTHERS         = 2.

    IF sy-subrc <> 0.
    ENDIF.

    IF gt_out_tmp-meins EQ gt_out_tmp-lmein.
      gt_out_tmp-mm_netpr = gt_out_tmp-b_netpr1 * gt_out_tmp-mm_peinh.
    ELSE.
      DATA(lv_prcunit_buom) = gt_out_tmp-peinh * ( gt_out_tmp-umrez / gt_out_tmp-umren ).
      gt_out_tmp-mm_netpr = ( gt_out_tmp-b_netpr / lv_prcunit_buom ) * gt_out_tmp-mm_peinh.
    ENDIF.
*}  SAPCI-2801 End

你可能感兴趣的:(2019-12-27 查询PIR - 2 GETDATA)