2013年4月1日 星期一

SAP指標與參考物件應用

1. 宣告參考物件:DATA dref TYPE REF TO DATA.(任意類型)
                        STANDARD TABLE.(Table類型的宣告)       
2. 建立參考物件的記憶體位址以及其資料結構:CREATE DATA dref {TYPE type}|{LIKE dobj}
3. dref->* :取得記體位址的資料結構
4. ASSIGN dref->* TO :將記憶體位址的資料結構指派給指標變數
5. Select ... INTO
   Loop at xxx INTO
   ASSIGN COMPONENT sy-index
         OF STRUCTURE TO .
   指派值到指標變數所指的位址
6. 參考物件無法直接使用,所以必須透過指標存取

REPORT z_barry_test                            .

TYPE-POOLS : abap.
FIELD-SYMBOLS: TYPE STANDARD TABLE,  "宣告指標變數
               ,
               .
DATA: dy_table TYPE REF TO data,  "宣告參考物件
      dy_line TYPE REF TO data,
      xfc TYPE lvc_s_fcat,
      ifc TYPE lvc_t_fcat.
DATA: itab LIKE alsmex_tabline OCCURS 0 WITH HEADER LINE.

PARAMETERS p_file TYPE localfile.

AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file.
  PERFORM get_file_name USING p_file.


START-OF-SELECTION.
  PERFORM get_file_data.
  PERFORM get_structure.
  PERFORM create_dynamic_itab.
  PERFORM get_data.
  PERFORM write_out.
*---------------------------------------------------------------------*
*       FORM get_structure                                            *
*---------------------------------------------------------------------*
FORM get_structure.
  LOOP AT itab.
    CLEAR xfc.
    xfc-fieldname = itab-value .
    xfc-inttype = 'C'.
    xfc-intlen = 18.
*    xfc-decimals = 4.
    APPEND xfc TO ifc.
  ENDLOOP.
ENDFORM.                    "get_structure
*---------------------------------------------------------------------*
*       FORM create_dynamic_itab                                      *
*---------------------------------------------------------------------*
FORM create_dynamic_itab.

  CALL METHOD cl_alv_table_create=>create_dynamic_table "建立參考物件
    EXPORTING
      it_fieldcatalog = ifc
    IMPORTING
      ep_table        = dy_table.
  ASSIGN dy_table->* TO .   "指派參考物件記憶體位址連結到指標變數

  CREATE DATA dy_line LIKE LINE OF . "建立與指標變數所指的記憶體空間結構相同的參考物件
  ASSIGN dy_line->* TO .
ENDFORM.                    "create_dynamic_itab

*---------------------------------------------------------------------*
*       FORM get_data                                                 *
*---------------------------------------------------------------------*
FORM get_data.
  DATA: BEGIN OF itab OCCURS 0 .
          INCLUDE STRUCTURE eord.
  DATA: END OF itab.

  SELECT matnr  werks vdatu bdatu lifnr reswk ekorg flifn
      UP TO 10 ROWS
      INTO CORRESPONDING FIELDS OF TABLE
               FROM eord.
ENDFORM.                    "get_data
*---------------------------------------------------------------------*
*       FORM write_out                                                *
*---------------------------------------------------------------------*
FORM write_out.
  LOOP AT INTO .
    DO.
      ASSIGN COMPONENT sy-index
         OF STRUCTURE TO .
      IF sy-subrc <> 0.
        EXIT.
      ENDIF.
      IF sy-index = 1.
        WRITE:/ .
      ELSE.
        WRITE: .
      ENDIF.
    ENDDO.
  ENDLOOP.
ENDFORM.                    "write_out
*&---------------------------------------------------------------------*
*&      Form  GET_FILE_NAME
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->P_P_FILE  text
*----------------------------------------------------------------------*
FORM get_file_name  USING     p_file.
  CALL FUNCTION 'F4_FILENAME'
*   EXPORTING
*     PROGRAM_NAME        = SYST-CPROG
*     DYNPRO_NUMBER       = SYST-DYNNR
*     FIELD_NAME          = ' '
   IMPORTING
     file_name           = p_file
            .
ENDFORM.                    " GET_FILE_NAME
*&---------------------------------------------------------------------*
*&      Form  GET_FILE_DATA
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*  -->  p1        text
*  <-- br="" nbsp="" p2="" text="">*----------------------------------------------------------------------*
FORM get_file_data .
  TYPE-POOLS : truxs.
  DATA: file_str TYPE rlgrap-filename.
  DATA: lv_werks1 TYPE string.
  DATA: lv_werks2 TYPE string.
  DATA: lv_lifnr LIKE eina-lifnr,
        lv_matnr LIKE eina-matnr.

  file_str = p_file.

  CALL FUNCTION 'ZALSM_EXCEL_TO_INTERNAL_TABLE'
    EXPORTING
      filename                      = file_str
      i_begin_col                   = '1'
      i_begin_row                   = '1'
      i_end_col                     = '255'
      i_end_row                     = '9999'
*   SHEET_NAME                    =
    TABLES
      intern                        = itab
* EXCEPTIONS
*   INCONSISTENT_PARAMETERS       = 1
*   UPLOAD_OLE                    = 2
*   OTHERS                        = 3
            .
  IF sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
  ENDIF.


ENDFORM.                    " GET_FILE_DATA

沒有留言:

張貼留言